-
Notifications
You must be signed in to change notification settings - Fork 6
/
market.go
242 lines (209 loc) · 8.67 KB
/
market.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
// Market Data Endpoints (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-marketdata)
package iperpetual
import (
"errors"
"github.com/ginarea/gobybit/bybitv2/transport"
)
// Query Symbol (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-querysymbol)
type SymbolInfo struct {
Name string `json:"name"`
Alias string `json:"alias"`
Status ContractStatus `json:"status"`
BaseCurrency string `json:"base_currency"`
QuoteCurrency string `json:"quote_currency"`
PriceScale float64 `json:"price_scale"`
TakerFee transport.Float64 `json:"taker_fee"`
MakerFee transport.Float64 `json:"maker_fee"`
FundingInterval float64 `json:"funding_interval"`
LeverageFilter LeverageFilter `json:"leverage_filter"`
PriceFilter PriceFilter `json:"price_filter"`
LotSizeFilter LotSizeFilter `json:"lot_size_filter"`
}
type LeverageFilter struct {
Min int `json:"min_leverage"`
Max int `json:"max_leverage"`
Step transport.Float64 `json:"leverage_step"`
}
type PriceFilter struct {
Min transport.Float64 `json:"min_price"`
Max transport.Float64 `json:"max_price"`
TickSize transport.Float64 `json:"tick_size"`
}
type LotSizeFilter struct {
MaxTradingQty float64 `json:"max_trading_qty"`
MinTradingQty float64 `json:"min_trading_qty"`
QtyStep float64 `json:"qty_step"`
PostOnlyMaxTradingQty transport.Float64 `json:"post_only_max_trading_qty"`
}
func (o *Client) QuerySymbol() ([]SymbolInfo, error) {
return GetPublic[[]SymbolInfo](o, "symbols", nil)
}
func (o *Client) QuerySymbolNames() ([]string, error) {
result, err := o.QuerySymbol()
names := make([]string, len(result))
for n, s := range result {
names[n] = s.Name
}
return names, err
}
// Order Book (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-orderbook)
type OrderBook struct {
Symbol string `param:"symbol"`
}
func (o OrderBook) Do(client *Client) ([]OrderBookItem, error) {
return GetPublic[[]OrderBookItem](client, "orderBook/L2", o)
}
type OrderBookItem struct {
Symbol string `json:"symbol"`
Price string `json:"price"`
Size int `json:"size"`
Side Side `json:"side"`
}
func (o *Client) OrderBook(symbol string) ([]OrderBookItem, error) {
return OrderBook{Symbol: symbol}.Do(o)
}
// Query Kline (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-querykline)
//
// symbol Required string Symbol
// interval Required string Data refresh interval. Enum : 1 3 5 15 30 60 120 240 360 720 "D" "M" "W"
// from Required integer From timestamp in seconds
// limit integer Limit for data size per page, max size is 200. Default as showing 200 pieces of data per page
type QueryKline struct {
Symbol string `param:"symbol"`
Interval KlineInterval `param:"interval"`
From int64 `param:"from"`
Limit *int `param:"limit"`
}
func (o QueryKline) Do(client *Client) ([]KlineItem, error) {
return GetPublic[[]KlineItem](client, "kline/list", o)
}
type KlineItem struct {
Symbol string `json:"symbol"`
Interval KlineInterval `json:"interval"`
OpenTime uint64 `json:"open_time"`
Open string `json:"open"`
High string `json:"high"`
Low string `json:"low"`
Close string `json:"close"`
Volume string `json:"volume"`
Turnover string `json:"turnover"`
}
func (o *Client) QueryKline(v QueryKline) ([]KlineItem, error) {
return v.Do(o)
}
// Latest Information for Symbol (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-latestsymbolinfo)
type SymbolLatestInformation struct {
Symbol *string `param:"symbol"`
}
func (o SymbolLatestInformation) Do(client *Client) ([]LatestInformation, error) {
return GetPublic[[]LatestInformation](client, "tickers", o)
}
type LatestInformation struct {
Symbol string `json:"symbol"`
BidPrice transport.Float64 `json:"bid_price"`
AskPrice transport.Float64 `json:"ask_price"`
LastPrice transport.Float64 `json:"last_price"`
LastTickDirection TickDirection `json:"last_tick_direction"`
PrevPrice24h transport.Float64 `json:"prev_price_24h"`
Price24hPcnt transport.Float64 `json:"price_24h_pcnt"`
HighPrice24h transport.Float64 `json:"high_price_24h"`
LowPrice24h transport.Float64 `json:"low_price_24h"`
PrevPrice1h transport.Float64 `json:"prev_price_1h"`
Price1hPcnt transport.Float64 `json:"price_1h_pcnt"`
MarkPrice transport.Float64 `json:"mark_price"`
IndexPrice transport.Float64 `json:"index_price"`
OpenInterest float64 `json:"open_interest"`
OpenValue transport.Float64 `json:"open_value"`
TotalTurnover transport.Float64 `json:"total_turnover"`
Turnover24h transport.Float64 `json:"turnover_24h"`
TotalVolume float64 `json:"total_volume"`
Volume24h float64 `json:"volume_24h"`
FundingRate transport.Float64 `json:"funding_rate"`
PredictedFundingRate transport.Float64 `json:"predicted_funding_rate"`
NextFundingTime string `json:"next_funding_time"`
CountdownHour int `json:"countdown_hour"`
DeliveryFeeRate string `json:"delivery_fee_rate"`
PredictedDeliveryPrice string `json:"predicted_delivery_price"`
DeliveryTime string `json:"delivery_time"`
}
func (o *Client) SymbolLatestInformation(symbol *string) ([]LatestInformation, error) {
return SymbolLatestInformation{Symbol: symbol}.Do(o)
}
func (o *Client) OneSymbolLatestInformation(symbol string) (i LatestInformation, err error) {
ret, err := o.SymbolLatestInformation(&symbol)
if err == nil {
if len(ret) == 1 {
i = ret[0]
} else {
err = errors.New("symbol latest len != 1")
}
}
return
}
// Public Trading Records (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-publictradingrecords)
//
// symbol Required string Symbol
// limit integer Limit for data size, max size is 1000. Default size is 500
type PublicTradingRecords struct {
Symbol string `param:"symbol"`
Limit *int `param:"limit"`
}
func (o PublicTradingRecords) Do(client *Client) ([]PublicTradingRecord, error) {
return GetPublic[[]PublicTradingRecord](client, "trading-records", o)
}
type PublicTradingRecord struct {
ID int `json:"id"`
Symbol string `json:"symbol"`
Price float64 `json:"price"`
Qty int `json:"qty"`
Side Side `json:"side"`
Time string `json:"time"`
}
func (o *Client) PublicTradingRecords(v PublicTradingRecords) ([]PublicTradingRecord, error) {
return v.Do(o)
}
// Query Mark Price Kline (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-markpricekline)
//
// Query mark price kline (like Query Kline but for mark price)
func (o QueryKline) DoMark(client *Client) ([]MarkKlineItem, error) {
return GetPublic[[]MarkKlineItem](client, "mark-price-kline", o)
}
type MarkKlineItem struct {
Symbol string `json:"symbol"`
Interval KlineInterval `json:"period"`
OpenTime uint64 `json:"start_at"`
Open int `json:"open"`
High int `json:"high"`
Low int `json:"low"`
Close int `json:"close"`
}
func (o *Client) QueryMarkKline(v QueryKline) ([]MarkKlineItem, error) {
return v.DoMark(o)
}
// Query Index Price Kline (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-queryindexpricekline)
//
// Index price kline. Tracks BTC spot prices, with a frequency of every second
func (o QueryKline) DoIndex(client *Client) ([]IndexKlineItem, error) {
return GetPublic[[]IndexKlineItem](client, "index-price-kline", o)
}
type IndexKlineItem struct {
Symbol string `json:"symbol"`
Interval KlineInterval `json:"period"`
OpenTime uint64 `json:"open_time"`
Open string `json:"open"`
High string `json:"high"`
Low string `json:"low"`
Close string `json:"close"`
}
func (o *Client) QueryIndexKline(v QueryKline) ([]IndexKlineItem, error) {
return v.DoIndex(o)
}
// Query Premium Index Kline (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-querypremiumindexkline)
//
// Premium index kline. Tracks the premium / discount of BTC perpetual contracts relative to the mark price per minute
func (o QueryKline) DoPremium(client *Client) ([]IndexKlineItem, error) {
return GetPublic[[]IndexKlineItem](client, "premium-index-kline", o)
}
func (o *Client) QueryPremiumKline(v QueryKline) ([]IndexKlineItem, error) {
return v.DoPremium(o)
}