forked from beldur/kraken-go-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
512 lines (480 loc) · 13.9 KB
/
types.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package krakenapi
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
const (
BCHEUR = "BCHEUR"
BCHUSD = "BCHUSD"
BCHXBT = "BCHXBT"
DASHEUR = "DASHEUR"
DASHUSD = "DASHUSD"
DASHXBT = "DASHXBT"
EOSETH = "EOSETH"
EOSEUR = "EOSEUR"
EOSUSD = "EOSUSD"
EOSXBT = "EOSXBT"
GNOETH = "GNOETH"
GNOEUR = "GNOEUR"
GNOUSD = "GNOUSD"
GNOXBT = "GNOXBT"
USDTZUSD = "USDTZUSD"
XETCXETH = "XETCXETH"
XETCXUSD = "XETCXUSD"
XETCXXBT = "XETCXXBT"
XETCZEUR = "XETCZEUR"
XETHXXBT = "XETHXXBT"
XETHZCAD = "XETHZCAD"
XETHZEUR = "XETHZEUR"
XETHZGBP = "XETHZGBP"
XETHZJPY = "XETHZJPY"
XETHZUSD = "XETHZUSD"
XICNXETH = "XICNXETH"
XICNXXBT = "XICNXXBT"
XLTCXXBT = "XLTCXXBT"
XLTCZEUR = "XLTCZEUR"
XLTCZUSD = "XLTCZUSD"
XMLNXETH = "XMLNXETH"
XMLNXXBT = "XMLNXXBT"
XREPXETH = "XREPXETH"
XREPXXBT = "XREPXXBT"
XREPZEUR = "XREPZEUR"
XREPZUSD = "XREPZUSD"
XXBTZCAD = "XXBTZCAD"
XXBTZEUR = "XXBTZEUR"
XXBTZGBP = "XXBTZGBP"
XXBTZJPY = "XXBTZJPY"
XXBTZUSD = "XXBTZUSD"
XXDGXXBT = "XXDGXXBT"
XXLMXXBT = "XXLMXXBT"
XXLMZEUR = "XXLMZEUR"
XXLMZUSD = "XXLMZUSD"
XXMRXXBT = "XXMRXXBT"
XXMRZEUR = "XXMRZEUR"
XXMRZUSD = "XXMRZUSD"
XXRPXXBT = "XXRPXXBT"
XXRPZCAD = "XXRPZCAD"
XXRPZEUR = "XXRPZEUR"
XXRPZJPY = "XXRPZJPY"
XXRPZUSD = "XXRPZUSD"
XZECXXBT = "XZECXXBT"
XZECZEUR = "XZECZEUR"
XZECZUSD = "XZECZUSD"
)
// It's for the type of price
const (
BUY = "b"
SELL = "s"
MARKET = "m"
LIMIT = "l"
)
// KrakenResponse wraps the Kraken API JSON response
type KrakenResponse struct {
Error []string `json:"error"`
Result interface{} `json:"result"`
}
// TimeResponse represents the server's time
type TimeResponse struct {
// Unix timestamp
Unixtime int64
// RFC 1123 time format
Rfc1123 string
}
// AssetPairsResponse includes asset pair informations
type AssetPairsResponse struct {
BCHEUR AssetPairInfo
BCHUSD AssetPairInfo
BCHXBT AssetPairInfo
DASHEUR AssetPairInfo
DASHUSD AssetPairInfo
DASHXBT AssetPairInfo
EOSETH AssetPairInfo
EOSEUR AssetPairInfo
EOSUSD AssetPairInfo
EOSXBT AssetPairInfo
GNOETH AssetPairInfo
GNOEUR AssetPairInfo
GNOUSD AssetPairInfo
GNOXBT AssetPairInfo
USDTZUSD AssetPairInfo
XETCXETH AssetPairInfo
XETCXUSD AssetPairInfo
XETCXXBT AssetPairInfo
XETCZEUR AssetPairInfo
XETHXXBT AssetPairInfo
XETHZCAD AssetPairInfo
XETHZEUR AssetPairInfo
XETHZGBP AssetPairInfo
XETHZJPY AssetPairInfo
XETHZUSD AssetPairInfo
XICNXETH AssetPairInfo
XICNXXBT AssetPairInfo
XLTCXXBT AssetPairInfo
XLTCZEUR AssetPairInfo
XLTCZUSD AssetPairInfo
XMLNXETH AssetPairInfo
XMLNXXBT AssetPairInfo
XREPXETH AssetPairInfo
XREPXXBT AssetPairInfo
XREPZEUR AssetPairInfo
XREPZUSD AssetPairInfo
XXBTZCAD AssetPairInfo
XXBTZEUR AssetPairInfo
XXBTZGBP AssetPairInfo
XXBTZJPY AssetPairInfo
XXBTZUSD AssetPairInfo
XXDGXXBT AssetPairInfo
XXLMXXBT AssetPairInfo
XXLMZEUR AssetPairInfo
XXLMZUSD AssetPairInfo
XXMRXXBT AssetPairInfo
XXMRZEUR AssetPairInfo
XXMRZUSD AssetPairInfo
XXRPXXBT AssetPairInfo
XXRPZCAD AssetPairInfo
XXRPZEUR AssetPairInfo
XXRPZJPY AssetPairInfo
XXRPZUSD AssetPairInfo
XZECXXBT AssetPairInfo
XZECZEUR AssetPairInfo
XZECZUSD AssetPairInfo
}
// AssetPairInfo represents asset pair information
type AssetPairInfo struct {
// Alternate pair name
Altname string `json:"altname"`
// Asset class of base component
AssetClassBase string `json:"aclass_base"`
// Asset id of base component
Base string `json:"base"`
// Asset class of quote component
AssetClassQuote string `json:"aclass_quote"`
// Asset id of quote component
Quote string `json:"quote"`
// Volume lot size
Lot string `json:"lot"`
// Scaling decimal places for pair
PairDecimals int `json:"pair_decimals"`
// Scaling decimal places for volume
LotDecimals int `json:"lot_decimals"`
// Amount to multiply lot volume by to get currency volume
LotMultiplier int `json:"lot_multiplier"`
// Array of leverage amounts available when buying
LeverageBuy []float32 `json:"leverage_buy"`
// Array of leverage amounts available when selling
LeverageSell []float32 `json:"leverage_sell"`
// Fee schedule array in [volume, percent fee] tuples
Fees [][]float64 `json:"fees"`
// // Maker fee schedule array in [volume, percent fee] tuples (if on maker/taker)
FeesMaker [][]float64 `json:"fees_maker"`
// // Volume discount currency
FeeVolumeCurrency string `json:"fee_volume_currency"`
// Margin call level
MarginCall int `json:"margin_call"`
// Stop-out/Liquidation margin level
MarginStop int `json:"margin_stop"`
}
// AssetsResponse includes asset informations
type AssetsResponse struct {
BCH AssetInfo
DASH AssetInfo
EOS AssetInfo
GNO AssetInfo
KFEE AssetInfo
USDT AssetInfo
XDAO AssetInfo
XETC AssetInfo
XETH AssetInfo
XICN AssetInfo
XLTC AssetInfo
XMLN AssetInfo
XNMC AssetInfo
XREP AssetInfo
XXBT AssetInfo
XXDG AssetInfo
XXLM AssetInfo
XXMR AssetInfo
XXRP AssetInfo
XXVN AssetInfo
XZEC AssetInfo
ZCAD AssetInfo
ZEUR AssetInfo
ZGBP AssetInfo
ZJPY AssetInfo
ZKRW AssetInfo
ZUSD AssetInfo
}
// AssetInfo represents an asset information
type AssetInfo struct {
// Alternate name
Altname string
// Asset class
AssetClass string `json:"aclass"`
// Scaling decimal places for record keeping
Decimals int
// Scaling decimal places for output display
DisplayDecimals int `json:"display_decimals"`
}
// BalanceResponse permit to build a Balance object directly from JSON
type BalanceResponse struct {
BCH float32 `json:"BCH,string"`
DASH float32 `json:"DASH,string"`
EOS float32 `json:"EOS,string"`
GNO float32 `json:"GNO,string"`
KFEE float32 `json:"KFEE,string"`
USDT float32 `json:"USDT,string"`
XDAO float32 `json:"XDAO,string"`
XETC float32 `json:"XETC,string"`
XETH float32 `json:"XETH,string"`
XICN float32 `json:"XICN,string"`
XLTC float32 `json:"XLTC,string"`
XMLN float32 `json:"XMLN,string"`
XNMC float32 `json:"XNMC,string"`
XREP float32 `json:"XREP,string"`
XXBT float32 `json:"XXBT,string"`
XXDG float32 `json:"XXDG,string"`
XXLM float32 `json:"XXLM,string"`
XXMR float32 `json:"XXMR,string"`
XXRP float32 `json:"XXRP,string"`
XXVN float32 `json:"XXVN,string"`
XZEC float32 `json:"XZEC,string"`
ZCAD float32 `json:"ZCAD,string"`
ZEUR float32 `json:"ZEUR,string"`
ZGBP float32 `json:"ZGBP,string"`
ZJPY float32 `json:"ZJPY,string"`
ZKRW float32 `json:"ZKRW,string"`
ZUSD float32 `json:"ZUSD,string"`
}
// TickerResponse includes the requested ticker pairs
type TickerResponse struct {
BCHEUR PairTickerInfo
BCHUSD PairTickerInfo
BCHXBT PairTickerInfo
DASHEUR PairTickerInfo
DASHUSD PairTickerInfo
DASHXBT PairTickerInfo
EOSETH PairTickerInfo
EOSEUR PairTickerInfo
EOSUSD PairTickerInfo
EOSXBT PairTickerInfo
GNOETH PairTickerInfo
GNOEUR PairTickerInfo
GNOUSD PairTickerInfo
GNOXBT PairTickerInfo
USDTZUSD PairTickerInfo
XETCXETH PairTickerInfo
XETCXUSD PairTickerInfo
XETCXXBT PairTickerInfo
XETCZEUR PairTickerInfo
XETHXXBT PairTickerInfo
XETHZCAD PairTickerInfo
XETHZEUR PairTickerInfo
XETHZGBP PairTickerInfo
XETHZJPY PairTickerInfo
XETHZUSD PairTickerInfo
XICNXETH PairTickerInfo
XICNXXBT PairTickerInfo
XLTCXXBT PairTickerInfo
XLTCZEUR PairTickerInfo
XLTCZUSD PairTickerInfo
XMLNXETH PairTickerInfo
XMLNXXBT PairTickerInfo
XREPXETH PairTickerInfo
XREPXXBT PairTickerInfo
XREPZEUR PairTickerInfo
XREPZUSD PairTickerInfo
XXBTZCAD PairTickerInfo
XXBTZEUR PairTickerInfo
XXBTZGBP PairTickerInfo
XXBTZJPY PairTickerInfo
XXBTZUSD PairTickerInfo
XXDGXXBT PairTickerInfo
XXLMXXBT PairTickerInfo
XXLMZEUR PairTickerInfo
XXLMZUSD PairTickerInfo
XXMRXXBT PairTickerInfo
XXMRZEUR PairTickerInfo
XXMRZUSD PairTickerInfo
XXRPXXBT PairTickerInfo
XXRPZCAD PairTickerInfo
XXRPZEUR PairTickerInfo
XXRPZJPY PairTickerInfo
XXRPZUSD PairTickerInfo
XZECXXBT PairTickerInfo
XZECZEUR PairTickerInfo
XZECZUSD PairTickerInfo
}
// PairTickerInfo represents ticker information for a pair
type PairTickerInfo struct {
// Ask array(<price>, <whole lot volume>, <lot volume>)
Ask []string `json:"a"`
// Bid array(<price>, <whole lot volume>, <lot volume>)
Bid []string `json:"b"`
// Last trade closed array(<price>, <lot volume>)
Close []string `json:"c"`
// Volume array(<today>, <last 24 hours>)
Volume []string `json:"v"`
// Volume weighted average price array(<today>, <last 24 hours>)
VolumeAveragePrice []string `json:"p"`
// Number of trades array(<today>, <last 24 hours>)
Trades []int `json:"t"`
// Low array(<today>, <last 24 hours>)
Low []string `json:"l"`
// High array(<today>, <last 24 hours>)
High []string `json:"h"`
// Today's opening price
OpeningPrice float32 `json:"o,string"`
}
// TradesResponse represents a list of the last trades
type TradesResponse struct {
Last int64
Trades []TradeInfo
}
// TradeInfo represents a trades information
type TradeInfo struct {
Price string
PriceFloat float64
Volume string
VolumeFloat float64
Time int64
Buy bool
Sell bool
Market bool
Limit bool
Miscellaneous string
}
// OrderTypes for AddOrder
const (
OTMarket = "market"
OTLimit = "limit" // (price = limit price)
OTStopLoss = "stop-loss" // (price = stop loss price)
OTTakeProfi = "take-profit" // (price = take profit price)
OTStopLossProfit = "stop-loss-profit" // (price = stop loss price, price2 = take profit price)
OTStopLossProfitLimit = "stop-loss-profit-limit" // (price = stop loss price, price2 = take profit price)
OTStopLossLimit = "stop-loss-limit" // (price = stop loss trigger price, price2 = triggered limit price)
OTTakeProfitLimit = "take-profit-limit" // (price = take profit trigger price, price2 = triggered limit price)
OTTrailingStop = "trailing-stop" // (price = trailing stop offset)
OTTrailingStopLimit = "trailing-stop-limit" // (price = trailing stop offset, price2 = triggered limit offset)
OTStopLossAndLimit = "stop-loss-and-limit" // (price = stop loss price, price2 = limit price)
OTSettlePosition = "settle-position"
)
// OrderDescription represents an orders description
type OrderDescription struct {
AssetPair string `json:"pair"`
Close string `json:"close"`
Leverage string `json:"leverage"`
Order string `json:"order"`
OrderType string `json:"ordertype"`
PrimaryPrice string `json:"price"`
SecondaryPrice string `json:"price2"`
Type string `json:"type"`
}
// Order represents a single order
type Order struct {
TransactionID string `json:"-"`
ReferenceID string `json:"refid"`
UserRef string `json:"userref"`
Status string `json:"status"`
OpenTime float64 `json:"opentm"`
StartTime float64 `json:"starttm"`
ExpireTime float64 `json:"expiretm"`
Description OrderDescription `json:"descr"`
Volume string `json:"vol1"`
VolumeExecuted float64 `json:"vol_exec,string"`
Cost float64 `json:"cost,string"`
Fee float64 `json:"fee,string"`
Price float64 `json:"price,string"`
StopPrice float64 `json:"stopprice.string"`
LimitPrice float64 `json:"limitprice,string"`
Misc string `json:"misc"`
OrderFlags string `json:"oflags"`
CloseTime float64 `json:"closetm"`
Reason string `json:"reason"`
}
// ClosedOrdersResponse represents a list of closed orders, indexed by id
type ClosedOrdersResponse struct {
Closed map[string]Order `json:"closed"`
Count int `json:"count"`
}
// OrderBookItem is a piece of information about an order.
type OrderBookItem struct {
Price float64
Amount float64
Ts int64
}
// UnmarshalJSON takes a json array from kraken and converts it into an OrderBookItem.
func (o *OrderBookItem) UnmarshalJSON(data []byte) error {
tmp_struct := struct {
price string
amount string
ts int64
}{}
tmp_arr := []interface{}{&tmp_struct.price, &tmp_struct.amount, &tmp_struct.ts}
err := json.Unmarshal(data, &tmp_arr)
if err != nil {
return err
}
o.Price, err = strconv.ParseFloat(tmp_struct.price, 64)
if err != nil {
return err
}
o.Amount, err = strconv.ParseFloat(tmp_struct.amount, 64)
if err != nil {
return err
}
o.Ts = tmp_struct.ts
return nil
}
// DepthResponse is a response from kraken to Depth request.
type DepthResponse map[string]OrderBook
// OrderBook contains top asks and bids.
type OrderBook struct {
Asks []OrderBookItem
Bids []OrderBookItem
}
type OpenOrdersResponse struct {
Open map[string]Order `json:"open"`
Count int `json:"count"`
}
type AddOrderResponse struct {
Description OrderDescription `json:"descr"`
TransactionIds []string `json:"txid"`
}
type CancelOrderResponse struct {
Count int `json:"count"`
Pending bool `json:"pending"`
}
type QueryOrdersResponse map[string]Order
func NewOHLC(input []interface{}) (*OHLC, error) {
if len(input) != 8 {
return nil, fmt.Errorf("the length is not 8 but %d", len(input))
}
tmp := new(OHLC)
tmp.Time = time.Unix(int64(input[0].(float64)), 0)
tmp.Open, _ = strconv.ParseFloat(input[1].(string), 64)
tmp.High, _ = strconv.ParseFloat(input[2].(string), 64)
tmp.Low, _ = strconv.ParseFloat(input[3].(string), 64)
tmp.Close, _ = strconv.ParseFloat(input[4].(string), 64)
tmp.Vwap, _ = strconv.ParseFloat(input[5].(string), 64)
tmp.Volume, _ = strconv.ParseFloat(input[6].(string), 64)
tmp.Count = int(input[7].(float64))
return tmp, nil
}
// OHLC represents the "Open-high-low-close chart"
type OHLC struct {
Time time.Time `json:"time"`
Open float64 `json:"open"`
High float64 `json:"high"`
Low float64 `json:"low"`
Close float64 `json:"close"`
Vwap float64 `json:"vwap"`
Volume float64 `json:"volume"`
Count int `json:"count"`
}
type OHLCResponse struct {
Pair string `json:"pair"`
OHLC []*OHLC `json:"OHLC"`
Last int64 `json:"last"`
}