bbgo_origin/pkg/exchange/binance/stream.go

439 lines
9.9 KiB
Go
Raw Normal View History

2020-07-11 05:02:53 +00:00
package binance
import (
"context"
2020-10-03 11:14:15 +00:00
"fmt"
2020-08-07 01:15:50 +00:00
"strings"
2020-10-05 06:16:55 +00:00
"sync"
2020-07-13 08:03:47 +00:00
"time"
2020-07-11 05:02:53 +00:00
"github.com/adshao/go-binance"
"github.com/gorilla/websocket"
2020-07-13 08:03:47 +00:00
2020-11-10 06:19:33 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
2020-10-11 08:46:15 +00:00
"github.com/c9s/bbgo/pkg/types"
2020-07-11 05:02:53 +00:00
)
type StreamRequest struct {
// request ID is required
ID int `json:"id"`
Method string `json:"method"`
Params []string `json:"params"`
}
2020-10-03 10:35:28 +00:00
//go:generate callbackgen -type Stream -interface
type Stream struct {
types.StandardStream
2020-08-07 01:15:50 +00:00
Client *binance.Client
ListenKey string
Conn *websocket.Conn
2020-07-11 08:07:09 +00:00
// custom callbacks
2020-10-03 12:09:22 +00:00
depthEventCallbacks []func(e *DepthEvent)
kLineEventCallbacks []func(e *KLineEvent)
kLineClosedEventCallbacks []func(e *KLineEvent)
2020-07-11 08:07:09 +00:00
balanceUpdateEventCallbacks []func(event *BalanceUpdateEvent)
outboundAccountInfoEventCallbacks []func(event *OutboundAccountInfoEvent)
executionReportEventCallbacks []func(event *ExecutionReportEvent)
2020-07-11 05:02:53 +00:00
}
2020-10-03 12:09:22 +00:00
func NewStream(client *binance.Client) *Stream {
// binance BalanceUpdate = withdrawal or deposit changes
/*
stream.OnBalanceUpdateEvent(func(e *binance.BalanceUpdateEvent) {
a.mu.Lock()
defer a.mu.Unlock()
delta := util.MustParseFloat(e.Delta)
if balance, ok := a.Balances[e.Asset]; ok {
balance.Available += delta
a.Balances[e.Asset] = balance
}
})
*/
stream := &Stream{
Client: client,
}
2020-10-05 06:16:55 +00:00
var depthFrames = make(map[string]*DepthFrame)
stream.OnDepthEvent(func(e *DepthEvent) {
f, ok := depthFrames[e.Symbol]
if !ok {
f = &DepthFrame{
client: client,
Symbol: e.Symbol,
}
f.OnReady(func(e DepthEvent, bufEvents []DepthEvent) {
snapshot, err := e.OrderBook()
if err != nil {
log.WithError(err).Error("book convert error")
return
}
stream.EmitBookSnapshot(snapshot)
for _, e := range bufEvents {
book, err := e.OrderBook()
if err != nil {
log.WithError(err).Error("book convert error")
return
}
stream.EmitBookUpdate(book)
}
})
f.OnPush(func(e DepthEvent) {
book, err := e.OrderBook()
if err != nil {
log.WithError(err).Error("book convert error")
return
}
stream.EmitBookUpdate(book)
})
depthFrames[e.Symbol] = f
} else {
f.PushEvent(*e)
}
})
stream.OnOutboundAccountInfoEvent(func(e *OutboundAccountInfoEvent) {
snapshot := types.BalanceMap{}
for _, balance := range e.Balances {
2020-11-10 06:19:33 +00:00
available := fixedpoint.Must(fixedpoint.NewFromString(balance.Free))
locked := fixedpoint.Must(fixedpoint.NewFromString(balance.Locked))
snapshot[balance.Asset] = types.Balance{
Currency: balance.Asset,
Available: available,
Locked: locked,
}
}
stream.EmitBalanceSnapshot(snapshot)
})
stream.OnKLineEvent(func(e *KLineEvent) {
kline := e.KLine.KLine()
if e.KLine.Closed {
stream.EmitKLineClosedEvent(e)
stream.EmitKLineClosed(kline)
} else {
stream.EmitKLine(kline)
}
})
stream.OnExecutionReportEvent(func(e *ExecutionReportEvent) {
switch e.CurrentExecutionType {
2020-10-29 13:10:01 +00:00
case "NEW", "CANCELED", "REJECTED", "EXPIRED", "REPLACED":
order, err := e.Order()
if err != nil {
log.WithError(err).Error("order convert error")
return
}
stream.EmitOrderUpdate(*order)
case "TRADE":
trade, err := e.Trade()
if err != nil {
2020-10-05 06:16:55 +00:00
log.WithError(err).Error("trade convert error")
2020-10-29 13:10:01 +00:00
return
}
2020-10-05 06:16:55 +00:00
stream.EmitTradeUpdate(*trade)
}
})
2020-11-07 04:38:57 +00:00
stream.OnConnect(func() {
2020-10-03 12:09:22 +00:00
var params []string
for _, subscription := range stream.Subscriptions {
params = append(params, convertSubscription(subscription))
}
if len(params) == 0 {
return
}
2020-10-03 12:09:22 +00:00
log.Infof("[binance] subscribing channels: %+v", params)
err := stream.Conn.WriteJSON(StreamRequest{
Method: "SUBSCRIBE",
Params: params,
ID: 1,
})
2020-10-03 12:09:22 +00:00
if err != nil {
log.WithError(err).Error("subscribe error")
}
})
return stream
}
2020-10-03 10:35:28 +00:00
func (s *Stream) dial(listenKey string) (*websocket.Conn, error) {
2020-07-12 14:52:24 +00:00
url := "wss://stream.binance.com:9443/ws/" + listenKey
2020-07-11 05:02:53 +00:00
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
2020-07-12 14:52:24 +00:00
if err != nil {
return nil, err
}
return conn, nil
}
2020-10-03 10:35:28 +00:00
func (s *Stream) connect(ctx context.Context) error {
2020-07-12 14:52:24 +00:00
log.Infof("[binance] creating user data stream...")
listenKey, err := s.Client.NewStartUserStreamService().Do(ctx)
if err != nil {
return err
}
s.ListenKey = listenKey
log.Infof("[binance] user data stream created. listenKey: %s", s.ListenKey)
conn, err := s.dial(s.ListenKey)
2020-07-11 05:02:53 +00:00
if err != nil {
return err
}
2020-07-11 08:07:09 +00:00
log.Infof("[binance] websocket connected")
2020-07-11 05:02:53 +00:00
s.Conn = conn
2020-11-07 04:38:57 +00:00
s.EmitConnect()
2020-10-03 12:09:22 +00:00
return nil
2020-07-12 14:52:24 +00:00
}
2020-07-11 05:02:53 +00:00
2020-10-03 11:14:15 +00:00
func convertSubscription(s types.Subscription) string {
// binance uses lower case symbol name,
// for kline, it's "<symbol>@kline_<interval>"
// for depth, it's "<symbol>@depth OR <symbol>@depth@100ms"
switch s.Channel {
case types.KLineChannel:
return fmt.Sprintf("%s@%s_%s", strings.ToLower(s.Symbol), s.Channel, s.Options.String())
case types.BookChannel:
return fmt.Sprintf("%s@depth", strings.ToLower(s.Symbol))
}
return fmt.Sprintf("%s@%s", strings.ToLower(s.Symbol), s.Channel)
}
2020-10-03 10:35:28 +00:00
func (s *Stream) Connect(ctx context.Context) error {
2020-07-12 14:52:24 +00:00
err := s.connect(ctx)
2020-07-11 05:02:53 +00:00
if err != nil {
return err
}
go s.read(ctx)
2020-07-11 05:02:53 +00:00
return nil
}
2020-10-03 10:35:28 +00:00
func (s *Stream) read(ctx context.Context) {
2020-07-11 05:02:53 +00:00
2020-08-07 01:17:35 +00:00
pingTicker := time.NewTicker(1 * time.Minute)
defer pingTicker.Stop()
keepAliveTicker := time.NewTicker(5 * time.Minute)
defer keepAliveTicker.Stop()
2020-07-11 05:02:53 +00:00
for {
select {
case <-ctx.Done():
return
2020-08-07 01:17:35 +00:00
case <-keepAliveTicker.C:
err := s.Client.NewKeepaliveUserStreamService().ListenKey(s.ListenKey).Do(ctx)
if err != nil {
log.WithError(err).Errorf("listen key keep-alive error: %v key: %s", err, maskListenKey(s.ListenKey))
}
case <-pingTicker.C:
2020-08-07 01:15:50 +00:00
if err := s.Conn.WriteControl(websocket.PingMessage, []byte("hb"), time.Now().Add(1*time.Second)); err != nil {
2020-07-18 02:43:27 +00:00
log.WithError(err).Error("ping error", err)
}
2020-07-11 05:02:53 +00:00
default:
2020-07-13 05:25:48 +00:00
if err := s.Conn.SetReadDeadline(time.Now().Add(30 * time.Second)); err != nil {
2020-07-11 08:07:09 +00:00
log.WithError(err).Errorf("set read deadline error: %s", err.Error())
2020-07-11 05:02:53 +00:00
}
mt, message, err := s.Conn.ReadMessage()
if err != nil {
2020-07-12 17:06:04 +00:00
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
log.WithError(err).Errorf("read error: %s", err.Error())
}
2020-07-12 14:52:24 +00:00
// reconnect
for err != nil {
2020-07-12 17:06:04 +00:00
select {
case <-ctx.Done():
return
default:
2020-07-13 05:25:48 +00:00
_ = s.invalidateListenKey(ctx, s.ListenKey)
2020-07-12 17:06:04 +00:00
err = s.connect(ctx)
time.Sleep(5 * time.Second)
}
2020-07-12 14:52:24 +00:00
}
continue
2020-07-11 05:02:53 +00:00
}
// skip non-text messages
if mt != websocket.TextMessage {
continue
}
2020-07-11 08:07:09 +00:00
log.Debugf("[binance] recv: %s", message)
2020-07-11 05:02:53 +00:00
e, err := ParseEvent(string(message))
if err != nil {
2020-07-11 08:07:09 +00:00
log.WithError(err).Errorf("[binance] event parse error")
2020-07-11 05:02:53 +00:00
continue
}
2020-10-22 02:47:54 +00:00
// log.NotifyTo("[binance] event: %+v", e)
2020-07-11 08:07:09 +00:00
switch e := e.(type) {
case *OutboundAccountInfoEvent:
log.Info(e.Event, " ", e.Balances)
s.EmitOutboundAccountInfoEvent(e)
case *BalanceUpdateEvent:
log.Info(e.Event, " ", e.Asset, " ", e.Delta)
s.EmitBalanceUpdateEvent(e)
case *KLineEvent:
s.EmitKLineEvent(e)
2020-10-03 12:09:22 +00:00
case *DepthEvent:
s.EmitDepthEvent(e)
2020-07-11 08:07:09 +00:00
case *ExecutionReportEvent:
2020-11-17 05:25:59 +00:00
log.Info(e.Event, " ", e)
2020-07-11 08:07:09 +00:00
s.EmitExecutionReportEvent(e)
}
2020-07-11 05:02:53 +00:00
}
}
}
2020-10-03 10:35:28 +00:00
func (s *Stream) invalidateListenKey(ctx context.Context, listenKey string) error {
2020-07-12 14:52:24 +00:00
// use background context to invalidate the user stream
err := s.Client.NewCloseUserStreamService().ListenKey(listenKey).Do(ctx)
2020-07-11 05:02:53 +00:00
if err != nil {
2020-07-12 14:52:24 +00:00
log.WithError(err).Error("[binance] error deleting listen key")
2020-07-11 05:02:53 +00:00
return err
}
2020-07-11 09:02:37 +00:00
return nil
2020-07-11 05:02:53 +00:00
}
2020-07-12 14:52:24 +00:00
2020-10-03 10:35:28 +00:00
func (s *Stream) Close() error {
2020-07-12 14:52:24 +00:00
log.Infof("[binance] closing user data stream...")
defer s.Conn.Close()
err := s.invalidateListenKey(context.Background(), s.ListenKey)
log.Infof("[binance] user data stream closed")
return err
}
2020-08-07 01:17:35 +00:00
func maskListenKey(listenKey string) string {
maskKey := listenKey[0:5]
return maskKey + strings.Repeat("*", len(listenKey)-1-5)
}
2020-10-05 06:16:55 +00:00
//go:generate callbackgen -type DepthFrame
type DepthFrame struct {
client *binance.Client
mu sync.Mutex
once sync.Once
SnapshotDepth *DepthEvent
Symbol string
BufEvents []DepthEvent
readyCallbacks []func(snapshotDepth DepthEvent, bufEvents []DepthEvent)
pushCallbacks []func(e DepthEvent)
}
func (f *DepthFrame) Reset() {
f.mu.Lock()
f.SnapshotDepth = nil
f.once = sync.Once{}
f.mu.Unlock()
}
func (f *DepthFrame) PushEvent(e DepthEvent) {
f.mu.Lock()
if f.SnapshotDepth == nil {
f.BufEvents = append(f.BufEvents, e)
f.mu.Unlock()
go f.once.Do(func() {
depth, err := f.fetch(context.Background())
if err != nil {
return
}
f.mu.Lock()
f.SnapshotDepth = depth
var events []DepthEvent
for _, e := range f.BufEvents {
/*
if i == 0 {
if e.FirstUpdateID > f.SnapshotDepth.FinalUpdateID+1 {
// FIXME: we missed some events
log.Warn("miss matched final update id for order book")
f.SnapshotDepth = nil
f.mu.Unlock()
return
}
}
*/
if e.FirstUpdateID <= f.SnapshotDepth.FinalUpdateID || e.FinalUpdateID <= f.SnapshotDepth.FinalUpdateID {
continue
}
events = append(events, e)
}
f.BufEvents = nil
f.EmitReady(*depth, events)
f.mu.Unlock()
})
} else {
if e.FirstUpdateID > f.SnapshotDepth.FinalUpdateID || e.FinalUpdateID > f.SnapshotDepth.FinalUpdateID {
f.EmitPush(e)
}
f.mu.Unlock()
}
}
// fetch fetches the depth and convert to the depth event so that we can reuse the event structure to convert it to the global orderbook type
func (f *DepthFrame) fetch(ctx context.Context) (*DepthEvent, error) {
response, err := f.client.NewDepthService().Symbol(f.Symbol).Do(ctx)
if err != nil {
return nil, err
}
event := DepthEvent{
FirstUpdateID: 0,
FinalUpdateID: response.LastUpdateID,
}
for _, entry := range response.Bids {
event.Bids = append(event.Bids, DepthEntry{PriceLevel: entry.Price, Quantity: entry.Quantity})
}
for _, entry := range response.Asks {
event.Asks = append(event.Asks, DepthEntry{PriceLevel: entry.Price, Quantity: entry.Quantity})
}
return &event, nil
}