-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
market.go
268 lines (213 loc) · 5.37 KB
/
market.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
package okexapi
import (
"context"
"fmt"
"net/url"
"strconv"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
type Candle struct {
InstrumentID string
Interval string
Time time.Time
Open fixedpoint.Value
High fixedpoint.Value
Low fixedpoint.Value
Close fixedpoint.Value
Volume fixedpoint.Value
VolumeInCurrency fixedpoint.Value
}
type CandlesticksRequest struct {
client *RestClient
instId string
limit *int
bar *string
after *int64
before *int64
}
func (r *CandlesticksRequest) After(after int64) *CandlesticksRequest {
r.after = &after
return r
}
func (r *CandlesticksRequest) Before(before int64) *CandlesticksRequest {
r.before = &before
return r
}
func (r *CandlesticksRequest) Bar(bar string) *CandlesticksRequest {
r.bar = &bar
return r
}
func (r *CandlesticksRequest) Limit(limit int) *CandlesticksRequest {
r.limit = &limit
return r
}
func (r *CandlesticksRequest) InstrumentID(instId string) *CandlesticksRequest {
r.instId = instId
return r
}
func (r *CandlesticksRequest) Do(ctx context.Context) ([]Candle, error) {
// SPOT, SWAP, FUTURES, OPTION
var params = url.Values{}
params.Add("instId", r.instId)
if r.bar != nil {
params.Add("bar", *r.bar)
}
if r.before != nil {
params.Add("before", strconv.FormatInt(*r.before, 10))
}
if r.after != nil {
params.Add("after", strconv.FormatInt(*r.after, 10))
}
if r.limit != nil {
params.Add("limit", strconv.Itoa(*r.limit))
}
req, err := r.client.newRequest("GET", "/api/v5/market/candles", params, nil)
if err != nil {
return nil, err
}
resp, err := r.client.sendRequest(req)
if err != nil {
return nil, err
}
type candleEntry [7]string
var candlesResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data []candleEntry `json:"data"`
}
if err := resp.DecodeJSON(&candlesResponse); err != nil {
return nil, err
}
var candles []Candle
for _, entry := range candlesResponse.Data {
timestamp, err := strconv.ParseInt(entry[0], 10, 64)
if err != nil {
return candles, err
}
open, err := fixedpoint.NewFromString(entry[1])
if err != nil {
return candles, err
}
high, err := fixedpoint.NewFromString(entry[2])
if err != nil {
return candles, err
}
low, err := fixedpoint.NewFromString(entry[3])
if err != nil {
return candles, err
}
cls, err := fixedpoint.NewFromString(entry[4])
if err != nil {
return candles, err
}
vol, err := fixedpoint.NewFromString(entry[5])
if err != nil {
return candles, err
}
volCcy, err := fixedpoint.NewFromString(entry[6])
if err != nil {
return candles, err
}
var interval = "1m"
if r.bar != nil {
interval = *r.bar
}
candles = append(candles, Candle{
InstrumentID: r.instId,
Interval: interval,
Time: time.Unix(0, timestamp*int64(time.Millisecond)),
Open: open,
High: high,
Low: low,
Close: cls,
Volume: vol,
VolumeInCurrency: volCcy,
})
}
return candles, nil
}
type MarketTickersRequest struct {
client *RestClient
instType string
}
func (r *MarketTickersRequest) InstrumentType(instType string) *MarketTickersRequest {
r.instType = instType
return r
}
func (r *MarketTickersRequest) Do(ctx context.Context) ([]MarketTicker, error) {
// SPOT, SWAP, FUTURES, OPTION
var params = url.Values{}
params.Add("instType", string(r.instType))
req, err := r.client.newRequest("GET", "/api/v5/market/tickers", params, nil)
if err != nil {
return nil, err
}
response, err := r.client.sendRequest(req)
if err != nil {
return nil, err
}
var tickerResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data []MarketTicker `json:"data"`
}
if err := response.DecodeJSON(&tickerResponse); err != nil {
return nil, err
}
return tickerResponse.Data, nil
}
type MarketTickerRequest struct {
client *RestClient
instId string
}
func (r *MarketTickerRequest) InstrumentID(instId string) *MarketTickerRequest {
r.instId = instId
return r
}
func (r *MarketTickerRequest) Do(ctx context.Context) (*MarketTicker, error) {
// SPOT, SWAP, FUTURES, OPTION
var params = url.Values{}
params.Add("instId", r.instId)
req, err := r.client.newRequest("GET", "/api/v5/market/ticker", params, nil)
if err != nil {
return nil, err
}
response, err := r.client.sendRequest(req)
if err != nil {
return nil, err
}
var tickerResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data []MarketTicker `json:"data"`
}
if err := response.DecodeJSON(&tickerResponse); err != nil {
return nil, err
}
if len(tickerResponse.Data) == 0 {
return nil, fmt.Errorf("ticker of %s not found", r.instId)
}
return &tickerResponse.Data[0], nil
}
type MarketDataService struct {
client *RestClient
}
func (c *MarketDataService) NewMarketTickerRequest(instId string) *MarketTickerRequest {
return &MarketTickerRequest{
client: c.client,
instId: instId,
}
}
func (c *MarketDataService) NewMarketTickersRequest(instType string) *MarketTickersRequest {
return &MarketTickersRequest{
client: c.client,
instType: instType,
}
}
func (c *MarketDataService) NewCandlesticksRequest(instId string) *CandlesticksRequest {
return &CandlesticksRequest{
client: c.client,
instId: instId,
}
}