forked from adshao/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 1
/
order_service.go
623 lines (567 loc) · 16.6 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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package delivery
import (
"context"
"encoding/json"
"net/http"
)
// CreateOrderService create order
type CreateOrderService struct {
c *Client
symbol string
side SideType
positionSide *PositionSideType
orderType OrderType
timeInForce *TimeInForceType
quantity string
reduceOnly *bool
price *string
newClientOrderID *string
stopPrice *string
closePosition *bool
activationPrice *string
callbackRate *string
workingType *WorkingType
priceProtect *bool
newOrderRespType NewOrderRespType
}
// Symbol set symbol
func (s *CreateOrderService) Symbol(symbol string) *CreateOrderService {
s.symbol = symbol
return s
}
// Side set side
func (s *CreateOrderService) Side(side SideType) *CreateOrderService {
s.side = side
return s
}
// PositionSide set side
func (s *CreateOrderService) PositionSide(positionSide PositionSideType) *CreateOrderService {
s.positionSide = &positionSide
return s
}
// Type set type
func (s *CreateOrderService) Type(orderType OrderType) *CreateOrderService {
s.orderType = orderType
return s
}
// TimeInForce set timeInForce
func (s *CreateOrderService) TimeInForce(timeInForce TimeInForceType) *CreateOrderService {
s.timeInForce = &timeInForce
return s
}
// Quantity set quantity
func (s *CreateOrderService) Quantity(quantity string) *CreateOrderService {
s.quantity = quantity
return s
}
// ReduceOnly set reduceOnly
func (s *CreateOrderService) ReduceOnly(reduceOnly bool) *CreateOrderService {
s.reduceOnly = &reduceOnly
return s
}
// Price set price
func (s *CreateOrderService) Price(price string) *CreateOrderService {
s.price = &price
return s
}
// NewClientOrderID set newClientOrderID
func (s *CreateOrderService) NewClientOrderID(newClientOrderID string) *CreateOrderService {
s.newClientOrderID = &newClientOrderID
return s
}
// StopPrice set stopPrice
func (s *CreateOrderService) StopPrice(stopPrice string) *CreateOrderService {
s.stopPrice = &stopPrice
return s
}
// WorkingType set workingType
func (s *CreateOrderService) WorkingType(workingType WorkingType) *CreateOrderService {
s.workingType = &workingType
return s
}
// ActivationPrice set activationPrice
func (s *CreateOrderService) ActivationPrice(activationPrice string) *CreateOrderService {
s.activationPrice = &activationPrice
return s
}
// CallbackRate set callbackRate
func (s *CreateOrderService) CallbackRate(callbackRate string) *CreateOrderService {
s.callbackRate = &callbackRate
return s
}
// PriceProtect set priceProtect
func (s *CreateOrderService) PriceProtect(priceProtect bool) *CreateOrderService {
s.priceProtect = &priceProtect
return s
}
// NewOrderResponseType set newOrderResponseType
func (s *CreateOrderService) NewOrderResponseType(newOrderResponseType NewOrderRespType) *CreateOrderService {
s.newOrderRespType = newOrderResponseType
return s
}
// ClosePosition set closePosition
func (s *CreateOrderService) ClosePosition(closePosition bool) *CreateOrderService {
s.closePosition = &closePosition
return s
}
func (s *CreateOrderService) createOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, err error) {
r := &request{
method: http.MethodPost,
endpoint: endpoint,
secType: secTypeSigned,
}
m := params{
"symbol": s.symbol,
"side": s.side,
"type": s.orderType,
"quantity": s.quantity,
"newOrderRespType": s.newOrderRespType,
}
if s.positionSide != nil {
m["positionSide"] = *s.positionSide
}
if s.timeInForce != nil {
m["timeInForce"] = *s.timeInForce
}
if s.reduceOnly != nil {
m["reduceOnly"] = *s.reduceOnly
}
if s.price != nil {
m["price"] = *s.price
}
if s.newClientOrderID != nil {
m["newClientOrderId"] = *s.newClientOrderID
}
if s.stopPrice != nil {
m["stopPrice"] = *s.stopPrice
}
if s.workingType != nil {
m["workingType"] = *s.workingType
}
if s.priceProtect != nil {
m["priceProtect"] = *s.priceProtect
}
if s.activationPrice != nil {
m["activationPrice"] = *s.activationPrice
}
if s.callbackRate != nil {
m["callbackRate"] = *s.callbackRate
}
if s.closePosition != nil {
m["closePosition"] = *s.closePosition
}
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 *CreateOrderResponse, err error) {
data, err := s.createOrder(ctx, "/dapi/v1/order", opts...)
if err != nil {
return nil, err
}
res = new(CreateOrderResponse)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// CreateOrderResponse define create order response
type CreateOrderResponse struct {
ClientOrderID string `json:"clientOrderId"`
CumQuantity string `json:"cumQty"`
CumBase string `json:"cumBase"`
ExecutedQuantity string `json:"executedQty"`
OrderID int64 `json:"orderId"`
AvgPrice string `json:"avgPrice"`
OrigQuantity string `json:"origQty"`
Price string `json:"price"`
ReduceOnly bool `json:"reduceOnly"`
Side SideType `json:"side"`
PositionSide PositionSideType `json:"positionSide"`
Status OrderStatusType `json:"status"`
StopPrice string `json:"stopPrice"`
ClosePosition bool `json:"closePosition"`
Symbol string `json:"symbol"`
Pair string `json:"pair"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
OrigType OrderType `json:"origType"`
ActivatePrice string `json:"activatePrice"`
PriceRate string `json:"priceRate"`
UpdateTime int64 `json:"updateTime"`
WorkingType WorkingType `json:"workingType"`
PriceProtect bool `json:"priceProtect"`
}
// ListOpenOrdersService list opened orders
type ListOpenOrdersService struct {
c *Client
symbol string
pair string
}
// Symbol set symbol
func (s *ListOpenOrdersService) Symbol(symbol string) *ListOpenOrdersService {
s.symbol = symbol
return s
}
// Pair set pair
func (s *ListOpenOrdersService) Pair(pair string) *ListOpenOrdersService {
s.pair = pair
return s
}
// Do send request
func (s *ListOpenOrdersService) Do(ctx context.Context, opts ...RequestOption) (res []*Order, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/dapi/v1/openOrders",
secType: secTypeSigned,
}
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
if s.pair != "" {
r.setParam("pair", s.symbol)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*Order{}, err
}
res = make([]*Order, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*Order{}, err
}
return res, nil
}
// GetOrderService get an order
type GetOrderService struct {
c *Client
symbol string
orderID *int64
origClientOrderID *string
}
// Symbol set symbol
func (s *GetOrderService) Symbol(symbol string) *GetOrderService {
s.symbol = symbol
return s
}
// OrderID set orderID
func (s *GetOrderService) OrderID(orderID int64) *GetOrderService {
s.orderID = &orderID
return s
}
// OrigClientOrderID set origClientOrderID
func (s *GetOrderService) OrigClientOrderID(origClientOrderID string) *GetOrderService {
s.origClientOrderID = &origClientOrderID
return s
}
// Do send request
func (s *GetOrderService) Do(ctx context.Context, opts ...RequestOption) (res *Order, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/dapi/v1/order",
secType: secTypeSigned,
}
r.setParam("symbol", s.symbol)
if s.orderID != nil {
r.setParam("orderId", *s.orderID)
}
if s.origClientOrderID != nil {
r.setParam("origClientOrderId", *s.origClientOrderID)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(Order)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// Order define order info
type Order struct {
AvgPrice string `json:"avgPrice"`
ClientOrderID string `json:"clientOrderId"`
CumBase string `json:"cumBase"`
ExecutedQuantity string `json:"executedQty"`
OrderID int64 `json:"orderId"`
OrigQuantity string `json:"origQty"`
OrigType OrderType `json:"origType"`
Price string `json:"price"`
ReduceOnly bool `json:"reduceOnly"`
Side SideType `json:"side"`
PositionSide PositionSideType `json:"positionSide"`
Status OrderStatusType `json:"status"`
StopPrice string `json:"stopPrice"`
ClosePosition bool `json:"closePosition"`
Symbol string `json:"symbol"`
Pair string `json:"pair"`
Time int64 `json:"time"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
ActivatePrice string `json:"activatePrice"`
PriceRate string `json:"priceRate"`
UpdateTime int64 `json:"updateTime"`
WorkingType WorkingType `json:"workingType"`
PriceProtect bool `json:"priceProtect"`
}
// ListOrdersService all account orders; active, canceled, or filled
type ListOrdersService struct {
c *Client
symbol string
pair string
orderID *int64
startTime *int64
endTime *int64
limit *int
}
// Symbol set symbol
func (s *ListOrdersService) Symbol(symbol string) *ListOrdersService {
s.symbol = symbol
return s
}
// Pair set pair
func (s *ListOrdersService) Pair(pair string) *ListOrdersService {
s.pair = pair
return s
}
// OrderID set orderID
func (s *ListOrdersService) OrderID(orderID int64) *ListOrdersService {
s.orderID = &orderID
return s
}
// StartTime set starttime
func (s *ListOrdersService) StartTime(startTime int64) *ListOrdersService {
s.startTime = &startTime
return s
}
// EndTime set endtime
func (s *ListOrdersService) EndTime(endTime int64) *ListOrdersService {
s.endTime = &endTime
return s
}
// Limit set limit
func (s *ListOrdersService) Limit(limit int) *ListOrdersService {
s.limit = &limit
return s
}
// Do send request
func (s *ListOrdersService) Do(ctx context.Context, opts ...RequestOption) (res []*Order, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/dapi/v1/allOrders",
secType: secTypeSigned,
}
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
if s.pair != "" {
r.setParam("pair", s.pair)
}
if s.orderID != nil {
r.setParam("orderId", *s.orderID)
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*Order{}, err
}
res = make([]*Order, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*Order{}, err
}
return res, nil
}
// CancelOrderService cancel an order
type CancelOrderService struct {
c *Client
symbol string
orderID *int64
origClientOrderID *string
}
// Symbol set symbol
func (s *CancelOrderService) Symbol(symbol string) *CancelOrderService {
s.symbol = symbol
return s
}
// OrderID set orderID
func (s *CancelOrderService) OrderID(orderID int64) *CancelOrderService {
s.orderID = &orderID
return s
}
// OrigClientOrderID set origClientOrderID
func (s *CancelOrderService) OrigClientOrderID(origClientOrderID string) *CancelOrderService {
s.origClientOrderID = &origClientOrderID
return s
}
// Do send request
func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res *CancelOrderResponse, err error) {
r := &request{
method: http.MethodDelete,
endpoint: "/dapi/v1/order",
secType: secTypeSigned,
}
r.setFormParam("symbol", s.symbol)
if s.orderID != nil {
r.setFormParam("orderId", *s.orderID)
}
if s.origClientOrderID != nil {
r.setFormParam("origClientOrderId", *s.origClientOrderID)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(CancelOrderResponse)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// CancelOrderResponse define response of canceling order
type CancelOrderResponse struct {
AvgPrice string `json:"avgPrice"`
ClientOrderID string `json:"clientOrderId"`
CumQuantity string `json:"cumQty"`
CumBase string `json:"cumBase"`
ExecutedQuantity string `json:"executedQty"`
OrderID int64 `json:"orderId"`
OrigQuantity string `json:"origQty"`
OrigType OrderType `json:"origType"`
Price string `json:"price"`
ReduceOnly bool `json:"reduceOnly"`
Side SideType `json:"side"`
PositionSide PositionSideType `json:"positionSide"`
Status OrderStatusType `json:"status"`
StopPrice string `json:"stopPrice"`
ClosePosition bool `json:"closePosition"`
Symbol string `json:"symbol"`
Pair string `json:"pair"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
ActivatePrice string `json:"activatePrice"`
PriceRate string `json:"priceRate"`
UpdateTime int64 `json:"updateTime"`
WorkingType WorkingType `json:"workingType"`
PriceProtect bool `json:"priceProtect"`
}
// CancelAllOpenOrdersService cancel all open orders
type CancelAllOpenOrdersService struct {
c *Client
symbol string
}
// Symbol set symbol
func (s *CancelAllOpenOrdersService) Symbol(symbol string) *CancelAllOpenOrdersService {
s.symbol = symbol
return s
}
// Do send request
func (s *CancelAllOpenOrdersService) Do(ctx context.Context, opts ...RequestOption) (err error) {
r := &request{
method: http.MethodDelete,
endpoint: "/dapi/v1/allOpenOrders",
secType: secTypeSigned,
}
r.setFormParam("symbol", s.symbol)
_, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
return nil
}
// ListLiquidationOrdersService list liquidation orders
type ListLiquidationOrdersService struct {
c *Client
symbol *string
pair *string
startTime *int64
endTime *int64
limit *int
}
// Symbol set symbol
func (s *ListLiquidationOrdersService) Symbol(symbol string) *ListLiquidationOrdersService {
s.symbol = &symbol
return s
}
// Pair set pair
func (s *ListLiquidationOrdersService) Pair(pair string) *ListLiquidationOrdersService {
s.pair = &pair
return s
}
// StartTime set startTime
func (s *ListLiquidationOrdersService) StartTime(startTime int64) *ListLiquidationOrdersService {
s.startTime = &startTime
return s
}
// EndTime set startTime
func (s *ListLiquidationOrdersService) EndTime(endTime int64) *ListLiquidationOrdersService {
s.endTime = &endTime
return s
}
// Limit set limit
func (s *ListLiquidationOrdersService) Limit(limit int) *ListLiquidationOrdersService {
s.limit = &limit
return s
}
// Do send request
func (s *ListLiquidationOrdersService) Do(ctx context.Context, opts ...RequestOption) (res []*LiquidationOrder, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/dapi/v1/allForceOrders",
secType: secTypeNone,
}
if s.pair != nil {
r.setParam("pair", *s.pair)
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*LiquidationOrder{}, err
}
res = make([]*LiquidationOrder, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*LiquidationOrder{}, err
}
return res, nil
}
// LiquidationOrder define liquidation order
type LiquidationOrder struct {
Symbol string `json:"symbol"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
AveragePrice string `json:"avragePrice"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
Time int64 `json:"time"`
}