-
Notifications
You must be signed in to change notification settings - Fork 1
/
swap_api.go
541 lines (451 loc) · 14.6 KB
/
swap_api.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package okex
/*
OKEX Swap Api
@author Lingting Fu
@date 2018-12-27
@version 1.0.0
*/
import (
"errors"
"strings"
)
/*
获取某个合约的持仓信息
GET /api/swap/v3/<instrument_id>/position
*/
func (client *Client) GetSwapPositionByInstrument(instrumentId string) (*SwapPosition, error) {
sp := SwapPosition{}
if _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_POSITION, instrumentId), nil, &sp); err != nil {
return nil, err
}
return &sp, nil
}
/*
所有合约持仓信息
获取所有合约的持仓信息
限速规则:1次/10s
GET /api/swap/v3/position
*/
func (client *Client) GetSwapPositions() (*SwapPositionList, error) {
sp := SwapPositionList{}
if _, err := client.Request(GET, SWAP_POSITION, nil, &sp); err != nil {
return nil, err
}
return &sp, nil
}
func (client *Client) getSwapAccounts(uri string) (*SwapAccounts, error) {
sa := SwapAccounts{}
if _, err := client.Request(GET, uri, nil, &sa); err != nil {
return nil, err
}
return &sa, nil
}
/*
获取所有币种合约的账户信息
HTTP请求
GET /api/swap/v3/accounts
*/
func (client *Client) GetSwapAccounts() (*SwapAccounts, error) {
return client.getSwapAccounts(SWAP_ACCOUNTS)
}
/*
单个币种合约账户信息
HTTP请求
GET /api/swap/v3/<instrument_id>/accounts
*/
func (client *Client) GetSwapAccount(instrumentId string) (*SwapAccount, error) {
sa := SwapAccount{}
uri := GetInstrumentIdUri(SWAP_INSTRUMENT_ACCOUNT, instrumentId)
if _, err := client.Request(GET, uri, nil, &sa); err != nil {
return nil, err
}
return &sa, nil
}
/*
获取某个合约的杠杆倍数,持仓模式
HTTP请求
GET /api/swap/v3/accounts/<instrument_id>/settings
*/
func (client *Client) GetSwapAccountsSettingsByInstrument(instrumentId string) (*SwapAccountsSetting, error) {
as := SwapAccountsSetting{}
if _, err := client.Request(GET, GetInstrumentIdUri(SWAP_ACCOUNTS_SETTINGS, instrumentId), nil, &as); err != nil {
return nil, err
}
return &as, nil
}
/*
设定某个合约的杠杆倍数
HTTP请求
POST /api/swap/v3/accounts/<instrument_id>/leverage
*/
func (client *Client) PostSwapAccountsLeverage(instrumentId string, leverage string, side string) (*SwapAccountsSetting, error) {
params := make(map[string]string)
params["leverage"] = leverage
params["side"] = side
as := SwapAccountsSetting{}
if _, err := client.Request(POST, GetInstrumentIdUri(SWAP_ACCOUNTS_LEVERAGE, instrumentId), params, &as); err != nil {
return nil, err
}
return &as, nil
}
/*
账单流水查询
列出账户资产流水,账户资产流水是指导致账户余额增加或减少的行为。流水会分页,每页100条数据,并且按照时间倒序排序和存储,最新的排在最前面。
HTTP请求
GET /api/swap/v3/accounts/<instrument_id>/ledger
*/
func (client *Client) GetSwapAccountLedger(instrumentId string, optionalParams map[string]string) (*SwapAccountsLedgerList, error) {
baseUri := GetInstrumentIdUri(SWAP_ACCOUNTS_LEDGER, instrumentId)
uri := baseUri
if optionalParams != nil {
uri = BuildParams(baseUri, optionalParams)
}
ll := SwapAccountsLedgerList{}
if _, err := client.Request(GET, uri, nil, &ll); err != nil {
return nil, err
}
return &ll, nil
}
/*
API交易提供限价单下单模式,只有当您的账户有足够的资金才能下单。一旦下单,您的账户资金将在订单生命周期内被冻结,被冻结的资金以及数量取决于订单指定的类型和参数。
HTTP请求
POST /api/swap/v3/order
*/
func (client *Client) PostSwapOrder(instrumentId string, order *BasePlaceOrderInfo) (*SwapOrderResult, error) {
or := SwapOrderResult{}
info := PlaceOrderInfo{*order, instrumentId}
if _, err := client.Request(POST, SWAP_ORDER, info, &or); err != nil {
return nil, err
}
return &or, nil
}
/*
批量进行下单请求。
HTTP请求
POST /api/swap/v3/orders
*/
func (client *Client) PostSwapOrders(instrumentId string, orders []*BasePlaceOrderInfo) (*SwapOrdersResult, error) {
sor := SwapOrdersResult{}
orderData := PlaceOrdersInfo{InstrumentId: instrumentId, OrderData: orders}
if _, err := client.Request(POST, SWAP_ORDERS, orderData, &sor); err != nil {
return nil, err
}
return &sor, nil
}
/*
撤销之前下的未完成订单。
HTTP请求
POST /api/swap/v3/cancel_order/<instrument_id>/<order_id>
*/
func (client *Client) PostSwapCancelOrder(instrumentId string, orderId string) (*SwapCancelOrderResult, error) {
uri := "/api/swap/v3/cancel_order/" + instrumentId + "/" + orderId
or := SwapCancelOrderResult{}
if _, err := client.Request(POST, uri, nil, &or); err != nil {
return nil, err
}
return &or, nil
}
/*
批量撤销之前下的未完成订单。
HTTP请求
POST /api/swap/v3/cancel_batch_orders/<instrument_id>
*/
func (client *Client) PostSwapBatchCancelOrders(instrumentId string, orderIds []string) (*SwapCancelOrderResult, error) {
uri := GetInstrumentIdUri(SWAP_CANCEL_BATCH_ORDERS, instrumentId)
or := SwapCancelOrderResult{}
params := map[string]interface{}{}
params["ids"] = orderIds
if _, err := client.Request(POST, uri, params, &or); err != nil {
return nil, err
}
return &or, nil
}
/*
列出您当前所有的订单信息。
HTTP请求
GET /api/swap/v3/orders/<instrument_id>
请求示例
GET /api/swap/v3/orders/BTC-USD-SWAP?status=2&from=4&limit=30
*/
func (client *Client) GetSwapOrderByInstrumentId(instrumentId string, paramMap map[string]string) (*SwapOrdersInfo, error) {
if paramMap["status"] == "" || len(instrumentId) == 0 {
return nil, errors.New("Request Parameter's not correct, instrument_id and status is required.")
}
baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_ORDER_LIST, instrumentId)
kvParams := BuildOrderParams(paramMap)
uri := baseUri + "?" + kvParams
soi := SwapOrdersInfo{}
if _, err := client.Request(GET, uri, nil, &soi); err != nil {
return nil, err
}
return &soi, nil
}
/*
通过订单id获取单个订单信息。
HTTP请求
GET /api/swap/v3/orders/<instrument_id>/<order_id>
请求示例
GET /api/swap/v3/orders/BTC-USD-SWAP/64-2a-26132f931-3
*/
func (client *Client) GetSwapOrderByOrderId(instrumentId string, orderId string) (*BaseOrderInfo, error) {
return client.GetSwapOrderById(instrumentId, orderId)
}
/*
获取订单信息
通过订单id获取单个订单信息。
限速规则:40次/2s
HTTP请求
GET /api/swap/v3/orders/<instrument_id>/<order_id>
or
GET /api/swap/v3/orders/<instrument_id>/<client_oid>
*/
func (client *Client) GetSwapOrderById(instrumentId, orderOrClientId string) (*BaseOrderInfo, error) {
orderInfo := BaseOrderInfo{}
baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_ORDER_BY_ID, instrumentId)
uri := strings.Replace(baseUri, "{order_client_id}", orderOrClientId, -1)
if _, err := client.Request(GET, uri, nil, &orderInfo); err != nil {
return nil, err
}
return &orderInfo, nil
}
/*
获取最近的成交明细列表。
HTTP请求
GET /api/swap/v3/fills
请求示例
GET /api/swap/v3/fills?order_id=64-2b-16122f931-3&instrument_id=BTC-USD-SWAP&from=1&limit=50(返回BTC-USD-SWAP中order_id为64-2b-16122f931-3的订单中第1页前50笔成交信息)
*/
func (client *Client) GetSwapFills(instrumentId string, orderId string, options map[string]string) (interface{}, error) {
m := make(map[string]string)
m["instrument_id"] = instrumentId
m["order_id"] = orderId
m["from"] = options["from"]
m["to"] = options["to"]
m["limit"] = options["limit"]
uri := BuildParams(SWAP_FILLS, m)
sfi := SwapFillsInfo{}
if _, err := client.Request(GET, uri, nil, &sfi); err != nil {
return nil, err
}
return &sfi, nil
}
/*
获取可用合约的列表,这组公开接口提供了行情数据的快照,无需认证即可调用。 获取可用合约的列表,查询各合约的交易限制和价格步长等信息。
HTTP请求
GET /api/swap/v3/instruments
*/
func (client *Client) GetSwapInstruments() (*SwapInstrumentList, error) {
sil := SwapInstrumentList{}
if _, err := client.Request(GET, SWAP_INSTRUMENTS, nil, &sil); err != nil {
return nil, err
}
return &sil, nil
}
/*
获取合约的深度列表。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/depth
请求示例
GET /api/swap/v3/instruments/<instrument_id>/depth?size=50
*/
func (client *Client) GetSwapDepthByInstrumentId(instrumentId string, optionalSize string) (interface{}, error) {
sid := SwapInstrumentDepth{}
baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_DEPTH, instrumentId)
if optionalSize != "" {
baseUri = baseUri + "?size=" + optionalSize
}
if _, err := client.Request(GET, baseUri, nil, &sid); err != nil {
return nil, err
}
return sid, nil
}
/*
获取平台全部合约的最新成交价、买一价、卖一价和24交易量。
HTTP请求
GET /api/swap/v3/instruments/ticker
*/
func (client *Client) GetSwapInstrumentsTicker() (*SwapTickerList, error) {
stl := SwapTickerList{}
if _, err := client.Request(GET, SWAP_INSTRUMENTS_TICKER, nil, &stl); err != nil {
return nil, err
}
return &stl, nil
}
/*
获取合约的最新成交价、买一价、卖一价和24交易量。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/ticker
*/
func (client *Client) GetSwapTickerByInstrument(instrumentId string) (*BaseTickerInfo, error) {
bti := BaseTickerInfo{}
uri := GetInstrumentIdUri(SWAP_INSTRUMENT_TICKER, instrumentId)
if _, err := client.Request(GET, uri, nil, &bti); err != nil {
return nil, err
}
bti.InstrumentId = instrumentId
return &bti, nil
}
/*
获取合约的成交记录。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/trades
请求示例
GET /api/swap/v3/instruments/BTC-USD-SWAP/trades?from=1&limit=50
*/
func (client *Client) GetSwapTradesByInstrument(instrumentId string, optionalParams map[string]string) (*SwapTradeList, error) {
stl := SwapTradeList{}
baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_TRADES, instrumentId)
uri := BuildParams(baseUri, optionalParams)
if _, err := client.Request(GET, uri, nil, &stl); err != nil {
return nil, err
}
return &stl, nil
}
/*
获取合约的K线数据。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/candles
请求示例
GET /api/swap/v3/instruments/BTC-USD-SWAP/candles?start=2018-10-26T02:31:00.000Z&end=2018-10-26T02:55:00.000Z&granularity=60(查询BTC-USD-SWAP的2018年10月26日02点31分到2018年10月26日02点55分的1分钟K线数据)
*/
func (client *Client) GetSwapCandlesByInstrument(instrumentId string, optionalParams map[string]string) (*SwapCandleList, error) {
scl := SwapCandleList{}
baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_CANDLES, instrumentId)
uri := baseUri
if len(optionalParams) > 0 {
uri = BuildParams(baseUri, optionalParams)
}
if _, err := client.Request(GET, uri, nil, &scl); err != nil {
return nil, err
}
return &scl, nil
}
/*
获取币种指数。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/index
请求示例
GET /api/swap/v3/instruments/BTC-USD-SWAP/index
*/
func (client *Client) GetSwapIndexByInstrument(instrumentId string) (*SwapIndexInfo, error) {
sii := SwapIndexInfo{}
if _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_INDEX, instrumentId), nil, &sii); err != nil {
return nil, err
}
return &sii, nil
}
/*
获取合约整个平台的总持仓量。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/open_interest
*/
func (client *Client) GetSwapOpenInterestByInstrument(instrumentId string) (*SwapOpenInterest, error) {
sii := SwapOpenInterest{}
if _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_OPEN_INTEREST, instrumentId), nil, &sii); err != nil {
return nil, err
}
return &sii, nil
}
/*
获取合约当前开仓的最高买价和最低卖价。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/price_limit
*/
func (client *Client) GetSwapPriceLimitByInstrument(instrumentId string) (*SwapPriceLimit, error) {
sii := SwapPriceLimit{}
if _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_PRICE_LIMIT, instrumentId), nil, &sii); err != nil {
return nil, err
}
return &sii, nil
}
/*
获取合约爆仓单。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/liquidation
请求示例
GET /api/swap/v3/instruments/BTC-USD-SWAP/liquidation?status=0&from=1&limit=50
*/
func (client *Client) GetSwapLiquidationByInstrument(instrumentId string, status string, optionalParams map[string]string) (*SwapLiquidationList, error) {
scl := SwapLiquidationList{}
baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_LIQUIDATION, instrumentId)
uri := baseUri
if optionalParams != nil {
optionalParams["status"] = status
uri = BuildParams(baseUri, optionalParams)
} else {
oParams := map[string]string{}
oParams["status"] = status
uri = BuildParams(baseUri, oParams)
}
if _, err := client.Request(GET, uri, nil, &scl); err != nil {
return nil, err
}
return &scl, nil
}
/*
获取合约挂单冻结数量。
HTTP请求
GET /api/swap/v3/accounts/<instrument_id>/holds
*/
func (client *Client) GetSwapAccountsHoldsByInstrument(instrumentId string) (*SwapAccountHolds, error) {
r := SwapAccountHolds{}
if _, err := client.Request(GET, GetInstrumentIdUri(SWAP_ACCOUNTS_HOLDS, instrumentId), nil, &r); err != nil {
return nil, err
}
return &r, nil
}
/*
获取合约下一次的结算时间。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/funding_time
*/
func (client *Client) GetSwapFundingTimeByInstrument(instrumentId string) (*SwapFundingTime, error) {
r := SwapFundingTime{}
if _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_FUNDING_TIME, instrumentId), nil, &r); err != nil {
return nil, err
}
return &r, nil
}
/*
获取合约标记价格。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/mark_price
*/
func (client *Client) GetSwapMarkPriceByInstrument(instrumentId string) (*SwapMarkPrice, error) {
r := SwapMarkPrice{}
if _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_MARK_PRICE, instrumentId), nil, &r); err != nil {
return nil, err
}
return &r, nil
}
/*
获取合约历史资金费率。
HTTP请求
GET /api/swap/v3/instruments/<instrument_id>/historical_funding_rate
请求示例
GET /api/swap/v3/instruments/BTC-USD-SWAP/historical_funding_rate?from=1&limit=50
*/
func (client *Client) GetSwapHistoricalFundingRateByInstrument(instrumentId string, optionalParams map[string]string) (*SwapHistoricalFundingRateList, error) {
r := SwapHistoricalFundingRateList{}
baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_HISTORICAL_FUNDING_RATE, instrumentId)
uri := baseUri
if optionalParams != nil {
uri = BuildParams(baseUri, optionalParams)
}
if _, err := client.Request(GET, uri, nil, &r); err != nil {
return nil, err
}
return &r, nil
}
/*
获取法币汇率。
HTTP请求
GET /api/swap/v3/rate
*/
func (client *Client) GetSwapRate() (*SwapRate, error) {
sr := SwapRate{}
if _, err := client.Request(GET, SWAP_RATE, nil, &sr); err != nil {
return nil, err
}
return &sr, nil
}