-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorder_service.go
597 lines (538 loc) · 14.3 KB
/
order_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package phemex
import (
"context"
"encoding/json"
"github.com/Krisa/go-phemex/common"
)
// CreateOrderService create order
type CreateOrderService struct {
c *Client
symbol string
clOrdID *string
actionBy *string
side SideType
orderQty *float64
priceEp *int64
ordType *OrderType
stopPxEp *int64
timeInForce *TimeInForceType
reduceOnly *bool
closeOnTrigger *bool
takeProfitEp *int64
stopLossEp *int64
pegOffsetValueEp *int64
triggerType *TriggerType
text *string
pegPriceType *string
}
// Symbol set symbol
func (s *CreateOrderService) Symbol(symbol string) *CreateOrderService {
s.symbol = symbol
return s
}
// ClOrdID set clOrID
func (s *CreateOrderService) ClOrdID(clOrdID string) *CreateOrderService {
s.clOrdID = &clOrdID
return s
}
// ActionBy set actionBy
func (s *CreateOrderService) ActionBy(actionBy string) *CreateOrderService {
s.actionBy = &actionBy
return s
}
// Side set side
func (s *CreateOrderService) Side(side SideType) *CreateOrderService {
s.side = side
return s
}
// OrderQty set orderQty
func (s *CreateOrderService) OrderQty(orderQty float64) *CreateOrderService {
s.orderQty = &orderQty
return s
}
// PriceEp set priceEp
func (s *CreateOrderService) PriceEp(priceEp int64) *CreateOrderService {
s.priceEp = &priceEp
return s
}
// OrdType set ordType
func (s *CreateOrderService) OrdType(ordType OrderType) *CreateOrderService {
s.ordType = &ordType
return s
}
// StopPxEp set stopPxEp
func (s *CreateOrderService) StopPxEp(stopPxEp int64) *CreateOrderService {
s.stopPxEp = &stopPxEp
return s
}
// TimeInForce set timeInForce
func (s *CreateOrderService) TimeInForce(timeInForce TimeInForceType) *CreateOrderService {
s.timeInForce = &timeInForce
return s
}
// ReduceOnly set reduceOnly
func (s *CreateOrderService) ReduceOnly(reduceOnly bool) *CreateOrderService {
s.reduceOnly = &reduceOnly
return s
}
// CloseOnTrigger set closeOnTrigger
func (s *CreateOrderService) CloseOnTrigger(closeOnTrigger bool) *CreateOrderService {
s.closeOnTrigger = &closeOnTrigger
return s
}
// TakeProfitEp set takeProfitEp
func (s *CreateOrderService) TakeProfitEp(takeProfitEp int64) *CreateOrderService {
s.takeProfitEp = &takeProfitEp
return s
}
// StopLossEp set stopLossEp
func (s *CreateOrderService) StopLossEp(stopLossEp int64) *CreateOrderService {
s.stopLossEp = &stopLossEp
return s
}
// TriggerType set triggerType
func (s *CreateOrderService) TriggerType(triggerType TriggerType) *CreateOrderService {
s.triggerType = &triggerType
return s
}
// Text set text
func (s *CreateOrderService) Text(text string) *CreateOrderService {
s.text = &text
return s
}
// PegOffsetValueEp set pegOffsetValueEp
func (s *CreateOrderService) PegOffsetValueEp(pegOffsetValueEp int64) *CreateOrderService {
s.pegOffsetValueEp = &pegOffsetValueEp
return s
}
// PegPriceType set pegPriceType
func (s *CreateOrderService) PegPriceType(pegPriceType string) *CreateOrderService {
s.pegPriceType = &pegPriceType
return s
}
func (s *CreateOrderService) createOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, err error) {
r := &request{
method: "POST",
endpoint: endpoint,
secType: secTypeSigned,
}
m := params{
"symbol": s.symbol,
"side": s.side,
}
if s.clOrdID != nil {
m["clOrdID"] = *s.clOrdID
}
if s.orderQty != nil {
m["orderQty"] = *s.orderQty
}
if s.actionBy != nil {
m["actionBy"] = *s.actionBy
}
if s.priceEp != nil {
m["priceEp"] = *s.priceEp
}
if s.ordType != nil {
m["ordType"] = *s.ordType
}
if s.stopPxEp != nil {
m["stopPxEp"] = *s.stopPxEp
}
if s.timeInForce != nil {
m["timeInForce"] = *s.timeInForce
}
if s.reduceOnly != nil {
m["reduceOnly"] = *s.reduceOnly
}
if s.closeOnTrigger != nil {
m["closeOnTrigger"] = *s.closeOnTrigger
}
if s.takeProfitEp != nil {
m["takeProfitEp"] = *s.takeProfitEp
}
if s.stopLossEp != nil {
m["stopLossEp"] = *s.stopLossEp
}
if s.triggerType != nil {
m["triggerType"] = *s.triggerType
}
if s.text != nil {
m["text"] = *s.text
}
if s.pegPriceType != nil {
m["pegPriceType"] = *s.pegPriceType
}
if s.pegOffsetValueEp != nil {
m["pegOffsetValueEp"] = *s.pegOffsetValueEp
}
r.setFormParams(m)
data, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return []byte{}, err
}
return data, nil
}
// Do send request
func (s *CreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res *OrderResponse, err error) {
data, err := s.createOrder(ctx, "/orders", opts...)
if err != nil {
return nil, err
}
resp := new(BaseResponse)
resp.Data = new(OrderResponse)
err = json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
if resp.Code > 0 {
return nil, &common.APIError{
Code: resp.Code,
Message: resp.Msg,
}
}
return resp.Data.(*OrderResponse), nil
}
// OrderResponse define create order response
type OrderResponse struct {
BizError int `json:"bizError"`
OrderID string `json:"orderID"`
ClOrdID string `json:"clOrdID"`
Symbol string `json:"symbol"`
Side SideType `json:"side"`
ActionTimeNs int64 `json:"actionTimeNs"`
TransactTimeNs int64 `json:"transactTimeNs"`
OrderType OrderType `json:"orderType"`
PriceEp int64 `json:"priceEp"`
Price float64 `json:"price"`
OrderQty float64 `json:"orderQty"`
DisplayQty float64 `json:"displayQty"`
TimeInForce TimeInForceType `json:"timeInForce"`
ReduceOnly bool `json:"reduceOnly"`
TakeProfitEp int64 `json:"takeProfitEp"`
TakeProfit float64 `json:"takeProfit"`
StopPxEp int64 `json:"stopPxEp"`
StopPx float64 `json:"stopPx"`
StopLossEp int64 `json:"stopLossEp"`
ClosedPnlEv int64 `json:"closedPnlEv"`
ClosedPnl float64 `json:"closedPnl"`
ClosedSize float64 `json:"closedSize"`
CumQty float64 `json:"cumQty"`
CumValueEv int64 `json:"cumValueEv"`
CumValue float64 `json:"cumValue"`
LeavesQty float64 `json:"leavesQty"`
LeavesValueEv int64 `json:"leavesValueEv"`
LeavesValue float64 `json:"leavesValue"`
StopLoss float64 `json:"stopLoss"`
StopDirection string `json:"stopDirection"`
OrdStatus string `json:"ordStatus"`
Trigger string `json:"trigger"`
}
// CreateReplaceOrderService create order
type CreateReplaceOrderService struct {
c *Client
symbol string
orderID string
origClOrdID *string
clOrdID *string
price *float64
priceEp *int64
orderQty *float64
stopPx *float64
stopPxEp *int64
takeProfit *float64
takeProfitEp *int64
stopLoss *float64
stopLossEp *int64
pegOffset *float64
pegOffsetEp *int64
}
// Symbol set symbol
func (s *CreateReplaceOrderService) Symbol(symbol string) *CreateReplaceOrderService {
s.symbol = symbol
return s
}
// OrderID set orderID
func (s *CreateReplaceOrderService) OrderID(orderID string) *CreateReplaceOrderService {
s.orderID = orderID
return s
}
// OrigClOrdID set origClOrdID
func (s *CreateReplaceOrderService) OrigClOrdID(origClOrdID string) *CreateReplaceOrderService {
s.origClOrdID = &origClOrdID
return s
}
// ClOrdID set clOrID
func (s *CreateReplaceOrderService) ClOrdID(clOrdID string) *CreateReplaceOrderService {
s.clOrdID = &clOrdID
return s
}
// Price set price
func (s *CreateReplaceOrderService) Price(price float64) *CreateReplaceOrderService {
s.price = &price
return s
}
// PriceEp set priceEp
func (s *CreateReplaceOrderService) PriceEp(priceEp int64) *CreateReplaceOrderService {
s.priceEp = &priceEp
return s
}
// OrderQty set orderQty
func (s *CreateReplaceOrderService) OrderQty(orderQty float64) *CreateReplaceOrderService {
s.orderQty = &orderQty
return s
}
// StopPx set stopPx
func (s *CreateReplaceOrderService) StopPx(stopPx float64) *CreateReplaceOrderService {
s.stopPx = &stopPx
return s
}
// StopPxEp set stopPxEp
func (s *CreateReplaceOrderService) StopPxEp(stopPxEp int64) *CreateReplaceOrderService {
s.stopPxEp = &stopPxEp
return s
}
// TakeProfit set takeProfit
func (s *CreateReplaceOrderService) TakeProfit(takeProfit float64) *CreateReplaceOrderService {
s.takeProfit = &takeProfit
return s
}
// TakeProfitEp set takeProfitEp
func (s *CreateReplaceOrderService) TakeProfitEp(takeProfitEp int64) *CreateReplaceOrderService {
s.takeProfitEp = &takeProfitEp
return s
}
// StopLoss set stopLoss
func (s *CreateReplaceOrderService) StopLoss(stopLoss float64) *CreateReplaceOrderService {
s.stopLoss = &stopLoss
return s
}
// StopLossEp set stopLossEp
func (s *CreateReplaceOrderService) StopLossEp(stopLossEp int64) *CreateReplaceOrderService {
s.stopLossEp = &stopLossEp
return s
}
// PegOffset set pegOffset
func (s *CreateReplaceOrderService) PegOffset(pegOffset float64) *CreateReplaceOrderService {
s.pegOffset = &pegOffset
return s
}
// PegOffsetEp set pegOffsetEp
func (s *CreateReplaceOrderService) PegOffsetEp(pegOffsetEp int64) *CreateReplaceOrderService {
s.pegOffsetEp = &pegOffsetEp
return s
}
func (s *CreateReplaceOrderService) createOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, err error) {
r := &request{
method: "PUT",
endpoint: endpoint,
secType: secTypeSigned,
}
r.setParam("symbol", s.symbol)
r.setParam("orderID", s.orderID)
if s.origClOrdID != nil {
r.setParam("origClOrdID", *s.origClOrdID)
}
if s.clOrdID != nil {
r.setParam("clOrdID", *s.clOrdID)
}
if s.price != nil {
r.setParam("price", *s.price)
}
if s.priceEp != nil {
r.setParam("priceEp", *s.priceEp)
}
if s.orderQty != nil {
r.setParam("orderQty", *s.orderQty)
}
if s.stopPx != nil {
r.setParam("stopPx", *s.stopPx)
}
if s.stopPxEp != nil {
r.setParam("stopPxEp", *s.stopPxEp)
}
if s.takeProfit != nil {
r.setParam("takeProfit", *s.takeProfit)
}
if s.takeProfitEp != nil {
r.setParam("takeProfitEp", *s.takeProfitEp)
}
if s.stopLoss != nil {
r.setParam("stopLoss", *s.stopLoss)
}
if s.stopLossEp != nil {
r.setParam("stopLossEp", *s.stopLossEp)
}
if s.pegOffset != nil {
r.setParam("pegOffset", *s.pegOffset)
}
if s.pegOffsetEp != nil {
r.setParam("pegOffsetEp", *s.pegOffsetEp)
}
data, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return []byte{}, err
}
return data, nil
}
// Do send request
func (s *CreateReplaceOrderService) Do(ctx context.Context, opts ...RequestOption) (res *OrderResponse, err error) {
data, err := s.createOrder(ctx, "/orders/replace", opts...)
if err != nil {
return nil, err
}
resp := new(BaseResponse)
resp.Data = new(OrderResponse)
err = json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
if resp.Code > 0 {
return nil, &common.APIError{
Code: resp.Code,
Message: resp.Msg,
}
}
return resp.Data.(*OrderResponse), nil
}
// ListOpenOrdersService list opened orders
type ListOpenOrdersService struct {
c *Client
symbol string
}
// Symbol set symbol
func (s *ListOpenOrdersService) Symbol(symbol string) *ListOpenOrdersService {
s.symbol = symbol
return s
}
// Do send request
func (s *ListOpenOrdersService) Do(ctx context.Context, opts ...RequestOption) (res []*OrderResponse, err error) {
r := &request{
method: "GET",
endpoint: "/orders/activeList",
secType: secTypeSigned,
}
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*OrderResponse{}, err
}
resp := new(BaseResponse)
resp.Data = new(RowsOrderResponse)
err = json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
if resp.Code > 0 {
return nil, &common.APIError{
Code: resp.Code,
Message: resp.Msg,
}
}
rows := resp.Data.(*RowsOrderResponse)
return rows.Rows, nil
}
// RowsOrderResponse rows order response
type RowsOrderResponse struct {
Rows []*OrderResponse `json:"rows"`
}
// CancelOrderService cancel an order
type CancelOrderService struct {
c *Client
symbol string
orderID *string
}
// Symbol set symbol
func (s *CancelOrderService) Symbol(symbol string) *CancelOrderService {
s.symbol = symbol
return s
}
// OrderID set orderID
func (s *CancelOrderService) OrderID(orderID string) *CancelOrderService {
s.orderID = &orderID
return s
}
// Do send request
func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res *OrderResponse, err error) {
r := &request{
method: "DELETE",
endpoint: "/orders/cancel",
secType: secTypeSigned,
}
r.setParam("symbol", s.symbol)
if s.orderID != nil {
r.setParam("orderID", *s.orderID)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
resp := new(BaseResponse)
resp.Data = new(OrderResponse)
err = json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
if resp.Code > 0 {
return nil, &common.APIError{
Code: resp.Code,
Message: resp.Msg,
}
}
return resp.Data.(*OrderResponse), nil
}
// QueryOrderService cancel an order
type QueryOrderService struct {
c *Client
symbol string
orderID *string
clOrderID *string
}
// Symbol set symbol
func (s *QueryOrderService) Symbol(symbol string) *QueryOrderService {
s.symbol = symbol
return s
}
// OrderID set orderID
func (s *QueryOrderService) OrderID(orderID string) *QueryOrderService {
s.orderID = &orderID
return s
}
// ClOrderID set clOrderID
func (s *QueryOrderService) ClOrderID(clOrderID string) *QueryOrderService {
s.clOrderID = &clOrderID
return s
}
// Do send request
func (s *QueryOrderService) Do(ctx context.Context, opts ...RequestOption) (res []*OrderResponse, err error) {
r := &request{
method: "GET",
endpoint: "/exchange/order",
secType: secTypeSigned,
}
r.setParam("symbol", s.symbol)
if s.orderID != nil {
r.setParam("orderID", *s.orderID)
}
if s.clOrderID != nil {
r.setParam("clOrderID", *s.clOrderID)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
resp := new(BaseResponse)
resp.Data = new([]*OrderResponse)
err = json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
if resp.Code > 0 {
return nil, &common.APIError{
Code: resp.Code,
Message: resp.Msg,
}
}
return *resp.Data.(*[]*OrderResponse), nil
}