forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Kraken.go
373 lines (305 loc) · 9.23 KB
/
Kraken.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
package kraken
import (
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
. "github.com/GitTsewell/GoEx"
"net/http"
"net/url"
"sort"
"strings"
"time"
)
type BaseResponse struct {
Error []string `json:"error"`
Result interface{} `json:"result"`
}
type NewOrderResponse struct {
Description interface{} `json:"descr"`
TxIds []string `json:"txid"`
}
type Kraken struct {
httpClient *http.Client
accessKey,
secretKey string
}
var (
BASE_URL = "https://api.kraken.com"
API_V0 = "/0/"
API_DOMAIN = BASE_URL + API_V0
PUBLIC = "public/"
PRIVATE = "private/"
)
func New(client *http.Client, accesskey, secretkey string) *Kraken {
return &Kraken{client, accesskey, secretkey}
}
func (k *Kraken) placeOrder(orderType, side, amount, price string, pair CurrencyPair) (*Order, error) {
apiuri := "private/AddOrder"
params := url.Values{}
params.Set("pair", k.convertPair(pair).ToSymbol(""))
params.Set("type", side)
params.Set("ordertype", orderType)
params.Set("price", price)
params.Set("volume", amount)
var resp NewOrderResponse
err := k.doAuthenticatedRequest("POST", apiuri, params, &resp)
//log.Println
if err != nil {
return nil, err
}
var tradeSide TradeSide = SELL
if "buy" == side {
tradeSide = BUY
}
return &Order{
Currency: pair,
OrderID2: resp.TxIds[0],
Amount: ToFloat64(amount),
Price: ToFloat64(price),
Side: tradeSide,
Status: ORDER_UNFINISH}, nil
}
func (k *Kraken) LimitBuy(amount, price string, currency CurrencyPair) (*Order, error) {
return k.placeOrder("limit", "buy", amount, price, currency)
}
func (k *Kraken) LimitSell(amount, price string, currency CurrencyPair) (*Order, error) {
return k.placeOrder("limit", "sell", amount, price, currency)
}
func (k *Kraken) MarketBuy(amount, price string, currency CurrencyPair) (*Order, error) {
return k.placeOrder("market", "buy", amount, price, currency)
}
func (k *Kraken) MarketSell(amount, price string, currency CurrencyPair) (*Order, error) {
return k.placeOrder("market", "sell", amount, price, currency)
}
func (k *Kraken) CancelOrder(orderId string, currency CurrencyPair) (bool, error) {
params := url.Values{}
apiuri := "private/CancelOrder"
params.Set("txid", orderId)
var respmap map[string]interface{}
err := k.doAuthenticatedRequest("POST", apiuri, params, &respmap)
if err != nil {
return false, err
}
//log.Println(respmap)
return true, nil
}
func (k *Kraken) toOrder(orderinfo interface{}) Order {
omap := orderinfo.(map[string]interface{})
descmap := omap["descr"].(map[string]interface{})
return Order{
Amount: ToFloat64(omap["vol"]),
Price: ToFloat64(descmap["price"]),
DealAmount: ToFloat64(omap["vol_exec"]),
AvgPrice: ToFloat64(omap["price"]),
Side: AdaptTradeSide(descmap["type"].(string)),
Status: k.convertOrderStatus(omap["status"].(string)),
Fee: ToFloat64(omap["fee"]),
OrderTime: ToInt(omap["opentm"]),
}
}
func (k *Kraken) GetOrderInfos(txids ...string) ([]Order, error) {
params := url.Values{}
params.Set("txid", strings.Join(txids, ","))
var resultmap map[string]interface{}
err := k.doAuthenticatedRequest("POST", "private/QueryOrders", params, &resultmap)
if err != nil {
return nil, err
}
//log.Println(resultmap)
var ords []Order
for txid, v := range resultmap {
ord := k.toOrder(v)
ord.OrderID2 = txid
ords = append(ords, ord)
}
return ords, nil
}
func (k *Kraken) GetOneOrder(orderId string, currency CurrencyPair) (*Order, error) {
orders, err := k.GetOrderInfos(orderId)
if err != nil {
return nil, err
}
if len(orders) == 0 {
return nil, errors.New("not fund the order " + orderId)
}
ord := &orders[0]
ord.Currency = currency
return ord, nil
}
func (k *Kraken) GetUnfinishOrders(currency CurrencyPair) ([]Order, error) {
var result struct {
Open map[string]interface{} `json:"open"`
}
err := k.doAuthenticatedRequest("POST", "private/OpenOrders", url.Values{}, &result)
if err != nil {
return nil, err
}
var orders []Order
for txid, v := range result.Open {
ord := k.toOrder(v)
ord.OrderID2 = txid
ord.Currency = currency
orders = append(orders, ord)
}
return orders, nil
}
func (k *Kraken) GetOrderHistorys(currency CurrencyPair, currentPage, pageSize int) ([]Order, error) {
panic("")
}
func (k *Kraken) GetAccount() (*Account, error) {
params := url.Values{}
apiuri := "private/Balance"
var resustmap map[string]interface{}
err := k.doAuthenticatedRequest("POST", apiuri, params, &resustmap)
if err != nil {
return nil, err
}
acc := new(Account)
acc.Exchange = k.GetExchangeName()
acc.SubAccounts = make(map[Currency]SubAccount)
for key, v := range resustmap {
currency := k.convertCurrency(key)
amount := ToFloat64(v)
//log.Println(symbol, amount)
acc.SubAccounts[currency] = SubAccount{Currency: currency, Amount: amount, ForzenAmount: 0, LoanAmount: 0}
if currency.Symbol == "XBT" { // adapt to btc
acc.SubAccounts[BTC] = SubAccount{Currency: BTC, Amount: amount, ForzenAmount: 0, LoanAmount: 0}
}
}
return acc, nil
}
//func (k *Kraken) GetTradeBalance() {
// var resultmap map[string]interface{}
// k.doAuthenticatedRequest("POST", "private/TradeBalance", url.Values{}, &resultmap)
// log.Println(resultmap)
//}
func (k *Kraken) GetTicker(currency CurrencyPair) (*Ticker, error) {
var resultmap map[string]interface{}
err := k.doAuthenticatedRequest("GET", "public/Ticker?pair="+k.convertPair(currency).ToSymbol(""), url.Values{}, &resultmap)
if err != nil {
return nil, err
}
ticker := new(Ticker)
ticker.Pair = currency
for _, t := range resultmap {
tickermap := t.(map[string]interface{})
ticker.Last = ToFloat64(tickermap["c"].([]interface{})[0])
ticker.Buy = ToFloat64(tickermap["b"].([]interface{})[0])
ticker.Sell = ToFloat64(tickermap["a"].([]interface{})[0])
ticker.Low = ToFloat64(tickermap["l"].([]interface{})[0])
ticker.High = ToFloat64(tickermap["h"].([]interface{})[0])
ticker.Vol = ToFloat64(tickermap["v"].([]interface{})[0])
}
return ticker, nil
}
func (k *Kraken) GetDepth(size int, currency CurrencyPair) (*Depth, error) {
apiuri := fmt.Sprintf("public/Depth?pair=%s&count=%d", k.convertPair(currency).ToSymbol(""), size)
var resultmap map[string]interface{}
err := k.doAuthenticatedRequest("GET", apiuri, url.Values{}, &resultmap)
if err != nil {
return nil, err
}
//log.Println(respmap)
dep := Depth{}
dep.Pair = currency
for _, d := range resultmap {
depmap := d.(map[string]interface{})
asksmap := depmap["asks"].([]interface{})
bidsmap := depmap["bids"].([]interface{})
for _, v := range asksmap {
ask := v.([]interface{})
dep.AskList = append(dep.AskList, DepthRecord{ToFloat64(ask[0]), ToFloat64(ask[1])})
}
for _, v := range bidsmap {
bid := v.([]interface{})
dep.BidList = append(dep.BidList, DepthRecord{ToFloat64(bid[0]), ToFloat64(bid[1])})
}
break
}
sort.Sort(sort.Reverse(dep.AskList)) //reverse
return &dep, nil
}
func (k *Kraken) GetKlineRecords(currency CurrencyPair, period, size, since int) ([]Kline, error) {
panic("")
}
//非个人,整个交易所的交易记录
func (k *Kraken) GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, error) {
panic("")
}
func (k *Kraken) GetExchangeName() string {
return KRAKEN
}
func (k *Kraken) buildParamsSigned(apiuri string, postForm *url.Values) string {
postForm.Set("nonce", fmt.Sprintf("%d", time.Now().UnixNano()))
urlPath := API_V0 + apiuri
secretByte, _ := base64.StdEncoding.DecodeString(k.secretKey)
encode := []byte(postForm.Get("nonce") + postForm.Encode())
sha := sha256.New()
sha.Write(encode)
shaSum := sha.Sum(nil)
pathSha := append([]byte(urlPath), shaSum...)
mac := hmac.New(sha512.New, secretByte)
mac.Write(pathSha)
macSum := mac.Sum(nil)
sign := base64.StdEncoding.EncodeToString(macSum)
return sign
}
func (k *Kraken) doAuthenticatedRequest(method, apiuri string, params url.Values, ret interface{}) error {
headers := map[string]string{}
if "POST" == method {
signature := k.buildParamsSigned(apiuri, ¶ms)
headers = map[string]string{
"API-Key": k.accessKey,
"API-Sign": signature,
}
}
resp, err := NewHttpRequest(k.httpClient, method, API_DOMAIN+apiuri, params.Encode(), headers)
if err != nil {
return err
}
//println(string(resp))
var base BaseResponse
base.Result = ret
err = json.Unmarshal(resp, &base)
if err != nil {
return err
}
//println(string(resp))
if len(base.Error) > 0 {
return errors.New(base.Error[0])
}
return nil
}
func (k *Kraken) convertCurrency(currencySymbol string) Currency {
if len(currencySymbol) >= 4 {
currencySymbol = strings.Replace(currencySymbol, "X", "", 1)
currencySymbol = strings.Replace(currencySymbol, "Z", "", 1)
}
return NewCurrency(currencySymbol, "")
}
func (k *Kraken) convertPair(pair CurrencyPair) CurrencyPair {
if "BTC" == pair.CurrencyA.Symbol {
return NewCurrencyPair(XBT, pair.CurrencyB)
}
if "BTC" == pair.CurrencyB.Symbol {
return NewCurrencyPair(pair.CurrencyA, XBT)
}
return pair
}
func (k *Kraken) convertOrderStatus(status string) TradeStatus {
switch status {
case "open", "pending":
return ORDER_UNFINISH
case "canceled", "expired":
return ORDER_CANCEL
case "filled", "closed":
return ORDER_FINISH
case "partialfilled":
return ORDER_PART_FINISH
}
return ORDER_UNFINISH
}