-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpublic_market_data.go
405 lines (340 loc) · 9.29 KB
/
public_market_data.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package kraken
import (
"fmt"
"math"
"net/url"
"strconv"
"time"
"github.com/shopspring/decimal"
)
// ServerTime
// Get the server's time.
// https://docs.kraken.com/rest/#operation/getServerTime
func (c *Client) ServerTime() (*ServerTime, error) {
payload := Payload{}
response := ServerTime{}
err := c.doRequest("Time", false, url.Values(payload), &response)
return &response, err
}
// SystemStatus
// Get the last system status or trading mode.
// https://docs.kraken.com/rest/#operation/getSystemStatus
func (c *Client) SystemStatus() (*SystemStatus, error) {
payload := Payload{}
response := SystemStatus{}
err := c.doRequest("SystemStatus", false, url.Values(payload), &response)
return &response, err
}
type AssetsConfig struct {
// AssetClass is optional
// Default: currency
AssetClass AssetClass
// Assets is optional
Assets []Asset
}
// Assets
// Get information about the assets that are available for deposit, withdrawal, trading and staking.
// https://docs.kraken.com/rest/#operation/getAssetInfo
func (c *Client) Assets(config AssetsConfig) (map[Asset]AssetInfo, error) {
payload := Payload{}
payload.OptAssetClass(config.AssetClass)
payload.OptAssets(config.Assets...)
response := make(map[Asset]AssetInfo)
err := c.doRequest("Assets", false, url.Values(payload), &response)
return response, err
}
type AssetPairsConfig struct {
// AssetPairs is optional
AssetPairs []AssetPair
// Information is optional
// Default: info
Information Information
}
// AssetPairs
// Get tradable asset pairs
// https://docs.kraken.com/rest/#operation/getTradableAssetPairs
func (c *Client) AssetPairs(config AssetPairsConfig) (map[AssetPair]AssetPairsInfo, error) {
payload := Payload{}
payload.OptAssetPairs(config.AssetPairs...)
payload.OptInformations(config.Information)
var response map[AssetPair]AssetPairsInfo
err := c.doRequest("AssetPairs", false, url.Values(payload), &response)
if err != nil {
return nil, err
}
return response, nil
}
type TickerInformationConfig struct {
// AssetPairs is optional
AssetPairs []AssetPair
}
// TickerInformation
// Today's prices start at midnight UTC. Leaving the pair parameter blank will return tickers for all tradeable
// assets on Kraken.
// https://docs.kraken.com/rest/#tag/Market-Data/operation/getTradableAssetPairs
func (c *Client) TickerInformation(config TickerInformationConfig) (map[AssetPair]AssetTickerInfo, error) {
payload := Payload{}
payload.OptAssetPairs(config.AssetPairs...)
var response map[AssetPair]AssetTickerInfo
err := c.doRequest("Ticker", false, url.Values(payload), &response)
if err != nil {
return nil, err
}
return response, nil
}
type OHLCConfig struct {
// AssetPair is required
AssetPair AssetPair
// Interval is optional
// Default: 1min
Interval Interval
// Since is optional
Since time.Time
}
// OHLC
// Means "Open-high-low-close chart"
// Note: the last entry in the OHLC array is for the last, not-yet-committed frame and will always
// be present, regardless of the value of `since`.
// https://docs.kraken.com/rest/#operation/getOHLCData
func (c *Client) OHLC(config OHLCConfig) ([]OHLCData, time.Time, error) {
if config.AssetPair == "" {
return nil, time.Time{}, fmt.Errorf("AssetPair is required")
}
payload := Payload{}
payload.OptAssetPairs(config.AssetPair)
payload.OptInterval(config.Interval)
payload.OptSince(config.Since)
var resp interface{}
err := c.doRequest("OHLC", false, url.Values(payload), &resp)
if err != nil {
return nil, time.Time{}, err
}
var last time.Time
response := []OHLCData{}
for key, value := range resp.(map[string]interface{}) {
if key == "last" {
last = time.Unix(int64(value.(float64)), 0)
continue
}
for _, array := range value.([]interface{}) {
a := array.([]interface{})
if len(a) == 8 {
t := time.Unix(int64(a[0].(float64)), 0)
open, err := decimal.NewFromString(a[1].(string))
if err != nil {
continue
}
high, err := decimal.NewFromString(a[2].(string))
if err != nil {
continue
}
low, err := decimal.NewFromString(a[3].(string))
if err != nil {
continue
}
close, err := decimal.NewFromString(a[4].(string))
if err != nil {
continue
}
vwap, err := decimal.NewFromString(a[5].(string))
if err != nil {
continue
}
volume, err := decimal.NewFromString(a[6].(string))
if err != nil {
continue
}
ohlcData := OHLCData{
Time: t,
Open: open,
High: high,
Low: low,
Close: close,
VWAP: vwap,
Volume: volume,
Count: int64(a[7].(float64)),
}
response = append(response, ohlcData)
}
}
}
return response, last, nil
}
type OrderBookConfig struct {
// AssetPair is required
AssetPair AssetPair
// Count is optional
// Default: 100
Count int64
}
// OrderBook
// https://docs.kraken.com/rest/#operation/getOrderBook
func (c *Client) OrderBook(config OrderBookConfig) (*OrderBook, error) {
if config.AssetPair == "" {
return nil, fmt.Errorf("AssetPair is required")
}
payload := Payload{}
payload.OptAssetPairs(config.AssetPair)
payload.OptCount(config.Count)
var resp interface{}
err := c.doRequest("Depth", false, url.Values(payload), &resp)
if err != nil {
return nil, err
}
response := OrderBook{}
for _, value := range resp.(map[string]interface{}) {
for side, array := range value.(map[string]interface{}) {
orderBookEntries := []OrderBookEntry{}
for _, a := range array.([]interface{}) {
obe := a.([]interface{})
if len(obe) == 3 {
t := time.Unix(int64(obe[2].(float64)), 0)
price, err := decimal.NewFromString(obe[0].(string))
if err != nil {
continue
}
volume, err := decimal.NewFromString(obe[1].(string))
if err != nil {
continue
}
orderBookEntry := OrderBookEntry{
Price: price,
Volume: volume,
Time: t,
}
orderBookEntries = append(orderBookEntries, orderBookEntry)
}
}
if side == "asks" {
response.Asks = orderBookEntries
} else if side == "bids" {
response.Bids = orderBookEntries
}
}
break
}
return &response, nil
}
type RecentTradesConfig struct {
// AssetPair is required
AssetPair AssetPair
// Since is optional
Since time.Time
}
// RecentTrades
// Returns the last 1000 trades by default
// https://docs.kraken.com/rest/#operation/getRecentTrades
func (c *Client) RecentTrades(config RecentTradesConfig) ([]TradeData, time.Time, error) {
if config.AssetPair == "" {
return nil, time.Time{}, fmt.Errorf("AssetPair is required")
}
payload := Payload{}
payload.OptAssetPairs(config.AssetPair)
payload.OptSince(config.Since)
var resp interface{}
err := c.doRequest("Trades", false, url.Values(payload), &resp)
if err != nil {
return nil, time.Time{}, err
}
var last time.Time
response := []TradeData{}
for key, value := range resp.(map[string]interface{}) {
if key == "last" {
i, err := strconv.ParseInt(value.(string), 10, 64)
if err != nil {
continue
}
last = time.Unix(0, i)
continue
}
for _, array := range value.([]interface{}) {
a := array.([]interface{})
if len(a) == 7 {
price, err := decimal.NewFromString(a[0].(string))
if err != nil {
continue
}
volume, err := decimal.NewFromString(a[1].(string))
if err != nil {
continue
}
integer, decimal := math.Modf(a[2].(float64))
t := time.Unix(int64(integer), int64(decimal*10000))
var tradeType Type
if a[3].(string) == "s" {
tradeType = Sell
} else if a[3].(string) == "b" {
tradeType = Buy
}
var orderType OrderType
if a[4].(string) == "m" {
orderType = Market
} else if a[4].(string) == "l" {
orderType = Limit
}
tradeData := TradeData{
Price: price,
Volume: volume,
Time: t,
Type: tradeType,
OrderType: orderType,
Miscellaneous: a[5].(string),
TradeID: int64(a[6].(float64)),
}
response = append(response, tradeData)
}
}
}
return response, last, err
}
type RecentSpreadsConfig struct {
// AssetPair is required
AssetPair AssetPair
// Since is optional
Since time.Time
}
// RecentSpreads
// Returns the last 1000 trades by default
// https://docs.kraken.com/rest/#operation/getRecentSpreads
func (c *Client) RecentSpreads(config RecentSpreadsConfig) ([]SpreadData, time.Time, error) {
if config.AssetPair == "" {
return nil, time.Time{}, fmt.Errorf("AssetPair is required")
}
payload := Payload{}
payload.OptAssetPairs(config.AssetPair)
payload.OptSince(config.Since)
var resp interface{}
err := c.doRequest("Spread", false, url.Values(payload), &resp)
if err != nil {
return nil, time.Time{}, err
}
var last time.Time
response := []SpreadData{}
for key, value := range resp.(map[string]interface{}) {
if key == "last" {
last = time.Unix(int64(value.(float64)), 0)
continue
}
for _, array := range value.([]interface{}) {
a := array.([]interface{})
if len(a) == 3 {
t := time.Unix(int64(a[0].(float64)), 0)
bid, err := decimal.NewFromString(a[1].(string))
if err != nil {
continue
}
ask, err := decimal.NewFromString(a[2].(string))
if err != nil {
continue
}
spreadData := SpreadData{
Time: t,
Bid: bid,
Ask: ask,
}
response = append(response, spreadData)
}
}
}
return response, last, nil
}