2021-05-22 19:34:40 +00:00
|
|
|
package okex
|
|
|
|
|
|
|
|
import (
|
2021-05-25 18:11:02 +00:00
|
|
|
"context"
|
2023-09-08 07:33:09 +00:00
|
|
|
"fmt"
|
2021-05-25 18:11:02 +00:00
|
|
|
"math"
|
2021-05-27 18:45:09 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2021-05-25 18:11:02 +00:00
|
|
|
|
2021-05-27 19:05:59 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-05-22 19:34:40 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-09-08 07:33:09 +00:00
|
|
|
"go.uber.org/multierr"
|
2022-05-03 03:14:53 +00:00
|
|
|
"golang.org/x/time/rate"
|
2022-01-10 17:36:19 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
|
2022-02-03 04:55:25 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-05-03 03:14:53 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2021-05-22 19:34:40 +00:00
|
|
|
)
|
|
|
|
|
2023-09-08 07:33:09 +00:00
|
|
|
// Okex rate limit list in each api document
|
|
|
|
// The default order limiter apply 30 requests per second and a 5 initial bucket
|
|
|
|
// this includes QueryOrder, QueryOrderTrades, SubmitOrder, QueryOpenOrders, CancelOrders
|
|
|
|
// Market data limiter means public api, this includes QueryMarkets, QueryTicker, QueryTickers, QueryKLines
|
|
|
|
var (
|
|
|
|
marketDataLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 5)
|
|
|
|
orderRateLimiter = rate.NewLimiter(rate.Every(300*time.Millisecond), 5)
|
|
|
|
)
|
2022-05-03 03:14:53 +00:00
|
|
|
|
2023-05-17 05:45:38 +00:00
|
|
|
const ID = "okex"
|
|
|
|
|
|
|
|
// PlatformToken is the platform currency of OKEx, pre-allocate static string here
|
|
|
|
const PlatformToken = "OKB"
|
2021-05-25 18:13:59 +00:00
|
|
|
|
2023-09-18 07:55:58 +00:00
|
|
|
const (
|
|
|
|
// Constant For query limit
|
|
|
|
defaultQueryLimit = 100
|
|
|
|
)
|
|
|
|
|
2021-05-22 19:34:40 +00:00
|
|
|
var log = logrus.WithFields(logrus.Fields{
|
2023-05-17 05:45:38 +00:00
|
|
|
"exchange": ID,
|
2021-05-22 19:34:40 +00:00
|
|
|
})
|
|
|
|
|
2023-08-22 09:23:16 +00:00
|
|
|
var ErrSymbolRequired = errors.New("symbol is a required parameter")
|
2023-08-21 07:31:30 +00:00
|
|
|
|
2021-05-22 19:34:40 +00:00
|
|
|
type Exchange struct {
|
2021-05-25 18:11:02 +00:00
|
|
|
key, secret, passphrase string
|
|
|
|
|
|
|
|
client *okexapi.RestClient
|
|
|
|
}
|
|
|
|
|
2023-09-07 03:25:12 +00:00
|
|
|
func New(key, secret, passphrase string) *Exchange {
|
2023-09-06 13:21:13 +00:00
|
|
|
client := okexapi.NewClient()
|
2021-12-08 15:27:01 +00:00
|
|
|
|
|
|
|
if len(key) > 0 && len(secret) > 0 {
|
|
|
|
client.Auth(key, secret, passphrase)
|
|
|
|
}
|
2021-05-25 18:11:02 +00:00
|
|
|
|
|
|
|
return &Exchange{
|
2023-05-17 05:45:38 +00:00
|
|
|
key: key,
|
2021-05-25 18:11:02 +00:00
|
|
|
secret: secret,
|
|
|
|
passphrase: passphrase,
|
2021-05-26 17:07:25 +00:00
|
|
|
client: client,
|
2023-09-07 03:25:12 +00:00
|
|
|
}
|
2021-05-25 18:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) Name() types.ExchangeName {
|
|
|
|
return types.ExchangeOKEx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
|
2023-09-06 11:14:21 +00:00
|
|
|
instruments, err := e.client.NewGetInstrumentsRequest().
|
2021-05-25 18:11:02 +00:00
|
|
|
InstrumentType(okexapi.InstrumentTypeSpot).
|
|
|
|
Do(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
markets := types.MarketMap{}
|
|
|
|
for _, instrument := range instruments {
|
|
|
|
symbol := toGlobalSymbol(instrument.InstrumentID)
|
|
|
|
market := types.Market{
|
2021-05-25 18:13:59 +00:00
|
|
|
Symbol: symbol,
|
|
|
|
LocalSymbol: instrument.InstrumentID,
|
|
|
|
|
2021-05-25 18:11:02 +00:00
|
|
|
QuoteCurrency: instrument.QuoteCurrency,
|
|
|
|
BaseCurrency: instrument.BaseCurrency,
|
|
|
|
|
|
|
|
// convert tick size OKEx to precision
|
|
|
|
PricePrecision: int(-math.Log10(instrument.TickSize.Float64())),
|
|
|
|
VolumePrecision: int(-math.Log10(instrument.LotSize.Float64())),
|
|
|
|
|
|
|
|
// TickSize: OKEx's price tick, for BTC-USDT it's "0.1"
|
2022-02-03 04:55:25 +00:00
|
|
|
TickSize: instrument.TickSize,
|
2021-05-25 18:11:02 +00:00
|
|
|
|
|
|
|
// Quantity step size, for BTC-USDT, it's "0.00000001"
|
2022-02-03 04:55:25 +00:00
|
|
|
StepSize: instrument.LotSize,
|
2021-05-25 18:11:02 +00:00
|
|
|
|
|
|
|
// for BTC-USDT, it's "0.00001"
|
2022-02-03 04:55:25 +00:00
|
|
|
MinQuantity: instrument.MinSize,
|
2021-05-25 18:11:02 +00:00
|
|
|
|
|
|
|
// OKEx does not offer minimal notional, use 1 USD here.
|
2022-02-03 04:55:25 +00:00
|
|
|
MinNotional: fixedpoint.One,
|
|
|
|
MinAmount: fixedpoint.One,
|
2021-05-25 18:11:02 +00:00
|
|
|
}
|
|
|
|
markets[symbol] = market
|
|
|
|
}
|
|
|
|
|
|
|
|
return markets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) {
|
2021-05-25 18:44:03 +00:00
|
|
|
symbol = toLocalSymbol(symbol)
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
marketTicker, err := e.client.MarketTicker(ctx, symbol)
|
2021-05-25 18:44:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-25 19:04:49 +00:00
|
|
|
return toGlobalTicker(*marketTicker), nil
|
2021-05-25 18:11:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 19:04:49 +00:00
|
|
|
func (e *Exchange) QueryTickers(ctx context.Context, symbols ...string) (map[string]types.Ticker, error) {
|
2023-09-06 11:14:21 +00:00
|
|
|
marketTickers, err := e.client.MarketTickers(ctx, okexapi.InstrumentTypeSpot)
|
2021-05-25 19:04:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tickers := make(map[string]types.Ticker)
|
|
|
|
for _, marketTicker := range marketTickers {
|
|
|
|
symbol := toGlobalSymbol(marketTicker.InstrumentID)
|
|
|
|
ticker := toGlobalTicker(marketTicker)
|
|
|
|
tickers[symbol] = *ticker
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(symbols) == 0 {
|
|
|
|
return tickers, nil
|
|
|
|
}
|
|
|
|
|
2021-05-27 07:11:44 +00:00
|
|
|
selectedTickers := make(map[string]types.Ticker, len(symbols))
|
2021-05-25 19:04:49 +00:00
|
|
|
for _, symbol := range symbols {
|
|
|
|
if ticker, ok := tickers[symbol]; ok {
|
|
|
|
selectedTickers[symbol] = ticker
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selectedTickers, nil
|
2021-05-25 18:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) PlatformFeeCurrency() string {
|
2023-05-17 05:45:38 +00:00
|
|
|
return PlatformToken
|
2021-05-22 19:34:40 +00:00
|
|
|
}
|
2021-05-25 19:05:54 +00:00
|
|
|
|
|
|
|
func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
|
2023-09-06 11:14:21 +00:00
|
|
|
accountBalance, err := e.client.AccountBalances(ctx)
|
2021-05-26 16:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var account = types.Account{
|
2023-08-09 06:05:26 +00:00
|
|
|
AccountType: types.AccountTypeSpot,
|
2021-05-26 16:24:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 17:14:11 +00:00
|
|
|
var balanceMap = toGlobalBalance(accountBalance)
|
2021-05-26 16:24:16 +00:00
|
|
|
account.UpdateBalances(balanceMap)
|
|
|
|
return &account, nil
|
2021-05-25 19:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
|
2023-09-06 11:14:21 +00:00
|
|
|
accountBalances, err := e.client.AccountBalances(ctx)
|
2021-05-26 16:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:45:09 +00:00
|
|
|
var balanceMap = toGlobalBalance(accountBalances)
|
2021-05-26 16:24:16 +00:00
|
|
|
return balanceMap, nil
|
2021-05-25 19:05:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (*types.Order, error) {
|
2023-09-06 11:14:21 +00:00
|
|
|
orderReq := e.client.NewPlaceOrderRequest()
|
2021-05-27 18:45:09 +00:00
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
orderType, err := toLocalOrderType(order.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
orderReq.InstrumentID(toLocalSymbol(order.Symbol))
|
|
|
|
orderReq.Side(toLocalSideType(order.Side))
|
2021-05-27 18:45:09 +00:00
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
if order.Market.Symbol != "" {
|
|
|
|
orderReq.Quantity(order.Market.FormatQuantity(order.Quantity))
|
|
|
|
} else {
|
|
|
|
// TODO report error
|
|
|
|
orderReq.Quantity(order.Quantity.FormatString(8))
|
|
|
|
}
|
2021-05-27 18:45:09 +00:00
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
// set price field for limit orders
|
|
|
|
switch order.Type {
|
|
|
|
case types.OrderTypeStopLimit, types.OrderTypeLimit:
|
2022-01-10 17:36:19 +00:00
|
|
|
if order.Market.Symbol != "" {
|
2022-09-09 10:41:06 +00:00
|
|
|
orderReq.Price(order.Market.FormatPrice(order.Price))
|
2021-05-27 18:45:09 +00:00
|
|
|
} else {
|
2022-02-03 04:55:25 +00:00
|
|
|
// TODO report error
|
2022-09-09 10:41:06 +00:00
|
|
|
orderReq.Price(order.Price.FormatString(8))
|
2021-05-27 18:45:09 +00:00
|
|
|
}
|
2022-09-09 10:41:06 +00:00
|
|
|
}
|
2021-05-27 18:45:09 +00:00
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
switch order.TimeInForce {
|
|
|
|
case "FOK":
|
|
|
|
orderReq.OrderType(okexapi.OrderTypeFOK)
|
|
|
|
case "IOC":
|
|
|
|
orderReq.OrderType(okexapi.OrderTypeIOC)
|
|
|
|
default:
|
|
|
|
orderReq.OrderType(orderType)
|
|
|
|
}
|
2021-05-27 18:45:09 +00:00
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
orderHead, err := orderReq.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-05-27 18:45:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
orderID, err := strconv.ParseInt(orderHead.OrderID, 10, 64)
|
2021-05-27 18:45:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
return &types.Order{
|
|
|
|
SubmitOrder: order,
|
|
|
|
Exchange: types.ExchangeOKEx,
|
|
|
|
OrderID: uint64(orderID),
|
|
|
|
Status: types.OrderStatusNew,
|
|
|
|
ExecutedQuantity: fixedpoint.Zero,
|
|
|
|
IsWorking: true,
|
|
|
|
CreationTime: types.Time(time.Now()),
|
|
|
|
UpdateTime: types.Time(time.Now()),
|
|
|
|
IsMargin: false,
|
|
|
|
IsIsolated: false,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
// TODO: move this to batch place orders interface
|
|
|
|
/*
|
|
|
|
batchReq := e.client.TradeService.NewBatchPlaceOrderRequest()
|
|
|
|
batchReq.Add(reqs...)
|
|
|
|
orderHeads, err := batchReq.Do(ctx)
|
2021-05-27 18:45:09 +00:00
|
|
|
if err != nil {
|
2022-09-09 10:41:06 +00:00
|
|
|
return nil, err
|
2021-05-27 18:45:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
for idx, orderHead := range orderHeads {
|
|
|
|
orderID, err := strconv.ParseInt(orderHead.OrderID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return createdOrder, err
|
|
|
|
}
|
|
|
|
|
|
|
|
submitOrder := order[idx]
|
|
|
|
createdOrder = append(createdOrder, types.Order{
|
|
|
|
SubmitOrder: submitOrder,
|
|
|
|
Exchange: types.ExchangeOKEx,
|
|
|
|
OrderID: uint64(orderID),
|
|
|
|
Status: types.OrderStatusNew,
|
|
|
|
ExecutedQuantity: fixedpoint.Zero,
|
|
|
|
IsWorking: true,
|
|
|
|
CreationTime: types.Time(time.Now()),
|
|
|
|
UpdateTime: types.Time(time.Now()),
|
|
|
|
IsMargin: false,
|
|
|
|
IsIsolated: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
*/
|
2021-05-25 19:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
|
2021-05-27 18:45:09 +00:00
|
|
|
instrumentID := toLocalSymbol(symbol)
|
2023-09-06 11:14:21 +00:00
|
|
|
req := e.client.NewGetPendingOrderRequest().InstrumentType(okexapi.InstrumentTypeSpot).InstrumentID(instrumentID)
|
2021-05-27 18:45:09 +00:00
|
|
|
orderDetails, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return orders, err
|
|
|
|
}
|
|
|
|
|
|
|
|
orders, err = toGlobalOrders(orderDetails)
|
|
|
|
return orders, err
|
2021-05-25 19:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error {
|
2021-05-28 15:34:21 +00:00
|
|
|
if len(orders) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:45:09 +00:00
|
|
|
var reqs []*okexapi.CancelOrderRequest
|
|
|
|
for _, order := range orders {
|
2021-05-27 19:05:59 +00:00
|
|
|
if len(order.Symbol) == 0 {
|
2023-08-21 07:31:30 +00:00
|
|
|
return ErrSymbolRequired
|
2021-05-27 19:05:59 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
req := e.client.NewCancelOrderRequest()
|
2021-05-27 19:05:59 +00:00
|
|
|
req.InstrumentID(toLocalSymbol(order.Symbol))
|
2021-05-27 18:45:09 +00:00
|
|
|
req.OrderID(strconv.FormatUint(order.OrderID, 10))
|
2021-05-27 19:05:59 +00:00
|
|
|
if len(order.ClientOrderID) > 0 {
|
|
|
|
req.ClientOrderID(order.ClientOrderID)
|
|
|
|
}
|
2021-05-27 18:45:09 +00:00
|
|
|
reqs = append(reqs, req)
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
batchReq := e.client.NewBatchCancelOrderRequest()
|
2021-05-27 18:45:09 +00:00
|
|
|
batchReq.Add(reqs...)
|
|
|
|
_, err := batchReq.Do(ctx)
|
|
|
|
return err
|
2021-05-25 19:05:54 +00:00
|
|
|
}
|
2021-05-26 16:35:51 +00:00
|
|
|
|
|
|
|
func (e *Exchange) NewStream() types.Stream {
|
2021-05-26 17:07:25 +00:00
|
|
|
return NewStream(e.client)
|
2021-05-26 16:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
|
2022-06-17 04:01:15 +00:00
|
|
|
if err := marketDataLimiter.Wait(ctx); err != nil {
|
2022-05-03 03:14:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-02 04:55:30 +00:00
|
|
|
intervalParam, err := toLocalInterval(interval)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("fail to get interval: %w", err)
|
|
|
|
}
|
2022-05-03 03:14:53 +00:00
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
req := e.client.NewCandlesticksRequest(toLocalSymbol(symbol))
|
2022-05-03 03:14:53 +00:00
|
|
|
req.Bar(intervalParam)
|
2021-05-28 12:51:10 +00:00
|
|
|
|
|
|
|
if options.StartTime != nil {
|
2022-05-03 03:14:53 +00:00
|
|
|
req.After(options.StartTime.Unix())
|
2021-05-28 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.EndTime != nil {
|
2022-05-03 03:14:53 +00:00
|
|
|
req.Before(options.EndTime.Unix())
|
2021-05-28 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
candles, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var klines []types.KLine
|
|
|
|
for _, candle := range candles {
|
|
|
|
klines = append(klines, types.KLine{
|
|
|
|
Exchange: types.ExchangeOKEx,
|
|
|
|
Symbol: symbol,
|
|
|
|
Interval: interval,
|
2022-02-02 11:37:18 +00:00
|
|
|
Open: candle.Open,
|
|
|
|
High: candle.High,
|
|
|
|
Low: candle.Low,
|
|
|
|
Close: candle.Close,
|
2021-05-28 12:51:10 +00:00
|
|
|
Closed: true,
|
2022-02-02 11:37:18 +00:00
|
|
|
Volume: candle.Volume,
|
|
|
|
QuoteVolume: candle.VolumeInCurrency,
|
2021-12-15 05:04:01 +00:00
|
|
|
StartTime: types.Time(candle.Time),
|
|
|
|
EndTime: types.Time(candle.Time.Add(interval.Duration() - time.Millisecond)),
|
2021-05-28 12:51:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return klines, nil
|
|
|
|
|
2021-05-26 16:35:51 +00:00
|
|
|
}
|
2023-08-09 07:05:26 +00:00
|
|
|
|
|
|
|
func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.Order, error) {
|
|
|
|
if len(q.Symbol) == 0 {
|
2023-08-21 07:31:30 +00:00
|
|
|
return nil, ErrSymbolRequired
|
2023-08-09 07:05:26 +00:00
|
|
|
}
|
|
|
|
if len(q.OrderID) == 0 && len(q.ClientOrderID) == 0 {
|
2023-08-21 07:31:30 +00:00
|
|
|
return nil, errors.New("okex.QueryOrder: OrderId or ClientOrderId is required parameter")
|
2023-08-09 07:05:26 +00:00
|
|
|
}
|
2023-09-06 11:14:21 +00:00
|
|
|
req := e.client.NewGetOrderDetailsRequest()
|
2023-09-18 07:55:58 +00:00
|
|
|
req.InstrumentID(toLocalSymbol(q.Symbol)).
|
2023-08-09 07:05:26 +00:00
|
|
|
OrderID(q.OrderID).
|
|
|
|
ClientOrderID(q.ClientOrderID)
|
|
|
|
|
|
|
|
var order *okexapi.OrderDetails
|
2023-08-11 01:28:58 +00:00
|
|
|
order, err := req.Do(ctx)
|
2023-08-09 07:05:26 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-21 07:31:30 +00:00
|
|
|
return toGlobalOrder(order)
|
2023-08-09 07:05:26 +00:00
|
|
|
}
|
2023-09-08 07:33:09 +00:00
|
|
|
|
|
|
|
// Query order trades can query trades in last 3 months.
|
|
|
|
func (e *Exchange) QueryOrderTrades(ctx context.Context, q types.OrderQuery) ([]types.Trade, error) {
|
|
|
|
if len(q.ClientOrderID) != 0 {
|
|
|
|
log.Warn("!!!OKEX EXCHANGE API NOTICE!!! Okex does not support searching for trades using OrderClientId.")
|
|
|
|
}
|
|
|
|
|
2023-09-18 07:55:58 +00:00
|
|
|
req := e.client.NewGetTransactionHistoryRequest()
|
2023-09-08 07:33:09 +00:00
|
|
|
if len(q.Symbol) != 0 {
|
2023-09-18 07:55:58 +00:00
|
|
|
req.InstrumentID(toLocalSymbol(q.Symbol))
|
2023-09-08 07:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.OrderID) != 0 {
|
|
|
|
req.OrderID(q.OrderID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := orderRateLimiter.Wait(ctx); err != nil {
|
|
|
|
return nil, fmt.Errorf("order rate limiter wait error: %w", err)
|
|
|
|
}
|
|
|
|
response, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to query order trades, err: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var trades []types.Trade
|
|
|
|
var errs error
|
|
|
|
for _, trade := range response {
|
|
|
|
res, err := toGlobalTrade(&trade)
|
|
|
|
if err != nil {
|
|
|
|
errs = multierr.Append(errs, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
trades = append(trades, *res)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil {
|
|
|
|
return nil, errs
|
|
|
|
}
|
2023-09-18 07:55:58 +00:00
|
|
|
return trades, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
QueryClosedOrders can query closed orders in last 3 months, there are no time interval limitations, as long as until >= since.
|
|
|
|
Please Use lastOrderID as cursor, only return orders later than that order, that order is not included.
|
|
|
|
If you want to query orders by time range, please just pass since and until.
|
|
|
|
If you want to query by cursor, please pass lastOrderID.
|
|
|
|
Because it gets the correct response even when you pass all parameters with the right time interval and invalid lastOrderID, like 0.
|
|
|
|
Time interval boundary unit is second.
|
|
|
|
since is inclusive, ex. order created in 1694155903, get response if query since 1694155903, get empty if query since 1694155904
|
|
|
|
until is not inclusive, ex. order created in 1694155903, get response if query until 1694155904, get empty if query until 1694155903
|
|
|
|
*/
|
|
|
|
func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) ([]types.Order, error) {
|
|
|
|
if symbol == "" {
|
|
|
|
return nil, ErrSymbolRequired
|
|
|
|
}
|
|
|
|
|
2023-09-22 05:42:42 +00:00
|
|
|
if err := orderRateLimiter.Wait(ctx); err != nil {
|
2023-09-18 07:55:58 +00:00
|
|
|
return nil, fmt.Errorf("query closed order rate limiter wait error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastOrder string
|
|
|
|
if lastOrderID <= 0 {
|
|
|
|
lastOrder = ""
|
|
|
|
} else {
|
|
|
|
lastOrder = strconv.FormatUint(lastOrderID, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := e.client.NewGetOrderHistoryRequest().
|
|
|
|
InstrumentID(toLocalSymbol(symbol)).
|
|
|
|
StartTime(since).
|
|
|
|
EndTime(until).
|
|
|
|
Limit(defaultQueryLimit).
|
|
|
|
Before(lastOrder).
|
|
|
|
Do(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to call get order histories error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var orders []types.Order
|
|
|
|
|
|
|
|
for _, order := range res {
|
|
|
|
o, err2 := toGlobalOrder(&order)
|
|
|
|
if err2 != nil {
|
|
|
|
err = multierr.Append(err, err2)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
orders = append(orders, *o)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-08 07:33:09 +00:00
|
|
|
|
2023-09-18 07:55:58 +00:00
|
|
|
return types.SortOrdersAscending(orders), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
QueryTrades can query trades in last 3 months, there are no time interval limitations, as long as end_time >= start_time.
|
|
|
|
OKEX do not provide api to query by tradeID, So use /api/v5/trade/orders-history-archive as its official site do.
|
|
|
|
If you want to query trades by time range, please just pass start_time and end_time.
|
|
|
|
Because it gets the correct response even when you pass all parameters with the right time interval and invalid LastTradeID, like 0.
|
2023-10-03 07:14:49 +00:00
|
|
|
No matter how you pass parameter, QueryTrades return descending order.
|
|
|
|
If you query time period 3 months earlier with start time and end time, will return [] empty slice
|
|
|
|
But If you query time period 3 months earlier JUST with start time, will return like start with 3 months ago.
|
2023-09-18 07:55:58 +00:00
|
|
|
*/
|
|
|
|
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
|
|
|
if symbol == "" {
|
|
|
|
return nil, ErrSymbolRequired
|
|
|
|
}
|
|
|
|
|
2023-09-22 05:42:42 +00:00
|
|
|
if options.LastTradeID > 0 {
|
|
|
|
log.Warn("!!!OKEX EXCHANGE API NOTICE!!! Okex does not support searching for trades using TradeId.")
|
|
|
|
}
|
|
|
|
|
2023-10-02 10:47:05 +00:00
|
|
|
req := e.client.NewGetTransactionHistoryRequest().InstrumentID(toLocalSymbol(symbol))
|
2023-09-18 07:55:58 +00:00
|
|
|
|
|
|
|
limit := uint64(options.Limit)
|
|
|
|
if limit > defaultQueryLimit || limit <= 0 {
|
2023-10-02 02:49:33 +00:00
|
|
|
limit = defaultQueryLimit
|
2023-10-03 04:29:30 +00:00
|
|
|
req.Limit(defaultQueryLimit)
|
|
|
|
log.Debugf("limit is exceeded default limit %d or zero, got: %d, use default limit", defaultQueryLimit, options.Limit)
|
2023-09-18 07:55:58 +00:00
|
|
|
} else {
|
|
|
|
req.Limit(limit)
|
|
|
|
}
|
|
|
|
|
2023-09-22 05:42:42 +00:00
|
|
|
if err := orderRateLimiter.Wait(ctx); err != nil {
|
2023-09-18 07:55:58 +00:00
|
|
|
return nil, fmt.Errorf("query trades rate limiter wait error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var response []okexapi.OrderDetails
|
2023-09-22 05:42:42 +00:00
|
|
|
if options.StartTime == nil && options.EndTime == nil {
|
2023-10-03 04:29:30 +00:00
|
|
|
return nil, fmt.Errorf("StartTime and EndTime are required parameter!")
|
2023-09-22 05:42:42 +00:00
|
|
|
} else { // query by time interval
|
2023-09-18 07:55:58 +00:00
|
|
|
if options.StartTime != nil {
|
|
|
|
req.StartTime(*options.StartTime)
|
|
|
|
}
|
|
|
|
if options.EndTime != nil {
|
|
|
|
req.EndTime(*options.EndTime)
|
|
|
|
}
|
|
|
|
|
2023-10-02 10:47:05 +00:00
|
|
|
var billID = "" // billId should be emtpy, can't be 0
|
|
|
|
for { // pagenation should use "after" (earlier than)
|
2023-10-02 02:49:33 +00:00
|
|
|
res, err := req.
|
2023-10-02 10:47:05 +00:00
|
|
|
After(billID).
|
2023-09-27 03:06:41 +00:00
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to call get order histories error: %w", err)
|
|
|
|
}
|
|
|
|
response = append(response, res...)
|
2023-10-03 04:29:30 +00:00
|
|
|
if len(res) != int(limit) {
|
2023-09-27 03:06:41 +00:00
|
|
|
break
|
|
|
|
}
|
2023-10-03 07:14:49 +00:00
|
|
|
billID = strconv.Itoa(int(res[limit-1].BillID))
|
2023-09-18 07:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trades, err := toGlobalTrades(response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to trans order detail to trades error: %w", err)
|
|
|
|
}
|
2023-09-08 07:33:09 +00:00
|
|
|
return trades, nil
|
|
|
|
}
|
2023-10-02 04:55:30 +00:00
|
|
|
|
|
|
|
func (e *Exchange) SupportedInterval() map[types.Interval]int {
|
|
|
|
return SupportedIntervals
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) IsSupportedInterval(interval types.Interval) bool {
|
|
|
|
_, ok := SupportedIntervals[interval]
|
|
|
|
return ok
|
|
|
|
}
|