-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfutures_test.go
More file actions
397 lines (355 loc) · 10.5 KB
/
futures_test.go
File metadata and controls
397 lines (355 loc) · 10.5 KB
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
package okex
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
const (
InstrumentId = "BTC-USD-181228"
currency = "BTC"
)
/*
OKEX general api's testing
*/
func TestGetServerTime(t *testing.T) {
serverTime, err := NewTestClient().GetServerTime()
if err != nil {
t.Error(err)
}
FmtPrintln("OKEX's server time: ", serverTime)
}
func TestGetFuturesExchangeRate(t *testing.T) {
exchangeRate, err := NewTestClient().GetFuturesExchangeRate()
if err != nil {
t.Error(err)
}
FmtPrintln("Current exchange rate: ", exchangeRate)
}
/*
Futures market api's testing
*/
func TestGetFuturesInstruments(t *testing.T) {
Instruments, err := NewTestClient().GetFuturesInstruments()
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instruments: ", Instruments)
}
func TestGetFuturesInstrumentsCurrencies(t *testing.T) {
currencies, err := NewTestClient().GetFuturesInstrumentCurrencies()
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument currencies: ", currencies)
}
func TestGetFuturesInstrumentBook(t *testing.T) {
insId := getValidInstrumentId()
params := map[string]string{}
params["size"] = "10"
book, err := NewTestClient().GetFuturesInstrumentBook(insId, params)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument book: ", book)
}
func TestGetFuturesInstrumentBook2(t *testing.T) {
params := NewParams()
params["size"] = "10"
params["depth"] = "0.1"
insId := getValidInstrumentId()
r, err := NewTestClient().GetFuturesInstrumentBook(insId, nil)
simpleAssertTrue(r, err, t, false)
}
func TestGetFuturesInstrumentAllTicker(t *testing.T) {
tickers, err := NewTestClient().GetFuturesInstrumentAllTicker()
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument all ticker: ", tickers)
}
func TestGetFuturesInstrumentTicker(t *testing.T) {
ticker, err := NewTestClient().GetFuturesInstrumentTicker(InstrumentId)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument ticker: ", ticker)
}
func TestGetFuturesInstrumentTrades(t *testing.T) {
trades, err := NewTestClient().GetFuturesInstrumentTrades(InstrumentId)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument trades: ", trades)
}
func TestGetFuturesInstrumentCandles(t *testing.T) {
//start := "2018-06-20T02:31:00Z"
//end := "2018-06-20T02:55:00Z"
granularity := CANDLES_1MIN
optional := map[string]string{}
//optional["start"] = start
//optional["end"] = end
optional["granularity"] = Int2String(granularity)
insId := getValidInstrumentId()
candles, err := NewTestClient().GetFuturesInstrumentCandles(insId, optional)
if err != nil {
t.Error(err)
}
fmt.Println("Futures Instrument candles:")
for i, outLen := 0, len(candles); i < outLen; i++ {
candle := candles[i]
for j, inLen := 0, 7; j < inLen; j++ {
if j == 0 {
fmt.Print("timestamp:")
fmt.Print(candle[j])
} else if j == 1 {
fmt.Print(" open:")
fmt.Print(candle[j])
} else if j == 2 {
fmt.Print(" high:")
fmt.Print(candle[j])
} else if j == 3 {
fmt.Print(" low:")
fmt.Print(candle[j])
} else if j == 4 {
fmt.Print(" close:")
fmt.Print(candle[j])
} else if j == 5 {
fmt.Print(" volume:")
fmt.Print(candle[j])
} else if j == 6 {
fmt.Print(" currency_volume:")
fmt.Println(candle[j])
}
}
}
}
func TestGetFuturesInstrumentIndex(t *testing.T) {
index, err := NewTestClient().GetFuturesInstrumentIndex(InstrumentId)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument index: ", index)
}
func TestGetFuturesInstrumentEstimatedPrice(t *testing.T) {
estimatedPrice, err := NewTestClient().GetFuturesInstrumentEstimatedPrice(InstrumentId)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument estimated price: ", estimatedPrice)
}
func TestGetFuturesInstrumentOpenInterest(t *testing.T) {
priceLimit, err := NewTestClient().GetFuturesInstrumentOpenInterest(InstrumentId)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument open interest: ", priceLimit)
}
func TestGetFuturesInstrumentPriceLimit(t *testing.T) {
priceLimit, err := NewTestClient().GetFuturesInstrumentPriceLimit(InstrumentId)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument price limit: ", priceLimit)
}
func TestGetFuturesInstrumentLiquidation(t *testing.T) {
InstrumentIdx := "EOS-USD-181228"
status, from, to, limit := 1, 1, 0, 5
liquidation, err := NewTestClient().GetFuturesInstrumentLiquidation(InstrumentIdx, status, from, to, limit)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument liquidation: ", liquidation)
}
/*
Futures trade api's testing
*/
func TestGetFuturesPositions(t *testing.T) {
position, err := NewTestClient().GetFuturesPositions()
if err != nil {
t.Error(err)
}
if position.MarginMode == "crossed" {
FmtPrintln("Futures crossed position: ", position)
} else if position.MarginMode == "fixed" {
FmtPrintln("Futures fixed position: ", position)
} else {
FmtPrintln("Futures position failed: ", position)
}
}
func TestGetFuturesInstrumentPosition(t *testing.T) {
position, err := NewTestClient().GetFuturesInstrumentPosition(InstrumentId)
if err != nil {
t.Error(err)
}
if position.MarginMode == "crossed" {
FmtPrintln("Futures crossed position: ", position)
}
if position.MarginMode == "fixed" {
FmtPrintln("Futures fixed position: ", position)
} else {
FmtPrintln("Futures position failed: ", position)
}
}
func TestGetFuturesAccounts(t *testing.T) {
// account, err := NewTestClient().GetFuturesAccounts()
// if err != nil {
// t.Error(err)
// }
// if account.MarginMode == "crossed" {
// FmtPrintln("Futures crossed account: ", account)
// } else if account.MarginMode == "fixed" {
// FmtPrintln("Futures fixed account: ", account)
// } else {
// FmtPrintln("Futures account failed: ", account)
// }
}
func TestGetFuturesAccountsByCurrency(t *testing.T) {
currencyAccounts, err := NewTestClient().GetFuturesAccountsByCurrency(currency)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures currency accounts: ", currencyAccounts)
}
func TestGetFuturesAccountsLedgerByCurrency(t *testing.T) {
from, to, limit := 1, 0, 2
ledger, err := NewTestClient().GetFuturesAccountsLedgerByCurrency(currency, from, to, limit)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures currency ledger: ", ledger)
}
func TestGetFuturesAccountsHoldsByInstrumentId(t *testing.T) {
holds, err := NewTestClient().GetFuturesAccountsHoldsByInstrumentId(InstrumentId)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures currency holds: ", holds)
}
func TestFuturesOrder(t *testing.T) {
var newOrderParams FuturesNewOrderParams
newOrderParams.ClientOid = "od12345678"
newOrderParams.InstrumentId = InstrumentId
newOrderParams.Type = IntToString(OPEN_SHORT)
newOrderParams.Price = "100000.00"
newOrderParams.Size = "1"
newOrderParams.MatchPrice = "0"
newOrderParams.Leverage = "20"
_, result, err := NewTestClient().FuturesOrder(newOrderParams)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures new order: ", result)
}
func TestFuturesOrders(t *testing.T) {
var batchNewOrder FuturesBatchNewOrderParams
batchNewOrder.InstrumentId = InstrumentId
batchNewOrder.Leverage = "20"
var ordersData [5]FuturesBatchNewOrderItem
for i, loop := 1, 6; i < loop; i++ {
var item FuturesBatchNewOrderItem
item.ClientOid = "od" + IntToString(12345670+i)
item.Type = IntToString(OPEN_SHORT)
item.Price = IntToString(100000 + i)
item.Size = "1"
item.MatchPrice = "0"
ordersData[i-1] = item
}
json, err := Struct2JsonString(ordersData)
if err != nil {
t.Error(err)
}
batchNewOrder.OrdersData = json
_, result, err := NewTestClient().FuturesOrders(batchNewOrder)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures new orders: ", result)
}
func TestGetFuturesOrders(t *testing.T) {
status, limit := 0, 5
after, before := "", ""
orderList, err := NewTestClient().GetFuturesOrders(InstrumentId, status, after, before, limit)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument order list: ", orderList)
}
func TestGetFuturesOrder(t *testing.T) {
// orderId := int64(1713584667466752)
// order, err := NewTestClient().GetFuturesOrder(InstrumentId, orderId)
// if err != nil {
// t.Error(err)
// }
// FmtPrintln("Futures Instrument order: ", order)
}
func TestBatchCancelFuturesInstrumentOrders(t *testing.T) {
var orderIds [3]int64
orderIds[0] = 1713484060138496
orderIds[1] = 1713484060990464
orderIds[2] = 1713484061907968
json, err := Struct2JsonString(orderIds)
if err != nil {
t.Error(err)
}
_, result, err := NewTestClient().BatchCancelFuturesInstrumentOrders(InstrumentId, json)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument batch cancel order: ", result)
}
func TestCancelFuturesInstrumentOrder(t *testing.T) {
// orderId := int64(1713484063611904)
// result, err := NewTestClient().CancelFuturesInstrumentOrder(InstrumentId, orderId)
// if err != nil {
// t.Error(err)
// }
// FmtPrintln("Futures Instrument cancel order: ", result)
}
func TestGetFuturesFills(t *testing.T) {
orderId := int64(1713584667466752)
from, to, limit := 1, 0, 5
optionals := map[string]int{}
optionals["from"] = from
optionals["to"] = to
optionals["limit"] = limit
result, err := NewTestClient().GetFuturesFills(InstrumentId, orderId, optionals)
if err != nil {
t.Error(err)
}
FmtPrintln("Futures Instrument fills: ", result)
}
func getValidInstrumentId() string {
c := NewTestClient()
insList, err := c.GetFuturesInstruments()
if err == nil {
return insList[0].InstrumentId
}
return InstrumentId
}
func TestGetInstrumentMarkPrice(t *testing.T) {
insId := getValidInstrumentId()
r, e := NewTestClient().GetInstrumentMarkPrice(insId)
simpleAssertTrue(r, e, t, false)
assert.True(t, r.Code == 0)
}
func TestFuturesAccountsLeverage(t *testing.T) {
c := NewTestClient()
r, e := c.GetFuturesAccountsLeverage(currency)
//assert.True(t, r["code"] == nil)
simpleAssertTrue(r, e, t, true)
// PostFuturesAccountsLeverage. 设定合约账户币种杠杆倍数,注意当前仓位有持仓或者挂单禁止切换杠杆。
// lingting.fu. 20190225. Cleanup your test env yourself before running test case.
//
// The following 2 cases might fail because of
// a. Not satisfying position or order requirements.
// b. Invalid Authority
// Post C1. Full Position
r, e = c.PostFuturesAccountsLeverage(currency, 10, nil)
simpleAssertTrue(r, e, t, false)
// Post C2. One Position
params := NewParams()
params["instrument_id"] = getValidInstrumentId()
params["direction"] = "long"
r, e = c.PostFuturesAccountsLeverage(currency, 10, params)
simpleAssertTrue(r, e, t, false)
}