Skip to content
forked from c9s/bbgo

Commit

Permalink
pkg/exchange: upgrade kline ws to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
bailantaotao committed Aug 12, 2024
1 parent 4925d8b commit b02be2c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 50 deletions.
1 change: 1 addition & 0 deletions pkg/exchange/bitget/bitgetapi/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

const (
PrivateWebSocketURL = "wss://ws.bitget.com/v2/ws/private"
PublicWebSocketURL = "wss://ws.bitget.com/v2/ws/public"
)

type APIResponse = bitgetapi.APIResponse
Expand Down
4 changes: 2 additions & 2 deletions pkg/exchange/bitget/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *Stream) Unsubscribe() {
func (s *Stream) createEndpoint(_ context.Context) (string, error) {
var url string
if s.PublicOnly {
url = bitgetapi.PublicWebSocketURL
url = v2.PublicWebSocketURL
} else {
url = v2.PrivateWebSocketURL
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func (s *Stream) ping(conn *websocket.Conn) error {
func convertSubscription(sub types.Subscription) (WsArg, error) {
arg := WsArg{
// support spot only
InstType: instSp,
InstType: instSpV2,
Channel: "",
InstId: sub.Symbol,
}
Expand Down
67 changes: 34 additions & 33 deletions pkg/exchange/bitget/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
input := `{
"event":"subscribe",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"books5",
"instId":"BTCUSDT"
}
Expand All @@ -191,7 +191,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
assert.Equal(t, WsEvent{
Event: WsEventSubscribe,
Arg: WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook5,
InstId: "BTCUSDT",
},
Expand All @@ -204,7 +204,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
input := `{
"event":"unsubscribe",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"books5",
"instId":"BTCUSDT"
}
Expand All @@ -216,7 +216,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
assert.Equal(t, WsEvent{
Event: WsEventUnsubscribe,
Arg: WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook5,
InstId: "BTCUSDT",
},
Expand All @@ -227,7 +227,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
input := `{
"event":"error",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"books5",
"instId":"BTCUSDT-"
},
Expand All @@ -245,7 +245,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
Msg: "instType:sp,channel:books5,instId:BTCUSDT- doesn't exist",
Op: "subscribe",
Arg: WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook5,
InstId: "BTCUSDT-",
},
Expand All @@ -256,7 +256,7 @@ func TestStream_parseWebSocketEvent(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"books5",
"instId":"BTCUSDT"
},
Expand Down Expand Up @@ -349,7 +349,7 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"trade",
"instId":"BTCUSDT"
},
Expand Down Expand Up @@ -434,7 +434,7 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"trade",
"instId":"BTCUSDT"
},
Expand All @@ -456,7 +456,7 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"trade",
"instId":"BTCUSDT"
},
Expand All @@ -478,7 +478,7 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"trade",
"instId":"BTCUSDT"
},
Expand All @@ -500,7 +500,7 @@ func Test_parseWebSocketEvent_MarketTrade(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"trade",
"instId":"BTCUSDT"
},
Expand All @@ -524,12 +524,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.49","34458.98","34355.53","34416.41","99.6631"]
["1698744600000","34361.49","34458.98","34355.53","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
Expand All @@ -549,6 +549,7 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
LowestPrice: fixedpoint.NewFromFloat(34355.53),
ClosePrice: fixedpoint.NewFromFloat(34416.41),
Volume: fixedpoint.NewFromFloat(99.6631),
QuoteVolume: fixedpoint.NewFromFloat(123456),
},
},
actionType: actionType,
Expand All @@ -571,12 +572,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","34458.98","34355.53","34416.41","99.6631", "123456"]
["1698744600000","34361.49","34458.98","34355.53","34416.41","99.6631", "123456", "123", "123"]
],
"ts":1697697791670
}`
Expand All @@ -588,12 +589,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["timestamp","34361.49","34458.98","34355.53","34416.41","99.6631"]
["timestamp","34361.49","34458.98","34355.53","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
Expand All @@ -605,12 +606,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","1p","34458.98","34355.53","34416.41","99.6631"]
["1698744600000","1p","34458.98","34355.53","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
Expand All @@ -622,12 +623,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","3p","34355.53","34416.41","99.6631"]
["1698744600000","34361.49","3p","34355.53","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
Expand All @@ -639,12 +640,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","34458.98","1p","34416.41","99.6631"]
["1698744600000","34361.49","34458.98","1p","34416.41","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
Expand All @@ -656,12 +657,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","34458.98","34355.53","1c","99.6631"]
["1698744600000","34361.49","34458.98","34355.53","1c","99.6631", "123456", "123"]
],
"ts":1697697791670
}`
Expand All @@ -673,12 +674,12 @@ func Test_parseWebSocketEvent_KLine(t *testing.T) {
input := `{
"action":"%s",
"arg":{
"instType":"sp",
"instType":"SPOT",
"channel":"candle5m",
"instId":"BTCUSDT"
},
"data":[
["1698744600000","34361.45","34458.98","34355.53","34416.41", "1v"]
["1698744600000","34361.49","34458.98","34355.53","34416.41","1v", "123456", "123"]
],
"ts":1697697791670
}`
Expand All @@ -698,7 +699,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook5,
InstId: "BTCUSDT",
}, res)
Expand All @@ -713,7 +714,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook15,
InstId: "BTCUSDT",
}, res)
Expand All @@ -728,7 +729,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelOrderBook,
InstId: "BTCUSDT",
}, res)
Expand All @@ -741,7 +742,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelTrade,
InstId: "BTCUSDT",
}, res)
Expand All @@ -757,7 +758,7 @@ func Test_convertSubscription(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, WsArg{
InstType: instSp,
InstType: instSpV2,
Channel: ChannelType(localInterval),
InstId: "BTCUSDT",
}, res)
Expand Down
40 changes: 25 additions & 15 deletions pkg/exchange/bitget/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
type InstType string

const (
instSp InstType = "sp"
instSpV2 InstType = "SPOT"
)

Expand Down Expand Up @@ -285,21 +284,27 @@ var (
types.Interval30m: "candle30m",
types.Interval1h: "candle1H",
types.Interval4h: "candle4H",
types.Interval12h: "candle12H",
types.Interval1d: "candle1D",
types.Interval1w: "candle1W",
types.Interval6h: "candle6Hutc",
types.Interval12h: "candle12Hutc",
types.Interval1d: "candle1Dutc",
types.Interval3d: "candle3Dutc",
types.Interval1w: "candle1Wutc",
types.Interval1mo: "candle1Mutc",
}

toGlobalInterval = map[string]types.Interval{
"candle1m": types.Interval1m,
"candle5m": types.Interval5m,
"candle15m": types.Interval15m,
"candle30m": types.Interval30m,
"candle1H": types.Interval1h,
"candle4H": types.Interval4h,
"candle12H": types.Interval12h,
"candle1D": types.Interval1d,
"candle1W": types.Interval1w,
"candle1m": types.Interval1m,
"candle5m": types.Interval5m,
"candle15m": types.Interval15m,
"candle30m": types.Interval30m,
"candle1H": types.Interval1h,
"candle4H": types.Interval4h,
"candle6Hutc": types.Interval6h,
"candle12Hutc": types.Interval12h,
"candle1Dutc": types.Interval1d,
"candle3Dutc": types.Interval3d,
"candle1Wutc": types.Interval1w,
"candle1Mutc": types.Interval1mo,
}

// we align utc time zone
Expand Down Expand Up @@ -345,6 +350,7 @@ type KLine struct {
LowestPrice fixedpoint.Value
ClosePrice fixedpoint.Value
Volume fixedpoint.Value
QuoteVolume fixedpoint.Value
}

func (k KLine) ToGlobal(interval types.Interval, symbol string) types.KLine {
Expand All @@ -361,7 +367,7 @@ func (k KLine) ToGlobal(interval types.Interval, symbol string) types.KLine {
High: k.HighestPrice,
Low: k.LowestPrice,
Volume: k.Volume,
QuoteVolume: fixedpoint.Zero, // not supported
QuoteVolume: k.QuoteVolume,
TakerBuyBaseAssetVolume: fixedpoint.Zero, // not supported
TakerBuyQuoteAssetVolume: fixedpoint.Zero, // not supported
LastTradeID: 0, // not supported
Expand Down Expand Up @@ -400,9 +406,10 @@ func parseKLineSliceJSON(in []byte) (slice KLineSlice, err error) {
}

for _, raw := range rawKLines {
if len(raw) != 6 {
if len(raw) != 8 {
return nil, fmt.Errorf("unexpected kline length: %d, data: %q", len(raw), raw)
}
// even though it supports 8 fields, we only parse the ones we need.
var kline KLine
if err = json.Unmarshal(raw[0], &kline.StartTime); err != nil {
return nil, fmt.Errorf("failed to unmarshal into timestamp: %q", raw[0])
Expand All @@ -422,6 +429,9 @@ func parseKLineSliceJSON(in []byte) (slice KLineSlice, err error) {
if err = json.Unmarshal(raw[5], &kline.Volume); err != nil {
return nil, fmt.Errorf("failed to unmarshal into volume: %q", raw[5])
}
if err = json.Unmarshal(raw[6], &kline.QuoteVolume); err != nil {
return nil, fmt.Errorf("failed to unmarshal into quote volume: %q", raw[6])
}

slice = append(slice, kline)
}
Expand Down

0 comments on commit b02be2c

Please sign in to comment.