forked from ccxt/go-binance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrades_service.go
122 lines (109 loc) · 2.59 KB
/
trades_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
package eoptions
import (
"context"
"encoding/json"
"net/http"
)
type Trade struct {
Id uint64 `json:"id"`
TradeId int `json:"tradeId"`
Symbol string `json:"symbol"`
Price string `json:"price"`
Qty string `json:"qty"`
QuoteQty string `json:"quoteQty"`
Side int `json:"side"`
Time uint64 `json:"time"`
}
// TradesService list recent trades in orderbook
type TradesService struct {
c *Client
symbol string
limit *int
}
// Symbol set symbol
func (s *TradesService) Symbol(symbol string) *TradesService {
s.symbol = symbol
return s
}
// Limit set limit
func (s *TradesService) Limit(limit int) *TradesService {
s.limit = &limit
return s
}
// Do send request
func (s *TradesService) Do(ctx context.Context, opts ...RequestOption) (res []*Trade, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/eapi/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
}
// 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 []*HistoricalTrade, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/eapi/v1/historicalTrades",
}
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 []*HistoricalTrade{}, err
}
res = make([]*HistoricalTrade, 0)
err = json.Unmarshal(data, &res)
if err != nil {
return []*HistoricalTrade{}, err
}
return res, nil
}
// HistoricalTrade define historical trade info
type HistoricalTrade struct {
Id uint64 `json:"id"`
TradeId int `json:"tradeId"`
Price string `json:"price"`
Qty string `json:"qty"`
QuoteQty string `json:"quoteQty"`
Side int `json:"side"`
Time uint64 `json:"time"`
}