2023-07-21 09:05:19 +00:00
|
|
|
package okexapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-10 08:39:52 +00:00
|
|
|
"fmt"
|
2023-07-21 09:05:19 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2024-01-10 08:39:52 +00:00
|
|
|
"github.com/google/uuid"
|
2023-07-21 09:05:19 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getTestClientOrSkip(t *testing.T) *RestClient {
|
|
|
|
if b, _ := strconv.ParseBool(os.Getenv("CI")); b {
|
|
|
|
t.Skip("skip test for CI")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, secret, passphrase, ok := testutil.IntegrationTestWithPassphraseConfigured(t, "OKEX")
|
|
|
|
if !ok {
|
2023-08-21 07:31:30 +00:00
|
|
|
t.Skip("Please configure all credentials about OKEX")
|
2023-07-21 09:05:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-06 13:21:13 +00:00
|
|
|
client := NewClient()
|
2023-07-21 09:05:19 +00:00
|
|
|
client.Auth(key, secret, passphrase)
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetInstrumentsRequest(t *testing.T) {
|
2023-09-06 13:21:13 +00:00
|
|
|
client := NewClient()
|
2023-07-21 09:05:19 +00:00
|
|
|
ctx := context.Background()
|
2024-01-09 03:55:49 +00:00
|
|
|
req := client.NewGetInstrumentsInfoRequest()
|
2023-07-21 09:05:19 +00:00
|
|
|
|
2024-01-09 03:55:49 +00:00
|
|
|
instruments, err := req.Do(ctx)
|
2023-07-21 09:05:19 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, instruments)
|
|
|
|
t.Logf("instruments: %+v", instruments)
|
|
|
|
}
|
|
|
|
|
2024-01-09 05:57:03 +00:00
|
|
|
func TestClient_GetMarketTickers(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
ctx := context.Background()
|
|
|
|
req := client.NewGetTickersRequest()
|
|
|
|
|
|
|
|
tickers, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, tickers)
|
|
|
|
t.Logf("tickers: %+v", tickers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetMarketTicker(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
ctx := context.Background()
|
|
|
|
req := client.NewGetTickerRequest().InstId("BTC-USDT")
|
|
|
|
|
|
|
|
tickers, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, tickers)
|
|
|
|
t.Logf("tickers: %+v", tickers)
|
|
|
|
}
|
|
|
|
|
2024-01-09 06:20:56 +00:00
|
|
|
func TestClient_GetAcountInfo(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
req := client.NewGetAccountInfoRequest()
|
|
|
|
|
|
|
|
acct, err := req.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, acct)
|
|
|
|
t.Logf("acct: %+v", acct)
|
|
|
|
}
|
|
|
|
|
2023-07-21 09:05:19 +00:00
|
|
|
func TestClient_GetFundingRateRequest(t *testing.T) {
|
2023-09-06 13:21:13 +00:00
|
|
|
client := NewClient()
|
2023-07-21 09:05:19 +00:00
|
|
|
ctx := context.Background()
|
2023-09-06 11:14:21 +00:00
|
|
|
req := client.NewGetFundingRate()
|
2023-07-21 09:05:19 +00:00
|
|
|
|
|
|
|
instrument, err := req.
|
|
|
|
InstrumentID("BTC-USDT-SWAP").
|
|
|
|
Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, instrument)
|
|
|
|
t.Logf("instrument: %+v", instrument)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_PlaceOrderRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
2023-09-06 11:14:21 +00:00
|
|
|
req := client.NewPlaceOrderRequest()
|
2023-07-21 09:05:19 +00:00
|
|
|
|
|
|
|
order, err := req.
|
2023-08-11 01:28:58 +00:00
|
|
|
InstrumentID("BTC-USDT").
|
2024-01-10 02:31:27 +00:00
|
|
|
TradeMode(TradeModeCash).
|
|
|
|
Side(SideTypeSell).
|
2023-07-21 09:05:19 +00:00
|
|
|
OrderType(OrderTypeLimit).
|
2024-01-10 02:31:27 +00:00
|
|
|
TargetCurrency(TargetCurrencyBase).
|
|
|
|
Price("48000").
|
|
|
|
Size("0.001").
|
2023-07-21 09:05:19 +00:00
|
|
|
Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, order)
|
2023-08-11 01:28:58 +00:00
|
|
|
t.Logf("place order: %+v", order)
|
2024-01-10 02:31:27 +00:00
|
|
|
|
|
|
|
c := client.NewGetOrderDetailsRequest().OrderID(order[0].OrderID).InstrumentID("BTC-USDT")
|
|
|
|
res, err := c.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Log(res)
|
2023-07-21 09:05:19 +00:00
|
|
|
}
|
2023-08-09 07:05:26 +00:00
|
|
|
|
2024-01-10 08:39:52 +00:00
|
|
|
func TestClient_CancelOrderRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
req := client.NewPlaceOrderRequest()
|
|
|
|
clientId := fmt.Sprintf("%d", uuid.New().ID())
|
|
|
|
|
|
|
|
order, err := req.
|
|
|
|
InstrumentID("BTC-USDT").
|
|
|
|
TradeMode(TradeModeCash).
|
|
|
|
Side(SideTypeSell).
|
|
|
|
OrderType(OrderTypeLimit).
|
|
|
|
TargetCurrency(TargetCurrencyBase).
|
|
|
|
ClientOrderID(clientId).
|
|
|
|
Price("48000").
|
|
|
|
Size("0.001").
|
|
|
|
Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, order)
|
|
|
|
t.Logf("place order: %+v", order)
|
|
|
|
|
|
|
|
c := client.NewGetOrderDetailsRequest().ClientOrderID(clientId).InstrumentID("BTC-USDT")
|
|
|
|
res, err := c.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Log(res)
|
|
|
|
|
|
|
|
cancelResp, err := client.NewCancelOrderRequest().ClientOrderID(clientId).InstrumentID("BTC-USDT").Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Log(cancelResp)
|
|
|
|
}
|
|
|
|
|
2024-01-11 08:41:42 +00:00
|
|
|
func TestClient_OpenOrdersRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
orders := []OpenOrder{}
|
|
|
|
beforeId := int64(0)
|
|
|
|
for {
|
|
|
|
c := client.NewGetOpenOrdersRequest().InstrumentID("BTC-USDT").Limit("1").After(fmt.Sprintf("%d", beforeId))
|
|
|
|
res, err := c.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if len(res) != 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
orders = append(orders, res...)
|
|
|
|
beforeId = int64(res[0].OrderId)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log(orders)
|
|
|
|
}
|
|
|
|
|
2024-01-10 08:39:52 +00:00
|
|
|
func TestClient_BatchCancelOrderRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
req := client.NewPlaceOrderRequest()
|
|
|
|
clientId := fmt.Sprintf("%d", uuid.New().ID())
|
|
|
|
|
|
|
|
order, err := req.
|
|
|
|
InstrumentID("BTC-USDT").
|
|
|
|
TradeMode(TradeModeCash).
|
|
|
|
Side(SideTypeSell).
|
|
|
|
OrderType(OrderTypeLimit).
|
|
|
|
TargetCurrency(TargetCurrencyBase).
|
|
|
|
ClientOrderID(clientId).
|
|
|
|
Price("48000").
|
|
|
|
Size("0.001").
|
|
|
|
Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, order)
|
|
|
|
t.Logf("place order: %+v", order)
|
|
|
|
|
|
|
|
c := client.NewGetOrderDetailsRequest().ClientOrderID(clientId).InstrumentID("BTC-USDT")
|
|
|
|
res, err := c.Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Log(res)
|
|
|
|
|
|
|
|
cancelResp, err := client.NewBatchCancelOrderRequest().Add(&CancelOrderRequest{instrumentID: "BTC-USDT", clientOrderID: &clientId}).Do(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Log(cancelResp)
|
|
|
|
}
|
|
|
|
|
2023-08-09 07:05:26 +00:00
|
|
|
func TestClient_GetOrderDetailsRequest(t *testing.T) {
|
|
|
|
client := getTestClientOrSkip(t)
|
|
|
|
ctx := context.Background()
|
2023-09-06 11:14:21 +00:00
|
|
|
req := client.NewGetOrderDetailsRequest()
|
2023-08-09 07:05:26 +00:00
|
|
|
|
|
|
|
orderDetail, err := req.
|
|
|
|
InstrumentID("BTC-USDT").
|
2023-08-11 01:28:58 +00:00
|
|
|
OrderID("609869603774656544").
|
2023-08-09 07:05:26 +00:00
|
|
|
Do(ctx)
|
2023-08-11 01:28:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, orderDetail)
|
|
|
|
t.Logf("order detail: %+v", orderDetail)
|
2023-08-09 07:05:26 +00:00
|
|
|
}
|