-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrades.go
64 lines (53 loc) · 1.38 KB
/
trades.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
package binance
import (
"encoding/json"
"strconv"
)
type Trade struct {
Id int
Price float64 `json:"price,string"`
Quantity float64 `json:"qty,string"`
Time int `json:"time"`
IsBuyerMaker bool `json:"isBuyerMaker"`
IsBestMatch bool `json:"isBestMatch"`
}
type tradesQuery struct {
symbol string
limit int
fromId int
}
// Returns the required query for the Trades endpoint.
func NewTradesQuery(symbol string) *tradesQuery {
return &tradesQuery{
symbol: symbol,
limit: 500,
}
}
// Sets the optional limit parameter that by default is 500.
func (t *tradesQuery) Limit(limit int) *tradesQuery {
t.limit = limit
return t
}
// TradeId to fetch from. Default gets most recent trades.
func (t *tradesQuery) FromId(fromId int) *tradesQuery {
t.fromId = fromId
return t
}
func parseTradesResponse(jsonContent []byte) ([]Trade, error) {
response := make([]Trade, 0)
err := json.Unmarshal(jsonContent, &response)
return response, err
}
func (sdk *Sdk) Trades(query *tradesQuery) ([]Trade, error) {
request := newRequest("GET", "/api/v1/historicalTrades").
Param("symbol", query.symbol).
Param("limit", strconv.Itoa(query.limit))
if query.fromId > 0 {
request.Param("fromId", strconv.Itoa(query.fromId))
}
responseContent, err := sdk.client.Do(request)
if err != nil {
return nil, err
}
return parseTradesResponse(responseContent)
}