forked from hirokisan/bybit
/
market.go
672 lines (589 loc) · 16.6 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
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
661
662
663
664
665
666
667
668
669
670
671
672
package bybit
import (
"encoding/json"
"net/http"
"strconv"
)
// MarketService :
type MarketService struct {
Client *Client
}
// OrderBookResponse :
type OrderBookResponse struct {
CommonResponse `json:",inline"`
Result []OrderBookResult `json:"result"`
}
// UnmarshalJSON :
func (r *OrderBookResponse) UnmarshalJSON(data []byte) error {
return nil
}
// OrderBookResult :
type OrderBookResult struct {
Symbol SymbolInverse `json:"symbol"`
Price float64 `json:"price"`
Size float64 `json:"size"`
Side Side `json:"side"`
}
// OrderBook :
func (s *MarketService) OrderBook(symbol SymbolInverse) (*OrderBookResponse, error) {
var res OrderBookResponse
params := map[string]string{
"symbol": string(symbol),
}
url, err := s.Client.BuildPublicURL("/v2/public/orderBook/L2", params)
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// ListKlineParam :
type ListKlineParam struct {
Symbol SymbolInverse `json:"symbol"`
Interval Interval `json:"interval"`
From int `json:"from"`
Limit *int `json:"limit"`
}
func (p *ListKlineParam) build() map[string]string {
result := map[string]string{
"symbol": string(p.Symbol),
"interval": string(p.Interval),
"from": strconv.Itoa(p.From),
}
if p.Limit != nil {
result["limit"] = strconv.Itoa(*p.Limit)
}
return result
}
// ListKlineResponse :
type ListKlineResponse struct {
CommonResponse `json:",inline"`
Result []ListKlineResult `json:"result"`
}
// ListKlineResult :
type ListKlineResult struct {
Symbol SymbolInverse `json:"symbol"`
Interval string `json:"interval"`
OpenTime int `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"`
}
// ListKline :
func (s *MarketService) ListKline(param ListKlineParam) (*ListKlineResponse, error) {
var res ListKlineResponse
url, err := s.Client.BuildPublicURL("/v2/public/kline/list", param.build())
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// TickersResponse :
type TickersResponse struct {
CommonResponse `json:",inline"`
Result []TickersResult `json:"result"`
}
// TickersResult :
type TickersResult struct {
Symbol SymbolInverse `json:"symbol"`
BidPrice string `json:"bid_price"`
AskPrice string `json:"ask_price"`
LastPrice string `json:"last_price"`
LastTickDirection TickDirection `json:"last_tick_direction"`
PrevPrice24h string `json:"prev_price_24h"`
Price24hPcnt string `json:"price_24h_pcnt"`
HighPrice24h string `json:"high_price_24h"`
LowPrice24h string `json:"low_price_24h"`
PrevPrice1h string `json:"prev_price_1h"`
Price1hPcnt string `json:"price_1h_pcnt"`
MarkPrice string `json:"mark_price"`
IndexPrice string `json:"index_price"`
OpenInterest float64 `json:"open_interest"`
OpenValue string `json:"open_value"`
TotalTurnover string `json:"total_turnover"`
Turnover24h string `json:"turnover_24h"`
TotalVolume float64 `json:"total_volume"`
Volume24h float64 `json:"volume_24h"`
FundingRate string `json:"funding_rate"`
PredictedFundingRate string `json:"predicted_funding_rate"`
NextFundingTime string `json:"next_funding_time"`
CountdownHour float64 `json:"countdown_hour"`
}
// Tickers :
func (s *MarketService) Tickers(symbol SymbolInverse) (*TickersResponse, error) {
var res TickersResponse
params := map[string]string{
"symbol": string(symbol),
}
url, err := s.Client.BuildPublicURL("/v2/public/tickers", params)
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// TradingRecordsParam :
type TradingRecordsParam struct {
Symbol SymbolInverse `json:"symbol"`
From *int `json:"from"`
Limit *int `json:"limit"`
}
func (p *TradingRecordsParam) build() map[string]string {
result := map[string]string{
"symbol": string(p.Symbol),
}
if p.From != nil {
result["from"] = strconv.Itoa(*p.From)
}
if p.Limit != nil {
result["limit"] = strconv.Itoa(*p.Limit)
}
return result
}
// TradingRecordsResponse :
type TradingRecordsResponse struct {
CommonResponse `json:",inline"`
Result []TradingRecordsResult `json:"result"`
}
// TradingRecordsResult :
type TradingRecordsResult struct {
ID float64 `json:"id"`
Symbol SymbolInverse `json:"symbol"`
Price float64 `json:"price"`
Qty float64 `json:"qty"`
Side Side `json:"side"`
Time string `json:"time"`
}
// TradingRecords :
func (s *MarketService) TradingRecords(param TradingRecordsParam) (*TradingRecordsResponse, error) {
var res TradingRecordsResponse
url, err := s.Client.BuildPublicURL("/v2/public/trading-records", param.build())
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// SymbolsResponse :
type SymbolsResponse struct {
CommonResponse `json:",inline"`
Result []SymbolsResult `json:"result"`
}
// SymbolsResult :
type SymbolsResult struct {
Name string `json:"name"`
BaseCurrency string `json:"base_currency"`
QuoteCurrency string `json:"quote_currency"`
PriceScale float64 `json:"price_scale"`
TakerFee string `json:"taker_fee"`
MakerFee string `json:"maker_fee"`
LeverageFilter LeverageFilter `json:"leverage_filter"`
PriceFilter PriceFilter `json:"price_filter"`
LotSizeFilter LotSizeFilter `json:"lot_size_filter"`
}
// LeverageFilter :
type LeverageFilter struct {
MinLeverage float64 `json:"min_leverage"`
MaxLeverage float64 `json:"max_leverage"`
LeverageStep string `json:"leverage_step"`
}
// PriceFilter :
type PriceFilter struct {
MinPrice string `json:"min_price"`
MaxPrice string `json:"max_price"`
TickSize string `json:"tick_size"`
}
// LotSizeFilter :
type LotSizeFilter struct {
MaxTradingQty float64 `json:"max_trading_qty"`
MinTradingQty float64 `json:"min_trading_qty"`
QtyStep float64 `json:"qty_step"`
}
// Symbols :
func (s *MarketService) Symbols() (*SymbolsResponse, error) {
var res SymbolsResponse
url, err := s.Client.BuildPublicURL("/v2/public/symbols", nil)
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// LiqRecordsResponse :
type LiqRecordsResponse struct {
CommonResponse `json:",inline"`
Result []LiqRecordsResult `json:"result"`
}
// LiqRecordsResult :
type LiqRecordsResult struct {
ID float64 `json:"id"`
Qty float64 `json:"qty"`
Side Side `json:"side"`
Time int `json:"time"` // or float64
Symbol SymbolInverse `json:"symbol"`
Price float64 `json:"price"`
}
// LiqRecordsParam :
type LiqRecordsParam struct {
Symbol SymbolInverse `json:"symbol"`
From *int `json:"from"`
Limit *int `json:"limit"`
StartTime *int `json:"start_time"`
EndTime *int `json:"end_time"`
}
func (p *LiqRecordsParam) build() map[string]string {
result := map[string]string{
"symbol": string(p.Symbol),
}
if p.From != nil {
result["from"] = strconv.Itoa(*p.From)
}
if p.Limit != nil {
result["limit"] = strconv.Itoa(*p.Limit)
}
if p.StartTime != nil {
result["start_time"] = strconv.Itoa(*p.StartTime)
}
if p.EndTime != nil {
result["end_time"] = strconv.Itoa(*p.EndTime)
}
return result
}
// LiqRecords :
func (s *MarketService) LiqRecords(param LiqRecordsParam) (*LiqRecordsResponse, error) {
var res LiqRecordsResponse
url, err := s.Client.BuildPublicURL("/v2/public/liq-records", param.build())
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// MarkPriceKlineResponse :
type MarkPriceKlineResponse struct {
CommonResponse `json:",inline"`
Result []MarkPriceKlineResult `json:"result"`
}
// MarkPriceKlineResult :
type MarkPriceKlineResult struct {
Symbol SymbolInverse `json:"symbol"`
Period Period `json:"period"`
StartAt int `json:"start_at"`
Open float64 `json:"open"`
High float64 `json:"high"`
Low float64 `json:"low"`
Close float64 `json:"close"`
}
// MarkPriceKlineParam :
type MarkPriceKlineParam struct {
Symbol SymbolInverse `json:"symbol"`
Interval Interval `json:"interval"`
From int `json:"from"`
Limit *int `json:"limit"`
}
func (p *MarkPriceKlineParam) build() map[string]string {
result := map[string]string{
"symbol": string(p.Symbol),
"interval": string(p.Interval),
"from": strconv.Itoa(p.From),
}
if p.Limit != nil {
result["limit"] = strconv.Itoa(*p.Limit)
}
return result
}
// MarkPriceKline :
func (s *MarketService) MarkPriceKline(param MarkPriceKlineParam) (*MarkPriceKlineResponse, error) {
var res MarkPriceKlineResponse
url, err := s.Client.BuildPublicURL("/v2/public/mark-price-kline", param.build())
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// IndexPriceKlineResponse :
type IndexPriceKlineResponse struct {
CommonResponse `json:",inline"`
Result []IndexPriceKlineResult `json:"result"`
}
// IndexPriceKlineResult :
type IndexPriceKlineResult struct {
Symbol SymbolInverse `json:"symbol"`
Period Period `json:"period"`
OpenTime int `json:"open_time"`
Open string `json:"open"`
High string `json:"high"`
Low string `json:"low"`
Close string `json:"close"`
}
// IndexPriceKlineParam :
type IndexPriceKlineParam struct {
Symbol SymbolInverse `json:"symbol"`
Interval Interval `json:"interval"`
From int `json:"from"`
Limit *int `json:"limit"`
}
func (p *IndexPriceKlineParam) build() map[string]string {
result := map[string]string{
"symbol": string(p.Symbol),
"interval": string(p.Interval),
"from": strconv.Itoa(p.From),
}
if p.Limit != nil {
result["limit"] = strconv.Itoa(*p.Limit)
}
return result
}
// IndexPriceKline :
func (s *MarketService) IndexPriceKline(param IndexPriceKlineParam) (*IndexPriceKlineResponse, error) {
var res IndexPriceKlineResponse
url, err := s.Client.BuildPublicURL("/v2/public/index-price-kline", param.build())
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// PremiumIndexKlineResponse :
type PremiumIndexKlineResponse struct {
CommonResponse `json:",inline"`
Result []PremiumIndexKlineResult `json:"result"`
}
// PremiumIndexKlineResult :
type PremiumIndexKlineResult struct {
Symbol SymbolInverse `json:"symbol"`
Period Period `json:"period"`
OpenTime int `json:"open_time"`
Open string `json:"open"`
High string `json:"high"`
Low string `json:"low"`
Close string `json:"close"`
}
// PremiumIndexKlineParam :
type PremiumIndexKlineParam struct {
Symbol SymbolInverse `json:"symbol"`
Interval Interval `json:"interval"`
From int `json:"from"`
Limit *int `json:"limit"`
}
func (p *PremiumIndexKlineParam) build() map[string]string {
result := map[string]string{
"symbol": string(p.Symbol),
"interval": string(p.Interval),
"from": strconv.Itoa(p.From),
}
if p.Limit != nil {
result["limit"] = strconv.Itoa(*p.Limit)
}
return result
}
// PremiumIndexKline :
func (s *MarketService) PremiumIndexKline(param PremiumIndexKlineParam) (*PremiumIndexKlineResponse, error) {
var res PremiumIndexKlineResponse
url, err := s.Client.BuildPublicURL("/v2/public/premium-index-kline", param.build())
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// OpenInterestResponse :
type OpenInterestResponse struct {
CommonResponse `json:",inline"`
Result []OpenInterestResult `json:"result"`
}
// OpenInterestResult :
type OpenInterestResult struct {
OpenInterest int `json:"open_interest"`
Timestamp int `json:"timestamp"`
Symbol SymbolInverse `json:"symbol"`
}
// OpenInterestParam :
type OpenInterestParam struct {
Symbol SymbolInverse `json:"symbol"`
Period Period `json:"period"`
Limit *int `json:"limit"`
}
func (p *OpenInterestParam) build() map[string]string {
result := map[string]string{
"symbol": string(p.Symbol),
"period": string(p.Period),
}
if p.Limit != nil {
result["limit"] = strconv.Itoa(*p.Limit)
}
return result
}
// OpenInterest :
func (s *MarketService) OpenInterest(param OpenInterestParam) (*OpenInterestResponse, error) {
var res OpenInterestResponse
url, err := s.Client.BuildPublicURL("/v2/public/open-interest", param.build())
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// BigDealResponse :
type BigDealResponse struct {
CommonResponse `json:",inline"`
Result []BigDealResult `json:"result"`
}
// BigDealResult :
type BigDealResult struct {
Symbol SymbolInverse `json:"symbol"`
Side Side `json:"side"`
Timestamp int `json:"timestamp"`
Value int `json:"value"`
}
// BigDealParam :
type BigDealParam struct {
Symbol SymbolInverse `json:"symbol"`
Limit *int `json:"limit"`
}
func (p *BigDealParam) build() map[string]string {
result := map[string]string{
"symbol": string(p.Symbol),
}
if p.Limit != nil {
result["limit"] = strconv.Itoa(*p.Limit)
}
return result
}
// BigDeal :
func (s *MarketService) BigDeal(param BigDealParam) (*BigDealResponse, error) {
var res BigDealResponse
url, err := s.Client.BuildPublicURL("/v2/public/big-deal", param.build())
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}
// AccountRatioResponse :
type AccountRatioResponse struct {
CommonResponse `json:",inline"`
Result []AccountRatioResult `json:"result"`
}
// AccountRatioResult :
type AccountRatioResult struct {
Symbol SymbolInverse `json:"symbol"`
BuyRatio float64 `json:"buy_ratio"`
SellRatio float64 `json:"sell_ratio"`
Timestamp int `json:"timestamp"`
}
// AccountRatioParam :
type AccountRatioParam struct {
Symbol SymbolInverse `json:"symbol"`
Period Period `json:"period"`
Limit *int `json:"limit"`
}
func (p *AccountRatioParam) build() map[string]string {
result := map[string]string{
"symbol": string(p.Symbol),
"period": string(p.Period),
}
if p.Limit != nil {
result["limit"] = strconv.Itoa(*p.Limit)
}
return result
}
// AccountRatio :
func (s *MarketService) AccountRatio(param AccountRatioParam) (*AccountRatioResponse, error) {
var res AccountRatioResponse
url, err := s.Client.BuildPublicURL("/v2/public/account-ratio", param.build())
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return &res, nil
}