-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
v5_market_service.go
1107 lines (943 loc) · 33.9 KB
/
v5_market_service.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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package bybit
import (
"encoding/json"
"errors"
"fmt"
"github.com/google/go-querystring/query"
)
// V5MarketServiceI :
type V5MarketServiceI interface {
GetKline(V5GetKlineParam) (*V5GetKlineResponse, error)
GetMarkPriceKline(V5GetMarkPriceKlineParam) (*V5GetMarkPriceKlineResponse, error)
GetIndexPriceKline(V5GetIndexPriceKlineParam) (*V5GetIndexPriceKlineResponse, error)
GetPremiumIndexPriceKline(V5GetPremiumIndexPriceKlineParam) (*V5GetPremiumIndexPriceKlineResponse, error)
GetInstrumentsInfo(V5GetInstrumentsInfoParam) (*V5GetInstrumentsInfoResponse, error)
GetOrderbook(V5GetOrderbookParam) (*V5GetOrderbookResponse, error)
GetTickers(V5GetTickersParam) (*V5GetTickersResponse, error)
GetFundingRateHistory(V5GetFundingRateHistoryParam) (*V5GetFundingRateHistoryResponse, error)
GetPublicTradingHistory(V5GetPublicTradingHistoryParam) (*V5GetPublicTradingHistoryResponse, error)
GetOpenInterest(V5GetOpenInterestParam) (*V5GetOpenInterestResponse, error)
GetHistoricalVolatility(V5GetHistoricalVolatilityParam) (*V5GetHistoricalVolatilityResponse, error)
GetInsurance(V5GetInsuranceParam) (*V5GetInsuranceResponse, error)
GetRiskLimit(V5GetRiskLimitParam) (*V5GetRiskLimitResponse, error)
}
// V5MarketService :
type V5MarketService struct {
client *Client
}
// V5GetKlineParam :
type V5GetKlineParam struct {
Category CategoryV5 `url:"category"`
Symbol SymbolV5 `url:"symbol"`
Interval Interval `url:"interval"`
Start *int64 `url:"start,omitempty"` // timestamp point for result, in milliseconds
End *int64 `url:"end,omitempty"` // timestamp point for result, in milliseconds
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 200]. Default: 200
}
// V5GetKlineResponse :
type V5GetKlineResponse struct {
CommonV5Response `json:",inline"`
Result V5GetKlineResult `json:"result"`
}
// V5GetKlineResult :
type V5GetKlineResult struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
List V5GetKlineList `json:"list"`
}
// V5GetKlineList :
type V5GetKlineList []V5GetKlineItem
// V5GetKlineItem :
type V5GetKlineItem struct {
StartTime string
Open string
High string
Low string
Close string
Volume string
Turnover string
}
// UnmarshalJSON :
func (l *V5GetKlineList) UnmarshalJSON(data []byte) error {
parsedData := [][]interface{}{}
if err := json.Unmarshal(data, &parsedData); err != nil {
return err
}
for _, d := range parsedData {
if len(d) != 7 {
return errors.New("so far len(items) must be 7, please check it on documents")
}
*l = append(*l, V5GetKlineItem{
StartTime: d[0].(string),
Open: d[1].(string),
High: d[2].(string),
Low: d[3].(string),
Close: d[4].(string),
Volume: d[5].(string),
Turnover: d[6].(string),
})
}
return nil
}
// GetKline :
func (s *V5MarketService) GetKline(param V5GetKlineParam) (*V5GetKlineResponse, error) {
var res V5GetKlineResponse
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/kline", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetMarkPriceKlineParam :
type V5GetMarkPriceKlineParam struct {
Category CategoryV5 `url:"category"`
Symbol SymbolV5 `url:"symbol"`
Interval Interval `url:"interval"`
Start *int64 `url:"start,omitempty"` // timestamp point for result, in milliseconds
End *int64 `url:"end,omitempty"` // timestamp point for result, in milliseconds
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 200]. Default: 200
}
// V5GetMarkPriceKlineResponse :
type V5GetMarkPriceKlineResponse struct {
CommonV5Response `json:",inline"`
Result V5GetMarkPriceKlineResult `json:"result"`
}
// V5GetMarkPriceKlineResult :
type V5GetMarkPriceKlineResult struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
List V5GetMarkPriceKlineList `json:"list"`
}
// V5GetMarkPriceKlineList :
type V5GetMarkPriceKlineList []V5GetMarkPriceKlineItem
// V5GetMarkPriceKlineItem :
type V5GetMarkPriceKlineItem struct {
StartTime string
Open string
High string
Low string
Close string
}
// UnmarshalJSON :
func (l *V5GetMarkPriceKlineList) UnmarshalJSON(data []byte) error {
parsedData := [][]interface{}{}
if err := json.Unmarshal(data, &parsedData); err != nil {
return err
}
for _, d := range parsedData {
if len(d) != 5 {
return errors.New("so far len(items) must be 5, please check it on documents")
}
*l = append(*l, V5GetMarkPriceKlineItem{
StartTime: d[0].(string),
Open: d[1].(string),
High: d[2].(string),
Low: d[3].(string),
Close: d[4].(string),
})
}
return nil
}
// GetMarkPriceKline :
func (s *V5MarketService) GetMarkPriceKline(param V5GetMarkPriceKlineParam) (*V5GetMarkPriceKlineResponse, error) {
var res V5GetMarkPriceKlineResponse
if param.Category != CategoryV5Linear && param.Category != CategoryV5Inverse {
return nil, fmt.Errorf("category should be linear or inverse")
}
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/mark-price-kline", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetIndexPriceKlineParam :
type V5GetIndexPriceKlineParam struct {
Category CategoryV5 `url:"category"`
Symbol SymbolV5 `url:"symbol"`
Interval Interval `url:"interval"`
Start *int64 `url:"start,omitempty"` // timestamp point for result, in milliseconds
End *int64 `url:"end,omitempty"` // timestamp point for result, in milliseconds
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 200]. Default: 200
}
// V5GetIndexPriceKlineResponse :
type V5GetIndexPriceKlineResponse struct {
CommonV5Response `json:",inline"`
Result V5GetIndexPriceKlineResult `json:"result"`
}
// V5GetIndexPriceKlineResult :
type V5GetIndexPriceKlineResult struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
List V5GetIndexPriceKlineList `json:"list"`
}
// V5GetIndexPriceKlineList :
type V5GetIndexPriceKlineList []V5GetIndexPriceKlineItem
// V5GetIndexPriceKlineItem :
type V5GetIndexPriceKlineItem struct {
StartTime string
Open string
High string
Low string
Close string
}
// UnmarshalJSON :
func (l *V5GetIndexPriceKlineList) UnmarshalJSON(data []byte) error {
parsedData := [][]interface{}{}
if err := json.Unmarshal(data, &parsedData); err != nil {
return err
}
for _, d := range parsedData {
if len(d) != 5 {
return errors.New("so far len(items) must be 5, please check it on documents")
}
*l = append(*l, V5GetIndexPriceKlineItem{
StartTime: d[0].(string),
Open: d[1].(string),
High: d[2].(string),
Low: d[3].(string),
Close: d[4].(string),
})
}
return nil
}
// GetIndexPriceKline :
func (s *V5MarketService) GetIndexPriceKline(param V5GetIndexPriceKlineParam) (*V5GetIndexPriceKlineResponse, error) {
var res V5GetIndexPriceKlineResponse
if param.Category != CategoryV5Linear && param.Category != CategoryV5Inverse {
return nil, fmt.Errorf("category should be linear or inverse")
}
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/index-price-kline", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetPremiumIndexPriceKlineParam :
type V5GetPremiumIndexPriceKlineParam struct {
Category CategoryV5 `url:"category"`
Symbol SymbolV5 `url:"symbol"`
Interval Interval `url:"interval"`
Start *int64 `url:"start,omitempty"` // timestamp point for result, in milliseconds
End *int64 `url:"end,omitempty"` // timestamp point for result, in milliseconds
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 200]. Default: 200
}
// V5GetPremiumIndexPriceKlineResponse :
type V5GetPremiumIndexPriceKlineResponse struct {
CommonV5Response `json:",inline"`
Result V5GetPremiumIndexPriceKlineResult `json:"result"`
}
// V5GetPremiumIndexPriceKlineResult :
type V5GetPremiumIndexPriceKlineResult struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
List V5GetPremiumIndexPriceKlineList `json:"list"`
}
// V5GetPremiumIndexPriceKlineList :
type V5GetPremiumIndexPriceKlineList []V5GetPremiumIndexPriceKlineItem
// V5GetPremiumIndexPriceKlineItem :
type V5GetPremiumIndexPriceKlineItem struct {
StartTime string
Open string
High string
Low string
Close string
}
// UnmarshalJSON :
func (l *V5GetPremiumIndexPriceKlineList) UnmarshalJSON(data []byte) error {
parsedData := [][]interface{}{}
if err := json.Unmarshal(data, &parsedData); err != nil {
return err
}
for _, d := range parsedData {
if len(d) != 5 {
return errors.New("so far len(items) must be 5, please check it on documents")
}
logger.Printf("parse v5 get premium index price kline list item: %v", d)
*l = append(*l, V5GetPremiumIndexPriceKlineItem{
StartTime: d[0].(string),
Open: d[1].(string),
High: d[2].(string),
Low: d[3].(string),
Close: d[4].(string),
})
}
return nil
}
// GetPremiumIndexPriceKline :
func (s *V5MarketService) GetPremiumIndexPriceKline(param V5GetPremiumIndexPriceKlineParam) (*V5GetPremiumIndexPriceKlineResponse, error) {
var res V5GetPremiumIndexPriceKlineResponse
if param.Category != CategoryV5Linear {
return nil, fmt.Errorf("category should be linear")
}
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/premium-index-price-kline", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetInstrumentsInfoParam :
// Spot does not support pagination, so limit, cursor are invalid.
// When query by baseCoin, regardless of category=linear or inverse, the result will have Linear contract and Inverse contract symbols.
type V5GetInstrumentsInfoParam struct {
Category CategoryV5 `url:"category"`
Symbol *SymbolV5 `url:"symbol,omitempty"`
BaseCoin *Coin `url:"baseCoin,omitempty"` // Base coin. linear,inverse,option only
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 1000]. Default: 500
Cursor *string `url:"cursor,omitempty"`
}
// V5GetInstrumentsInfoResponse :
type V5GetInstrumentsInfoResponse struct {
CommonV5Response `json:",inline"`
Result V5GetInstrumentsInfoResult `json:"result"`
}
// V5GetInstrumentsInfoResult :
// Responses are filled according to category.
type V5GetInstrumentsInfoResult struct {
LinearInverse *V5GetInstrumentsInfoLinearInverseResult
Option *V5GetInstrumentsInfoOptionResult
Spot *V5GetInstrumentsInfoSpotResult
}
// UnmarshalJSON :
func (r *V5GetInstrumentsInfoResult) UnmarshalJSON(data []byte) error {
var categoryJudge struct {
Category CategoryV5 `json:"category"`
}
if err := json.Unmarshal(data, &categoryJudge); err != nil {
return err
}
switch categoryJudge.Category {
case CategoryV5Linear, CategoryV5Inverse:
if err := json.Unmarshal(data, &r.LinearInverse); err != nil {
return err
}
case CategoryV5Option:
if err := json.Unmarshal(data, &r.Option); err != nil {
return err
}
case CategoryV5Spot:
if err := json.Unmarshal(data, &r.Spot); err != nil {
return err
}
default:
return fmt.Errorf("unexpected category %s given", categoryJudge.Category)
}
return nil
}
// V5GetInstrumentsInfoLinearInverseResult :
type V5GetInstrumentsInfoLinearInverseResult struct {
Category CategoryV5 `json:"category"`
NextPageCursor string `json:"nextPageCursor"`
List []V5GetInstrumentsInfoLinearInverseItem `json:"list"`
}
type LinearInverseLeverageFilterV5 struct {
MinLeverage string `json:"minLeverage"`
MaxLeverage string `json:"maxLeverage"`
LeverageStep string `json:"leverageStep"`
}
type V5GetInstrumentsInfoLinearInverseItem struct {
Symbol SymbolV5 `json:"symbol"`
ContractType ContractType `json:"contractType"`
Status InstrumentStatus `json:"status"`
BaseCoin Coin `json:"baseCoin"`
QuoteCoin Coin `json:"quoteCoin"`
SettleCoin Coin `json:"settleCoin"`
LaunchTime string `json:"launchTime"`
DeliveryTime string `json:"deliveryTime"`
DeliveryFeeRate string `json:"deliveryFeeRate"`
PriceScale string `json:"priceScale"`
LeverageFilter LinearInverseLeverageFilterV5 `json:"leverageFilter"`
PriceFilter LinearInversePriceFilterV5 `json:"priceFilter"`
LotSizeFilter LinearInverseLotSizeFilterV5 `json:"lotSizeFilter"`
UnifiedMarginTrade bool `json:"unifiedMarginTrade"`
FundingInterval int `json:"fundingInterval"`
}
type LinearInversePriceFilterV5 struct {
MinPrice string `json:"minPrice"`
MaxPrice string `json:"maxPrice"`
TickSize string `json:"tickSize"`
}
type LinearInverseLotSizeFilterV5 struct {
MaxOrderQty string `json:"maxOrderQty"`
MinOrderQty string `json:"minOrderQty"`
QtyStep string `json:"qtyStep"`
PostOnlyMaxOrderQty string `json:"postOnlyMaxOrderQty"`
MaxMktOrderQty string `json:"maxMktOrderQty"`
MinNotionalValue string `json:"minNotionalValue"`
}
// V5GetInstrumentsInfoOptionResult :
type V5GetInstrumentsInfoOptionResult struct {
Category CategoryV5 `json:"category"`
NextPageCursor string `json:"nextPageCursor"`
List []V5GetInstrumentsInfoOptionItem `json:"list"`
}
type V5GetInstrumentsInfoOptionItem struct {
Symbol SymbolV5 `json:"symbol"`
OptionsType OptionsType `json:"optionsType"`
Status InstrumentStatus `json:"status"`
BaseCoin Coin `json:"baseCoin"`
QuoteCoin Coin `json:"quoteCoin"`
SettleCoin Coin `json:"settleCoin"`
LaunchTime string `json:"launchTime"`
DeliveryTime string `json:"deliveryTime"`
DeliveryFeeRate string `json:"deliveryFeeRate"`
PriceFilter OptionPriceFilterV5 `json:"priceFilter"`
LotSizeFilter OptionLotSizeFilterV5 `json:"lotSizeFilter"`
}
type OptionPriceFilterV5 struct {
MinPrice string `json:"minPrice"`
MaxPrice string `json:"maxPrice"`
TickSize string `json:"tickSize"`
}
type OptionLotSizeFilterV5 struct {
MaxOrderQty string `json:"maxOrderQty"`
MinOrderQty string `json:"minOrderQty"`
QtyStep string `json:"qtyStep"`
}
// V5GetInstrumentsInfoSpotResult :
type V5GetInstrumentsInfoSpotResult struct {
Category CategoryV5 `json:"category"`
List []V5GetInstrumentsInfoSpotItem `json:"list"`
}
type V5GetInstrumentsInfoSpotItem struct {
Symbol SymbolV5 `json:"symbol"`
BaseCoin Coin `json:"baseCoin"`
QuoteCoin Coin `json:"quoteCoin"`
Innovation Innovation `json:"innovation"`
Status InstrumentStatus `json:"status"`
LotSizeFilter SpotLotSizeFilterV5 `json:"lotSizeFilter"`
PriceFilter SpotPriceFilterV5 `json:"priceFilter"`
}
type SpotLotSizeFilterV5 struct {
BasePrecision string `json:"basePrecision"`
QuotePrecision string `json:"quotePrecision"`
MaxOrderQty string `json:"maxOrderQty"`
MinOrderQty string `json:"minOrderQty"`
MinOrderAmt string `json:"minOrderAmt"`
MaxOrderAmt string `json:"maxOrderAmt"`
}
type SpotPriceFilterV5 struct {
TickSize string `json:"tickSize"`
}
// GetInstrumentsInfo :
func (s *V5MarketService) GetInstrumentsInfo(param V5GetInstrumentsInfoParam) (*V5GetInstrumentsInfoResponse, error) {
var res V5GetInstrumentsInfoResponse
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/instruments-info", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetOrderbookParam :
type V5GetOrderbookParam struct {
Category CategoryV5 `url:"category"`
Symbol SymbolV5 `url:"symbol"`
// spot: [1, 50]. Default: 1.
// linear&inverse: [1, 200]. Default: 25.
// option: [1, 25]. Default: 1.
Limit *int `url:"limit,omitempty"`
}
// V5GetOrderbookResponse :
type V5GetOrderbookResponse struct {
CommonV5Response `json:",inline"`
Result V5GetOrderbookResult `json:"result"`
}
// V5GetOrderbookResult :
type V5GetOrderbookResult struct {
Symbol SymbolV5 `json:"s"`
Bids V5GetOrderbookBidAsks `json:"b"`
Asks V5GetOrderbookBidAsks `json:"a"`
Timestamp int64 `json:"ts"`
UpdateID int `json:"u"`
}
// V5GetOrderbookBidAsks :
type V5GetOrderbookBidAsks []V5GetOrderbookBidAsk
// UnmarshalJSON :
func (b *V5GetOrderbookBidAsks) UnmarshalJSON(data []byte) error {
parsedData := [][]string{}
if err := json.Unmarshal(data, &parsedData); err != nil {
return err
}
items := V5GetOrderbookBidAsks{}
for _, item := range parsedData {
item := item
if len(item) != 2 {
return errors.New("so far len(item) must be 2, please check it on documents")
}
items = append(items, V5GetOrderbookBidAsk{
Price: item[0],
Quantity: item[1],
})
}
*b = items
return nil
}
// V5GetOrderbookBidAsk :
type V5GetOrderbookBidAsk struct {
Price string `json:"price"`
Quantity string `json:"quantity"`
}
// GetOrderbook :
func (s *V5MarketService) GetOrderbook(param V5GetOrderbookParam) (*V5GetOrderbookResponse, error) {
var res V5GetOrderbookResponse
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/orderbook", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetTickersParam :
type V5GetTickersParam struct {
Category CategoryV5 `url:"category"`
Symbol *SymbolV5 `url:"symbol,omitempty"`
BaseCoin *Coin `url:"baseCoin,omitempty"` // Base coin. For option only
ExpDate *string `url:"expDate,omitempty"` // Expiry date. e.g., 25DEC22. For option only
}
func (p V5GetTickersParam) validate() error {
if p.Category == CategoryV5Option && (p.Symbol == nil && p.BaseCoin == nil) {
return fmt.Errorf("symbol or baseCoin must be passed for option")
}
if p.BaseCoin != nil && p.Category != CategoryV5Option {
return fmt.Errorf("baseCoin is for option only")
}
if p.ExpDate != nil && p.Category != CategoryV5Option {
return fmt.Errorf("expDate is for option only")
}
return nil
}
// V5GetTickersResponse :
type V5GetTickersResponse struct {
CommonV5Response `json:",inline"`
Result V5GetTickersResult `json:"result"`
}
// V5GetTickersResult :
// Responses are filled according to category.
type V5GetTickersResult struct {
LinearInverse *V5GetTickersLinearInverseResult
Option *V5GetTickersOptionResult
Spot *V5GetTickersSpotResult
}
// UnmarshalJSON :
func (r *V5GetTickersResult) UnmarshalJSON(data []byte) error {
var categoryJudge struct {
Category CategoryV5 `json:"category"`
}
if err := json.Unmarshal(data, &categoryJudge); err != nil {
return err
}
switch categoryJudge.Category {
case CategoryV5Linear, CategoryV5Inverse:
if err := json.Unmarshal(data, &r.LinearInverse); err != nil {
return err
}
case CategoryV5Option:
if err := json.Unmarshal(data, &r.Option); err != nil {
return err
}
case CategoryV5Spot:
if err := json.Unmarshal(data, &r.Spot); err != nil {
return err
}
default:
return fmt.Errorf("unexpected category %s given", categoryJudge.Category)
}
return nil
}
// V5GetTickersLinearInverseResult :
type V5GetTickersLinearInverseResult struct {
Category CategoryV5 `json:"category"`
List []V5GetTickersLinearInverseItem `json:"list"`
}
type V5GetTickersLinearInverseItem struct {
Symbol SymbolV5 `json:"symbol"`
LastPrice string `json:"lastPrice"`
IndexPrice string `json:"indexPrice"`
MarkPrice string `json:"markPrice"`
PrevPrice24H string `json:"prevPrice24h"`
Price24HPcnt string `json:"price24hPcnt"`
HighPrice24H string `json:"highPrice24h"`
LowPrice24H string `json:"lowPrice24h"`
PrevPrice1H string `json:"prevPrice1h"`
OpenInterest string `json:"openInterest"`
OpenInterestValue string `json:"openInterestValue"`
Turnover24H string `json:"turnover24h"`
Volume24H string `json:"volume24h"`
FundingRate string `json:"fundingRate"`
NextFundingTime string `json:"nextFundingTime"`
PredictedDeliveryPrice string `json:"predictedDeliveryPrice"`
BasisRate string `json:"basisRate"`
DeliveryFeeRate string `json:"deliveryFeeRate"`
DeliveryTime string `json:"deliveryTime"`
Ask1Size string `json:"ask1Size"`
Bid1Price string `json:"bid1Price"`
Ask1Price string `json:"ask1Price"`
Bid1Size string `json:"bid1Size"`
}
// V5GetTickersOptionResult :
type V5GetTickersOptionResult struct {
Category CategoryV5 `json:"category"`
List []V5GetTickersOptionItem `json:"list"`
}
type V5GetTickersOptionItem struct {
Symbol SymbolV5 `json:"symbol"`
Bid1Price string `json:"bid1Price"`
Bid1Size string `json:"bid1Size"`
Bid1Iv string `json:"bid1Iv"`
Ask1Price string `json:"ask1Price"`
Ask1Size string `json:"ask1Size"`
Ask1Iv string `json:"ask1Iv"`
LastPrice string `json:"lastPrice"`
HighPrice24H string `json:"highPrice24h"`
LowPrice24H string `json:"lowPrice24h"`
MarkPrice string `json:"markPrice"`
IndexPrice string `json:"indexPrice"`
MarkIv string `json:"markIv"`
UnderlyingPrice string `json:"underlyingPrice"`
OpenInterest string `json:"openInterest"`
Turnover24H string `json:"turnover24h"`
Volume24H string `json:"volume24h"`
TotalVolume string `json:"totalVolume"`
TotalTurnover string `json:"totalTurnover"`
Delta string `json:"delta"`
Gamma string `json:"gamma"`
Vega string `json:"vega"`
Theta string `json:"theta"`
PredictedDeliveryPrice string `json:"predictedDeliveryPrice"`
Change24H string `json:"change24h"`
}
// V5GetTickersSpotResult :
type V5GetTickersSpotResult struct {
Category CategoryV5 `json:"category"`
List []V5GetTickersSpotItem `json:"list"`
}
type V5GetTickersSpotItem struct {
Symbol SymbolV5 `json:"symbol"`
Bid1Price string `json:"bid1Price"`
Bid1Size string `json:"bid1Size"`
Ask1Price string `json:"ask1Price"`
Ask1Size string `json:"ask1Size"`
LastPrice string `json:"lastPrice"`
PrevPrice24H string `json:"prevPrice24h"`
Price24HPcnt string `json:"price24hPcnt"`
HighPrice24H string `json:"highPrice24h"`
LowPrice24H string `json:"lowPrice24h"`
Turnover24H string `json:"turnover24h"`
Volume24H string `json:"volume24h"`
UsdIndexPrice string `json:"usdIndexPrice"`
}
// GetTickers :
func (s *V5MarketService) GetTickers(param V5GetTickersParam) (*V5GetTickersResponse, error) {
var res V5GetTickersResponse
if err := param.validate(); err != nil {
return nil, fmt.Errorf("validate param: %w", err)
}
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/tickers", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetFundingRateHistoryParam :
type V5GetFundingRateHistoryParam struct {
Category CategoryV5 `url:"category"`
Symbol SymbolV5 `url:"symbol"`
StartTime *int64 `url:"startTime,omitempty"` // The start timestamp (ms)
EndTime *int64 `url:"endTime,omitempty"` // The start timestamp (ms)
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 200]. Default: 200
}
func (p V5GetFundingRateHistoryParam) validate() error {
if p.Category != CategoryV5Linear && p.Category != CategoryV5Inverse {
return fmt.Errorf("only linear and inverse are supported for category")
}
return nil
}
// V5GetFundingRateHistoryResponse :
type V5GetFundingRateHistoryResponse struct {
CommonV5Response `json:",inline"`
Result V5GetFundingRateHistoryResult `json:"result"`
}
// V5GetFundingRateHistoryResult :
type V5GetFundingRateHistoryResult struct {
Category CategoryV5 `json:"category"`
List []V5GetFundingRateHistoryItem `json:"list"`
}
type V5GetFundingRateHistoryItem struct {
Symbol SymbolV5 `json:"symbol"`
FundingRate string `json:"fundingRate"`
FundingRateTimestamp string `json:"fundingRateTimestamp"`
}
// GetFundingRateHistory :
func (s *V5MarketService) GetFundingRateHistory(param V5GetFundingRateHistoryParam) (*V5GetFundingRateHistoryResponse, error) {
var res V5GetFundingRateHistoryResponse
if err := param.validate(); err != nil {
return nil, fmt.Errorf("validate param: %w", err)
}
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/funding/history", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetPublicTradingHistoryParam :
type V5GetPublicTradingHistoryParam struct {
Category CategoryV5 `url:"category"`
Symbol SymbolV5 `url:"symbol"`
BaseCoin *Coin `url:"baseCoin,omitempty"` // For option only. If not passed, return BTC data by default
OptionType *OptionsType `url:"optionType,omitempty"`
// Limit for data size per page.
// - spot: [1,60], default: 60
// - others: [1,1000], default: 500
Limit *int `url:"limit,omitempty"`
}
func (p V5GetPublicTradingHistoryParam) validate() error {
if p.BaseCoin != nil && p.Category != CategoryV5Option {
return fmt.Errorf("baseCoin is for option only")
}
return nil
}
// V5GetPublicTradingHistoryResponse :
type V5GetPublicTradingHistoryResponse struct {
CommonV5Response `json:",inline"`
Result V5GetPublicTradingHistoryResult `json:"result"`
}
// V5GetPublicTradingHistoryResult :
type V5GetPublicTradingHistoryResult struct {
Category CategoryV5 `json:"category"`
List []V5GetPublicTradingHistoryItem `json:"list"`
}
type V5GetPublicTradingHistoryItem struct {
ExecID string `json:"execId"`
Symbol SymbolV5 `json:"symbol"`
Price string `json:"price"`
Size string `json:"size"`
Side Side `json:"side"`
Time string `json:"time"`
IsBlockTrade bool `json:"isBlockTrade"`
}
// GetPublicTradingHistory :
func (s *V5MarketService) GetPublicTradingHistory(param V5GetPublicTradingHistoryParam) (*V5GetPublicTradingHistoryResponse, error) {
var res V5GetPublicTradingHistoryResponse
if err := param.validate(); err != nil {
return nil, fmt.Errorf("validate param: %w", err)
}
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/recent-trade", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetOpenInterestParam :
type V5GetOpenInterestParam struct {
Category CategoryV5 `url:"category"`
Symbol SymbolV5 `url:"symbol"`
IntervalTime Period `url:"intervalTime"`
StartTime *int64 `url:"startTime,omitempty"` // The start timestamp (ms)
EndTime *int64 `url:"endTime,omitempty"` // The start timestamp (ms)
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 200]. Default: 50
Cursor *string `url:"cursor,omitempty"`
}
func (p V5GetOpenInterestParam) validate() error {
if p.Category != CategoryV5Linear && p.Category != CategoryV5Inverse {
return fmt.Errorf("only linear and inverse are supported for category")
}
return nil
}
// V5GetOpenInterestResponse :
type V5GetOpenInterestResponse struct {
CommonV5Response `json:",inline"`
Result V5GetOpenInterestResult `json:"result"`
}
// V5GetOpenInterestResult :
type V5GetOpenInterestResult struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
List []V5GetOpenInterestItem `json:"list"`
NextPageCursor string `json:"nextPageCursor"`
}
type V5GetOpenInterestItem struct {
OpenInterest string `json:"openInterest"`
Timestamp string `json:"timestamp"`
}
// GetOpenInterest :
func (s *V5MarketService) GetOpenInterest(param V5GetOpenInterestParam) (*V5GetOpenInterestResponse, error) {
var res V5GetOpenInterestResponse
if err := param.validate(); err != nil {
return nil, fmt.Errorf("validate param: %w", err)
}
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getPublicly("/v5/market/open-interest", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetHistoricalVolatilityParam :
type V5GetHistoricalVolatilityParam struct {
Category CategoryV5 `url:"category"` // option only
BaseCoin *Coin `url:"baseCoin,omitempty"`
Period *int `url:"period,omitempty"`
StartTime *int64 `url:"startTime,omitempty"` // The start timestamp (ms)
EndTime *int64 `url:"endTime,omitempty"` // The start timestamp (ms)
}
func (p V5GetHistoricalVolatilityParam) validate() error {
if p.Category != CategoryV5Option {
return fmt.Errorf("only option is supported")
}
return nil
}
// V5GetHistoricalVolatilityResponse :
type V5GetHistoricalVolatilityResponse struct {
CommonV5Response `json:",inline"`
Result V5GetHistoricalVolatilityResult `json:"result"`
}
// UnmarshalJSON : Because the response structure is different from others
func (r *V5GetHistoricalVolatilityResponse) UnmarshalJSON(data []byte) error {
var tmp struct {
CommonV5Response `json:",inline"`
Category CategoryV5 `json:"category"`
List []V5GetHistoricalVolatilityListItem `json:"result"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
*r = V5GetHistoricalVolatilityResponse{
CommonV5Response: tmp.CommonV5Response,
Result: V5GetHistoricalVolatilityResult{
Category: tmp.Category,
List: tmp.List,
},
}
return nil
}
// V5GetHistoricalVolatilityResult :
type V5GetHistoricalVolatilityResult struct {
Category CategoryV5 `json:"category"`
List []V5GetHistoricalVolatilityListItem `json:"list"`
}
// V5GetHistoricalVolatilityListItem :
type V5GetHistoricalVolatilityListItem struct {
Period int `json:"period"`
Value string `json:"value"`
Time string `json:"time"`
}
// GetHistoricalVolatility :
func (s *V5MarketService) GetHistoricalVolatility(param V5GetHistoricalVolatilityParam) (*V5GetHistoricalVolatilityResponse, error) {
var res V5GetHistoricalVolatilityResponse
if err := param.validate(); err != nil {
return nil, fmt.Errorf("validate param: %w", err)
}
queryString, err := query.Values(param)