bbgo_origin/pkg/exchange/kucoin/websocket.go

160 lines
5.7 KiB
Go
Raw Permalink Normal View History

package kucoin
2021-12-22 16:30:17 +00:00
2021-12-22 18:37:11 +00:00
import (
"encoding/json"
"time"
2021-12-22 18:37:11 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type WebSocketMessageType string
const (
WebSocketMessageTypePing WebSocketMessageType = "ping"
WebSocketMessageTypeSubscribe WebSocketMessageType = "subscribe"
WebSocketMessageTypeUnsubscribe WebSocketMessageType = "unsubscribe"
WebSocketMessageTypeAck WebSocketMessageType = "ack"
WebSocketMessageTypeError WebSocketMessageType = "error"
2021-12-22 18:37:11 +00:00
WebSocketMessageTypePong WebSocketMessageType = "pong"
WebSocketMessageTypeWelcome WebSocketMessageType = "welcome"
WebSocketMessageTypeMessage WebSocketMessageType = "message"
)
type WebSocketSubject string
const (
WebSocketSubjectTradeTicker WebSocketSubject = "trade.ticker"
WebSocketSubjectTradeSnapshot WebSocketSubject = "trade.snapshot" // ticker snapshot
WebSocketSubjectTradeL2Update WebSocketSubject = "trade.l2update" // order book L2
WebSocketSubjectLevel2 WebSocketSubject = "level2" // level2
WebSocketSubjectTradeCandlesUpdate WebSocketSubject = "trade.candles.update"
WebSocketSubjectTradeCandlesAdd WebSocketSubject = "trade.candles.add"
2021-12-22 18:37:11 +00:00
// private subjects
WebSocketSubjectOrderChange WebSocketSubject = "orderChange"
WebSocketSubjectAccountBalance WebSocketSubject = "account.balance"
WebSocketSubjectStopOrder WebSocketSubject = "stopOrder"
)
2021-12-22 16:30:17 +00:00
type WebSocketCommand struct {
Id int64 `json:"id"`
2021-12-22 18:49:56 +00:00
Type WebSocketMessageType `json:"type"`
Topic string `json:"topic"`
PrivateChannel bool `json:"privateChannel"`
Response bool `json:"response"`
2021-12-22 16:30:17 +00:00
}
func (c *WebSocketCommand) JSON() ([]byte, error) {
2021-12-22 18:37:11 +00:00
type tt WebSocketCommand
var a = (*tt)(c)
return json.Marshal(a)
}
2021-12-22 18:03:13 +00:00
2021-12-23 03:04:22 +00:00
type WebSocketEvent struct {
2021-12-22 18:37:11 +00:00
Type WebSocketMessageType `json:"type"`
Topic string `json:"topic"`
Subject WebSocketSubject `json:"subject"`
Data json.RawMessage `json:"data"`
Code int `json:"code"` // used in type error
2021-12-22 18:37:11 +00:00
// Object is used for storing the parsed Data
Object interface{} `json:"-"`
2021-12-22 18:03:13 +00:00
}
type WebSocketTickerEvent struct {
2021-12-22 18:37:11 +00:00
Sequence string `json:"sequence"`
Price fixedpoint.Value `json:"price"`
Size fixedpoint.Value `json:"size"`
BestAsk fixedpoint.Value `json:"bestAsk"`
BestAskSize fixedpoint.Value `json:"bestAskSize"`
BestBid fixedpoint.Value `json:"bestBid"`
BestBidSize fixedpoint.Value `json:"bestBidSize"`
2021-12-22 18:03:13 +00:00
}
type WebSocketOrderBookL2Event struct {
2021-12-22 18:37:11 +00:00
SequenceStart int64 `json:"sequenceStart"`
SequenceEnd int64 `json:"sequenceEnd"`
Symbol string `json:"symbol"`
Changes struct {
Asks types.PriceVolumeSlice `json:"asks"`
Bids types.PriceVolumeSlice `json:"bids"`
2021-12-22 18:37:11 +00:00
} `json:"changes"`
Time types.MillisecondTimestamp `json:"time"`
2021-12-22 18:03:13 +00:00
}
type WebSocketCandleEvent struct {
Symbol string `json:"symbol"`
Candles []string `json:"candles"`
Time types.MillisecondTimestamp `json:"time"`
// Interval is an injected field (not from the payload)
Interval types.Interval
// Is a new candle or not
Add bool
}
func (e *WebSocketCandleEvent) KLine() types.KLine {
startTime := types.MustParseUnixTimestamp(e.Candles[0])
openPrice := fixedpoint.MustNewFromString(e.Candles[1])
closePrice := fixedpoint.MustNewFromString(e.Candles[2])
highPrice := fixedpoint.MustNewFromString(e.Candles[3])
lowPrice := fixedpoint.MustNewFromString(e.Candles[4])
volume := fixedpoint.MustNewFromString(e.Candles[5])
quoteVolume := fixedpoint.MustNewFromString(e.Candles[6])
kline := types.KLine{
Exchange: types.ExchangeKucoin,
Symbol: toGlobalSymbol(e.Symbol),
StartTime: types.Time(startTime),
EndTime: types.Time(startTime.Add(e.Interval.Duration() - time.Millisecond)),
Interval: e.Interval,
Open: openPrice,
Close: closePrice,
High: highPrice,
Low: lowPrice,
Volume: volume,
QuoteVolume: quoteVolume,
Closed: false,
}
return kline
2021-12-22 18:03:13 +00:00
}
type WebSocketPrivateOrderEvent struct {
OrderId string `json:"orderId"`
TradeId string `json:"tradeId"`
Symbol string `json:"symbol"`
OrderType string `json:"orderType"`
Side string `json:"side"`
Type string `json:"type"`
OrderTime types.NanosecondTimestamp `json:"orderTime"`
Price fixedpoint.Value `json:"price"`
Size fixedpoint.Value `json:"size"`
FilledSize fixedpoint.Value `json:"filledSize"`
RemainSize fixedpoint.Value `json:"remainSize"`
Liquidity string `json:"liquidity"`
MatchPrice fixedpoint.Value `json:"matchPrice"`
MatchSize fixedpoint.Value `json:"matchSize"`
2021-12-22 18:37:11 +00:00
ClientOid string `json:"clientOid"`
Status string `json:"status"`
Ts types.MillisecondTimestamp `json:"ts"`
2021-12-22 18:03:13 +00:00
}
type WebSocketAccountBalanceEvent struct {
2021-12-22 18:37:11 +00:00
Total fixedpoint.Value `json:"total"`
Available fixedpoint.Value `json:"available"`
AvailableChange fixedpoint.Value `json:"availableChange"`
Currency string `json:"currency"`
2021-12-22 18:37:11 +00:00
Hold fixedpoint.Value `json:"hold"`
HoldChange fixedpoint.Value `json:"holdChange"`
RelationEvent string `json:"relationEvent"`
RelationEventId string `json:"relationEventId"`
2021-12-22 18:37:11 +00:00
RelationContext struct {
Symbol string `json:"symbol"`
TradeId string `json:"tradeId"`
OrderId string `json:"orderId"`
} `json:"relationContext"`
Time string `json:"time"`
}