/
bybit.go
660 lines (597 loc) · 18.7 KB
/
bybit.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package bybit
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
"reflect"
"sort"
"strconv"
"time"
"github.com/TTRSQ/bbwrapper/domains/base"
"github.com/TTRSQ/bbwrapper/domains/board"
"github.com/TTRSQ/bbwrapper/domains/order"
"github.com/TTRSQ/bbwrapper/domains/order/id"
"github.com/TTRSQ/bbwrapper/domains/stock"
"github.com/TTRSQ/bbwrapper/interface/exchange"
)
type bybit struct {
name string
host string
key exchange.Key
httpClient *http.Client
}
// New return exchange obj.
func New(key exchange.Key) (exchange.Exchange, error) {
bb := bybit{}
bb.name = "bybit"
bb.host = "api.bybit.com"
if key.APIKey == "" || key.APISecKey == "" {
return nil, errors.New("APIKey and APISecKey Required")
}
bb.key = key
bb.httpClient = new(http.Client)
if key.SpecificParam["timeoutMS"] != nil {
bb.httpClient.Timeout = time.Duration(key.SpecificParam["timeoutMS"].(int)) * time.Millisecond
}
return &bb, nil
}
func (bb *bybit) ExchangeName() string {
return bb.name
}
func (bb *bybit) OrderTypes() exchange.OrderTypes {
return exchange.OrderTypes{
Limit: "Limit",
Market: "Market",
}
}
func (bb *bybit) CreateOrder(price, size float64, isBuy bool, symbol, orderType string) (*order.Responce, error) {
type Req struct {
Side string `json:"side"`
Symbol string `json:"symbol"`
OrderType string `json:"order_type"`
Qty float64 `json:"qty"`
Price float64 `json:"price"`
TimeInForce string `json:"time_in_force"`
}
res, err := bb.postRequest("/v2/private/order/create", structToMap(&Req{
Symbol: symbol,
OrderType: orderType,
Side: map[bool]string{true: "Buy", false: "Sell"}[isBuy],
Price: map[bool]float64{true: price, false: 0}[orderType == bb.OrderTypes().Limit],
Qty: size,
TimeInForce: "GoodTillCancel",
}))
if err != nil {
return nil, err
}
// レスポンスの変換
type Res struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
Result struct {
UserID int `json:"user_id"`
OrderID string `json:"order_id"`
Symbol string `json:"symbol"`
Side string `json:"side"`
OrderType string `json:"order_type"`
Price int `json:"price"`
Qty int `json:"qty"`
TimeInForce string `json:"time_in_force"`
OrderStatus string `json:"order_status"`
LastExecTime int `json:"last_exec_time"`
LastExecPrice int `json:"last_exec_price"`
LeavesQty int `json:"leaves_qty"`
CumExecQty int `json:"cum_exec_qty"`
CumExecValue int `json:"cum_exec_value"`
CumExecFee int `json:"cum_exec_fee"`
RejectReason string `json:"reject_reason"`
OrderLinkID string `json:"order_link_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
} `json:"result"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int64 `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
}
resData := Res{}
json.Unmarshal(res, &resData)
return &order.Responce{
ID: id.NewID(bb.name, symbol, fmt.Sprint(resData.Result.OrderID)),
FilledSize: size - float64(resData.Result.LeavesQty),
}, nil
}
func (bb *bybit) LiquidationOrder(price, size float64, isBuy bool, symbol, orderType string) (*order.Responce, error) {
return nil, errors.New("LiquidationOrder not supported.")
}
func (bb *bybit) OpenInterest(symbol string, minute, limit int) ([]base.OpenInterest, error) {
// リクエスト
type Req struct {
Symbol string `json:"symbol"`
Period string `json:"period"`
Limit string `json:"limit"`
}
res, err := bb.getRequest("/v2/public/open-interest", structToMap(&Req{
Symbol: symbol,
Period: fmt.Sprint(minute) + "min",
Limit: fmt.Sprint(limit),
}))
if err != nil {
return []base.OpenInterest{}, err
}
// レスポンスの変換
type Res struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
Result []struct {
OpenInterest int `json:"open_interest"`
Timestamp int `json:"timestamp"`
Symbol string `json:"symbol"`
} `json:"result"`
TimeNow string `json:"time_now"`
}
resData := Res{}
json.Unmarshal(res, &resData)
if resData.RetMsg != "OK" {
return []base.OpenInterest{}, errors.New(resData.RetMsg + ":" + resData.ExtCode)
}
ret := []base.OpenInterest{}
for i := range resData.Result {
item := resData.Result[i]
ret = append(ret, base.OpenInterest{
OpenInterest: item.OpenInterest,
Timestamp: item.Timestamp,
})
}
return ret, nil
}
func (bb *bybit) EditOrder(symbol, localID string, price, size float64) (*order.Order, error) {
// リクエスト
type Req struct {
OrderID string `json:"order_id"`
Symbol string `json:"symbol"`
Qty string `json:"p_r_qty"`
Price string `json:"p_r_price"`
}
res, err := bb.postRequest("/v2/private/order/replace", structToMap(&Req{
OrderID: localID,
Symbol: symbol,
Qty: fmt.Sprint(size),
Price: fmt.Sprint(price),
}))
if err != nil {
return nil, err
}
// レスポンスの変換
type Res struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
Result struct {
OrderID string `json:"order_id"`
} `json:"result"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int64 `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
}
resData := Res{}
json.Unmarshal(res, &resData)
if resData.RetMsg != "OK" {
return nil, errors.New(resData.RetMsg + ":" + resData.ExtCode)
}
t, _ := strconv.ParseFloat(resData.TimeNow, 64)
return &order.Order{
ID: id.NewID(bb.name, symbol, resData.Result.OrderID),
Request: order.Request{},
UpdatedAtUnix: int(t),
}, nil
}
func (bb *bybit) CancelOrder(symbol, localID string) error {
type Req struct {
Symbol string `json:"symbol"`
OrderID string `json:"order_id"`
}
type Res struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
Result struct {
UserID int `json:"user_id"`
OrderID string `json:"order_id"`
Symbol string `json:"symbol"`
Side string `json:"side"`
OrderType string `json:"order_type"`
Price int `json:"price"`
Qty int `json:"qty"`
TimeInForce string `json:"time_in_force"`
OrderStatus string `json:"order_status"`
LastExecTime int `json:"last_exec_time"`
LastExecPrice int `json:"last_exec_price"`
LeavesQty int `json:"leaves_qty"`
CumExecQty int `json:"cum_exec_qty"`
CumExecValue int `json:"cum_exec_value"`
CumExecFee int `json:"cum_exec_fee"`
RejectReason string `json:"reject_reason"`
OrderLinkID string `json:"order_link_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
} `json:"result"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int64 `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
}
res, err := bb.postRequest("/v2/private/order/cancel", structToMap(&Req{
Symbol: symbol,
OrderID: localID,
}))
if err != nil {
return err
}
resData := Res{}
json.Unmarshal(res, &resData)
return nil
}
func (bb *bybit) CancelAllOrder(symbol string) error {
type Req struct {
Symbol string `json:"symbol"`
}
_, err := bb.postRequest("/v2/private/order/cancelAll", structToMap(&Req{
Symbol: symbol,
}))
return err
}
func (bb *bybit) ActiveOrders(symbol string) ([]order.Order, error) {
type Req struct {
Symbol string `json:"symbol"`
OrderStatus string `json:"order_status"`
}
res, err := bb.getRequest("/v2/private/order/list", structToMap(&Req{
Symbol: symbol,
OrderStatus: "Created,New,PartiallyFilled", // 今後の取引に関わるもののみ
}))
if err != nil {
return []order.Order{}, err
}
type Res struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
Result struct {
Data []struct {
UserID int `json:"user_id"`
OrderStatus string `json:"order_status"`
Symbol string `json:"symbol"`
Side string `json:"side"`
OrderType string `json:"order_type"`
Price string `json:"price"`
Qty string `json:"qty"`
TimeInForce string `json:"time_in_force"`
OrderLinkID string `json:"order_link_id"`
OrderID string `json:"order_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LeavesQty string `json:"leaves_qty"`
LeavesValue string `json:"leaves_value"`
CumExecQty string `json:"cum_exec_qty"`
CumExecValue string `json:"cum_exec_value"`
CumExecFee string `json:"cum_exec_fee"`
RejectReason string `json:"reject_reason"`
} `json:"data"`
Cursor string `json:"cursor"`
} `json:"result"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int64 `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
}
resData := Res{}
json.Unmarshal(res, &resData)
orders := []order.Order{}
for _, v := range resData.Result.Data {
price, _ := strconv.ParseFloat(v.Price, 64)
size, _ := strconv.ParseFloat(v.Side, 64)
orders = append(orders, order.Order{
ID: id.NewID(bb.name, symbol, v.OrderID),
Request: order.Request{
Norm: base.Norm{
Price: price,
Size: size,
},
Symbol: symbol,
IsBuy: v.Side == "Buy",
OrderType: v.OrderType,
},
UpdatedAtUnix: int(v.UpdatedAt.Unix()),
})
}
return orders, nil
}
func (bb *bybit) Stocks(symbol string) (stock.Stock, error) {
type Req struct {
Symbol string `json:"symbol"`
}
res, err := bb.getRequest("/v2/private/position/list", structToMap(&Req{
Symbol: symbol,
}))
if err != nil {
return stock.Stock{}, err
}
// レスポンスの変換
type Res struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
Result struct {
ID int `json:"id"`
UserID int `json:"user_id"`
RiskID int `json:"risk_id"`
Symbol string `json:"symbol"`
Side string `json:"side"`
Size float64 `json:"size"`
PositionValue string `json:"position_value"`
EntryPrice string `json:"entry_price"`
IsIsolated bool `json:"is_isolated"`
AutoAddMargin int `json:"auto_add_margin"`
Leverage string `json:"leverage"`
EffectiveLeverage string `json:"effective_leverage"`
PositionMargin string `json:"position_margin"`
LiqPrice string `json:"liq_price"`
BustPrice string `json:"bust_price"`
OccClosingFee string `json:"occ_closing_fee"`
OccFundingFee string `json:"occ_funding_fee"`
TakeProfit string `json:"take_profit"`
StopLoss string `json:"stop_loss"`
TrailingStop string `json:"trailing_stop"`
PositionStatus string `json:"position_status"`
DeleverageIndicator int `json:"deleverage_indicator"`
OcCalcData string `json:"oc_calc_data"`
OrderMargin string `json:"order_margin"`
WalletBalance string `json:"wallet_balance"`
RealisedPnl string `json:"realised_pnl"`
UnrealisedPnl int `json:"unrealised_pnl"`
CumRealisedPnl string `json:"cum_realised_pnl"`
CrossSeq int `json:"cross_seq"`
PositionSeq int `json:"position_seq"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
} `json:"result"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int64 `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
}
resData := Res{}
json.Unmarshal(res, &resData)
size := resData.Result.Size
sizeAbs := math.Abs(size)
if resData.Result.Side == "Sell" {
size *= -1
}
stock := stock.Stock{Symbol: symbol, Summary: size}
if size > 0 {
stock.LongSize = sizeAbs
} else {
stock.ShortSize = sizeAbs
}
return stock, nil
}
func (bb *bybit) Balance() ([]base.Balance, error) {
res, err := bb.getRequest("/v2/private/wallet/balance", map[string]string{})
if err != nil {
return []base.Balance{}, err
}
type Res struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
Result map[string]struct {
Equity int `json:"equity"`
AvailableBalance float64 `json:"available_balance"`
UsedMargin float64 `json:"used_margin"`
OrderMargin float64 `json:"order_margin"`
PositionMargin int `json:"position_margin"`
OccClosingFee int `json:"occ_closing_fee"`
OccFundingFee int `json:"occ_funding_fee"`
WalletBalance int `json:"wallet_balance"`
RealisedPnl int `json:"realised_pnl"`
UnrealisedPnl int `json:"unrealised_pnl"`
CumRealisedPnl int `json:"cum_realised_pnl"`
GivenCash int `json:"given_cash"`
ServiceCash int `json:"service_cash"`
} `json:"result"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int64 `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
}
resData := Res{}
json.Unmarshal(res, &resData)
balances := []base.Balance{}
for k, v := range resData.Result {
balances = append(balances, base.Balance{
CurrencyCode: k,
Size: v.AvailableBalance,
})
}
return balances, nil
}
func (bb *bybit) Boards(symbol string) (board.Board, error) {
type Req struct {
Symbol string `json:"symbol"`
}
res, err := bb.getRequest("/v2/public/orderBook/L2", structToMap(&Req{
Symbol: symbol,
}))
if err != nil {
return board.Board{}, err
}
// レスポンスの変換
type Res struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
Result []struct {
Symbol string `json:"symbol"`
Price string `json:"price"`
Size int `json:"size"`
Side string `json:"side"`
} `json:"result"`
TimeNow string `json:"time_now"`
}
resData := Res{}
json.Unmarshal(res, &resData)
asks := []base.Norm{}
bids := []base.Norm{}
for _, v := range resData.Result {
price, _ := strconv.ParseFloat(v.Price, 64)
if v.Side == "Buy" {
bids = append(bids, base.Norm{
Price: price,
Size: float64(v.Size),
})
} else {
asks = append(asks, base.Norm{
Price: price,
Size: float64(v.Size),
})
}
}
sort.Slice(asks, func(i, j int) bool {
return asks[i].Price < asks[j].Price
})
sort.Slice(bids, func(i, j int) bool {
return bids[i].Price > bids[j].Price
})
midPrice := (bids[0].Price + asks[0].Price) / 2
return board.Board{
ExchangeName: bb.name,
Symbol: symbol,
MidPrice: midPrice,
Asks: asks,
Bids: bids,
}, nil
}
func (bb *bybit) InScheduledMaintenance() bool {
// TODO
return false
}
func (bb *bybit) postRequest(path string, param map[string]string) ([]byte, error) {
param["api_key"] = bb.key.APIKey
param["timestamp"] = fmt.Sprint(time.Now().UnixNano() / 1000000)
sign := getSignature(param, bb.key.APISecKey)
param["sign"] = sign
url := url.URL{Scheme: "https", Host: bb.host, Path: path}
jsonParam, _ := json.Marshal(param)
req, _ := http.NewRequest(
"POST",
url.String(),
bytes.NewBuffer(jsonParam),
)
req.Header.Add("Content-Type", "application/json")
return bb.request(req)
}
func (bb *bybit) getRequest(path string, param map[string]string) ([]byte, error) {
param["api_key"] = bb.key.APIKey
param["timestamp"] = fmt.Sprint(time.Now().UnixNano() / 1000000)
sign := getSignature(param, bb.key.APISecKey)
queryStr := getQuery(param) + "&sign=" + sign
url := url.URL{Scheme: "https", Host: bb.host, Path: path}
req, _ := http.NewRequest(
"GET",
url.String()+"?"+queryStr,
nil, //bytes.NewBuffer([]byte(queryStr)),
)
return bb.request(req)
}
func getSignature(params map[string]string, key string) string {
_val := getQuery(params)
h := hmac.New(sha256.New, []byte(key))
io.WriteString(h, _val)
return fmt.Sprintf("%x", h.Sum(nil))
}
func getQuery(params map[string]string) string {
keys := make([]string, len(params))
i := 0
_val := ""
for k := range params {
keys[i] = k
i++
}
sort.Strings(keys)
for _, k := range keys {
_val += k + "=" + params[k] + "&"
}
return _val[0 : len(_val)-1]
}
func structToMap(data interface{}) map[string]string {
result := make(map[string]string)
elem := reflect.ValueOf(data).Elem()
size := elem.NumField()
for i := 0; i < size; i++ {
field := elem.Type().Field(i).Tag.Get("json")
value := elem.Field(i).Interface()
result[field] = fmt.Sprint(value)
}
return result
}
func (bb *bybit) request(req *http.Request) ([]byte, error) {
resp, err := bb.httpClient.Do(req)
if err != nil {
errStr := fmt.Sprintf("err ==> %+v\nreq ==> %v\n", err, req)
return nil, errors.New(errStr)
}
if resp.StatusCode/100 != 2 {
body, _ := ioutil.ReadAll(resp.Body)
errStr := fmt.Sprintf("code:%d\n", resp.StatusCode)
errStr += fmt.Sprintf("body ==> %s\n", string(body))
errStr += fmt.Sprintf("resp ==> %+v\nreq ==> %v\n", resp, req)
return nil, errors.New(errStr)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
type errCheck struct {
Fail int `json:"ret_code"`
Message string `json:"ret_msg"`
Code string `json:"ext_code"`
Info string `json:"ext_info"`
}
check := errCheck{}
err = json.Unmarshal(body, &check)
if check.Fail != 0 || err != nil {
msg := fmt.Sprintf("msg: %s\n info: %s\n code: %s\n", check.Message, check.Info, check.Code)
if err != nil {
msg = err.Error()
}
return nil, errors.New(msg)
}
return body, err
}
func (bb *bybit) UpdateLTP(lastTimePrice float64) error {
return errors.New("not supported.")
}
func (bb *bybit) UpdateBestPrice(bestAsk, bestBid float64) error {
return errors.New("not supported.")
}