forked from hbdmapi/huobi_futures_Golang
/
ws_index_client.go
150 lines (123 loc) · 5.18 KB
/
ws_index_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package ws
import (
"encoding/json"
"fmt"
"github.com/alezh/huobi_futures_Golang/sdk/linearswap"
"github.com/alezh/huobi_futures_Golang/sdk/linearswap/ws/response/index"
"github.com/alezh/huobi_futures_Golang/sdk/wsbase"
"reflect"
)
type WSIndexClient struct {
WebSocketOp
}
func (wsIx *WSIndexClient) Init(host string) *WSIndexClient {
if host == "" {
host = linearswap.LINEAR_SWAP_DEFAULT_HOST
}
wsIx.open("/ws_index", host, "", "", true)
return wsIx
}
// -------------------------------------------------------------
// premium index kline start
type OnSubPremiumIndexKLineResponse func(*index.SubIndexKLineResponse)
type OnReqPremiumIndexKLineResponse func(*index.ReqIndexKLineResponse)
func (wsIx *WSIndexClient) SubPremiumIndexKLine(contractCode string, period string, callbackFun OnSubPremiumIndexKLineResponse, id string) {
if id == "" {
id = linearswap.DEFAULT_ID
}
ch := fmt.Sprintf("market.%s.premium_index.%s", contractCode, period)
subData := wsbase.WSSubData{Sub: ch, Id: id}
jdata, _ := json.Marshal(subData)
wsIx.sub(jdata, ch, callbackFun, reflect.TypeOf(index.SubIndexKLineResponse{}))
}
func (wsIx *WSIndexClient) ReqPremiumIndexKLine(contractCode string, period string, callbackFun OnReqPremiumIndexKLineResponse,
from int64, to int64, id string) {
if id == "" {
id = linearswap.DEFAULT_ID
}
ch := fmt.Sprintf("market.%s.premium_index.%s", contractCode, period)
reqData := wsbase.WSReqData{Req: ch, Id: id, From: from, To: to}
jdata, _ := json.Marshal(reqData)
wsIx.req(jdata, ch, callbackFun, reflect.TypeOf(index.ReqIndexKLineResponse{}))
}
// premium index kline end
//-------------------------------------------------------------
// -------------------------------------------------------------
// mark price kline start
type OnSubMarkPriceKLineResponse func(*index.SubIndexKLineResponse)
type OnReqMarkPriceKLineResponse func(*index.ReqIndexKLineResponse)
func (wsIx *WSIndexClient) SubMarkPriceKLine(contractCode string, period string, callbackFun OnSubMarkPriceKLineResponse, id string) {
if id == "" {
id = linearswap.DEFAULT_ID
}
ch := fmt.Sprintf("market.%s.mark_price.%s", contractCode, period)
subData := wsbase.WSSubData{Sub: ch, Id: id}
jdata, _ := json.Marshal(subData)
wsIx.sub(jdata, ch, callbackFun, reflect.TypeOf(index.SubIndexKLineResponse{}))
}
func (wsIx *WSIndexClient) ReqMarkPriceKLine(contractCode string, period string, callbackFun OnReqMarkPriceKLineResponse,
from int64, to int64, id string) {
if id == "" {
id = linearswap.DEFAULT_ID
}
ch := fmt.Sprintf("market.%s.mark_price.%s", contractCode, period)
reqData := wsbase.WSReqData{Req: ch, Id: id, From: from, To: to}
jdata, _ := json.Marshal(reqData)
wsIx.req(jdata, ch, callbackFun, reflect.TypeOf(index.ReqIndexKLineResponse{}))
}
// mark price kline end
//-------------------------------------------------------------
// -------------------------------------------------------------
// estimated rate kline start
type OnSubEstimatedRateResponse func(*index.SubIndexKLineResponse)
type OnReqEstimatedRateResponse func(*index.ReqIndexKLineResponse)
func (wsIx *WSIndexClient) SubEstimatedRateKLine(contractCode string, period string, callbackFun OnSubEstimatedRateResponse, id string) {
if id == "" {
id = linearswap.DEFAULT_ID
}
ch := fmt.Sprintf("market.%s.estimated_rate.%s", contractCode, period)
subData := wsbase.WSSubData{Sub: ch, Id: id}
jdata, _ := json.Marshal(subData)
wsIx.sub(jdata, ch, callbackFun, reflect.TypeOf(index.SubIndexKLineResponse{}))
}
func (wsIx *WSIndexClient) ReqEstimatedRateKLine(contractCode string, period string, callbackFun OnReqEstimatedRateResponse,
from int64, to int64, id string) {
if id == "" {
id = linearswap.DEFAULT_ID
}
ch := fmt.Sprintf("market.%s.estimated_rate.%s", contractCode, period)
reqData := wsbase.WSReqData{Req: ch, Id: id, From: from, To: to}
jdata, _ := json.Marshal(reqData)
wsIx.req(jdata, ch, callbackFun, reflect.TypeOf(index.ReqIndexKLineResponse{}))
}
// estimated rate kline end
//-------------------------------------------------------------
// -------------------------------------------------------------
// basis start
type OnSubBasisResponse func(*index.SubBasiesResponse)
type OnReqBasisResponse func(*index.ReqBasisResponse)
func (wsIx *WSIndexClient) SubBasis(contractCode string, period string, callbackFun OnSubBasisResponse, basisPriceType string, id string) {
if basisPriceType == "" {
basisPriceType = "open"
}
if id == "" {
id = linearswap.DEFAULT_ID
}
ch := fmt.Sprintf("market.%s.basis.%s.%s", contractCode, period, basisPriceType)
subData := wsbase.WSSubData{Sub: ch, Id: id}
jdata, _ := json.Marshal(subData)
wsIx.sub(jdata, ch, callbackFun, reflect.TypeOf(index.SubBasiesResponse{}))
}
func (wsIx *WSIndexClient) ReqBasis(contractCode string, period string, callbackFun OnReqBasisResponse, from int64, to int64,
basisPriceType string, id string) {
if basisPriceType == "" {
basisPriceType = "open"
}
if id == "" {
id = linearswap.DEFAULT_ID
}
ch := fmt.Sprintf("market.%s.basis.%s.%s", contractCode, period, basisPriceType)
reqData := wsbase.WSReqData{Req: ch, Id: id, From: from, To: to}
jdata, _ := json.Marshal(reqData)
wsIx.req(jdata, ch, callbackFun, reflect.TypeOf(index.ReqBasisResponse{}))
}