-
Notifications
You must be signed in to change notification settings - Fork 2
/
public.go
319 lines (250 loc) · 6.63 KB
/
public.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package hitbtc
import (
"encoding/json"
"strconv"
"strings"
"time"
)
type Currency struct {
Id string `json:"id"`
FullName string `json:"fullName"`
Crypto bool `json:"crypto"`
PayinEnabled bool `json:"payinEnabled"`
PayinPaymentId bool `json:"payinPaymentId"`
PayinConfirmations uint `json:"payinConfirmations"`
PayoutEnabled bool `json:"payoutEnabled"`
PayoutIsPaymentId bool `json:"payoutIsPaymentId"`
TransferEnabled bool `json:"transferEnabled"`
Delisted bool `json:"delisted"`
PayoutFee float64 `json:"payoutFee,string"`
}
func (h *HitBtc) GetCurrency(id string) (currency Currency, err error) {
respch := make(chan []byte)
errch := make(chan error)
go h.publicRequest("/api/2/public/currency/"+strings.ToUpper(id), respch, errch)
response := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(response, ¤cy)
return
}
func (h *HitBtc) GetCurrencies() (currencies []Currency, err error) {
respch := make(chan []byte)
errch := make(chan error)
go h.publicRequest("/api/2/public/currency/", respch, errch)
response := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(response, ¤cies)
return
}
type Symbol struct {
Id string `json:"id"`
BaseCurrency string `json:"baseCurrency"`
QuoteCurrency string `json:"quoteCurrency"`
QuantityIncrement float64 `json:"quantityIncrement,string"`
TickSize float64 `json:"tickSize,string"`
TakeLiquidityRate float64 `json:"takeLiquidityRate,string"`
ProvideLiquidityRate float64 `json:"provideLiquidityRate,string"`
FeeCurrency string `json:"feeCurrency"`
}
func (h *HitBtc) GetSymbol(id string) (symbol Symbol, err error) {
respch := make(chan []byte)
errch := make(chan error)
go h.publicRequest("/api/2/public/symbol/"+strings.ToUpper(id), respch, errch)
response := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(response, &symbol)
return
}
func (h *HitBtc) GetSymbols() (symbols []Symbol, err error) {
respch := make(chan []byte)
errch := make(chan error)
go h.publicRequest("/api/2/public/symbol/", respch, errch)
response := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(response, &symbols)
return
}
type Ticker struct {
Ask float64 `json:"ask,string"`
Bid float64 `json:"bid,string"`
Last float64 `json:"last,string"`
Open float64 `json:"open,string"`
Low float64 `json:"low,string"`
High float64 `json:"high,string"`
Volume float64 `json:"volume,string"`
VolumeQuote float64 `json:"volumeQuote,string"`
Timestamp time.Time `json:"timeStamp"`
Symbol string `json:"symbol"`
}
func (t *Ticker) UnmarshalJSON(b []byte) error {
var ticker map[string]string
err := json.Unmarshal(b, &ticker)
if err != nil {
return err
}
t.Ask, err = strconv.ParseFloat(ticker["ask"], 64)
if err != nil {
t.Ask = 0
}
t.Bid, err = strconv.ParseFloat(ticker["bid"], 64)
if err != nil {
t.Bid = 0
}
t.Last, err = strconv.ParseFloat(ticker["last"], 64)
if err != nil {
t.Last = 0
}
t.Open, err = strconv.ParseFloat(ticker["open"], 64)
if err != nil {
t.Open = 0
}
t.Low, err = strconv.ParseFloat(ticker["low"], 64)
if err != nil {
t.Low = 0
}
t.High, err = strconv.ParseFloat(ticker["high"], 64)
if err != nil {
t.High = 0
}
t.Volume, err = strconv.ParseFloat(ticker["volume"], 64)
if err != nil {
t.Volume = 0
}
t.VolumeQuote, err = strconv.ParseFloat(ticker["volumeQuote"], 64)
if err != nil {
t.VolumeQuote = 0
}
layout := "2006-01-02T15:04:05.000Z"
t.Timestamp, err = time.Parse(layout, ticker["timestamp"])
if err != nil {
t.Timestamp = time.Time{}
}
t.Symbol = ticker["symbol"]
return nil
}
func (h *HitBtc) GetTicker(symbol string) (ticker Ticker, err error) {
respch := make(chan []byte)
errch := make(chan error)
go h.publicRequest("/api/2/public/ticker/"+strings.ToUpper(symbol), respch, errch)
response := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(response, &ticker)
return
}
func (h *HitBtc) GetTickers() (tickers []Ticker, err error) {
respch := make(chan []byte)
errch := make(chan error)
go h.publicRequest("/api/2/public/ticker/", respch, errch)
response := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(response, &tickers)
return
}
type MarketTrade struct {
Id int `json:"id"`
Price float64 `json:"price,string"`
Quantity float64 `json:"quantity,string"`
Side string `json:"side"`
Timestamp time.Time "json:timestamp"
}
func (h *HitBtc) GetTrades(symbol string) (trades []MarketTrade, err error) {
respch := make(chan []byte)
errch := make(chan error)
go h.publicRequest("/api/2/public/trades/"+strings.ToUpper(symbol), respch, errch)
response := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(response, &trades)
return
}
type Book struct {
Price float64 `json:"price,string"`
Size float64 `json:"size,string"`
}
type OrderBook struct {
Ask []Book `json:"ask"`
Bid []Book `json:"bid"`
}
func (h *HitBtc) GetOrderBook(symbol string, args ...int) (orderbooks OrderBook, err error) {
var limit int
respch := make(chan []byte)
errch := make(chan error)
if len(args) > 0 {
limit = args[0]
} else {
limit = 100
}
go h.publicRequest(
"/api/2/public/orderbook/"+strings.ToUpper(symbol)+spr("?limit=%d", limit),
respch,
errch,
)
response := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(response, &orderbooks)
return
}
type Candle struct {
Timestamp time.Time `json:"timestamp"`
Open float64 `json:"open,string"`
Close float64 `json:"close,string"`
Min float64 `json:"min,string"`
Max float64 `json:"max,string"`
Volume float64 `json:"volume,string"`
VolumeQuote float64 `json:"volumeQuote,string"`
}
// period list
// M1 (one minute), M3, M5, M15, M30, H1, H4, D1, D7, 1M (one month).
func (h *HitBtc) GetCandles(symbol, period string, limit int) (candles []Candle, err error) {
switch period {
case "M1":
case "M3":
case "M5":
case "M15":
case "M30":
case "H1":
case "H4":
case "D1":
case "D7":
case "1M":
default:
return nil, Error(PeriodError)
}
respch := make(chan []byte)
errch := make(chan error)
go h.publicRequest(
"/api/2/public/candles/"+strings.ToUpper(symbol)+spr("?limit=%d&period=%s", limit, period),
respch,
errch,
)
response := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(response, &candles)
return
}