forked from tensam/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huobiwebsocket.go
219 lines (187 loc) · 6.4 KB
/
huobiwebsocket.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"github.com/thrasher-/socketio"
"log"
)
const (
HUOBI_SOCKETIO_ADDRESS = "https://hq.huobi.com:443"
//Service API
HUOBI_SOCKET_REQ_SYMBOL_LIST = "reqSymbolList"
HUOBI_SOCKET_REQ_SYMBOL_DETAIL = "reqSymbolDetail"
HUOBI_SOCKET_REQ_SUBSCRIBE = "reqMsgSubscribe"
HUOBI_SOCKET_REQ_UNSUBSCRIBE = "reqMsgUnsubscribe"
// Market data API
HUOBI_SOCKET_MARKET_DETAIL = "marketDetail"
HUOBI_SOCKET_TRADE_DETAIL = "tradeDetail"
HUOBI_SOCKET_MARKET_DEPTH_TOP = "marketDepthTop"
HUOBI_SOCKET_MARKET_DEPTH_TOP_SHORT = "marketDepthTopShort"
HUOBI_SOCKET_MARKET_DEPTH = "marketDepth"
HUOBI_SOCKET_MARKET_DEPTH_TOP_DIFF = "marketDepthTopDiff"
HUOBI_SOCKET_MARKET_DEPTH_DIFF = "marketDepthDiff"
HUOBI_SOCKET_MARKET_LAST_KLINE = "lastKLine"
HUOBI_SOCKET_MARKET_LAST_TIMELINE = "lastTimeLine"
HUOBI_SOCKET_MARKET_OVERVIEW = "marketOverview"
HUOBI_SOCKET_MARKET_STATIC = "marketStatic"
// History data API
HUOBI_SOCKET_REQ_TIMELINE = "reqTimeLine"
HUOBI_SOCKET_REQ_KLINE = "reqKLine"
HUOBI_SOCKET_REQ_DEPTH_TOP = "reqMarketDepthTop"
HUOBI_SOCKET_REQ_DEPTH = "reqMarketDepth"
HUOBI_SOCKET_REQ_TRADE_DETAIL_TOP = "reqTradeDetailTop"
HUOBI_SOCKET_REQ_MARKET_DETAIL = "reqMarketDetail"
)
var HuobiSocket *socketio.SocketIO
type HuobiDepth struct {
SymbolID string `json:"symbolId"`
Time float64 `json:"time"`
Version float64 `json:"version"`
BidName string `json:"bidName"`
BidPrice []float64 `json:"bidPrice"`
BidTotal []float64 `json:"bidTotal"`
BidAmount []float64 `json:"bidAmount"`
AskName string `json:"askName"`
AskPrice []float64 `json:"askPrice"`
AskTotal []float64 `json:"askTotal"`
AskAmount []float64 `json:"askAmount"`
}
type HuobiWebsocketTrade struct {
Price []float64 `json:"price"`
Level []float64 `json:"level"`
Amount []float64 `json:"amount"`
AccuAmount []float64 `json:"accuAmount"`
}
type HuobiWebsocketTradeDetail struct {
SymbolID string `json:"symbolId"`
TradeID []int64 `json:"tradeId"`
Price []float64 `json:"price"`
Time []int64 `json:"time"`
Amount []float64 `json:"amount"`
TopBids []HuobiWebsocketTrade `json:"topBids"`
TopAsks []HuobiWebsocketTrade `json:"topAsks"`
}
type HuobiWebsocketMarketOverview struct {
SymbolID string `json:"symbolId"`
Last float64 `json:"priceNew"`
Open float64 `json:"priceOpen"`
High float64 `json:"priceHigh"`
Low float64 `json:"priceLow"`
Ask float64 `json:"priceAsk"`
Bid float64 `json:"priceBid"`
Volume float64 `json:"totalVolume"`
TotalAmount float64 `json:"totalAmount"`
}
type HuobiWebsocketLastTimeline struct {
ID int64 `json:"_id"`
SymbolID string `json:"symbolId"`
Time int64 `json:"time"`
LastPrice float64 `json:"priceLast"`
Amount float64 `json:"amount"`
Volume float64 `json:"volume"`
Count int64 `json:"count"`
}
type HuobiResponse struct {
Version int `json:"version"`
MsgType string `json:"msgType"`
RequestIndex int64 `json:"requestIndex"`
RetCode int64 `json:"retCode"`
RetMessage string `json:"retMsg"`
Payload map[string]interface{} `json:"payload"`
}
func (h *HUOBI) BuildHuobiWebsocketRequest(msgType string, requestIndex int64, symbolRequest []string) map[string]interface{} {
request := map[string]interface{}{}
request["version"] = 1
request["msgType"] = msgType
if requestIndex != 0 {
request["requestIndex"] = requestIndex
}
if len(symbolRequest) != 0 {
request["symbolIdList"] = symbolRequest
}
return request
}
func (h *HUOBI) BuildHuobiWebsocketRequestExtra(msgType string, requestIndex int64, symbolIdList interface{}) interface{} {
request := map[string]interface{}{}
request["version"] = 1
request["msgType"] = msgType
if requestIndex != 0 {
request["requestIndex"] = requestIndex
}
request["symbolList"] = symbolIdList
return request
}
func (h *HUOBI) BuildHuobiWebsocketParamsList(objectName, currency, pushType, period, count, from, to, percentage string) interface{} {
list := map[string]interface{}{}
list["symbolId"] = currency
list["pushType"] = pushType
if period != "" {
list["period"] = period
}
if percentage != "" {
list["percent"] = percentage
}
if count != "" {
list["count"] = count
}
if from != "" {
list["from"] = from
}
if to != "" {
list["to"] = to
}
listArray := []map[string]interface{}{}
listArray = append(listArray, list)
listCompleted := make(map[string][]map[string]interface{})
listCompleted[objectName] = listArray
return listCompleted
}
func (h *HUOBI) OnConnect(output chan socketio.Message) {
if h.Verbose {
log.Printf("%s Connected to Websocket.", h.GetName())
}
for _, x := range h.EnabledPairs {
currency := StringToLower(x)
msg := h.BuildHuobiWebsocketRequestExtra(HUOBI_SOCKET_REQ_SUBSCRIBE, 100, h.BuildHuobiWebsocketParamsList(HUOBI_SOCKET_MARKET_OVERVIEW, currency, "pushLong", "", "", "", "", ""))
result, err := JSONEncode(msg)
if err != nil {
log.Println(err)
}
output <- socketio.CreateMessageEvent("request", string(result), nil, HuobiSocket.Version)
}
}
func (h *HUOBI) OnDisconnect(output chan socketio.Message) {
log.Printf("%s Disconnected from websocket server.. Reconnecting.\n", h.GetName())
h.WebsocketClient()
}
func (h *HUOBI) OnError() {
log.Printf("%s Error with Websocket connection.. Reconnecting.\n", h.GetName())
h.WebsocketClient()
}
func (h *HUOBI) OnMessage(message []byte, output chan socketio.Message) {
}
func (h *HUOBI) OnRequest(message []byte, output chan socketio.Message) {
response := HuobiResponse{}
err := JSONDecode(message, &response)
if err != nil {
log.Println(err)
}
}
func (h *HUOBI) WebsocketClient() {
events := make(map[string]func(message []byte, output chan socketio.Message))
events["request"] = h.OnRequest
events["message"] = h.OnMessage
HuobiSocket = &socketio.SocketIO{
Version: 0.9,
OnConnect: h.OnConnect,
OnEvent: events,
OnError: h.OnError,
OnDisconnect: h.OnDisconnect,
}
for h.Enabled && h.Websocket {
err := socketio.ConnectToSocket(HUOBI_SOCKETIO_ADDRESS, HuobiSocket)
if err != nil {
log.Printf("%s Unable to connect to Websocket. Err: %s\n", err)
continue
}
log.Printf("%s Disconnected from Websocket.")
}
}