forked from adshao/go-binance
/
ticker_service.go
291 lines (261 loc) · 7.36 KB
/
ticker_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
package binance
import (
"context"
"github.com/goccy/go-json"
"net/http"
"github.com/mmavka/go-binance/v2/common"
)
// ListBookTickersService list best price/qty on the order book for a symbol or symbols
type ListBookTickersService struct {
c *Client
symbol *string
}
// Symbol set symbol
func (s *ListBookTickersService) Symbol(symbol string) *ListBookTickersService {
s.symbol = &symbol
return s
}
// Do send request
func (s *ListBookTickersService) Do(ctx context.Context, opts ...RequestOption) (res []*BookTicker, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v3/ticker/bookTicker",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
}
data, err := s.c.callAPI(ctx, r, opts...)
data = common.ToJSONList(data)
if err != nil {
return []*BookTicker{}, err
}
res = make([]*BookTicker, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*BookTicker{}, err
}
return res, nil
}
// BookTicker define book ticker info
type BookTicker struct {
Symbol string `json:"symbol"`
BidPrice string `json:"bidPrice"`
BidQuantity string `json:"bidQty"`
AskPrice string `json:"askPrice"`
AskQuantity string `json:"askQty"`
}
// ListPricesService list latest price for a symbol or symbols
type ListPricesService struct {
c *Client
symbol *string
symbols []string
}
// Symbol set symbol
func (s *ListPricesService) Symbol(symbol string) *ListPricesService {
s.symbol = &symbol
return s
}
// Do send request
func (s *ListPricesService) Do(ctx context.Context, opts ...RequestOption) (res []*SymbolPrice, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v3/ticker/price",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
} else if s.symbols != nil {
s, _ := json.Marshal(s.symbols)
r.setParam("symbols", string(s))
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*SymbolPrice{}, err
}
data = common.ToJSONList(data)
res = make([]*SymbolPrice, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*SymbolPrice{}, err
}
return res, nil
}
// SymbolPrice define symbol and price pair
type SymbolPrice struct {
Symbol string `json:"symbol"`
Price string `json:"price"`
}
// ListPriceChangeStatsService show stats of price change in last 24 hours for all symbols
type ListPriceChangeStatsService struct {
c *Client
symbol *string
symbols []string
}
// Symbol set symbol
func (s *ListPriceChangeStatsService) Symbol(symbol string) *ListPriceChangeStatsService {
s.symbol = &symbol
return s
}
// Symbols set symbols
func (s *ListPriceChangeStatsService) Symbols(symbols []string) *ListPriceChangeStatsService {
s.symbols = symbols
return s
}
// Symbols set symbols
func (s *ListPricesService) Symbols(symbols []string) *ListPricesService {
s.symbols = symbols
return s
}
// Do send request
func (s *ListPriceChangeStatsService) Do(ctx context.Context, opts ...RequestOption) (res []*PriceChangeStats, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v3/ticker/24hr",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
} else if s.symbols != nil {
r.setParam("symbols", s.symbols)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return res, err
}
data = common.ToJSONList(data)
res = make([]*PriceChangeStats, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res, nil
}
// PriceChangeStats define price change stats
type PriceChangeStats struct {
Symbol string `json:"symbol"`
PriceChange string `json:"priceChange"`
PriceChangePercent string `json:"priceChangePercent"`
WeightedAvgPrice string `json:"weightedAvgPrice"`
PrevClosePrice string `json:"prevClosePrice"`
LastPrice string `json:"lastPrice"`
LastQty string `json:"lastQty"`
BidPrice string `json:"bidPrice"`
BidQty string `json:"bidQty"`
AskPrice string `json:"askPrice"`
AskQty string `json:"askQty"`
OpenPrice string `json:"openPrice"`
HighPrice string `json:"highPrice"`
LowPrice string `json:"lowPrice"`
Volume string `json:"volume"`
QuoteVolume string `json:"quoteVolume"`
OpenTime int64 `json:"openTime"`
CloseTime int64 `json:"closeTime"`
FirstID int64 `json:"firstId"`
LastID int64 `json:"lastId"`
Count int64 `json:"count"`
}
// AveragePriceService show current average price for a symbol
type AveragePriceService struct {
c *Client
symbol string
}
// Symbol set symbol
func (s *AveragePriceService) Symbol(symbol string) *AveragePriceService {
s.symbol = symbol
return s
}
// Do send request
func (s *AveragePriceService) Do(ctx context.Context, opts ...RequestOption) (res *AvgPrice, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v3/avgPrice",
}
r.setParam("symbol", s.symbol)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return res, err
}
res = new(AvgPrice)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// AvgPrice define average price
type AvgPrice struct {
Mins int64 `json:"mins"`
Price string `json:"price"`
}
type ListSymbolTickerService struct {
c *Client
symbol *string
symbols []string
windowSize *string
}
type SymbolTicker struct {
Symbol string `json:"symbol"`
PriceChange string `json:"priceChange"`
PriceChangePercent string `json:"priceChangePercent"`
WeightedAvgPrice string `json:"weightedAvgPrice"`
OpenPrice string `json:"openPrice"`
HighPrice string `json:"highPrice"`
LowPrice string `json:"lowPrice"`
LastPrice string `json:"lastPrice"`
Volume string `json:"volume"`
QuoteVolume string `json:"quoteVolume"`
OpenTime int64 `json:"openTime"`
CloseTime int64 `json:"closeTime"`
FirstId int64 `json:"firstId"`
LastId int64 `json:"lastId"`
Count int64 `json:"count"`
}
func (s *ListSymbolTickerService) Symbol(symbol string) *ListSymbolTickerService {
s.symbol = &symbol
return s
}
func (s *ListSymbolTickerService) Symbols(symbols []string) *ListSymbolTickerService {
s.symbols = symbols
return s
}
// Defaults to 1d if no parameter provided
//
// Supported windowSize values:
//
// - 1m,2m....59m for minutes
//
// - 1h, 2h....23h - for hours
//
// - 1d...7d - for days
//
// Units cannot be combined (e.g. 1d2h is not allowed).
//
// Reference: https://binance-docs.github.io/apidocs/spot/en/#rolling-window-price-change-statistics
func (s *ListSymbolTickerService) WindowSize(windowSize string) *ListSymbolTickerService {
s.windowSize = &windowSize
return s
}
func (s *ListSymbolTickerService) Do(ctx context.Context, opts ...RequestOption) (res []*SymbolTicker, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v3/ticker",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
} else if s.symbols != nil {
s, _ := json.Marshal(s.symbols)
r.setParam("symbols", string(s))
}
if s.windowSize != nil {
r.setParam("windowSize", *s.windowSize)
}
data, err := s.c.callAPI(ctx, r, opts...)
data = common.ToJSONList(data)
if err != nil {
return []*SymbolTicker{}, err
}
res = make([]*SymbolTicker, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*SymbolTicker{}, err
}
return res, nil
}