-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpublic.go
433 lines (346 loc) · 10.5 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package krakenapi
import (
"net/url"
"strconv"
"strings"
)
/*
URL: https://api.kraken.com/0/public/Time
Result: Server's time
unixtime = as unix timestamp
rfc1123 = as RFC 1123 time format
Note: This is to aid in approximating the skew time between the server and client.
*/
func (api *KrakenApi) ApiServerTime() (interface{}, error) {
resp, err := api.Query(URL_PUBLIC_TIME, url.Values{}, false)
if err != nil {
return nil, err
}
return parse(resp, nil)
}
/*
URL: https://api.kraken.com/0/public/Assets
Input:
info = info to retrieve (optional):
info = all info (default)
aclass = asset class (optional):
currency (default)
asset = comma delimited list of assets to get info on (optional. default = all for given asset class)
Result: array of asset names and their info
<asset_name> = asset name
altname = alternate name
aclass = asset class
decimals = scaling decimal places for record keeping
display_decimals = scaling decimal places for output display
*/
func (api *KrakenApi) ApiAssets() (map[string]Asset, error) {
resp, err := api.Query(URL_PUBLIC_ASSETS, url.Values{}, false)
if err != nil {
return nil, err
}
assets := make(map[string]Asset)
_, err = parse(resp, &assets)
if err != nil {
return nil, err
}
return assets, nil
}
/*
URL: https://api.kraken.com/0/public/AssetPairs
Input:
info = info to retrieve (optional):
info = all info (default)
leverage = leverage info
fees = fees schedule
margin = margin info
pair = comma delimited list of asset pairs to get info on (optional. default = all)
Result: array of pair names and their info
<pair_name> = pair name
altname = alternate pair name
aclass_base = asset class of base component
base = asset id of base component
aclass_quote = asset class of quote component
quote = asset id of quote component
lot = volume lot size
pair_decimals = scaling decimal places for pair
lot_decimals = scaling decimal places for volume
lot_multiplier = amount to multiply lot volume by to get currency volume
leverage_buy = array of leverage amounts available when buying
leverage_sell = array of leverage amounts available when selling
fees = fee schedule array in [volume, percent fee] tuples
fees_maker = maker fee schedule array in [volume, percent fee] tuples (if on maker/taker)
fee_volume_currency = volume discount currency
margin_call = margin call level
margin_stop = stop-out/liquidation margin level
Note: If an asset pair is on a maker/taker fee schedule, the taker side is given in "fees"
and maker side in "fees_maker". For pairs not on maker/taker, they will only be given in "fees".
*/
func (api *KrakenApi) ApiAssetPairs(info, pair string) (map[string]AssetPair, error) {
params := url.Values{}
if pair != "" {
params.Set("pair", pair)
}
if info != "" {
params.Set("info", info)
}
resp, err := api.Query(URL_PUBLIC_ASSET_PAIRS, params, false)
if err != nil {
return nil, err
}
assets_pairs := make(map[string]AssetPair)
_, err = parse(resp, &assets_pairs)
if err != nil {
return nil, err
}
return assets_pairs, nil
}
/*
URL: https://api.kraken.com/0/public/Ticker
Input:
pair = comma delimited list of asset pairs to get info on
Result: array of pair names and their ticker info
<pair_name> = pair name
a = ask array(<price>, <whole lot volume>, <lot volume>),
b = bid array(<price>, <whole lot volume>, <lot volume>),
c = last trade closed array(<price>, <lot volume>),
v = volume array(<today>, <last 24 hours>),
p = volume weighted average price array(<today>, <last 24 hours>),
t = number of trades array(<today>, <last 24 hours>),
l = low array(<today>, <last 24 hours>),
h = high array(<today>, <last 24 hours>),
o = today's opening price
Note: Today's prices start at 00:00:00 UTC
*/
func (api *KrakenApi) ApiTicker(pairs []string) (map[string]Ticker, error) {
params := url.Values{}
params.Set("pair", strings.Join(pairs, ","))
resp, err := api.Query(URL_PUBLIC_TICKER, params, false)
if err != nil {
return nil, err
}
tickers := make(map[string]Ticker)
_, err = parse(resp, &tickers)
if err != nil {
return nil, err
}
return tickers, nil
}
/*
URL: https://api.kraken.com/0/public/OHLC
Input:
pair = asset pair to get OHLC data for
interval = time frame interval in minutes (optional):
1 (default), 5, 15, 30, 60, 240, 1440, 10080, 21600
since = return committed OHLC data since given id (optional. exclusive)
Result: array of pair name and OHLC data
<pair_name> = pair name
array of array entries(<time>, <open>, <high>, <low>, <close>, <vwap>, <volume>, <count>)
last = id to be used as since when polling for new, committed OHLC data
Note: the last entry in the OHLC array is for the current, not-yet-committed frame and will always
be present, regardless of the value of "since".
*/
func (api *KrakenApi) ApiOHLC(pair string, interval int, since uint64) (float64, []OHLCEntry, error) {
params := url.Values{}
params.Set("pair", pair)
if interval != 0 {
params.Set("interval", strconv.Itoa(interval))
}
if since != 0 {
params.Set("since", strconv.FormatUint(since, 10))
}
resp, err := api.Query(URL_PUBLIC_OHLC, params, false)
if err != nil {
return 0, nil, err
}
content, err := parse(resp, nil)
if err != nil {
return 0, nil, err
}
var last float64
ohlc_data := make([]OHLCEntry, 0)
// Don't have much choice as returned json contains arrays, not structures...
for key, value := range content.(map[string]interface{}) {
if key == "last" {
last = value.(float64)
} else {
for _, subvalue := range value.([]interface{}) {
values := make([]interface{}, len(subvalue.([]interface{})))
for i, subsubvalue := range subvalue.([]interface{}) {
values[i] = subsubvalue
}
open, err := strconv.ParseFloat(values[1].(string), 64)
if err != nil {
return 0, nil, err
}
high, err := strconv.ParseFloat(values[2].(string), 64)
if err != nil {
return 0, nil, err
}
low, err := strconv.ParseFloat(values[3].(string), 64)
if err != nil {
return 0, nil, err
}
close, err := strconv.ParseFloat(values[4].(string), 64)
if err != nil {
return 0, nil, err
}
vwap, err := strconv.ParseFloat(values[5].(string), 64)
if err != nil {
return 0, nil, err
}
volume, err := strconv.ParseFloat(values[6].(string), 64)
if err != nil {
return 0, nil, err
}
entry := OHLCEntry{
Time: values[0].(float64),
Open: open,
High: high,
Low: low,
Close: close,
VWAP: vwap,
Volume: volume,
Count: values[7].(float64),
}
ohlc_data = append(ohlc_data, entry)
}
}
}
return last, ohlc_data, nil
}
/*
URL: https://api.kraken.com/0/public/Depth
Input:
pair = asset pair to get market depth for
count = maximum number of asks/bids (optional)
Result: array of pair name and market depth
<pair_name> = pair name
asks = ask side array of array entries(<price>, <volume>, <timestamp>)
bids = bid side array of array entries(<price>, <volume>, <timestamp>)
*/
func (api *KrakenApi) ApiDepth(pair string, count int) (map[string]PublicOrderBook, error) { // XXX
params := url.Values{}
params.Set("pair", pair)
if count != 0 {
params.Set("count", strconv.Itoa(count))
}
resp, err := api.Query(URL_PUBLIC_ORDER_BOOK, params, false)
if err != nil {
return nil, err
}
out := make(map[string]PublicOrderBook)
_, err = parse(resp, &out)
if err != nil {
return nil, err
}
return out, nil
}
/*
Input:
pair = asset pair to get trade data for
since = return trade data since given id (optional. exclusive)
Result: array of pair name and recent trade data
<pair_name> = pair name
array of array entries(<price>, <volume>, <time>, <buy/sell>, <market/limit>, <miscellaneous>)
last = id to be used as since when polling for new trade data
*/
func (api *KrakenApi) ApiTrades(pair string, since string) (map[string][]RecentTrade, float64, error) {
params := url.Values{}
params.Set("pair", pair)
if since != "" {
params.Set("since", since)
}
resp, err := api.Query(URL_PUBLIC_RECENT_TRADES, params, false)
if err != nil {
return nil, 0, err
}
content, err := parse(resp, nil)
if err != nil {
return nil, 0, err
}
var last float64
out := make(map[string][]RecentTrade)
for key, value := range content.(map[string]interface{}) {
if key == "last" {
last, err = strconv.ParseFloat(value.(string), 64)
if err != nil {
return nil, 0, err
}
break
}
trades := make([]RecentTrade, 0)
for _, subvalue := range value.([]interface{}) {
values := subvalue.([]interface{})
price, err := strconv.ParseFloat(values[0].(string), 64)
if err != nil {
return nil, 0, err
}
volume, err := strconv.ParseFloat(values[1].(string), 64)
if err != nil {
return nil, 0, err
}
trades = append(trades, RecentTrade{
Price: price,
Volume: volume,
Time: values[2].(float64),
Type: values[3].(string),
TradeType: values[4].(string),
Misc: values[5].(string),
})
}
out[key] = trades
}
return out, last, err
}
/*
Input:
pair = asset pair to get spread data for
since = return spread data since given id (optional. inclusive)
Result: array of pair name and recent spread data
<pair_name> = pair name
array of array entries(<time>, <bid>, <ask>)
last = id to be used as since when polling for new spread data
Note: "since" is inclusive so any returned data with the same time as the previous set should overwrite all of the previous set's entries at that time
*/
func (api *KrakenApi) ApiSpread(pair string, since string) (map[string][]Spread, float64, error) {
params := url.Values{}
params.Set("pair", pair)
if since != "" {
params.Set("since", since)
}
resp, err := api.Query(URL_PUBLIC_SPREAD, params, false)
if err != nil {
return nil, 0, err
}
content, err := parse(resp, nil)
if err != nil {
return nil, 0, err
}
var last float64
out := make(map[string][]Spread)
for key, value := range content.(map[string]interface{}) {
if key == "last" {
last = value.(float64)
continue
}
spreads := make([]Spread, 0)
for _, subvalue := range value.([]interface{}) {
values := subvalue.([]interface{})
ask, err := strconv.ParseFloat(values[1].(string), 64)
if err != nil {
return nil, 0, err
}
bid, err := strconv.ParseFloat(values[2].(string), 64)
if err != nil {
return nil, 0, err
}
spreads = append(spreads, Spread{
Time: values[0].(float64),
Ask: ask,
Bid: bid,
})
}
out[key] = spreads
}
return out, last, nil
}