forked from adshao/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_service.go
621 lines (561 loc) · 17.2 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
package binance
import (
"context"
"encoding/json"
)
// CreateOrderService create order
type CreateOrderService struct {
c *Client
symbol string
side SideType
orderType OrderType
timeInForce *TimeInForceType
newOrderRespType *NewOrderRespType
quantity *string
quoteOrderQty *string
price *string
newClientOrderID *string
stopPrice *string
icebergQuantity *string
}
// 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
}
// 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
}
// QuoteOrderQty set quoteOrderQty
func (s *CreateOrderService) QuoteOrderQty(quoteOrderQty string) *CreateOrderService {
s.quoteOrderQty = "eOrderQty
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
}
// IcebergQuantity set icebergQuantity
func (s *CreateOrderService) IcebergQuantity(icebergQuantity string) *CreateOrderService {
s.icebergQuantity = &icebergQuantity
return s
}
// NewOrderRespType set icebergQuantity
func (s *CreateOrderService) NewOrderRespType(newOrderRespType NewOrderRespType) *CreateOrderService {
s.newOrderRespType = &newOrderRespType
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,
"type": s.orderType,
}
if s.quantity != nil {
m["quantity"] = *s.quantity
}
if s.quoteOrderQty != nil {
m["quoteOrderQty"] = *s.quoteOrderQty
}
if s.timeInForce != nil {
m["timeInForce"] = *s.timeInForce
}
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.icebergQuantity != nil {
m["icebergQty"] = *s.icebergQuantity
}
if s.newOrderRespType != nil {
m["newOrderRespType"] = *s.newOrderRespType
}
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, "/api/v3/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
}
// Test send test api to check if the request is valid
func (s *CreateOrderService) Test(ctx context.Context, opts ...RequestOption) (err error) {
_, err = s.createOrder(ctx, "/api/v3/order/test", opts...)
return err
}
// CreateOrderResponse define create order response
type CreateOrderResponse struct {
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
TransactTime int64 `json:"transactTime"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CummulativeQuoteQuantity string `json:"cummulativeQuoteQty"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
Fills []*Fill `json:"fills"`
}
// Fill may be returned in an array of fills in a CreateOrderResponse.
type Fill struct {
Price string `json:"price"`
Quantity string `json:"qty"`
Commission string `json:"commission"`
CommissionAsset string `json:"commissionAsset"`
}
// CreateOCOService create order
type CreateOCOService struct {
c *Client
symbol string
listClientOrderID *string
side SideType
quantity *string
limitClientOrderID *string
price *string
limitIcebergQty *string
stopClientOrderID *string
stopPrice *string
stopLimitPrice *string
stopIcebergQty *string
stopLimitTimeInForce *TimeInForceType
newOrderRespType *NewOrderRespType
}
// Symbol set symbol
func (s *CreateOCOService) Symbol(symbol string) *CreateOCOService {
s.symbol = symbol
return s
}
// Side set side
func (s *CreateOCOService) Side(side SideType) *CreateOCOService {
s.side = side
return s
}
// Quantity set quantity
func (s *CreateOCOService) Quantity(quantity string) *CreateOCOService {
s.quantity = &quantity
return s
}
// LimitClientOrderID set limitClientOrderID
func (s *CreateOCOService) LimitClientOrderID(limitClientOrderID string) *CreateOCOService {
s.limitClientOrderID = &limitClientOrderID
return s
}
// Price set price
func (s *CreateOCOService) Price(price string) *CreateOCOService {
s.price = &price
return s
}
// limitIcebergQuantity set limitIcebergQuantity
func (s *CreateOCOService) limitIcebergQuantity(limitIcebergQty string) *CreateOCOService {
s.limitIcebergQty = &limitIcebergQty
return s
}
// StopClientOrderID set stopClientOrderID
func (s *CreateOCOService) StopClientOrderID(stopClientOrderID string) *CreateOCOService {
s.stopClientOrderID = &stopClientOrderID
return s
}
// StopPrice set stop price
func (s *CreateOCOService) StopPrice(stopPrice string) *CreateOCOService {
s.stopPrice = &stopPrice
return s
}
// StopLimitPrice set stop limit price
func (s *CreateOCOService) StopLimitPrice(stopLimitPrice string) *CreateOCOService {
s.stopLimitPrice = &stopLimitPrice
return s
}
// StopIcebergQty set stop limit price
func (s *CreateOCOService) StopIcebergQty(stopIcebergQty string) *CreateOCOService {
s.stopIcebergQty = &stopIcebergQty
return s
}
// StopLimitTimeInForce set stopLimitTimeInForce
func (s *CreateOCOService) StopLimitTimeInForce(stopLimitTimeInForce TimeInForceType) *CreateOCOService {
s.stopLimitTimeInForce = &stopLimitTimeInForce
return s
}
// NewOrderRespType set icebergQuantity
func (s *CreateOCOService) NewOrderRespType(newOrderRespType NewOrderRespType) *CreateOCOService {
s.newOrderRespType = &newOrderRespType
return s
}
func (s *CreateOCOService) 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,
"quantity": *s.quantity,
"price": *s.price,
"stopPrice": *s.stopPrice,
}
if s.listClientOrderID != nil {
m["listClientOrderId"] = *s.listClientOrderID
}
if s.limitClientOrderID != nil {
m["limitClientOrderId"] = *s.limitClientOrderID
}
if s.limitIcebergQty != nil {
m["limitIcebergQty"] = *s.limitIcebergQty
}
if s.stopClientOrderID != nil {
m["stopClientOrderId"] = *s.stopClientOrderID
}
if s.stopLimitPrice != nil {
m["stopLimitPrice"] = *s.stopLimitPrice
}
if s.stopIcebergQty != nil {
m["stopIcebergQty"] = *s.stopIcebergQty
}
if s.stopLimitTimeInForce != nil {
m["stopLimitTimeInForce"] = *s.stopLimitTimeInForce
}
if s.newOrderRespType != nil {
m["newOrderRespType"] = *s.newOrderRespType
}
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 *CreateOCOService) Do(ctx context.Context, opts ...RequestOption) (res *CreateOCOResponse, err error) {
data, err := s.createOrder(ctx, "/api/v3/order/oco", opts...)
if err != nil {
return nil, err
}
res = new(CreateOCOResponse)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// CreateOCOResponse define create order response
type CreateOCOResponse struct {
OrderListID int64 `json:"orderListId"`
ContingencyType string `json:"contingencyType"`
ListStatusType string `json:"listStatusType"`
ListOrderStatus string `json:"listOrderStatus"`
ListClientOrderID string `json:"listClientOrderId"`
TransactionTime int64 `json:"transactionTime"`
Symbol string `json:"symbol"`
Orders []*OCOOrder `json:"orders"`
OrderReports []*OCOOrderReport `json:"orderReports"`
}
// OCOOrder may be returned in an array of OCOOrder in a CreateOCOResponse.
type OCOOrder struct {
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
}
// OCOOrderReport may be returned in an array of OCOOrderReport in a CreateOCOResponse.
type OCOOrderReport struct {
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
OrderListID int64 `json:"orderListId"`
ClientOrderID string `json:"clientOrderId"`
TransactionTime int64 `json:"transactionTime"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CummulativeQuoteQuantity string `json:"cummulativeQuoteQty"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
StopPrice string `json:"stopPrice"`
}
// 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 []*Order, err error) {
r := &request{
method: "GET",
endpoint: "/api/v3/openOrders",
secType: secTypeSigned,
}
if s.symbol != "" {
r.setParam("symbol", 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: "GET",
endpoint: "/api/v3/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 {
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CummulativeQuoteQuantity string `json:"cummulativeQuoteQty"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
StopPrice string `json:"stopPrice"`
IcebergQuantity string `json:"icebergQty"`
Time int64 `json:"time"`
UpdateTime int64 `json:"updateTime"`
IsWorking bool `json:"isWorking"`
}
// ListOrdersService all account orders; active, canceled, or filled
type ListOrdersService struct {
c *Client
symbol string
orderID *int64
startTime *int64
endTime *int64
limit *int
}
// Symbol set symbol
func (s *ListOrdersService) Symbol(symbol string) *ListOrdersService {
s.symbol = symbol
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: "GET",
endpoint: "/api/v3/allOrders",
secType: secTypeSigned,
}
r.setParam("symbol", s.symbol)
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
newClientOrderID *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
}
// NewClientOrderID set newClientOrderID
func (s *CancelOrderService) NewClientOrderID(newClientOrderID string) *CancelOrderService {
s.newClientOrderID = &newClientOrderID
return s
}
// Do send request
func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res *CancelOrderResponse, err error) {
r := &request{
method: "DELETE",
endpoint: "/api/v3/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)
}
if s.newClientOrderID != nil {
r.setFormParam("newClientOrderId", *s.newClientOrderID)
}
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 {
Symbol string `json:"symbol"`
OrigClientOrderID string `json:"origClientOrderId"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
TransactTime int64 `json:"transactTime"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CummulativeQuoteQuantity string `json:"cummulativeQuoteQty"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
}