-
Notifications
You must be signed in to change notification settings - Fork 1
/
kline_service.go
112 lines (103 loc) · 2.92 KB
/
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
package binance
import (
"context"
"fmt"
"net/http"
)
// KlinesService list klines
type KlinesService struct {
c *Client
symbol string
interval string
limit *int
startTime *int64
endTime *int64
}
// Symbol set symbol
func (s *KlinesService) Symbol(symbol string) *KlinesService {
s.symbol = symbol
return s
}
// Interval set interval
func (s *KlinesService) Interval(interval string) *KlinesService {
s.interval = interval
return s
}
// Limit set limit
func (s *KlinesService) Limit(limit int) *KlinesService {
s.limit = &limit
return s
}
// StartTime set startTime
func (s *KlinesService) StartTime(startTime int64) *KlinesService {
s.startTime = &startTime
return s
}
// EndTime set endTime
func (s *KlinesService) EndTime(endTime int64) *KlinesService {
s.endTime = &endTime
return s
}
// Do send request
func (s *KlinesService) Do(ctx context.Context, opts ...RequestOption) (res []*Kline, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v3/klines",
}
r.setParam("symbol", s.symbol)
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 []*Kline{}, err
}
j, err := newJSON(data)
if err != nil {
return []*Kline{}, err
}
num := len(j.MustArray())
res = make([]*Kline, num)
for i := 0; i < num; i++ {
item := j.GetIndex(i)
if len(item.MustArray()) < 11 {
err = fmt.Errorf("invalid kline response")
return []*Kline{}, err
}
res[i] = &Kline{
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
}
// Kline define kline info
type Kline 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"`
}