forked from adshao/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade_service.go
294 lines (265 loc) · 6.43 KB
/
trade_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
package binance
import (
"context"
"net/http"
)
// ListTradesService list trades
type ListTradesService struct {
c *Client
symbol string
startTime *int64
endTime *int64
limit *int
fromID *int64
orderId *int64
}
// Symbol set symbol
func (s *ListTradesService) Symbol(symbol string) *ListTradesService {
s.symbol = symbol
return s
}
// StartTime set starttime
func (s *ListTradesService) StartTime(startTime int64) *ListTradesService {
s.startTime = &startTime
return s
}
// EndTime set endtime
func (s *ListTradesService) EndTime(endTime int64) *ListTradesService {
s.endTime = &endTime
return s
}
// Limit set limit
func (s *ListTradesService) Limit(limit int) *ListTradesService {
s.limit = &limit
return s
}
// FromID set fromID
func (s *ListTradesService) FromID(fromID int64) *ListTradesService {
s.fromID = &fromID
return s
}
// OrderId set OrderId
func (s *ListTradesService) OrderId(OrderId int64) *ListTradesService {
s.orderId = &OrderId
return s
}
// Do send request
func (s *ListTradesService) Do(ctx context.Context, opts ...RequestOption) (res []*TradeV3, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v3/myTrades",
secType: secTypeSigned,
}
r.setParam("symbol", s.symbol)
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)
}
if s.fromID != nil {
r.setParam("fromId", *s.fromID)
}
if s.orderId != nil {
r.setParam("orderId", *s.orderId)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*TradeV3{}, err
}
res = make([]*TradeV3, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*TradeV3{}, err
}
return res, nil
}
// HistoricalTradesService trades
type HistoricalTradesService struct {
c *Client
symbol string
limit *int
fromID *int64
}
// Symbol set symbol
func (s *HistoricalTradesService) Symbol(symbol string) *HistoricalTradesService {
s.symbol = symbol
return s
}
// Limit set limit
func (s *HistoricalTradesService) Limit(limit int) *HistoricalTradesService {
s.limit = &limit
return s
}
// FromID set fromID
func (s *HistoricalTradesService) FromID(fromID int64) *HistoricalTradesService {
s.fromID = &fromID
return s
}
// Do send request
func (s *HistoricalTradesService) Do(ctx context.Context, opts ...RequestOption) (res []*Trade, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v3/historicalTrades",
secType: secTypeAPIKey,
}
r.setParam("symbol", s.symbol)
if s.limit != nil {
r.setParam("limit", *s.limit)
}
if s.fromID != nil {
r.setParam("fromId", *s.fromID)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return
}
res = make([]*Trade, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return
}
return
}
// Trade define trade info
type Trade struct {
ID int64 `json:"id"`
Price string `json:"price"`
Quantity string `json:"qty"`
Time int64 `json:"time"`
IsBuyerMaker bool `json:"isBuyerMaker"`
IsBestMatch bool `json:"isBestMatch"`
IsIsolated bool `json:"isIsolated"`
}
// TradeV3 define v3 trade info
type TradeV3 struct {
ID int64 `json:"id"`
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
OrderListId int64 `json:"orderListId"`
Price string `json:"price"`
Quantity string `json:"qty"`
QuoteQuantity string `json:"quoteQty"`
Commission string `json:"commission"`
CommissionAsset string `json:"commissionAsset"`
Time int64 `json:"time"`
IsBuyer bool `json:"isBuyer"`
IsMaker bool `json:"isMaker"`
IsBestMatch bool `json:"isBestMatch"`
IsIsolated bool `json:"isIsolated"`
}
// AggTradesService list aggregate trades
type AggTradesService struct {
c *Client
symbol string
fromID *int64
startTime *int64
endTime *int64
limit *int
}
// Symbol set symbol
func (s *AggTradesService) Symbol(symbol string) *AggTradesService {
s.symbol = symbol
return s
}
// FromID set fromID
func (s *AggTradesService) FromID(fromID int64) *AggTradesService {
s.fromID = &fromID
return s
}
// StartTime set startTime
func (s *AggTradesService) StartTime(startTime int64) *AggTradesService {
s.startTime = &startTime
return s
}
// EndTime set endTime
func (s *AggTradesService) EndTime(endTime int64) *AggTradesService {
s.endTime = &endTime
return s
}
// Limit set limit
func (s *AggTradesService) Limit(limit int) *AggTradesService {
s.limit = &limit
return s
}
// Do send request
func (s *AggTradesService) Do(ctx context.Context, opts ...RequestOption) (res []*AggTrade, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v3/aggTrades",
}
r.setParam("symbol", s.symbol)
if s.fromID != nil {
r.setParam("fromId", *s.fromID)
}
if s.startTime != nil {
r.setParam("startTime", *s.startTime)
}
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*AggTrade{}, err
}
res = make([]*AggTrade, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*AggTrade{}, err
}
return res, nil
}
// AggTrade define aggregate trade info
type AggTrade struct {
AggTradeID int64 `json:"a"`
Price string `json:"p"`
Quantity string `json:"q"`
FirstTradeID int64 `json:"f"`
LastTradeID int64 `json:"l"`
Timestamp int64 `json:"T"`
IsBuyerMaker bool `json:"m"`
IsBestPriceMatch bool `json:"M"`
}
// RecentTradesService list recent trades
type RecentTradesService struct {
c *Client
symbol string
limit *int
}
// Symbol set symbol
func (s *RecentTradesService) Symbol(symbol string) *RecentTradesService {
s.symbol = symbol
return s
}
// Limit set limit
func (s *RecentTradesService) Limit(limit int) *RecentTradesService {
s.limit = &limit
return s
}
// Do send request
func (s *RecentTradesService) Do(ctx context.Context, opts ...RequestOption) (res []*Trade, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/api/v1/trades",
}
r.setParam("symbol", s.symbol)
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*Trade{}, err
}
res = make([]*Trade, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*Trade{}, err
}
return res, nil
}