forked from binance/binance-connector-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfutures_account.go
939 lines (834 loc) · 31.2 KB
/
futures_account.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
package binance_connector
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
)
// SideType define side type of order
type SideType string
// PositionSideType define position side type of order
type PositionSideType string
// OrderType define order type
type OrderType string
// NewOrderRespType define response JSON verbosity
type NewOrderRespType string
// OrderExecutionType define order execution type
type OrderExecutionType string
// OrderStatusType define order status type
type OrderStatusType string
// SymbolType define symbol type
type SymbolType string
// SymbolStatusType define symbol status type
type SymbolStatusType string
// SymbolFilterType define symbol filter type
type SymbolFilterType string
// SideEffectType define side effect type for orders
type SideEffectType string
// WorkingType define working type
type WorkingType string
// MarginType define margin type
type MarginType string
// ContractType define contract type
type ContractType string
// UserDataEventReasonType define reason type for user data event
type UserDataEventReasonType string
// ForceOrderCloseType define reason type for force order
type ForceOrderCloseType string
// Endpoints
const (
baseApiMainUrl = "https://fapi.binance.com"
baseApiTestnetUrl = "https://testnet.binancefuture.com"
)
// Global enums
const (
SideTypeBuy SideType = "BUY"
SideTypeSell SideType = "SELL"
PositionSideTypeBoth PositionSideType = "BOTH"
PositionSideTypeLong PositionSideType = "LONG"
PositionSideTypeShort PositionSideType = "SHORT"
OrderTypeLimit OrderType = "LIMIT"
OrderTypeMarket OrderType = "MARKET"
OrderTypeStop OrderType = "STOP"
OrderTypeStopMarket OrderType = "STOP_MARKET"
OrderTypeTakeProfit OrderType = "TAKE_PROFIT"
OrderTypeTakeProfitMarket OrderType = "TAKE_PROFIT_MARKET"
OrderTypeTrailingStopMarket OrderType = "TRAILING_STOP_MARKET"
TimeInForceTypeGTC TimeInForceType = "GTC" // Good Till Cancel
TimeInForceTypeIOC TimeInForceType = "IOC" // Immediate or Cancel
TimeInForceTypeFOK TimeInForceType = "FOK" // Fill or Kill
TimeInForceTypeGTX TimeInForceType = "GTX" // Good Till Crossing (Post Only)
NewOrderRespTypeACK NewOrderRespType = "ACK"
NewOrderRespTypeRESULT NewOrderRespType = "RESULT"
OrderExecutionTypeNew OrderExecutionType = "NEW"
OrderExecutionTypePartialFill OrderExecutionType = "PARTIAL_FILL"
OrderExecutionTypeFill OrderExecutionType = "FILL"
OrderExecutionTypeCanceled OrderExecutionType = "CANCELED"
OrderExecutionTypeCalculated OrderExecutionType = "CALCULATED"
OrderExecutionTypeExpired OrderExecutionType = "EXPIRED"
OrderExecutionTypeTrade OrderExecutionType = "TRADE"
OrderStatusTypeNew OrderStatusType = "NEW"
OrderStatusTypePartiallyFilled OrderStatusType = "PARTIALLY_FILLED"
OrderStatusTypeFilled OrderStatusType = "FILLED"
OrderStatusTypeCanceled OrderStatusType = "CANCELED"
OrderStatusTypeRejected OrderStatusType = "REJECTED"
OrderStatusTypeExpired OrderStatusType = "EXPIRED"
OrderStatusTypeNewInsurance OrderStatusType = "NEW_INSURANCE"
OrderStatusTypeNewADL OrderStatusType = "NEW_ADL"
SymbolTypeFuture SymbolType = "FUTURE"
WorkingTypeMarkPrice WorkingType = "MARK_PRICE"
WorkingTypeContractPrice WorkingType = "CONTRACT_PRICE"
SymbolStatusTypePreTrading SymbolStatusType = "PRE_TRADING"
SymbolStatusTypeTrading SymbolStatusType = "TRADING"
SymbolStatusTypePostTrading SymbolStatusType = "POST_TRADING"
SymbolStatusTypeEndOfDay SymbolStatusType = "END_OF_DAY"
SymbolStatusTypeHalt SymbolStatusType = "HALT"
SymbolStatusTypeAuctionMatch SymbolStatusType = "AUCTION_MATCH"
SymbolStatusTypeBreak SymbolStatusType = "BREAK"
SymbolFilterTypeLotSize SymbolFilterType = "LOT_SIZE"
SymbolFilterTypePrice SymbolFilterType = "PRICE_FILTER"
SymbolFilterTypePercentPrice SymbolFilterType = "PERCENT_PRICE"
SymbolFilterTypeMarketLotSize SymbolFilterType = "MARKET_LOT_SIZE"
SymbolFilterTypeMaxNumOrders SymbolFilterType = "MAX_NUM_ORDERS"
SymbolFilterTypeMaxNumAlgoOrders SymbolFilterType = "MAX_NUM_ALGO_ORDERS"
SymbolFilterTypeMinNotional SymbolFilterType = "MIN_NOTIONAL"
SideEffectTypeNoSideEffect SideEffectType = "NO_SIDE_EFFECT"
SideEffectTypeMarginBuy SideEffectType = "MARGIN_BUY"
SideEffectTypeAutoRepay SideEffectType = "AUTO_REPAY"
MarginTypeIsolated MarginType = "ISOLATED"
MarginTypeCrossed MarginType = "CROSSED"
ContractTypePerpetual ContractType = "PERPETUAL"
UserDataEventTypeListenKeyExpired UserDataEventType = "listenKeyExpired"
UserDataEventTypeMarginCall UserDataEventType = "MARGIN_CALL"
UserDataEventTypeAccountUpdate UserDataEventType = "ACCOUNT_UPDATE"
UserDataEventTypeOrderTradeUpdate UserDataEventType = "ORDER_TRADE_UPDATE"
UserDataEventTypeAccountConfigUpdate UserDataEventType = "ACCOUNT_CONFIG_UPDATE"
UserDataEventReasonTypeDeposit UserDataEventReasonType = "DEPOSIT"
UserDataEventReasonTypeWithdraw UserDataEventReasonType = "WITHDRAW"
UserDataEventReasonTypeOrder UserDataEventReasonType = "ORDER"
UserDataEventReasonTypeFundingFee UserDataEventReasonType = "FUNDING_FEE"
UserDataEventReasonTypeWithdrawReject UserDataEventReasonType = "WITHDRAW_REJECT"
UserDataEventReasonTypeAdjustment UserDataEventReasonType = "ADJUSTMENT"
UserDataEventReasonTypeInsuranceClear UserDataEventReasonType = "INSURANCE_CLEAR"
UserDataEventReasonTypeAdminDeposit UserDataEventReasonType = "ADMIN_DEPOSIT"
UserDataEventReasonTypeAdminWithdraw UserDataEventReasonType = "ADMIN_WITHDRAW"
UserDataEventReasonTypeMarginTransfer UserDataEventReasonType = "MARGIN_TRANSFER"
UserDataEventReasonTypeMarginTypeChange UserDataEventReasonType = "MARGIN_TYPE_CHANGE"
UserDataEventReasonTypeAssetTransfer UserDataEventReasonType = "ASSET_TRANSFER"
UserDataEventReasonTypeOptionsPremiumFee UserDataEventReasonType = "OPTIONS_PREMIUM_FEE"
UserDataEventReasonTypeOptionsSettleProfit UserDataEventReasonType = "OPTIONS_SETTLE_PROFIT"
ForceOrderCloseTypeLiquidation ForceOrderCloseType = "LIQUIDATION"
ForceOrderCloseTypeADL ForceOrderCloseType = "ADL"
)
// NewCreateOrderService init creating order service
func (c *Client) NewFuturesCreateOrderService() *FuturesCreateOrderService {
return &FuturesCreateOrderService{c: c}
}
func (c *Client) NewFuturesGetOrderService() *FuturesGetOrderService {
return &FuturesGetOrderService{c: c}
}
func (c *Client) NewFuturesGetBalanceService() *FuturesGetBalanceService {
return &FuturesGetBalanceService{c: c}
}
func (c *Client) NewFuturesGetPositionRiskService() *FuturesGetPositionRiskService {
return &FuturesGetPositionRiskService{c: c}
}
func (c *Client) NewFuturesGetIncomeService() *FuturesGetIncomeService {
return &FuturesGetIncomeService{c: c}
}
func (c *Client) NewFuturesUserTradesService() *FuturesUserTradesService {
return &FuturesUserTradesService{c: c}
}
func (c *Client) NewFuturesDeleteOrderService() *FuturesDeleteOrderService {
return &FuturesDeleteOrderService{c: c}
}
// NewGetAccountService init getting account service
func (c *Client) NewFuturesGetAccountService() *FuturesGetAccountService {
return &FuturesGetAccountService{c: c}
}
type FuturesCreateOrderService struct {
c *Client
symbol string
side SideType
positionSide *PositionSideType
orderType OrderType
timeInForce *TimeInForceType
quantity string
reduceOnly *string
price *string
newClientOrderID *string
stopPrice *string
workingType *WorkingType
activationPrice *string
callbackRate *string
priceProtect *string
newOrderRespType NewOrderRespType
closePosition *string
}
// Symbol set symbol
func (s *FuturesCreateOrderService) Symbol(symbol string) *FuturesCreateOrderService {
s.symbol = symbol
return s
}
// Side set side
func (s *FuturesCreateOrderService) Side(side SideType) *FuturesCreateOrderService {
s.side = side
return s
}
// PositionSide set side
func (s *FuturesCreateOrderService) PositionSide(positionSide PositionSideType) *FuturesCreateOrderService {
s.positionSide = &positionSide
return s
}
// Type set type
func (s *FuturesCreateOrderService) Type(orderType OrderType) *FuturesCreateOrderService {
s.orderType = orderType
return s
}
// TimeInForce set timeInForce
func (s *FuturesCreateOrderService) TimeInForce(timeInForce TimeInForceType) *FuturesCreateOrderService {
s.timeInForce = &timeInForce
return s
}
// Quantity set quantity
func (s *FuturesCreateOrderService) Quantity(quantity string) *FuturesCreateOrderService {
s.quantity = quantity
return s
}
// ReduceOnly set reduceOnly
func (s *FuturesCreateOrderService) ReduceOnly(reduceOnly bool) *FuturesCreateOrderService {
reduceOnlyStr := strconv.FormatBool(reduceOnly)
s.reduceOnly = &reduceOnlyStr
return s
}
// Price set price
func (s *FuturesCreateOrderService) Price(price string) *FuturesCreateOrderService {
s.price = &price
return s
}
// NewClientOrderID set newClientOrderID
func (s *FuturesCreateOrderService) NewClientOrderID(newClientOrderID string) *FuturesCreateOrderService {
s.newClientOrderID = &newClientOrderID
return s
}
// StopPrice set stopPrice
func (s *FuturesCreateOrderService) StopPrice(stopPrice string) *FuturesCreateOrderService {
s.stopPrice = &stopPrice
return s
}
// WorkingType set workingType
func (s *FuturesCreateOrderService) WorkingType(workingType WorkingType) *FuturesCreateOrderService {
s.workingType = &workingType
return s
}
// ActivationPrice set activationPrice
func (s *FuturesCreateOrderService) ActivationPrice(activationPrice string) *FuturesCreateOrderService {
s.activationPrice = &activationPrice
return s
}
// CallbackRate set callbackRate
func (s *FuturesCreateOrderService) CallbackRate(callbackRate string) *FuturesCreateOrderService {
s.callbackRate = &callbackRate
return s
}
// PriceProtect set priceProtect
func (s *FuturesCreateOrderService) PriceProtect(priceProtect bool) *FuturesCreateOrderService {
priceProtectStr := strconv.FormatBool(priceProtect)
s.priceProtect = &priceProtectStr
return s
}
// NewOrderResponseType set newOrderResponseType
func (s *FuturesCreateOrderService) NewOrderResponseType(newOrderResponseType NewOrderRespType) *FuturesCreateOrderService {
s.newOrderRespType = newOrderResponseType
return s
}
// ClosePosition set closePosition
func (s *FuturesCreateOrderService) ClosePosition(closePosition bool) *FuturesCreateOrderService {
closePositionStr := strconv.FormatBool(closePosition)
s.closePosition = &closePositionStr
return s
}
func (s *FuturesCreateOrderService) 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,
"newOrderRespType": s.newOrderRespType,
}
if s.quantity != "" {
m["quantity"] = s.quantity
}
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
}
for key, val := range m {
r.setParam(key, val)
}
data, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return []byte{}, err
}
return data, nil
}
// Do send request
func (s *FuturesCreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res *CreateOrderResponse, err error) {
data, err := s.createOrder(ctx, "/fapi/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 {
Symbol string `json:"symbol"` //
OrderID int64 `json:"orderId"` //
ClientOrderID string `json:"clientOrderId"` //
Price string `json:"price"` //
OrigQuantity string `json:"origQty"` //
ExecutedQuantity string `json:"executedQty"` //
CumQuote string `json:"cumQuote"` //
ReduceOnly bool `json:"reduceOnly"` //
Status OrderStatusType `json:"status"` //
StopPrice string `json:"stopPrice"` // please ignore when order type is TRAILING_STOP_MARKET
TimeInForce TimeInForceType `json:"timeInForce"` //
Type OrderType `json:"type"` //
Side SideType `json:"side"` //
UpdateTime int64 `json:"updateTime"` // update time
WorkingType WorkingType `json:"workingType"` //
ActivatePrice string `json:"activatePrice"` // activation price, only return with TRAILING_STOP_MARKET order
PriceRate string `json:"priceRate"` // callback rate, only return with TRAILING_STOP_MARKET order
AvgPrice string `json:"avgPrice"` //
PositionSide PositionSideType `json:"positionSide"` //
ClosePosition bool `json:"closePosition"` // if Close-All
PriceProtect bool `json:"priceProtect"` // if conditional order trigger is protected
PriceMatch string `json:"priceMatch"` // price match mode
SelfTradePreventionMode string `json:"selfTradePreventionMode"` // self trading prevention mode
GoodTillDate int64 `json:"goodTillDate"` // order pre-set auto cancel time for TIF GTD order
CumQty string `json:"cumQty"` //
OrigType OrderType `json:"origType"` //
RateLimitOrder10s string `json:"rateLimitOrder10s,omitempty"` //
RateLimitOrder1m string `json:"rateLimitOrder1m,omitempty"` //
}
// GetOrderService get an order
type FuturesGetOrderService struct {
c *Client
symbol string
orderID *int64
origClientOrderID *string
}
// Symbol set symbol
func (s *FuturesGetOrderService) Symbol(symbol string) *FuturesGetOrderService {
s.symbol = symbol
return s
}
// OrderID set orderID
func (s *FuturesGetOrderService) OrderID(orderID int64) *FuturesGetOrderService {
s.orderID = &orderID
return s
}
// OrigClientOrderID set origClientOrderID
func (s *FuturesGetOrderService) OrigClientOrderID(origClientOrderID string) *FuturesGetOrderService {
s.origClientOrderID = &origClientOrderID
return s
}
// Do send request
func (s *FuturesGetOrderService) Do(ctx context.Context, opts ...RequestOption) (res *Order, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/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 {
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
Price string `json:"price"`
ReduceOnly bool `json:"reduceOnly"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CumQuantity string `json:"cumQty"`
CumQuote string `json:"cumQuote"`
Status OrderStatusType `json:"status"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
StopPrice string `json:"stopPrice"`
Time int64 `json:"time"`
UpdateTime int64 `json:"updateTime"`
WorkingType WorkingType `json:"workingType"`
ActivatePrice string `json:"activatePrice"`
PriceRate string `json:"priceRate"`
AvgPrice string `json:"avgPrice"`
OrigType OrderType `json:"origType"`
PositionSide PositionSideType `json:"positionSide"`
PriceProtect bool `json:"priceProtect"`
ClosePosition bool `json:"closePosition"`
PriceMatch string `json:"priceMatch"`
SelfTradePreventionMode string `json:"selfTradePreventionMode"`
GoodTillDate int64 `json:"goodTillDate"`
}
// GetBalanceService get account balance
type FuturesGetBalanceService struct {
c *Client
}
// Balance define user balance of your account
type FuturesBalance struct {
AccountAlias string `json:"accountAlias"`
Asset string `json:"asset"`
Balance string `json:"balance"`
CrossWalletBalance string `json:"crossWalletBalance"`
CrossUnPnl string `json:"crossUnPnl"`
AvailableBalance string `json:"availableBalance"`
MaxWithdrawAmount string `json:"maxWithdrawAmount"`
}
// Do send request
func (s *FuturesGetBalanceService) Do(ctx context.Context, opts ...RequestOption) (res []*FuturesBalance, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v2/balance",
secType: secTypeSigned,
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*FuturesBalance{}, err
}
res = make([]*FuturesBalance, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*FuturesBalance{}, err
}
return res, nil
}
// GetPositionRiskService get account balance
type FuturesGetPositionRiskService struct {
c *Client
symbol string
}
// Symbol set symbol
func (s *FuturesGetPositionRiskService) Symbol(symbol string) *FuturesGetPositionRiskService {
s.symbol = symbol
return s
}
// Do send request
func (s *FuturesGetPositionRiskService) Do(ctx context.Context, opts ...RequestOption) (res []*PositionRisk, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v2/positionRisk",
secType: secTypeSigned,
}
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*PositionRisk{}, err
}
res = make([]*PositionRisk, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*PositionRisk{}, err
}
return res, nil
}
// PositionRisk define position risk info
type PositionRisk struct {
EntryPrice string `json:"entryPrice"`
BreakEvenPrice string `json:"breakEvenPrice"`
MarginType string `json:"marginType"`
IsAutoAddMargin string `json:"isAutoAddMargin"`
IsolatedMargin string `json:"isolatedMargin"`
Leverage string `json:"leverage"`
LiquidationPrice string `json:"liquidationPrice"`
MarkPrice string `json:"markPrice"`
MaxNotionalValue string `json:"maxNotionalValue"`
PositionAmt string `json:"positionAmt"`
Symbol string `json:"symbol"`
UnRealizedProfit string `json:"unRealizedProfit"`
PositionSide string `json:"positionSide"`
Notional string `json:"notional"`
IsolatedWallet string `json:"isolatedWallet"`
}
type FuturesGetIncomeService struct {
c *Client
symbol string
incomeType string
startTime int64
endTime int64
page int64
limit int64
recvWindow int64
}
// Symbol set symbol
func (s *FuturesGetIncomeService) Symbol(symbol string) *FuturesGetIncomeService {
s.symbol = symbol
return s
}
func (s *FuturesGetIncomeService) IncomeType(incomeType string) *FuturesGetIncomeService {
s.incomeType = incomeType
return s
}
func (s *FuturesGetIncomeService) StartTime(startTime int64) *FuturesGetIncomeService {
s.startTime = startTime
return s
}
func (s *FuturesGetIncomeService) EndTime(endTime int64) *FuturesGetIncomeService {
s.endTime = endTime
return s
}
func (s *FuturesGetIncomeService) Page(page int64) *FuturesGetIncomeService {
s.page = page
return s
}
func (s *FuturesGetIncomeService) Limit(limit int64) *FuturesGetIncomeService {
s.limit = limit
return s
}
type Income struct {
Symbol string `json:"symbol"`
IncomeType string `json:"incomeType"`
Income string `json:"income"`
Asset string `json:"asset"`
Info string `json:"info"`
Time int64 `json:"time"`
TranID int64 `json:"tranId"`
TradeID string `json:"tradeId"`
}
// Do send request
func (s *FuturesGetIncomeService) Do(ctx context.Context, opts ...RequestOption) (res []*Income, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v1/income",
secType: secTypeSigned,
}
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
if s.incomeType != "" {
r.setParam("incomeType", s.incomeType)
}
if s.startTime != 0 {
r.setParam("startTime", fmt.Sprintf("%d", s.startTime))
}
if s.endTime != 0 {
r.setParam("endTime", fmt.Sprintf("%d", s.endTime))
}
if s.page != 0 {
r.setParam("page", fmt.Sprintf("%d", s.page))
}
if s.limit != 0 {
r.setParam("limit", fmt.Sprintf("%d", s.limit))
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*Income{}, err
}
res = make([]*Income, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*Income{}, err
}
return res, nil
}
type FuturesUserTradesService struct {
c *Client
symbol string
orderId int64
startTime int64
endTime int64
fromId int64
limit int64
recvWindow int64
}
// Symbol set symbol
func (s *FuturesUserTradesService) Symbol(symbol string) *FuturesUserTradesService {
s.symbol = symbol
return s
}
func (s *FuturesUserTradesService) OrderID(orderId int64) *FuturesUserTradesService {
s.orderId = orderId
return s
}
func (s *FuturesUserTradesService) StartTime(startTime int64) *FuturesUserTradesService {
s.startTime = startTime
return s
}
func (s *FuturesUserTradesService) EndTime(endTime int64) *FuturesUserTradesService {
s.endTime = endTime
return s
}
func (s *FuturesUserTradesService) FromId(fromId int64) *FuturesUserTradesService {
s.fromId = fromId
return s
}
func (s *FuturesUserTradesService) Limit(limit int64) *FuturesUserTradesService {
s.limit = limit
return s
}
type UserTradesInfo struct {
Buyer bool `json:"buyer"`
Commission string `json:"commission"`
CommissionAsset string `json:"commissionAsset"`
ID int `json:"id"`
Maker bool `json:"maker"`
OrderID int `json:"orderId"`
Price string `json:"price"`
Qty string `json:"qty"`
QuoteQty string `json:"quoteQty"`
RealizedPnl string `json:"realizedPnl"`
Side string `json:"side"`
PositionSide string `json:"positionSide"`
Symbol string `json:"symbol"`
Time int64 `json:"time"`
}
// Do send request
func (s *FuturesUserTradesService) Do(ctx context.Context, opts ...RequestOption) (res []*UserTradesInfo, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v1/userTrades",
secType: secTypeSigned,
}
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
if s.orderId != 0 {
r.setParam("orderId", fmt.Sprintf("%d", s.orderId))
}
if s.startTime != 0 {
r.setParam("startTime", fmt.Sprintf("%d", s.startTime))
}
if s.endTime != 0 {
r.setParam("endTime", fmt.Sprintf("%d", s.endTime))
}
if s.fromId != 0 {
r.setParam("fromId", fmt.Sprintf("%d", s.fromId))
}
if s.limit != 0 {
r.setParam("limit", fmt.Sprintf("%d", s.limit))
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*UserTradesInfo{}, err
}
res = make([]*UserTradesInfo, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*UserTradesInfo{}, err
}
return res, nil
}
type FuturesDeleteOrderService struct {
c *Client
symbol string
orderID int64
clientOrderID string
recvWindow int64
}
func (s *FuturesDeleteOrderService) Symbol(symbol string) *FuturesDeleteOrderService {
s.symbol = symbol
return s
}
func (s *FuturesDeleteOrderService) OrderID(orderId int64) *FuturesDeleteOrderService {
s.orderID = orderId
return s
}
func (s *FuturesDeleteOrderService) ClientOrderID(clientOrderId string) *FuturesDeleteOrderService {
s.clientOrderID = clientOrderId
return s
}
func (s *FuturesDeleteOrderService) RecvWindow(recvWindow int64) *FuturesDeleteOrderService {
s.recvWindow = recvWindow
return s
}
type CancelOrderInfo struct {
ClientOrderID string `json:"clientOrderId"`
CumQty string `json:"cumQty"`
CumQuote string `json:"cumQuote"`
ExecutedQty string `json:"executedQty"`
OrderID int `json:"orderId"`
OrigQty string `json:"origQty"`
Price string `json:"price"`
ReduceOnly bool `json:"reduceOnly"`
Side string `json:"side"`
PositionSide string `json:"positionSide"`
Status string `json:"status"`
StopPrice string `json:"stopPrice"`
ClosePosition bool `json:"closePosition"`
Symbol string `json:"symbol"`
TimeInForce string `json:"timeInForce"`
OrigType string `json:"origType"`
Type string `json:"type"`
ActivatePrice string `json:"activatePrice"`
PriceRate string `json:"priceRate"`
UpdateTime int64 `json:"updateTime"`
WorkingType string `json:"workingType"`
PriceProtect bool `json:"priceProtect"`
PriceMatch string `json:"priceMatch"`
SelfTradePreventionMode string `json:"selfTradePreventionMode"`
GoodTillDate int `json:"goodTillDate"`
}
func (s *FuturesDeleteOrderService) Do(ctx context.Context, opts ...RequestOption) (res *CancelOrderInfo, err error) {
r := &request{
method: http.MethodDelete,
endpoint: "/fapi/v1/order",
secType: secTypeSigned,
}
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
if s.orderID > 0 {
r.setParam("orderId", fmt.Sprintf("%d", s.orderID))
}
if s.clientOrderID != "" {
r.setParam("origClientOrderId", s.clientOrderID)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &CancelOrderInfo{}, err
}
res = new(CancelOrderInfo)
err = json.Unmarshal(data, &res)
if err != nil {
return &CancelOrderInfo{}, err
}
return res, nil
}
// FuturesGetAccountService get account info
type FuturesGetAccountService struct {
c *Client
}
// Do send request
func (s *FuturesGetAccountService) Do(ctx context.Context, opts ...RequestOption) (res *Account, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v2/account",
secType: secTypeSigned,
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(Account)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// Account define account info
type Account struct {
Assets []*AccountAsset `json:"assets"`
FeeTier int `json:"feeTier"`
CanTrade bool `json:"canTrade"`
CanDeposit bool `json:"canDeposit"`
CanWithdraw bool `json:"canWithdraw"`
UpdateTime int64 `json:"updateTime"`
MultiAssetsMargin bool `json:"multiAssetsMargin"`
TotalInitialMargin string `json:"totalInitialMargin"`
TotalMaintMargin string `json:"totalMaintMargin"`
TotalWalletBalance string `json:"totalWalletBalance"`
TotalUnrealizedProfit string `json:"totalUnrealizedProfit"`
TotalMarginBalance string `json:"totalMarginBalance"`
TotalPositionInitialMargin string `json:"totalPositionInitialMargin"`
TotalOpenOrderInitialMargin string `json:"totalOpenOrderInitialMargin"`
TotalCrossWalletBalance string `json:"totalCrossWalletBalance"`
TotalCrossUnPnl string `json:"totalCrossUnPnl"`
AvailableBalance string `json:"availableBalance"`
MaxWithdrawAmount string `json:"maxWithdrawAmount"`
Positions []*AccountPosition `json:"positions"`
}
// AccountAsset define account asset
type AccountAsset struct {
Asset string `json:"asset"`
InitialMargin string `json:"initialMargin"`
MaintMargin string `json:"maintMargin"`
MarginBalance string `json:"marginBalance"`
MaxWithdrawAmount string `json:"maxWithdrawAmount"`
OpenOrderInitialMargin string `json:"openOrderInitialMargin"`
PositionInitialMargin string `json:"positionInitialMargin"`
UnrealizedProfit string `json:"unrealizedProfit"`
WalletBalance string `json:"walletBalance"`
CrossWalletBalance string `json:"crossWalletBalance"`
CrossUnPnl string `json:"crossUnPnl"`
AvailableBalance string `json:"availableBalance"`
MarginAvailable bool `json:"marginAvailable"`
UpdateTime int64 `json:"updateTime"`
}
// AccountPosition define account position
type AccountPosition struct {
Isolated bool `json:"isolated"`
Leverage string `json:"leverage"`
InitialMargin string `json:"initialMargin"`
MaintMargin string `json:"maintMargin"`
OpenOrderInitialMargin string `json:"openOrderInitialMargin"`
PositionInitialMargin string `json:"positionInitialMargin"`
Symbol string `json:"symbol"`
UnrealizedProfit string `json:"unrealizedProfit"`
EntryPrice string `json:"entryPrice"`
MaxNotional string `json:"maxNotional"`
PositionSide PositionSideType `json:"positionSide"`
PositionAmt string `json:"positionAmt"`
Notional string `json:"notional"`
BidNotional string `json:"bidNotional"`
AskNotional string `json:"askNotional"`
UpdateTime int64 `json:"updateTime"`
}