bbgo_origin/pkg/exchange/max/stream.go

144 lines
3.0 KiB
Go
Raw Normal View History

2020-08-31 04:32:51 +00:00
package max
2020-10-03 10:35:28 +00:00
import (
"context"
2020-10-19 14:23:49 +00:00
"strconv"
"time"
2020-10-03 10:35:28 +00:00
2020-10-11 08:46:15 +00:00
max "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
"github.com/c9s/bbgo/pkg/types"
2020-10-03 10:35:28 +00:00
)
var logger = log.WithField("exchange", "max")
type Stream struct {
types.StandardStream
websocketService *max.WebSocketService
}
func NewStream(key, secret string) *Stream {
wss := max.NewWebSocketService(max.WebSocketURL, key, secret)
2020-10-03 11:14:15 +00:00
2020-10-03 10:35:28 +00:00
stream := &Stream{
websocketService: wss,
}
2020-10-03 11:14:15 +00:00
wss.OnMessage(func(message []byte) {
logger.Infof("M: %s", message)
})
2020-10-19 14:23:49 +00:00
// wss.OnTradeEvent(func(e max.PublicTradeEvent) { })
wss.OnTradeUpdateEvent(func(e max.TradeUpdateEvent) {
for _, tradeUpdate := range e.Trades {
trade, err := convertWebSocketTrade(tradeUpdate)
if err != nil {
log.WithError(err).Error("websocket trade update convert error")
return
}
stream.EmitTrade(*trade)
}
})
2020-10-03 10:35:28 +00:00
wss.OnBookEvent(func(e max.BookEvent) {
newbook, err := e.OrderBook()
if err != nil {
logger.WithError(err).Error("book convert error")
return
}
newbook.Symbol = toGlobalSymbol(e.Market)
2020-10-03 10:35:28 +00:00
switch e.Event {
case "snapshot":
stream.EmitBookSnapshot(newbook)
case "update":
stream.EmitBookUpdate(newbook)
}
})
2020-10-03 11:14:15 +00:00
wss.OnAccountSnapshotEvent(func(e max.AccountSnapshotEvent) {
snapshot := map[string]types.Balance{}
for _, bm := range e.Balances {
balance, err := bm.Balance()
if err != nil {
continue
}
2020-10-19 13:33:12 +00:00
snapshot[toGlobalCurrency(balance.Currency)] = *balance
2020-10-03 11:14:15 +00:00
}
stream.EmitBalanceSnapshot(snapshot)
})
wss.OnAccountUpdateEvent(func(e max.AccountUpdateEvent) {
snapshot := map[string]types.Balance{}
for _, bm := range e.Balances {
balance, err := bm.Balance()
if err != nil {
continue
}
2020-10-19 13:33:12 +00:00
snapshot[toGlobalCurrency(balance.Currency)] = *balance
2020-10-03 11:14:15 +00:00
}
stream.EmitBalanceUpdate(snapshot)
})
2020-10-03 10:35:28 +00:00
return stream
}
2020-10-03 11:14:15 +00:00
func (s *Stream) Subscribe(channel types.Channel, symbol string, options types.SubscribeOptions) {
s.websocketService.Subscribe(string(channel), toLocalSymbol(symbol))
2020-10-03 11:14:15 +00:00
}
2020-10-03 10:35:28 +00:00
func (s *Stream) Connect(ctx context.Context) error {
2020-10-03 11:14:15 +00:00
return s.websocketService.Connect(ctx)
2020-10-03 10:35:28 +00:00
}
func (s *Stream) Close() error {
return s.websocketService.Close()
}
2020-10-19 14:23:49 +00:00
func convertWebSocketTrade(t max.TradeUpdate) (*types.Trade, error) {
// skip trade ID that is the same. however this should not happen
var side = toGlobalSideType(t.Side)
// trade time
mts := time.Unix(0, t.Timestamp*int64(time.Millisecond))
price, err := strconv.ParseFloat(t.Price, 64)
if err != nil {
return nil, err
}
quantity, err := strconv.ParseFloat(t.Volume, 64)
if err != nil {
return nil, err
}
quoteQuantity := price * quantity
fee, err := strconv.ParseFloat(t.Fee, 64)
if err != nil {
return nil, err
}
return &types.Trade{
ID: int64(t.ID),
Price: price,
Symbol: toGlobalSymbol(t.Market),
Exchange: "max",
Quantity: quantity,
Side: side,
IsBuyer: side == "bid",
IsMaker: t.Maker,
Fee: fee,
FeeCurrency: toGlobalCurrency(t.FeeCurrency),
QuoteQuantity: quoteQuantity,
Time: mts,
}, nil
}