forked from binance-exchange/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binance.go
801 lines (685 loc) · 20.1 KB
/
binance.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
package binance
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
// Binance is wrapper for Binance API.
//
// Read web documentation for more endpoints descriptions and list of
// mandatory and optional params. Wrapper is not responsible for client-side
// validation and only sends requests further.
//
// For each API-defined enum there's a special type and list of defined
// enum values to be used.
type Binance interface {
// Ping tests connectivity.
Ping() error
// Time returns server time.
Time() (time.Time, error)
// OrderBook returns list of orders.
OrderBook(obr OrderBookRequest) (*OrderBook, error)
// AggTrades returns compressed/aggregate list of trades.
AggTrades(atr AggTradesRequest) ([]*AggTrade, error)
// Klines returns klines/candlestick data.
Klines(kr KlinesRequest) ([]*Kline, error)
// Ticker24 returns 24hr price change statistics.
Ticker24(tr TickerRequest) (*Ticker24, error)
// TickerAllPrices returns ticker data for symbols.
TickerAllPrices() ([]*PriceTicker, error)
// TickerAllBooks returns tickers for all books.
TickerAllBooks() ([]*BookTicker, error)
// AvgPrice returnns weighted curred price for given symbol.
AvgPrice(ar AvgPriceRequest) (*AvgPrice, error)
// NewOrder places new order and returns ProcessedOrder.
NewOrder(nor NewOrderRequest) (*ProcessedOrder, error)
// NewOrder places testing order.
NewOrderTest(nor NewOrderRequest) error
// QueryOrder returns data about existing order.
QueryOrder(qor QueryOrderRequest) (*ExecutedOrder, error)
// CancelOrder cancels order.
CancelOrder(cor CancelOrderRequest) (*CanceledOrder, error)
// OpenOrders returns list of open orders.
OpenOrders(oor OpenOrdersRequest) ([]*ExecutedOrder, error)
// AllOrders returns list of all previous orders.
AllOrders(aor AllOrdersRequest) ([]*ExecutedOrder, error)
// Account returns account data.
Account(ar AccountRequest) (*Account, error)
// MyTrades list user's trades.
MyTrades(mtr MyTradesRequest) ([]*Trade, error)
// Withdraw executes withdrawal.
Withdraw(wr WithdrawRequest) (*WithdrawResult, error)
// DepositHistory lists deposit data.
DepositHistory(hr HistoryRequest) ([]*Deposit, error)
// WithdrawHistory lists withdraw data.
WithdrawHistory(hr HistoryRequest) ([]*Withdrawal, error)
// StartUserDataStream starts stream and returns Stream with ListenKey.
StartUserDataStream() (*Stream, error)
// KeepAliveUserDataStream prolongs stream livespan.
KeepAliveUserDataStream(s *Stream) error
// CloseUserDataStream closes opened stream.
CloseUserDataStream(s *Stream) error
DepthWebsocket(dwr DepthWebsocketRequest) (chan *DepthEvent, chan struct{}, error)
KlineWebsocket(kwr KlineWebsocketRequest) (chan *KlineEvent, chan struct{}, error)
TradeWebsocket(twr TradeWebsocketRequest) (chan *AggTradeEvent, chan struct{}, error)
SingleTradeWebsocket(twr TradeWebsocketRequest) (chan *SingleTradeEvent, chan struct{}, error)
UserDataWebsocket(udwr UserDataWebsocketRequest) (*UserDataEventChannels, chan struct{}, error)
ExchangeInfo() (*ExchangeInfo, error)
}
type binance struct {
Service Service
}
// Error represents Binance error structure with error code and message.
type Error struct {
Code int `json:"code"`
Message string `json:"msg"`
}
// Error returns formatted error message.
func (e Error) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.Message)
}
// NewBinance returns Binance instance.
func NewBinance(service Service) Binance {
return &binance{
Service: service,
}
}
// Ping tests connectivity.
func (b *binance) Ping() error {
return b.Service.Ping()
}
// Time returns server time.
func (b *binance) Time() (time.Time, error) {
return b.Service.Time()
}
// OrderBook represents Bids and Asks.
type OrderBook struct {
LastUpdateID int `json:"lastUpdateId"`
Bids []*Order
Asks []*Order
}
type DepthEvent struct {
WSEvent
UpdateID int
OrderBook
}
// Order represents single order information.
type Order struct {
Price float64
Quantity float64
}
// OrderBookRequest represents OrderBook request data.
type OrderBookRequest struct {
Symbol string
Limit int
}
// OrderBook returns list of orders.
func (b *binance) OrderBook(obr OrderBookRequest) (*OrderBook, error) {
return b.Service.OrderBook(obr)
}
// AggTrade represents aggregated trade.
type AggTrade struct {
ID int
Price float64
Quantity float64
FirstTradeID int
LastTradeID int
Timestamp time.Time
BuyerMaker bool
BestPriceMatch bool
}
type AggTradeEvent struct {
WSEvent
AggTrade
}
type SingleTrade struct {
ID int
Price float64
Quantity float64
BuyerOrderID int
SellerOrderID int
Timestamp time.Time
BuyerMaker bool
}
type SingleTradeEvent struct {
WSEvent
SingleTrade
}
// AggTradesRequest represents AggTrades request data.
type AggTradesRequest struct {
Symbol string
FromID int64
StartTime int64
EndTime int64
Limit int
}
// AggTrades returns compressed/aggregate list of trades.
func (b *binance) AggTrades(atr AggTradesRequest) ([]*AggTrade, error) {
return b.Service.AggTrades(atr)
}
// KlinesRequest represents Klines request data.
type KlinesRequest struct {
Symbol string
Interval Interval
Limit int
StartTime int64
EndTime int64
}
// Kline represents single Kline information.
type Kline struct {
OpenTime time.Time
Open float64
High float64
Low float64
Close float64
Volume float64
CloseTime time.Time
QuoteAssetVolume float64
NumberOfTrades int
TakerBuyBaseAssetVolume float64
TakerBuyQuoteAssetVolume float64
}
type KlineEvent struct {
WSEvent
Interval Interval
FirstTradeID int64
LastTradeID int64
Final bool
Kline
}
// Klines returns klines/candlestick data.
func (b *binance) Klines(kr KlinesRequest) ([]*Kline, error) {
return b.Service.Klines(kr)
}
// TickerRequest represents Ticker request data.
type TickerRequest struct {
Symbol string
}
// Ticker24 represents data for 24hr ticker.
type Ticker24 struct {
PriceChange float64
PriceChangePercent float64
WeightedAvgPrice float64
PrevClosePrice float64
LastPrice float64
BidPrice float64
AskPrice float64
OpenPrice float64
HighPrice float64
LowPrice float64
Volume float64
OpenTime time.Time
CloseTime time.Time
FirstID int
LastID int
Count int
}
// Ticker24 returns 24hr price change statistics.
func (b *binance) Ticker24(tr TickerRequest) (*Ticker24, error) {
return b.Service.Ticker24(tr)
}
// AvgPrice represents weighted current price for given symbol.
type AvgPrice struct {
Symbol string
Price float64
Mins int
}
type AvgPriceRequest struct {
Symbol string
}
func (b *binance) AvgPrice(ar AvgPriceRequest) (*AvgPrice, error) {
return b.Service.AvgPrice(ar)
}
// PriceTicker represents ticker data for price.
type PriceTicker struct {
Symbol string
Price float64
}
// TickerAllPrices returns ticker data for symbols.
func (b *binance) TickerAllPrices() ([]*PriceTicker, error) {
return b.Service.TickerAllPrices()
}
// BookTicker represents book ticker data.
type BookTicker struct {
Symbol string
BidPrice float64
BidQty float64
AskPrice float64
AskQty float64
}
// TickerAllBooks returns tickers for all books.
func (b *binance) TickerAllBooks() ([]*BookTicker, error) {
return b.Service.TickerAllBooks()
}
// NewOrderRequest represents NewOrder request data.
type NewOrderRequest struct {
Symbol string
Side OrderSide
Type OrderType
TimeInForce TimeInForce
Quantity float64
Price float64
NewClientOrderID string
StopPrice float64
IcebergQty float64
Timestamp time.Time
}
// ProcessedOrder represents data from processed order.
type ProcessedOrder struct {
Symbol string
OrderID int64
ClientOrderID string
TransactTime time.Time
}
// NewOrder places new order and returns ProcessedOrder.
func (b *binance) NewOrder(nor NewOrderRequest) (*ProcessedOrder, error) {
return b.Service.NewOrder(nor)
}
// NewOrder places testing order.
func (b *binance) NewOrderTest(nor NewOrderRequest) error {
return b.Service.NewOrderTest(nor)
}
// QueryOrderRequest represents QueryOrder request data.
type QueryOrderRequest struct {
Symbol string
OrderID int64
OrigClientOrderID string
RecvWindow time.Duration
Timestamp time.Time
}
// ExecutedOrder represents data about executed order.
type ExecutedOrder struct {
Symbol string
OrderID int
ClientOrderID string
Price float64
OrigQty float64
ExecutedQty float64
Status OrderStatus
TimeInForce TimeInForce
Type OrderType
Side OrderSide
StopPrice float64
IcebergQty float64
Time time.Time
}
// UpdatedOrder represents data about updated order.
type UpdatedOrder struct {
Symbol string
OrderID int64
ClientOrderID string
OriginalClientOrderID string
Side OrderSide
Type OrderType
TimeInForce TimeInForce
Qty float64
Price float64
StopPrice float64
IcebergQty float64
CurrentExecutionType string
CurrentOrderStatus OrderStatus
OrderRejectReason string
LastExecutedQty float64
CumulativeFilledQty float64
LastExecutedPrice float64
CommissionAmount float64
CommissionAsset string
TransactionTime time.Time
TradeId int64
IsOrderWorking bool
IsMakerSide bool
CreatedTime time.Time
CumulativeQuoteAssetTransactedQty float64
LastQuoteAssetTransactedQty float64
}
// QueryOrder returns data about existing order.
func (b *binance) QueryOrder(qor QueryOrderRequest) (*ExecutedOrder, error) {
return b.Service.QueryOrder(qor)
}
// CancelOrderRequest represents CancelOrder request data.
type CancelOrderRequest struct {
Symbol string
OrderID int64
OrigClientOrderID string
NewClientOrderID string
RecvWindow time.Duration
Timestamp time.Time
}
// CanceledOrder represents data about canceled order.
type CanceledOrder struct {
Symbol string
OrigClientOrderID string
OrderID int64
ClientOrderID string
}
// CancelOrder cancels order.
func (b *binance) CancelOrder(cor CancelOrderRequest) (*CanceledOrder, error) {
return b.Service.CancelOrder(cor)
}
// OpenOrdersRequest represents OpenOrders request data.
type OpenOrdersRequest struct {
Symbol string
RecvWindow time.Duration
Timestamp time.Time
}
// OpenOrders returns list of open orders.
func (b *binance) OpenOrders(oor OpenOrdersRequest) ([]*ExecutedOrder, error) {
return b.Service.OpenOrders(oor)
}
// AllOrdersRequest represents AllOrders request data.
type AllOrdersRequest struct {
Symbol string
OrderID int64
Limit int
RecvWindow time.Duration
Timestamp time.Time
}
// AllOrders returns list of all previous orders.
func (b *binance) AllOrders(aor AllOrdersRequest) ([]*ExecutedOrder, error) {
return b.Service.AllOrders(aor)
}
// AccountRequest represents Account request data.
type AccountRequest struct {
RecvWindow time.Duration
Timestamp time.Time
}
// Account represents user's account information.
type Account struct {
MakerCommision int64
TakerCommision int64
BuyerCommision int64
SellerCommision int64
CanTrade bool
CanWithdraw bool
CanDeposit bool
Balances []*Balance
}
type AccountEvent struct {
WSEvent
Account
}
type UpdatedOrderEvent struct {
WSEvent
UpdatedOrder
}
type UserDataEventChannels struct {
AccountEventChan chan *AccountEvent
UpdatedOrderEventChan chan *UpdatedOrderEvent
}
// Balance groups balance-related information.
type Balance struct {
Asset string
Free float64
Locked float64
}
// Account returns account data.
func (b *binance) Account(ar AccountRequest) (*Account, error) {
return b.Service.Account(ar)
}
// MyTradesRequest represents MyTrades request data.
type MyTradesRequest struct {
Symbol string
Limit int
FromID int64
RecvWindow time.Duration
Timestamp time.Time
}
// Trade represents data about trade.
type Trade struct {
ID int64
Price float64
Qty float64
Commission float64
CommissionAsset string
Time time.Time
IsBuyer bool
IsMaker bool
IsBestMatch bool
}
// MyTrades list user's trades.
func (b *binance) MyTrades(mtr MyTradesRequest) ([]*Trade, error) {
return b.Service.MyTrades(mtr)
}
// WithdrawRequest represents Withdraw request data.
type WithdrawRequest struct {
Asset string
Address string
Amount float64
Name string
RecvWindow time.Duration
Timestamp time.Time
}
// WithdrawResult represents Withdraw result.
type WithdrawResult struct {
Success bool
Msg string
}
// Withdraw executes withdrawal.
func (b *binance) Withdraw(wr WithdrawRequest) (*WithdrawResult, error) {
return b.Service.Withdraw(wr)
}
// HistoryRequest represents history-related calls request data.
type HistoryRequest struct {
Asset string
Status *int
StartTime time.Time
EndTime time.Time
RecvWindow time.Duration
Timestamp time.Time
}
// Deposit represents Deposit data.
type Deposit struct {
InsertTime time.Time
Amount float64
Asset string
Status int
}
// DepositHistory lists deposit data.
func (b *binance) DepositHistory(hr HistoryRequest) ([]*Deposit, error) {
return b.Service.DepositHistory(hr)
}
// Withdrawal represents withdrawal data.
type Withdrawal struct {
Amount float64
Address string
TxID string
Asset string
ApplyTime time.Time
Status int
}
// WithdrawHistory lists withdraw data.
func (b *binance) WithdrawHistory(hr HistoryRequest) ([]*Withdrawal, error) {
return b.Service.WithdrawHistory(hr)
}
// Stream represents stream information.
//
// Read web docs to get more information about using streams.
type Stream struct {
ListenKey string
}
// StartUserDataStream starts stream and returns Stream with ListenKey.
func (b *binance) StartUserDataStream() (*Stream, error) {
return b.Service.StartUserDataStream()
}
// KeepAliveUserDataStream prolongs stream livespan.
func (b *binance) KeepAliveUserDataStream(s *Stream) error {
return b.Service.KeepAliveUserDataStream(s)
}
// CloseUserDataStream closes opened stream.
func (b *binance) CloseUserDataStream(s *Stream) error {
return b.Service.CloseUserDataStream(s)
}
type WSEvent struct {
Type string
Time time.Time
Symbol string
}
type DepthWebsocketRequest struct {
Symbol string
}
func (b *binance) DepthWebsocket(dwr DepthWebsocketRequest) (chan *DepthEvent, chan struct{}, error) {
return b.Service.DepthWebsocket(dwr)
}
type KlineWebsocketRequest struct {
Symbol string
Interval Interval
}
func (b *binance) KlineWebsocket(kwr KlineWebsocketRequest) (chan *KlineEvent, chan struct{}, error) {
return b.Service.KlineWebsocket(kwr)
}
type TradeWebsocketRequest struct {
Symbol string
}
func (b *binance) TradeWebsocket(twr TradeWebsocketRequest) (chan *AggTradeEvent, chan struct{}, error) {
return b.Service.TradeWebsocket(twr)
}
func (b *binance) SingleTradeWebsocket(twr TradeWebsocketRequest) (chan *SingleTradeEvent, chan struct{}, error) {
return b.Service.SingleTradeWebsocket(twr)
}
type UserDataWebsocketRequest struct {
ListenKey string
}
func (b *binance) UserDataWebsocket(udwr UserDataWebsocketRequest) (*UserDataEventChannels, chan struct{}, error) {
return b.Service.UserDataWebsocket(udwr)
}
func (b *binance) ExchangeInfo() (*ExchangeInfo, error) {
return b.Service.ExchangeInfo()
}
type ExchangeInfo struct {
ExchangeFilters []interface{} // not implemented
RateLimits []*RateLimit
ServerTime time.Time
Symbols []*Symbol
Timezone string
}
type RateLimit struct {
Interval string `json:"interval"`
IntervalNum int `json:"intervalNum"`
Limit int `json:"limit"`
RateLimitType string `json:"rateLimitType"`
}
type Symbol struct {
BaseAsset string
BaseAssetPrecision int
Filters []*Filter
IcebergAllowed bool
IsMarginTradingAllowed bool
IsSpotTradingAllowed bool
OcoAllowed bool
OrderTypes []OrderType
QuoteAsset string
QuotePrecision int
Status string
Symbol string
}
type Filter struct {
FilterType FilterType `json:"filterType"`
MaxPrice float64 `json:"maxPrice"`
MinPrice float64 `json:"minPrice"`
StepSize float64 `json:"stepSize"`
TickSize float64 `json:"tickSize"`
MaxQty float64 `json:"maxQty"`
MinQty float64 `json:"minQty"`
MinNotional float64 `json:"minNotional"`
MultiplierDown float64 `json:"multiplierDown"`
MultiplierUp float64 `json:"multiplierUp"`
ApplyToMarket bool `json:"applyToMarket"`
AvgPriceMins int `json:"avgPriceMins"`
Limit int `json:"limit"`
MaxNumAlgoOrders int `json:"maxNumAlgoOrders"`
}
func (ei *ExchangeInfo) Symbol(sym string) (*Symbol, error) {
for _, s := range ei.Symbols {
if s.Symbol != sym {
continue
}
return s, nil
}
return nil, fmt.Errorf("Not found symbol %s", sym)
}
func (s *Symbol) Filter(ft FilterType) (*Filter, error) {
for _, f := range s.Filters {
if f.FilterType != ft {
continue
}
return f, nil
}
return nil, fmt.Errorf("Not found filter with type %s", ft)
}
func (f *Filter) UnmarshalJSON(data []byte) error {
var errs []string
errPrefix := "Failed to unmarshal Filter"
raw := map[string]interface{}{}
err := json.Unmarshal(data, &raw)
if err != nil {
return errors.Wrap(err, errPrefix)
}
rfl := reflect.ValueOf(f)
for fName, valI := range raw {
field := rfl.Elem().FieldByName(strings.Title(fName))
if !field.IsValid() {
errs = append(errs, fmt.Sprintf("field %s doesn't exist", fName))
continue
}
if !field.CanSet() {
errs = append(errs, fmt.Sprintf("field %s isn't settable", fName))
continue
}
valR := reflect.ValueOf(valI)
switch field.Kind() {
case reflect.Float64:
if valF, ok := valI.(float64); ok {
field.Set(reflect.ValueOf(valF).Convert(field.Type()))
continue
}
valS, ok := valI.(string)
if !ok {
errs = append(errs, fmt.Sprintf("%s cannot be converted to float64, received type is %s", fName, valR.Kind()))
continue
}
val, err := strconv.ParseFloat(valS, 64)
if err != nil {
errs = append(errs, fmt.Sprintf("%s failed to parse into float64: %s", fName, err.Error()))
continue
}
field.Set(reflect.ValueOf(val).Convert(field.Type()))
case reflect.Int:
val, ok := valI.(int)
if !ok {
if val2, ok := valI.(float64); ok {
val = int(val2)
} else {
err := fmt.Sprintf("%s cannot be converted into int, received type is %s", fName, valR.Kind())
errs = append(errs, err)
continue
}
}
field.Set(reflect.ValueOf(val).Convert(field.Type()))
case reflect.String:
val, ok := valI.(string)
if !ok {
errs = append(errs, fmt.Sprintf("%s cannot be converted into string, received type is %s", fName, valR.Kind()))
continue
}
field.Set(reflect.ValueOf(val).Convert(field.Type()))
case reflect.Bool:
val, ok := valI.(bool)
if !ok {
errs = append(errs, fmt.Sprintf("%s cannot be converted into bool, received type is %s", fName, valR.Kind()))
continue
}
field.Set(reflect.ValueOf(val).Convert(field.Type()))
default:
errs = append(errs, fmt.Sprintf("field %s received not imlemented kind %s", fName, valR.Kind()))
continue
}
}
if len(errs) != 0 {
return fmt.Errorf("%s: %s", errPrefix, strings.Join(errs, "; "))
}
return nil
}