github.com/frankrap/okex-api@v1.0.4/futures_test.go (about)

     1  package okex
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  const (
    11  	InstrumentId = "BTC-USD-181228"
    12  	currency     = "BTC"
    13  )
    14  
    15  /*
    16   OKEX general api's testing
    17  */
    18  func TestGetServerTime(t *testing.T) {
    19  	serverTime, err := NewTestClient().GetServerTime()
    20  	if err != nil {
    21  		t.Error(err)
    22  	}
    23  	FmtPrintln("OKEX's server time: ", serverTime)
    24  }
    25  
    26  func TestGetFuturesExchangeRate(t *testing.T) {
    27  	exchangeRate, err := NewTestClient().GetFuturesExchangeRate()
    28  	if err != nil {
    29  		t.Error(err)
    30  	}
    31  	FmtPrintln("Current exchange rate: ", exchangeRate)
    32  }
    33  
    34  /*
    35   Futures market api's testing
    36  */
    37  func TestGetFuturesInstruments(t *testing.T) {
    38  	Instruments, err := NewTestClient().GetFuturesInstruments()
    39  	if err != nil {
    40  		t.Error(err)
    41  	}
    42  	FmtPrintln("Futures Instruments: ", Instruments)
    43  }
    44  
    45  func TestGetFuturesInstrumentsCurrencies(t *testing.T) {
    46  	currencies, err := NewTestClient().GetFuturesInstrumentCurrencies()
    47  	if err != nil {
    48  		t.Error(err)
    49  	}
    50  	FmtPrintln("Futures Instrument currencies: ", currencies)
    51  }
    52  
    53  func TestGetFuturesInstrumentBook(t *testing.T) {
    54  	insId := getValidInstrumentId()
    55  	params := map[string]string{}
    56  	params["size"] = "10"
    57  	book, err := NewTestClient().GetFuturesInstrumentBook(insId, params)
    58  	if err != nil {
    59  		t.Error(err)
    60  	}
    61  	FmtPrintln("Futures Instrument book: ", book)
    62  }
    63  
    64  func TestGetFuturesInstrumentBook2(t *testing.T) {
    65  	params := NewParams()
    66  	params["size"] = "10"
    67  	params["depth"] = "0.1"
    68  	insId := getValidInstrumentId()
    69  	r, err := NewTestClient().GetFuturesInstrumentBook(insId, nil)
    70  
    71  	simpleAssertTrue(r, err, t, false)
    72  }
    73  
    74  func TestGetFuturesInstrumentAllTicker(t *testing.T) {
    75  	tickers, err := NewTestClient().GetFuturesInstrumentAllTicker()
    76  	if err != nil {
    77  		t.Error(err)
    78  	}
    79  	FmtPrintln("Futures Instrument all ticker: ", tickers)
    80  }
    81  
    82  func TestGetFuturesInstrumentTicker(t *testing.T) {
    83  	ticker, err := NewTestClient().GetFuturesInstrumentTicker(InstrumentId)
    84  	if err != nil {
    85  		t.Error(err)
    86  	}
    87  	FmtPrintln("Futures Instrument ticker: ", ticker)
    88  }
    89  
    90  func TestGetFuturesInstrumentTrades(t *testing.T) {
    91  	trades, err := NewTestClient().GetFuturesInstrumentTrades(InstrumentId)
    92  	if err != nil {
    93  		t.Error(err)
    94  	}
    95  	FmtPrintln("Futures Instrument trades: ", trades)
    96  }
    97  
    98  func TestGetFuturesInstrumentCandles(t *testing.T) {
    99  	//start := "2018-06-20T02:31:00Z"
   100  	//end := "2018-06-20T02:55:00Z"
   101  	granularity := CANDLES_1MIN
   102  
   103  	optional := map[string]string{}
   104  	//optional["start"] = start
   105  	//optional["end"] = end
   106  	optional["granularity"] = Int2String(granularity)
   107  
   108  	insId := getValidInstrumentId()
   109  
   110  	candles, err := NewTestClient().GetFuturesInstrumentCandles(insId, optional)
   111  	if err != nil {
   112  		t.Error(err)
   113  	}
   114  	fmt.Println("Futures Instrument candles:")
   115  	for i, outLen := 0, len(candles); i < outLen; i++ {
   116  		candle := candles[i]
   117  		for j, inLen := 0, 7; j < inLen; j++ {
   118  			if j == 0 {
   119  				fmt.Print("timestamp:")
   120  				fmt.Print(candle[j])
   121  			} else if j == 1 {
   122  				fmt.Print(" open:")
   123  				fmt.Print(candle[j])
   124  			} else if j == 2 {
   125  				fmt.Print(" high:")
   126  				fmt.Print(candle[j])
   127  			} else if j == 3 {
   128  				fmt.Print(" low:")
   129  				fmt.Print(candle[j])
   130  			} else if j == 4 {
   131  				fmt.Print(" close:")
   132  				fmt.Print(candle[j])
   133  			} else if j == 5 {
   134  				fmt.Print(" volume:")
   135  				fmt.Print(candle[j])
   136  			} else if j == 6 {
   137  				fmt.Print(" currency_volume:")
   138  				fmt.Println(candle[j])
   139  			}
   140  		}
   141  	}
   142  }
   143  
   144  func TestGetFuturesInstrumentIndex(t *testing.T) {
   145  	index, err := NewTestClient().GetFuturesInstrumentIndex(InstrumentId)
   146  	if err != nil {
   147  		t.Error(err)
   148  	}
   149  	FmtPrintln("Futures Instrument index: ", index)
   150  }
   151  
   152  func TestGetFuturesInstrumentEstimatedPrice(t *testing.T) {
   153  	estimatedPrice, err := NewTestClient().GetFuturesInstrumentEstimatedPrice(InstrumentId)
   154  	if err != nil {
   155  		t.Error(err)
   156  	}
   157  	FmtPrintln("Futures Instrument estimated price: ", estimatedPrice)
   158  }
   159  
   160  func TestGetFuturesInstrumentOpenInterest(t *testing.T) {
   161  	priceLimit, err := NewTestClient().GetFuturesInstrumentOpenInterest(InstrumentId)
   162  	if err != nil {
   163  		t.Error(err)
   164  	}
   165  	FmtPrintln("Futures Instrument open interest: ", priceLimit)
   166  }
   167  
   168  func TestGetFuturesInstrumentPriceLimit(t *testing.T) {
   169  	priceLimit, err := NewTestClient().GetFuturesInstrumentPriceLimit(InstrumentId)
   170  	if err != nil {
   171  		t.Error(err)
   172  	}
   173  	FmtPrintln("Futures Instrument price limit: ", priceLimit)
   174  }
   175  
   176  func TestGetFuturesInstrumentLiquidation(t *testing.T) {
   177  	InstrumentIdx := "EOS-USD-181228"
   178  	status, from, to, limit := 1, 1, 0, 5
   179  	liquidation, err := NewTestClient().GetFuturesInstrumentLiquidation(InstrumentIdx, status, from, to, limit)
   180  	if err != nil {
   181  		t.Error(err)
   182  	}
   183  	FmtPrintln("Futures Instrument liquidation: ", liquidation)
   184  }
   185  
   186  /*
   187   Futures trade api's testing
   188  */
   189  func TestGetFuturesPositions(t *testing.T) {
   190  	position, err := NewTestClient().GetFuturesPositions()
   191  	if err != nil {
   192  		t.Error(err)
   193  	}
   194  	if position.MarginMode == "crossed" {
   195  		FmtPrintln("Futures crossed position: ", position)
   196  	} else if position.MarginMode == "fixed" {
   197  		FmtPrintln("Futures fixed position: ", position)
   198  	} else {
   199  		FmtPrintln("Futures position failed: ", position)
   200  	}
   201  }
   202  
   203  func TestGetFuturesInstrumentPosition(t *testing.T) {
   204  	position, err := NewTestClient().GetFuturesInstrumentPosition(InstrumentId)
   205  	if err != nil {
   206  		t.Error(err)
   207  	}
   208  	if position.MarginMode == "crossed" {
   209  		FmtPrintln("Futures crossed position: ", position)
   210  	}
   211  	if position.MarginMode == "fixed" {
   212  		FmtPrintln("Futures fixed position: ", position)
   213  	} else {
   214  		FmtPrintln("Futures position failed: ", position)
   215  	}
   216  }
   217  
   218  func TestGetFuturesAccounts(t *testing.T) {
   219  	// account, err := NewTestClient().GetFuturesAccounts()
   220  	// if err != nil {
   221  	// 	t.Error(err)
   222  	// }
   223  	// if account.MarginMode == "crossed" {
   224  	// 	FmtPrintln("Futures crossed account: ", account)
   225  	// } else if account.MarginMode == "fixed" {
   226  	// 	FmtPrintln("Futures fixed account: ", account)
   227  	// } else {
   228  	// 	FmtPrintln("Futures account failed: ", account)
   229  	// }
   230  }
   231  
   232  func TestGetFuturesAccountsByCurrency(t *testing.T) {
   233  	currencyAccounts, err := NewTestClient().GetFuturesAccountsByCurrency(currency)
   234  	if err != nil {
   235  		t.Error(err)
   236  	}
   237  	FmtPrintln("Futures currency accounts: ", currencyAccounts)
   238  }
   239  
   240  func TestGetFuturesAccountsLedgerByCurrency(t *testing.T) {
   241  	from, to, limit := 1, 0, 2
   242  	ledger, err := NewTestClient().GetFuturesAccountsLedgerByCurrency(currency, from, to, limit)
   243  	if err != nil {
   244  		t.Error(err)
   245  	}
   246  	FmtPrintln("Futures currency ledger: ", ledger)
   247  }
   248  
   249  func TestGetFuturesAccountsHoldsByInstrumentId(t *testing.T) {
   250  	holds, err := NewTestClient().GetFuturesAccountsHoldsByInstrumentId(InstrumentId)
   251  	if err != nil {
   252  		t.Error(err)
   253  	}
   254  	FmtPrintln("Futures currency holds: ", holds)
   255  }
   256  
   257  func TestFuturesOrder(t *testing.T) {
   258  	var newOrderParams FuturesNewOrderParams
   259  	newOrderParams.ClientOid = "od12345678"
   260  	newOrderParams.InstrumentId = InstrumentId
   261  	newOrderParams.Type = IntToString(OPEN_SHORT)
   262  	newOrderParams.Price = "100000.00"
   263  	newOrderParams.Size = "1"
   264  	newOrderParams.MatchPrice = "0"
   265  	newOrderParams.Leverage = "20"
   266  
   267  	_, result, err := NewTestClient().FuturesOrder(newOrderParams)
   268  	if err != nil {
   269  		t.Error(err)
   270  	}
   271  	FmtPrintln("Futures new order: ", result)
   272  }
   273  
   274  func TestFuturesOrders(t *testing.T) {
   275  	var batchNewOrder FuturesBatchNewOrderParams
   276  	batchNewOrder.InstrumentId = InstrumentId
   277  	batchNewOrder.Leverage = "20"
   278  	var ordersData [5]FuturesBatchNewOrderItem
   279  	for i, loop := 1, 6; i < loop; i++ {
   280  		var item FuturesBatchNewOrderItem
   281  		item.ClientOid = "od" + IntToString(12345670+i)
   282  		item.Type = IntToString(OPEN_SHORT)
   283  		item.Price = IntToString(100000 + i)
   284  		item.Size = "1"
   285  		item.MatchPrice = "0"
   286  		ordersData[i-1] = item
   287  	}
   288  	json, err := Struct2JsonString(ordersData)
   289  	if err != nil {
   290  		t.Error(err)
   291  	}
   292  	batchNewOrder.OrdersData = json
   293  	_, result, err := NewTestClient().FuturesOrders(batchNewOrder)
   294  	if err != nil {
   295  		t.Error(err)
   296  	}
   297  	FmtPrintln("Futures new orders: ", result)
   298  }
   299  
   300  func TestGetFuturesOrders(t *testing.T) {
   301  	status, limit := 0, 5
   302  	after, before := "", ""
   303  	orderList, err := NewTestClient().GetFuturesOrders(InstrumentId, status, after, before, limit)
   304  	if err != nil {
   305  		t.Error(err)
   306  	}
   307  	FmtPrintln("Futures Instrument order list: ", orderList)
   308  }
   309  
   310  func TestGetFuturesOrder(t *testing.T) {
   311  	// orderId := int64(1713584667466752)
   312  	// order, err := NewTestClient().GetFuturesOrder(InstrumentId, orderId)
   313  	// if err != nil {
   314  	// 	t.Error(err)
   315  	// }
   316  	// FmtPrintln("Futures Instrument order: ", order)
   317  }
   318  
   319  func TestBatchCancelFuturesInstrumentOrders(t *testing.T) {
   320  	var orderIds [3]int64
   321  	orderIds[0] = 1713484060138496
   322  	orderIds[1] = 1713484060990464
   323  	orderIds[2] = 1713484061907968
   324  	json, err := Struct2JsonString(orderIds)
   325  	if err != nil {
   326  		t.Error(err)
   327  	}
   328  	_, result, err := NewTestClient().BatchCancelFuturesInstrumentOrders(InstrumentId, json)
   329  	if err != nil {
   330  		t.Error(err)
   331  	}
   332  	FmtPrintln("Futures Instrument batch cancel order: ", result)
   333  }
   334  
   335  func TestCancelFuturesInstrumentOrder(t *testing.T) {
   336  	// orderId := int64(1713484063611904)
   337  	// result, err := NewTestClient().CancelFuturesInstrumentOrder(InstrumentId, orderId)
   338  	// if err != nil {
   339  	// 	t.Error(err)
   340  	// }
   341  	// FmtPrintln("Futures Instrument cancel order: ", result)
   342  }
   343  
   344  func TestGetFuturesFills(t *testing.T) {
   345  	orderId := int64(1713584667466752)
   346  	from, to, limit := 1, 0, 5
   347  	optionals := map[string]int{}
   348  	optionals["from"] = from
   349  	optionals["to"] = to
   350  	optionals["limit"] = limit
   351  	result, err := NewTestClient().GetFuturesFills(InstrumentId, orderId, optionals)
   352  	if err != nil {
   353  		t.Error(err)
   354  	}
   355  	FmtPrintln("Futures Instrument fills: ", result)
   356  }
   357  
   358  func getValidInstrumentId() string {
   359  	c := NewTestClient()
   360  	insList, err := c.GetFuturesInstruments()
   361  	if err == nil {
   362  		return insList[0].InstrumentId
   363  	}
   364  
   365  	return InstrumentId
   366  }
   367  
   368  func TestGetInstrumentMarkPrice(t *testing.T) {
   369  	insId := getValidInstrumentId()
   370  	r, e := NewTestClient().GetInstrumentMarkPrice(insId)
   371  	simpleAssertTrue(r, e, t, false)
   372  	assert.True(t, r.Code == 0)
   373  }
   374  
   375  func TestFuturesAccountsLeverage(t *testing.T) {
   376  	c := NewTestClient()
   377  	r, e := c.GetFuturesAccountsLeverage(currency)
   378  	//assert.True(t, r["code"] == nil)
   379  	simpleAssertTrue(r, e, t, true)
   380  
   381  	// PostFuturesAccountsLeverage. 设定合约账户币种杠杆倍数,注意当前仓位有持仓或者挂单禁止切换杠杆。
   382  	// lingting.fu. 20190225. Cleanup your test env yourself before running test case.
   383  	//
   384  	// The following 2 cases might fail because of
   385  	// 		a. Not satisfying position or order requirements.
   386  	//		b. Invalid Authority
   387  	// Post C1. Full Position
   388  	r, e = c.PostFuturesAccountsLeverage(currency, 10, nil)
   389  	simpleAssertTrue(r, e, t, false)
   390  
   391  	// Post C2. One Position
   392  	params := NewParams()
   393  	params["instrument_id"] = getValidInstrumentId()
   394  	params["direction"] = "long"
   395  	r, e = c.PostFuturesAccountsLeverage(currency, 10, params)
   396  	simpleAssertTrue(r, e, t, false)
   397  }