forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitfinex.go
422 lines (352 loc) · 11.4 KB
/
bitfinex.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
package bitfinex
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
. "github.com/YuxSccc/goex"
)
type Bitfinex struct {
httpClient *http.Client
accessKey,
secretKey string
}
const (
baseURL string = "https://api.bitfinex.com"
apiURLV1 string = baseURL + "/v1"
apiURLV2 string = baseURL + "/v2"
)
func New(client *http.Client, accessKey, secretKey string) *Bitfinex {
return &Bitfinex{client, accessKey, secretKey}
}
func (bfx *Bitfinex) GetExchangeName() string {
return BITFINEX
}
func (bfx *Bitfinex) GetTicker(currencyPair CurrencyPair) (*Ticker, error) {
//pubticker
currencyPair = bfx.adaptCurrencyPair(currencyPair)
apiUrl := fmt.Sprintf("%s/pubticker/%s", apiURLV1, strings.ToLower(currencyPair.ToSymbol("")))
resp, err := HttpGet(bfx.httpClient, apiUrl)
if err != nil {
return nil, err
}
if resp["error"] != nil {
return nil, fmt.Errorf("%s", resp["error"])
}
//fmt.Println(resp)
ticker := new(Ticker)
ticker.Pair = currencyPair
ticker.Last = ToFloat64(resp["last_price"])
ticker.Vol = ToFloat64(resp["volume"])
ticker.High = ToFloat64(resp["high"])
ticker.Low = ToFloat64(resp["low"])
ticker.Sell = ToFloat64(resp["ask"])
ticker.Buy = ToFloat64(resp["bid"])
ticker.Date = uint64(bfx.adaptTimestamp(resp["timestamp"].(string)))
return ticker, nil
}
func (bfx *Bitfinex) GetDepth(size int, currencyPair CurrencyPair) (*Depth, error) {
apiUrl := fmt.Sprintf("%s/book/%s?limit_bids=%d&limit_asks=%d", apiURLV1, bfx.currencyPairToSymbol(currencyPair), size, size)
resp, err := HttpGet(bfx.httpClient, apiUrl)
if err != nil {
return nil, err
}
bids := resp["bids"].([]interface{})
asks := resp["asks"].([]interface{})
depth := new(Depth)
for _, bid := range bids {
_bid := bid.(map[string]interface{})
amount := ToFloat64(_bid["amount"])
price := ToFloat64(_bid["price"])
dr := DepthRecord{Amount: amount, Price: price}
depth.BidList = append(depth.BidList, dr)
}
for _, ask := range asks {
_ask := ask.(map[string]interface{})
amount := ToFloat64(_ask["amount"])
price := ToFloat64(_ask["price"])
dr := DepthRecord{Amount: amount, Price: price}
depth.AskList = append(depth.AskList, dr)
}
return depth, nil
}
func (bfx *Bitfinex) GetKlineRecords(currencyPair CurrencyPair, period KlinePeriod, size int, optional ...OptionalParameter) ([]Kline, error) {
symbol := convertPairToBitfinexSymbol("t", currencyPair)
if size == 0 {
size = 100
}
periodStr, ok := klinePeriods[period]
if !ok {
return nil, fmt.Errorf("invalid period")
}
apiURL := fmt.Sprintf("%s/candles/trade:%s:%s/hist?limit=%d", apiURLV2, periodStr, symbol, size)
respRaw, err := NewHttpRequest(bfx.httpClient, "GET", apiURL, "", nil)
if err != nil {
return nil, err
}
var candles []Kline
var resp [][]interface{}
if err := json.Unmarshal(respRaw, &resp); err != nil {
return nil, fmt.Errorf("Failed to unmarshal kline response: %v", err)
}
for _, r := range resp {
k := klineFromRaw(currencyPair, r)
candles = append(candles, *k)
}
return candles, nil
}
//非个人,整个交易所的交易记录
func (bfx *Bitfinex) GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, error) {
panic("not implement")
}
func (bfx *Bitfinex) GetWalletBalances() (map[string]*Account, error) {
var respmap []interface{}
err := bfx.doAuthenticatedRequest("GET", "balances", map[string]interface{}{}, &respmap)
if err != nil {
return nil, err
}
walletmap := make(map[string]*Account, 1)
for _, v := range respmap {
subacc := v.(map[string]interface{})
typeStr := subacc["type"].(string)
currency := NewCurrency(subacc["currency"].(string), "")
if currency == UNKNOWN {
continue
}
//typeS := subacc["type"].(string)
amount := ToFloat64(subacc["amount"])
available := ToFloat64(subacc["available"])
account := walletmap[typeStr]
if account == nil {
account = new(Account)
account.SubAccounts = make(map[Currency]SubAccount, 6)
}
account.NetAsset = amount
account.Asset = amount
account.SubAccounts[currency] = SubAccount{
Currency: currency,
Amount: available,
ForzenAmount: amount - available,
LoanAmount: 0}
walletmap[typeStr] = account
}
return walletmap, nil
}
/*defalut only return exchange wallet balance*/
func (bfx *Bitfinex) GetAccount() (*Account, error) {
wallets, err := bfx.GetWalletBalances()
if err != nil {
return nil, err
}
return wallets["exchange"], nil
}
func (bfx *Bitfinex) placeOrder(orderType, side, amount, price string, pair CurrencyPair) (*Order, error) {
path := "order/new"
params := map[string]interface{}{
"symbol": bfx.currencyPairToSymbol(pair),
"amount": amount,
"price": price,
"side": side,
"type": orderType,
"exchange": "bitfinex"}
var respmap map[string]interface{}
err := bfx.doAuthenticatedRequest("POST", path, params, &respmap)
if err != nil {
return nil, err
}
order := new(Order)
order.Currency = pair
order.OrderID = ToInt(respmap["id"])
order.OrderID2 = fmt.Sprint(ToInt(respmap["id"]))
order.Amount = ToFloat64(amount)
order.Price = ToFloat64(price)
order.AvgPrice = ToFloat64(respmap["avg_execution_price"])
order.DealAmount = ToFloat64(respmap["executed_amount"])
order.Status = ORDER_UNFINISH
switch side {
case "buy":
order.Side = BUY
if strings.Contains(orderType, "market") {
order.Side = BUY_MARKET
}
case "sell":
order.Side = SELL
if strings.Contains(orderType, "market") {
order.Side = SELL_MARKET
}
}
return order, nil
}
func (bfx *Bitfinex) LimitBuy(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return bfx.placeOrder("exchange limit", "buy", amount, price, currencyPair)
}
func (bfx *Bitfinex) LimitSell(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return bfx.placeOrder("exchange limit", "sell", amount, price, currencyPair)
}
func (bfx *Bitfinex) MarketBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
return bfx.placeOrder("exchange market", "buy", amount, price, currencyPair)
}
func (bfx *Bitfinex) MarketSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
return bfx.placeOrder("exchange market", "sell", amount, price, currencyPair)
}
func (bfx *Bitfinex) StopBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
return bfx.placeOrder("exchange stop", "buy", amount, price, currencyPair)
}
func (bfx *Bitfinex) StopSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
return bfx.placeOrder("exchange stop", "sell", amount, price, currencyPair)
}
func (bfx *Bitfinex) CancelOrder(orderId string, currencyPair CurrencyPair) (bool, error) {
var respmap map[string]interface{}
path := "order/cancel"
err := bfx.doAuthenticatedRequest("POST", path, map[string]interface{}{"order_id": ToInt(orderId)}, &respmap)
if err != nil {
return false, err
}
return respmap["is_cancelled"].(bool), nil
}
func (bfx *Bitfinex) toOrder(respmap map[string]interface{}) *Order {
order := new(Order)
order.Currency = symbolToCurrencyPair(respmap["symbol"].(string))
order.OrderID = ToInt(respmap["id"])
order.OrderID2 = fmt.Sprint(ToInt(respmap["id"]))
order.Amount = ToFloat64(respmap["original_amount"])
order.Price = ToFloat64(respmap["price"])
order.DealAmount = ToFloat64(respmap["executed_amount"])
order.AvgPrice = ToFloat64(respmap["avg_execution_price"])
order.OrderTime = bfx.adaptTimestamp(respmap["timestamp"].(string))
if order.DealAmount == order.Amount {
order.Status = ORDER_FINISH
} else if order.DealAmount > 0 {
order.Status = ORDER_PART_FINISH
}
side := respmap["side"].(string)
if side == "sell" {
order.Side = SELL
} else if side == "buy" {
order.Side = BUY
}
if respmap["is_cancelled"].(bool) {
order.Status = ORDER_CANCEL
}
return order
}
func (bfx *Bitfinex) GetOneOrder(orderId string, currencyPair CurrencyPair) (*Order, error) {
var respmap map[string]interface{}
path := "order/status"
err := bfx.doAuthenticatedRequest("POST", path, map[string]interface{}{"order_id": ToInt(orderId)}, &respmap)
if err != nil {
return nil, err
}
return bfx.toOrder(respmap), nil
}
func (bfx *Bitfinex) GetUnfinishOrders(currencyPair CurrencyPair) ([]Order, error) {
var ordersmap []interface{}
err := bfx.doAuthenticatedRequest("POST", "orders", map[string]interface{}{}, &ordersmap)
if err != nil {
return nil, err
}
var orders []Order
for _, v := range ordersmap {
ordermap := v.(map[string]interface{})
orders = append(orders, *bfx.toOrder(ordermap))
}
return orders, nil
}
func (bfx *Bitfinex) GetOrderHistorys(currencyPair CurrencyPair, optional ...OptionalParameter) ([]Order, error) {
panic("not implement")
}
func (bfx *Bitfinex) doAuthenticatedRequest(method, path string, payload map[string]interface{}, ret interface{}) error {
nonce := time.Now().UnixNano()
payload["request"] = "/v1/" + path
payload["nonce"] = fmt.Sprintf("%d.2", nonce)
p, err := json.Marshal(payload)
if err != nil {
return err
}
encoded := base64.StdEncoding.EncodeToString(p)
sign, _ := GetParamHmacSha384Sign(bfx.secretKey, encoded)
resp, err := NewHttpRequest(bfx.httpClient, method, apiURLV1+"/"+path, "", map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
"X-BFX-APIKEY": bfx.accessKey,
"X-BFX-PAYLOAD": encoded,
"X-BFX-SIGNATURE": sign})
if err != nil {
return err
}
//print(string(resp))
err = json.Unmarshal(resp, ret)
return err
}
func (bfx *Bitfinex) currencyPairToSymbol(currencyPair CurrencyPair) string {
return strings.ToUpper(currencyPair.ToSymbol(""))
}
func (bfx *Bitfinex) adaptTimestamp(timestamp string) int {
times := strings.Split(timestamp, ".")
intTime, _ := strconv.Atoi(times[0])
return intTime
}
func (bfx *Bitfinex) adaptCurrencyPair(pair CurrencyPair) CurrencyPair {
var currencyA Currency
var currencyB Currency
DASH := NewCurrency("DASH", "")
DSH := NewCurrency("DSH", "")
QTM := NewCurrency("QTM", "")
IOTA := NewCurrency("IOTA", "")
IOT := NewCurrency("IOT", "")
if pair.CurrencyA == DASH {
currencyA = DSH
} else if pair.CurrencyA == QTUM {
currencyA = QTM
} else if pair.CurrencyA == IOTA {
currencyA = IOT
} else {
currencyA = pair.CurrencyA
}
if pair.CurrencyB == USDT {
currencyB = USD
} else {
currencyB = pair.CurrencyB
}
return NewCurrencyPair(currencyA, currencyB)
}
func symbolToCurrencyPair(symbol string) CurrencyPair {
currencyA := strings.ToUpper(symbol[0:3])
currencyB := strings.ToUpper(symbol[3:])
return NewCurrencyPair(NewCurrency(currencyA, ""), NewCurrency(currencyB, ""))
}
var klinePeriods = map[KlinePeriod]string{
KLINE_PERIOD_1MIN: "1m",
KLINE_PERIOD_5MIN: "5m",
KLINE_PERIOD_15MIN: "15m",
KLINE_PERIOD_30MIN: "30m",
KLINE_PERIOD_60MIN: "1h",
KLINE_PERIOD_3H: "3h",
KLINE_PERIOD_6H: "6h",
KLINE_PERIOD_12H: "12h",
KLINE_PERIOD_1DAY: "1D",
KLINE_PERIOD_1WEEK: "7D",
KLINE_PERIOD_1MONTH: "1M",
}
func klineFromRaw(pair CurrencyPair, raw []interface{}) *Kline {
return &Kline{
Pair: pair,
Timestamp: ToInt64(raw[0]),
Open: ToFloat64(raw[1]),
Close: ToFloat64(raw[2]),
High: ToFloat64(raw[3]),
Low: ToFloat64(raw[4]),
Vol: ToFloat64(raw[5]),
}
}
func convertPairToBitfinexSymbol(prefix string, pair CurrencyPair) string {
symbol := pair.ToSymbol("")
return prefix + symbol
}
func convertKeyToPair(key string) CurrencyPair {
split := strings.Split(key, ":")
return symbolToCurrencyPair(split[2][1:])
}