forked from adshao/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
continuous_kline_service.go
120 lines (110 loc) · 3.38 KB
/
continuous_kline_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
package futures
import (
"context"
"fmt"
"net/http"
)
// ContinuousKlinesService list klines
type ContinuousKlinesService struct {
c *Client
pair string
contractType string
interval string
limit *int
startTime *int64
endTime *int64
}
// pair set pair
func (s *ContinuousKlinesService) Pair(pair string) *ContinuousKlinesService {
s.pair = pair
return s
}
// contractType set contractType
func (s *ContinuousKlinesService) ContractType(contractType string) *ContinuousKlinesService {
s.contractType = contractType
return s
}
// Interval set interval
func (s *ContinuousKlinesService) Interval(interval string) *ContinuousKlinesService {
s.interval = interval
return s
}
// Limit set limit
func (s *ContinuousKlinesService) Limit(limit int) *ContinuousKlinesService {
s.limit = &limit
return s
}
// StartTime set startTime
func (s *ContinuousKlinesService) StartTime(startTime int64) *ContinuousKlinesService {
s.startTime = &startTime
return s
}
// EndTime set endTime
func (s *ContinuousKlinesService) EndTime(endTime int64) *ContinuousKlinesService {
s.endTime = &endTime
return s
}
// Do send request
func (s *ContinuousKlinesService) Do(ctx context.Context, opts ...RequestOption) (res []*ContinuousKline, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/fapi/v1/continuousKlines",
}
r.setParam("pair", s.pair)
r.setParam("contractType", s.contractType)
r.setParam("interval", s.interval)
if s.limit != nil {
r.setParam("limit", *s.limit)
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*ContinuousKline{}, err
}
j, err := newJSON(data)
if err != nil {
return []*ContinuousKline{}, err
}
num := len(j.MustArray())
res = make([]*ContinuousKline, num)
for i := 0; i < num; i++ {
item := j.GetIndex(i)
if len(item.MustArray()) < 11 {
err = fmt.Errorf("invalid kline response")
return []*ContinuousKline{}, err
}
res[i] = &ContinuousKline{
OpenTime: item.GetIndex(0).MustInt64(),
Open: item.GetIndex(1).MustString(),
High: item.GetIndex(2).MustString(),
Low: item.GetIndex(3).MustString(),
Close: item.GetIndex(4).MustString(),
Volume: item.GetIndex(5).MustString(),
CloseTime: item.GetIndex(6).MustInt64(),
QuoteAssetVolume: item.GetIndex(7).MustString(),
TradeNum: item.GetIndex(8).MustInt64(),
TakerBuyBaseAssetVolume: item.GetIndex(9).MustString(),
TakerBuyQuoteAssetVolume: item.GetIndex(10).MustString(),
}
}
return res, nil
}
// ContinuousKline define ContinuousKline info
type ContinuousKline struct {
OpenTime int64 `json:"openTime"`
Open string `json:"open"`
High string `json:"high"`
Low string `json:"low"`
Close string `json:"close"`
Volume string `json:"volume"`
CloseTime int64 `json:"closeTime"`
QuoteAssetVolume string `json:"quoteAssetVolume"`
TradeNum int64 `json:"tradeNum"`
TakerBuyBaseAssetVolume string `json:"takerBuyBaseAssetVolume"`
TakerBuyQuoteAssetVolume string `json:"takerBuyQuoteAssetVolume"`
}