This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
binanceExchange_ws.go
463 lines (341 loc) · 10.1 KB
/
binanceExchange_ws.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
package plugins
import (
"fmt"
"log"
"strconv"
"strings"
"sync"
"time"
"github.com/adshao/go-binance/v2"
"github.com/adshao/go-binance/v2/common"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"
)
const (
STREAM_TICKER_FMT = "%s@ticker"
STREAM_BOOK_FMT = "%s@depth"
TTLTIME = time.Second * 3 // ttl time in seconds
)
var (
timeWaitForFirstEvent = time.Second * 2
)
var (
ErrConversionWsMarketEvent = errConversion{from: "interface", to: "*binance.WsMarketStatEvent"}
ErrConversionWsPartialDepthEvent = errConversion{from: "interface", to: "*binance.WsPartialDepthEvent"}
)
type Subscriber func(symbol string, state *mapEvents) (*stream, error)
type errMissingSymbol struct {
symbol string
}
func (err errMissingSymbol) Error() string {
return fmt.Sprintf("Symbol %s is missing from exchange intizialization", err.symbol)
}
type errConversion struct {
from string
to string
}
func (err errConversion) Error() string {
return fmt.Sprintf("Error conversion from %s to %s", err.from, err.to)
}
type stream struct {
doneC chan struct{}
stopC chan struct{}
cleanup func()
}
//Wait until the stream ends
func (s stream) Wait() {
if s.doneC == nil {
return
}
<-s.doneC
}
//Close the stream and cleanup any data
func (s stream) Close() {
if s.stopC == nil {
return
}
s.stopC <- struct{}{}
s.stopC = nil
if s.cleanup != nil {
s.cleanup()
}
}
//mapData... struct used to data from events and timestamp when they are cached
type mapData struct {
data interface{}
createdAt time.Time
}
//isStatle... check if data it's stale
func isStale(data mapData, ttl time.Duration) bool {
return time.Now().Sub(data.createdAt).Seconds() > ttl.Seconds()
}
//struct used to cache events
type mapEvents struct {
data map[string]mapData
mtx *sync.RWMutex
}
//Set ... set value
func (m *mapEvents) Set(key string, data interface{}) {
now := time.Now()
m.mtx.Lock()
defer m.mtx.Unlock()
m.data[key] = mapData{
data: data,
createdAt: now,
}
}
//Get ... get value
func (m *mapEvents) Get(key string) (mapData, bool) {
m.mtx.RLock()
defer m.mtx.RUnlock()
data, isData := m.data[key]
return data, isData
}
//Del ... delete cached value
func (m *mapEvents) Del(key string) {
m.mtx.Lock()
defer m.mtx.Unlock()
delete(m.data, key)
}
// create new map for cache
func makeMapEvents() *mapEvents {
return &mapEvents{
data: make(map[string]mapData),
mtx: &sync.RWMutex{},
}
}
//struct used to keep all cached data
type events struct {
SymbolStats *mapEvents
BookStats *mapEvents
}
func createStateEvents() *events {
events := &events{
SymbolStats: makeMapEvents(),
BookStats: makeMapEvents(),
}
return events
}
// 24hr rolling window ticker statistics for a single symbol. These are NOT the statistics of the UTC day, but a 24hr rolling window for the previous 24hrs.
// Stream Name: <symbol>@ticker
// Update Speed: 1000ms
func subcribeTicker(symbol string, state *mapEvents) (*stream, error) {
wsMarketStatHandler := func(ticker *binance.WsMarketStatEvent) {
state.Set(symbol, ticker)
}
errHandler := func(err error) {
log.Printf("Error WsMarketsStat for symbol %s: %v\n", symbol, err)
}
doneC, stopC, err := binance.WsMarketStatServe(symbol, wsMarketStatHandler, errHandler)
if err != nil {
return nil, err
}
keepConnection(doneC, func() {
subcribeTicker(symbol, state)
})
return &stream{doneC: doneC, stopC: stopC, cleanup: func() {
state.Del(symbol)
}}, err
}
//restart Connection with ws
// Binance close each connection after 24 hours
func keepConnection(doneC chan struct{}, reconnect func()) {
go func() {
<-doneC
reconnect()
}()
}
// Top <levels> bids and asks, pushed every second. Valid <levels> are 5, 10, or 20.
// <symbol>@depth<levels>@100ms
// 100ms
func subcribeBook(symbol string, state *mapEvents) (*stream, error) {
wsPartialDepthHandler := func(event *binance.WsPartialDepthEvent) {
state.Set(symbol, event)
}
errHandler := func(err error) {
log.Printf("Error WsPartialDepthServe for symbol %s: %v\n", symbol, err)
}
//Subscribe to highest level
doneC, stopC, err := binance.WsPartialDepthServe100Ms(symbol, "20", wsPartialDepthHandler, errHandler)
if err != nil {
return nil, err
}
keepConnection(doneC, func() {
subcribeBook(symbol, state)
})
return &stream{doneC: doneC, stopC: stopC, cleanup: func() {
state.Del(symbol)
}}, err
}
type binanceExchangeWs struct {
events *events
streams map[string]*stream
streamLock *sync.Mutex
assetConverter model.AssetConverterInterface
delimiter string
}
// makeBinanceWs is a factory method to make an binance exchange over ws
func makeBinanceWs() (*binanceExchangeWs, error) {
binance.WebsocketKeepalive = true
events := createStateEvents()
beWs := &binanceExchangeWs{
events: events,
delimiter: "",
assetConverter: model.CcxtAssetConverter,
streamLock: &sync.Mutex{},
streams: make(map[string]*stream),
}
return beWs, nil
}
//getPrceision... get precision for float string
func getPrecision(floatStr string) int8 {
strs := strings.Split(floatStr, ".")
if len(strs) != 2 {
log.Printf("could not get precision for float %s\n", floatStr)
return 0
}
return int8(len(strs[1]))
}
//subscribeStream and wait for the first event
func (beWs *binanceExchangeWs) subscribeStream(symbol, format string, subscribe Subscriber, state *mapEvents) (mapData, error) {
stream, err := subscribe(symbol, state)
streamName := fmt.Sprintf(format, symbol)
if err != nil {
return mapData{}, fmt.Errorf("error when subscribing for %s: %s", streamName, err)
}
//Store stream
beWs.streamLock.Lock()
beWs.streams[streamName] = stream
beWs.streamLock.Unlock()
//Wait for binance to send events
time.Sleep(timeWaitForFirstEvent)
data, isStream := state.Get(symbol)
//We couldn't subscribe for this pair
if !isStream {
return mapData{}, fmt.Errorf("error while subscribing for %s", streamName)
}
return data, nil
}
// GetTickerPrice impl.
func (beWs *binanceExchangeWs) GetTickerPrice(pairs []model.TradingPair) (map[model.TradingPair]api.Ticker, error) {
priceResult := map[model.TradingPair]api.Ticker{}
for _, p := range pairs {
symbol, err := p.ToString(beWs.assetConverter, beWs.delimiter)
if err != nil {
return nil, err
}
tickerData, isTicker := beWs.events.SymbolStats.Get(symbol)
if !isTicker {
tickerData, err = beWs.subscribeStream(symbol, STREAM_TICKER_FMT, subcribeTicker, beWs.events.SymbolStats)
if err != nil {
return nil, err
}
}
//Show how old is the ticker
log.Printf("Ticker for %s is %d milliseconds old!\n", symbol, time.Now().Sub(tickerData.createdAt).Milliseconds())
if isStale(tickerData, TTLTIME) {
return nil, fmt.Errorf("ticker for %s symbols is older than %v", symbol, TTLTIME)
}
tickerI := tickerData.data
//Convert to WsMarketStatEvent
ticker, isOk := tickerI.(*binance.WsMarketStatEvent)
if !isOk {
return nil, ErrConversionWsMarketEvent
}
askPrice, e := strconv.ParseFloat(ticker.AskPrice, 64)
if e != nil {
return nil, fmt.Errorf("unable to correctly parse 'ask': %s", e)
}
bidPrice, e := strconv.ParseFloat(ticker.BidPrice, 64)
if e != nil {
return nil, fmt.Errorf("unable to correctly parse 'bid': %s", e)
}
lastPrice, e := strconv.ParseFloat(ticker.LastPrice, 64)
if e != nil {
return nil, fmt.Errorf("unable to correctly parse 'last': %s", e)
}
priceResult[p] = api.Ticker{
AskPrice: model.NumberFromFloat(askPrice, getPrecision(ticker.AskPrice)),
BidPrice: model.NumberFromFloat(bidPrice, getPrecision(ticker.BidPrice)),
LastPrice: model.NumberFromFloat(lastPrice, getPrecision(ticker.LastPrice)),
}
}
return priceResult, nil
}
//GetOrderBook impl
func (beWs *binanceExchangeWs) GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error) {
var (
fetchSize = int(maxCount)
)
if fetchSize > 20 {
return nil, fmt.Errorf("Max supported depth level is 20")
}
symbol, err := pair.ToString(beWs.assetConverter, beWs.delimiter)
if err != nil {
return nil, fmt.Errorf("error converting pair to string: %s", err)
}
bookData, isBook := beWs.events.BookStats.Get(symbol)
if !isBook {
bookData, err = beWs.subscribeStream(symbol, STREAM_BOOK_FMT, subcribeBook, beWs.events.BookStats)
if err != nil {
return nil, err
}
}
//Show how old is the orderbook
log.Printf("OrderBook for %s is %d milliseconds old!\n", symbol, time.Now().Sub(bookData.createdAt).Milliseconds())
if isStale(bookData, TTLTIME) {
return nil, fmt.Errorf("ticker for %s symbols is older than %v", symbol, TTLTIME)
}
bookI := bookData.data
//Convert to WsMarketStatEvent
book, isOk := bookI.(*binance.WsPartialDepthEvent)
if !isOk {
return nil, ErrConversionWsPartialDepthEvent
}
askCcxtOrders := book.Asks
bidCcxtOrders := book.Bids
if len(askCcxtOrders) > fetchSize {
askCcxtOrders = askCcxtOrders[:fetchSize]
}
if len(bidCcxtOrders) > fetchSize {
bidCcxtOrders = bidCcxtOrders[:fetchSize]
}
asks, err := beWs.readOrders(askCcxtOrders, pair, model.OrderActionSell)
if err != nil {
return nil, err
}
bids, err := beWs.readOrders(bidCcxtOrders, pair, model.OrderActionBuy)
if err != nil {
return nil, err
}
return model.MakeOrderBook(pair, asks, bids), nil
}
//readOrders... transform orders from binance to model.Order
func (beWs *binanceExchangeWs) readOrders(orders []common.PriceLevel, pair *model.TradingPair, orderAction model.OrderAction) ([]model.Order, error) {
pricePrecision := getPrecision(orders[0].Price)
volumePrecision := getPrecision(orders[0].Quantity)
result := []model.Order{}
for _, o := range orders {
price, quantity, err := o.Parse()
if err != nil {
return nil, err
}
result = append(result, model.Order{
Pair: pair,
OrderAction: orderAction,
OrderType: model.OrderTypeLimit,
Price: model.NumberFromFloat(price, pricePrecision),
Volume: model.NumberFromFloat(quantity, volumePrecision),
Timestamp: nil,
})
}
return result, nil
}
//Unsubscribe ... unsubscribe from binance streams
func (beWs *binanceExchangeWs) Unsubscribe(stream string) {
beWs.streamLock.Lock()
if stream, isStream := beWs.streams[stream]; isStream {
stream.Close()
}
beWs.streamLock.Unlock()
}