Skip to content

Commit 4db239d

Browse files
committed
Add SubscribeDepthHF
1 parent 6e786f2 commit 4db239d

File tree

6 files changed

+140
-16
lines changed

6 files changed

+140
-16
lines changed

hbdm/models.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,23 @@ type WSDepth struct {
324324
Tick Tick `json:"tick"`
325325
}
326326

327+
type WSTickHF struct {
328+
Asks [][]float64 `json:"asks"`
329+
Bids [][]float64 `json:"bids"`
330+
Ch string `json:"ch"`
331+
Event string `json:"event"`
332+
ID int64 `json:"id"`
333+
Mrid int64 `json:"mrid"`
334+
Ts int64 `json:"ts"`
335+
Version int `json:"version"`
336+
}
337+
338+
type WSDepthHF struct {
339+
Ch string `json:"ch"`
340+
Tick WSTickHF `json:"tick"`
341+
Ts int64 `json:"ts"`
342+
}
343+
327344
type WSTradeItem struct {
328345
Amount int `json:"amount"`
329346
Ts int64 `json:"ts"`

hbdm/ws.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ type WS struct {
3030
cid string // 客户端请求唯一ID
3131
accessKey string
3232
secretKey string
33+
debugMode bool
3334
subscriptions map[string]interface{}
3435

35-
tickerCallback func(trade *WSTicker)
36-
depthCallback func(depth *WSDepth)
37-
tradeCallback func(trade *WSTrade)
36+
tickerCallback func(trade *WSTicker)
37+
depthCallback func(depth *WSDepth)
38+
depthHFCallback func(depth *WSDepthHF)
39+
tradeCallback func(trade *WSTrade)
3840
}
3941

4042
// SetProxy 设置代理地址
@@ -68,7 +70,20 @@ func (ws *WS) SubscribeTicker(id string, symbol string) {
6870
func (ws *WS) SubscribeDepth(id string, symbol string) {
6971
ch := map[string]interface{}{
7072
"id": id,
71-
"sub": fmt.Sprintf("market.%s.depth.step0", symbol)}
73+
"sub": fmt.Sprintf("market.%s.depth.step0", symbol),
74+
}
75+
ws.Subscribe(id, ch)
76+
}
77+
78+
// SubscribeDepthHF 订阅增量深度
79+
// size: 20/150 档位数,20:表示20档不合并的深度,150:表示150档不合并的深度
80+
// dateType: 数据类型,不填默认为全量数据,"incremental":增量数据,"snapshot":全量数据
81+
func (ws *WS) SubscribeDepthHF(id string, symbol string, size int, dateType string) {
82+
ch := map[string]interface{}{
83+
"id": id,
84+
"sub": fmt.Sprintf("market.%v.depth.size_%v.high_freq", symbol, size),
85+
"data_type": dateType,
86+
}
7287
ws.Subscribe(id, ch)
7388
}
7489

@@ -99,6 +114,10 @@ func (ws *WS) SetDepthCallback(callback func(depth *WSDepth)) {
99114
ws.depthCallback = callback
100115
}
101116

117+
func (ws *WS) SetDepthHFCallback(callback func(depth *WSDepthHF)) {
118+
ws.depthHFCallback = callback
119+
}
120+
102121
func (ws *WS) SetTradeCallback(callback func(trade *WSTrade)) {
103122
ws.tradeCallback = callback
104123
}
@@ -167,7 +186,9 @@ func (ws *WS) run() {
167186
func (ws *WS) handleMsg(messageType int, msg []byte) {
168187
ret := gjson.ParseBytes(msg)
169188

170-
//log.Printf("%v", string(msg))
189+
if ws.debugMode {
190+
log.Printf("%v", string(msg))
191+
}
171192

172193
if pingValue := ret.Get("ping"); pingValue.Exists() {
173194
// 心跳
@@ -180,10 +201,22 @@ func (ws *WS) handleMsg(messageType int, msg []byte) {
180201
// {"id":"depth_1","subbed":"market.BTC_CQ.depth.step0","ts":1586498957314,"status":"ok"}
181202

182203
if chValue := ret.Get("ch"); chValue.Exists() {
183-
// market.BTC_CQ.depth.step0
184204
ch := chValue.String()
185205
if strings.HasPrefix(ch, "market") {
186-
if strings.Contains(ch, ".depth") {
206+
if strings.HasSuffix(ch, ".high_freq") {
207+
// market.BTC_CQ.depth.size_20.high_freq
208+
var depth WSDepthHF
209+
err := json.Unmarshal(msg, &depth)
210+
if err != nil {
211+
log.Printf("%v", err)
212+
return
213+
}
214+
215+
if ws.depthHFCallback != nil {
216+
ws.depthHFCallback(&depth)
217+
}
218+
} else if strings.Contains(ch, ".depth") {
219+
// market.BTC_CQ.depth.step0
187220
var depth WSDepth
188221
err := json.Unmarshal(msg, &depth)
189222
if err != nil {
@@ -242,12 +275,13 @@ func (ws *WS) handlePing(ping int64) {
242275
// wsURL:
243276
// 正式地址 wss://api.hbdm.com/ws
244277
// 开发地址 wss://api.btcgateway.pro/ws
245-
func NewWS(wsURL string, accessKey string, secretKey string) *WS {
278+
func NewWS(wsURL string, accessKey string, secretKey string, debugMode bool) *WS {
246279
ws := &WS{
247280
wsURL: wsURL,
248281
cid: shortuuid.New(),
249282
accessKey: accessKey,
250283
secretKey: secretKey,
284+
debugMode: debugMode,
251285
subscriptions: make(map[string]interface{}),
252286
}
253287
ws.ctx, ws.cancel = context.WithCancel(context.Background())

hbdm/ws_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func newWSTest() *WS {
1818
secretKey := viper.GetString("secret_key")
1919

2020
wsURL := "wss://api.btcgateway.pro/ws"
21-
ws := NewWS(wsURL, accessKey, secretKey)
21+
ws := NewWS(wsURL, accessKey, secretKey, true)
2222
return ws
2323
}
2424

@@ -46,6 +46,18 @@ func TestWS_SubscribeDepth(t *testing.T) {
4646
select {}
4747
}
4848

49+
func TestWS_SubscribeDepthHighFreq(t *testing.T) {
50+
ws := newWSTest()
51+
52+
ws.SetDepthHFCallback(func(depth *WSDepthHF) {
53+
log.Printf("depth: %#v", depth)
54+
})
55+
ws.SubscribeDepthHF("depth_1", "BTC_CQ", 20, "incremental")
56+
ws.Start()
57+
58+
select {}
59+
}
60+
4961
func TestWS_SubscribeTrade(t *testing.T) {
5062
ws := newWSTest()
5163

hbdmswap/models.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,23 @@ type WSDepth struct {
265265
Tick Tick `json:"tick"`
266266
}
267267

268+
type WSTickHF struct {
269+
Asks [][]float64 `json:"asks"`
270+
Bids [][]float64 `json:"bids"`
271+
Ch string `json:"ch"`
272+
Event string `json:"event"`
273+
ID int64 `json:"id"`
274+
Mrid int64 `json:"mrid"`
275+
Ts int64 `json:"ts"`
276+
Version int `json:"version"`
277+
}
278+
279+
type WSDepthHF struct {
280+
Ch string `json:"ch"`
281+
Tick WSTickHF `json:"tick"`
282+
Ts int64 `json:"ts"`
283+
}
284+
268285
type WSTradeItem struct {
269286
Amount int `json:"amount"`
270287
Ts int64 `json:"ts"`

hbdmswap/ws.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ type WS struct {
2828
wsURL string
2929
accessKey string
3030
secretKey string
31+
debugMode bool
3132
subscriptions map[string]interface{}
3233

33-
tickerCallback func(trade *WSTicker)
34-
depthCallback func(depth *WSDepth)
35-
tradeCallback func(trade *WSTrade)
34+
tickerCallback func(trade *WSTicker)
35+
depthCallback func(depth *WSDepth)
36+
depthHFCallback func(depth *WSDepthHF)
37+
tradeCallback func(trade *WSTrade)
3638
}
3739

3840
// SetProxy 设置代理地址
@@ -70,6 +72,18 @@ func (ws *WS) SubscribeDepth(id string, symbol string) {
7072
ws.Subscribe(id, ch)
7173
}
7274

75+
// SubscribeDepthHF 订阅增量深度
76+
// size: 20/150 档位数,20:表示20档不合并的深度,150:表示150档不合并的深度
77+
// dateType: 数据类型,不填默认为全量数据,"incremental":增量数据,"snapshot":全量数据
78+
func (ws *WS) SubscribeDepthHF(id string, symbol string, size int, dateType string) {
79+
ch := map[string]interface{}{
80+
"id": id,
81+
"sub": fmt.Sprintf("market.%v.depth.size_%v.high_freq", symbol, size),
82+
"data_type": dateType,
83+
}
84+
ws.Subscribe(id, ch)
85+
}
86+
7387
// SubscribeTrade 订阅 Market Trade 数据
7488
// id: 订阅的编号
7589
// symbol: BTC_CQ
@@ -98,6 +112,10 @@ func (ws *WS) SetDepthCallback(callback func(depth *WSDepth)) {
98112
ws.depthCallback = callback
99113
}
100114

115+
func (ws *WS) SetDepthHFCallback(callback func(depth *WSDepthHF)) {
116+
ws.depthHFCallback = callback
117+
}
118+
101119
func (ws *WS) SetTradeCallback(callback func(trade *WSTrade)) {
102120
ws.tradeCallback = callback
103121
}
@@ -161,7 +179,9 @@ func (ws *WS) run() {
161179
func (ws *WS) handleMsg(messageType int, msg []byte) {
162180
ret := gjson.ParseBytes(msg)
163181

164-
//log.Printf("%v", string(msg))
182+
if ws.debugMode {
183+
log.Printf("%v", string(msg))
184+
}
165185

166186
if pingValue := ret.Get("ping"); pingValue.Exists() {
167187
// 心跳
@@ -177,7 +197,18 @@ func (ws *WS) handleMsg(messageType int, msg []byte) {
177197
// market.BTC_CQ.depth.step0
178198
ch := chValue.String()
179199
if strings.HasPrefix(ch, "market") {
180-
if strings.Contains(ch, ".depth") {
200+
if strings.HasSuffix(ch, ".high_freq") {
201+
var depth WSDepthHF
202+
err := json.Unmarshal(msg, &depth)
203+
if err != nil {
204+
log.Printf("%v", err)
205+
return
206+
}
207+
208+
if ws.depthHFCallback != nil {
209+
ws.depthHFCallback(&depth)
210+
}
211+
} else if strings.Contains(ch, ".depth") {
181212
var depth WSDepth
182213
err := json.Unmarshal(msg, &depth)
183214
if err != nil {
@@ -234,11 +265,12 @@ func (ws *WS) handlePing(ping int64) {
234265
// wsURL:
235266
// 正式地址 wss://api.hbdm.com/swap-ws
236267
// 开发地址 wss://api.btcgateway.pro/swap-ws
237-
func NewWS(wsURL string, accessKey string, secretKey string) *WS {
268+
func NewWS(wsURL string, accessKey string, secretKey string, debugMode bool) *WS {
238269
ws := &WS{
239270
wsURL: wsURL,
240271
accessKey: accessKey,
241272
secretKey: secretKey,
273+
debugMode: debugMode,
242274
subscriptions: make(map[string]interface{}),
243275
}
244276
ws.ctx, ws.cancel = context.WithCancel(context.Background())

hbdmswap/ws_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func newWSTest() *WS {
1818
secretKey := viper.GetString("secret_key")
1919

2020
wsURL := "wss://api.btcgateway.pro/swap-ws"
21-
ws := NewWS(wsURL, accessKey, secretKey)
21+
ws := NewWS(wsURL, accessKey, secretKey, true)
2222
return ws
2323
}
2424

@@ -45,6 +45,18 @@ func TestWS_SubscribeDepth(t *testing.T) {
4545
select {}
4646
}
4747

48+
func TestWS_SubscribeDepthHF(t *testing.T) {
49+
ws := newWSTest()
50+
51+
ws.SetDepthHFCallback(func(depth *WSDepthHF) {
52+
log.Printf("depth: %#v", depth)
53+
})
54+
ws.SubscribeDepthHF("depth_1", "BTC-USD", 20, "incremental")
55+
ws.Start()
56+
57+
select {}
58+
}
59+
4860
func TestWS_SubscribeTrade(t *testing.T) {
4961
ws := newWSTest()
5062

0 commit comments

Comments
 (0)