forked from adshao/go-binance
/
ticker_service.go
177 lines (158 loc) · 4.26 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
package delivery
import (
"context"
"encoding/json"
"net/http"
)
// ListBookTickersService list best price/qty on the order book for a symbol or symbols.
type ListBookTickersService struct {
c *Client
symbol *string
pair *string
}
// Symbol set symbol.
func (s *ListBookTickersService) Symbol(symbol string) *ListBookTickersService {
s.symbol = &symbol
return s
}
// Pair set pair.
func (s *ListBookTickersService) Pair(pair string) *ListBookTickersService {
s.pair = &pair
return s
}
// Do send request.
func (s *ListBookTickersService) Do(ctx context.Context, opts ...RequestOption) (res []*BookTicker, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/dapi/v1/ticker/bookTicker",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
}
if s.pair != nil {
r.setParam("pair", *s.pair)
}
data, err := s.c.callAPI(ctx, r, opts...)
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"`
Pair string `json:"pair"`
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
pair *string
}
// Symbol set symbol.
func (s *ListPricesService) Symbol(symbol string) *ListPricesService {
s.symbol = &symbol
return s
}
// Pair set pair.
func (s *ListPricesService) Pair(pair string) *ListPricesService {
s.pair = &pair
return s
}
// Do send request.
func (s *ListPricesService) Do(ctx context.Context, opts ...RequestOption) (res []*SymbolPrice, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/dapi/v1/ticker/price",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
}
if s.pair != nil {
r.setParam("pair", *s.pair)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*SymbolPrice{}, err
}
res = make([]*SymbolPrice, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*SymbolPrice{}, err
}
return res, nil
}
// SymbolPrice define symbol, price and pair.
type SymbolPrice struct {
Symbol string `json:"symbol"`
Pair string `json:"ps"`
Price string `json:"price"`
}
// ListPriceChangeStatsService show stats of price change in last 24 hours for single symbol, all symbols or pairs of symbols.
type ListPriceChangeStatsService struct {
c *Client
symbol *string
pair *string
}
// Symbol set symbol.
func (s *ListPriceChangeStatsService) Symbol(symbol string) *ListPriceChangeStatsService {
s.symbol = &symbol
return s
}
// Pair set pair.
func (s *ListPriceChangeStatsService) Pair(pair string) *ListPriceChangeStatsService {
s.pair = &pair
return s
}
// Do send request.
func (s *ListPriceChangeStatsService) Do(ctx context.Context, opts ...RequestOption) (res []*PriceChangeStats, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/dapi/v1/ticker/24hr",
}
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
}
if s.pair != nil {
r.setParam("pair", *s.pair)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return res, err
}
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"`
Pair string `json:"pair"`
PriceChange string `json:"priceChange"`
PriceChangePercent string `json:"priceChangePercent"`
WeightedAvgPrice string `json:"weightedAvgPrice"`
LastPrice string `json:"lastPrice"`
LastQuantity string `json:"lastQty"`
OpenPrice string `json:"openPrice"`
HighPrice string `json:"highPrice"`
LowPrice string `json:"lowPrice"`
Volume string `json:"volume"`
BaseVolume string `json:"baseVolume"`
OpenTime int64 `json:"openTime"`
CloseTime int64 `json:"closeTime"`
FristID int64 `json:"firstId"`
LastID int64 `json:"lastId"`
Count int64 `json:"count"`
}