forked from f0cii/bybit-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_public.go
132 lines (121 loc) · 3.19 KB
/
api_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
package rest
import (
"net/http"
"sort"
"strconv"
"time"
)
// GetServerTime Get server time.
func (b *ByBit) GetServerTime() (timeNow int64, err error) {
params := map[string]interface{}{}
var ret BaseResult
_, err = b.PublicRequest(http.MethodGet, "v2/public/time", params, &ret)
if err != nil {
return
}
var t float64
t, err = strconv.ParseFloat(ret.TimeNow, 64)
if err != nil {
return
}
timeNow = int64(t * 1000)
return
}
// GetOrderBook Get the orderbook
// 正反向合约通用
func (b *ByBit) GetOrderBook(symbol string) (result OrderBook, err error) {
var ret GetOrderBookResult
params := map[string]interface{}{}
params["symbol"] = symbol
_, err = b.PublicRequest(http.MethodGet, "v2/public/orderBook/L2", params, &ret)
if err != nil {
return
}
for _, v := range ret.Result {
if v.Side == "Sell" {
result.Asks = append(result.Asks, Item{
Price: v.Price,
Size: v.Size,
})
} else if v.Side == "Buy" {
result.Bids = append(result.Bids, Item{
Price: v.Price,
Size: v.Size,
})
}
}
sort.Slice(result.Asks, func(i, j int) bool {
return result.Asks[i].Price < result.Asks[j].Price
})
sort.Slice(result.Bids, func(i, j int) bool {
return result.Bids[i].Price > result.Bids[j].Price
})
var timeNow float64
timeNow, err = strconv.ParseFloat(ret.TimeNow, 64) // 1582011750.433202
if err != nil {
return
}
result.Time = time.Unix(0, int64(timeNow*1e9))
return
}
// GetKLine
// https://bybit-exchange.github.io/docs/inverse/#t-httprequest-2
// interval: 1 3 5 15 30 60 120 240 360 720 "D" "M" "W" "Y"
// from: From timestamp in seconds
// limit: Limit for data size per page, max size is 200. Default as showing 200 pieces of data per page
func (b *ByBit) GetKLine(symbol string, interval string, from int64, limit int) (result []OHLC, err error) {
var ret GetKlineResult
params := map[string]interface{}{}
params["symbol"] = symbol
params["interval"] = interval
params["from"] = from
if limit > 0 {
params["limit"] = limit
}
_, err = b.PublicRequest(http.MethodGet, "v2/public/kline/list", params, &ret)
if err != nil {
return
}
result = ret.Result
return
}
func (b *ByBit) GetTickers() (result []Ticker, err error) {
// https://api-testnet.bybit.com/v2/public/tickers
var ret GetTickersResult
params := map[string]interface{}{}
_, err = b.PublicRequest(http.MethodGet, "v2/public/tickers", params, &ret)
if err != nil {
return
}
result = ret.Result
return
}
// from: From ID. Default: return latest data
// limit: Number of results. Default 500; max 1000
func (b *ByBit) GetTradingRecords(symbol string, from int64, limit int) (result []TradingRecord, err error) {
var ret GetTradingRecordsResult
params := map[string]interface{}{}
params["symbol"] = symbol
if from > 0 {
params["from"] = from
}
if limit > 0 {
params["limit"] = limit
}
_, err = b.PublicRequest(http.MethodGet, "v2/public/trading-records", params, &ret)
if err != nil {
return
}
result = ret.Result
return
}
func (b *ByBit) GetSymbols() (result []SymbolInfo, err error) {
var ret GetSymbolsResult
params := map[string]interface{}{}
_, err = b.PublicRequest(http.MethodGet, "v2/public/symbols", params, &ret)
if err != nil {
return
}
result = ret.Result
return
}