2021-12-10 13:43:40 +00:00
|
|
|
|
package kucoin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2021-12-30 14:02:50 +00:00
|
|
|
|
"fmt"
|
2022-01-19 10:33:54 +00:00
|
|
|
|
"sort"
|
2021-12-25 15:53:34 +00:00
|
|
|
|
"strconv"
|
2021-12-25 16:27:52 +00:00
|
|
|
|
"time"
|
2021-12-10 13:43:40 +00:00
|
|
|
|
|
2021-12-25 15:53:34 +00:00
|
|
|
|
"github.com/pkg/errors"
|
2021-12-10 13:43:40 +00:00
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-12-25 17:27:22 +00:00
|
|
|
|
"go.uber.org/multierr"
|
2021-12-25 19:04:21 +00:00
|
|
|
|
"golang.org/x/time/rate"
|
2021-12-25 17:27:22 +00:00
|
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange/kucoin/kucoinapi"
|
2022-02-03 04:55:25 +00:00
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2022-03-18 09:33:10 +00:00
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2021-12-10 13:43:40 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-05-20 10:57:41 +00:00
|
|
|
|
var marketDataLimiter = rate.NewLimiter(rate.Every(6*time.Second), 1)
|
|
|
|
|
var queryTradeLimiter = rate.NewLimiter(rate.Every(6*time.Second), 1)
|
|
|
|
|
var queryOrderLimiter = rate.NewLimiter(rate.Every(6*time.Second), 1)
|
2021-12-30 07:58:58 +00:00
|
|
|
|
|
2021-12-25 16:27:52 +00:00
|
|
|
|
var ErrMissingSequence = errors.New("sequence is missing")
|
|
|
|
|
|
2023-07-21 06:55:27 +00:00
|
|
|
|
// KCS is the platform currency of Kucoin, pre-allocate static string here
|
2021-12-10 13:43:40 +00:00
|
|
|
|
const KCS = "KCS"
|
|
|
|
|
|
|
|
|
|
var log = logrus.WithFields(logrus.Fields{
|
|
|
|
|
"exchange": "kucoin",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
type Exchange struct {
|
|
|
|
|
key, secret, passphrase string
|
2021-12-21 16:30:16 +00:00
|
|
|
|
client *kucoinapi.RestClient
|
2021-12-10 13:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 16:30:16 +00:00
|
|
|
|
func New(key, secret, passphrase string) *Exchange {
|
|
|
|
|
client := kucoinapi.NewClient()
|
|
|
|
|
|
|
|
|
|
if len(key) > 0 && len(secret) > 0 && len(passphrase) > 0 {
|
|
|
|
|
client.Auth(key, secret, passphrase)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Exchange{
|
2022-06-17 07:04:23 +00:00
|
|
|
|
key: key,
|
|
|
|
|
// pragma: allowlist nextline secret
|
2021-12-21 16:30:16 +00:00
|
|
|
|
secret: secret,
|
|
|
|
|
passphrase: passphrase,
|
|
|
|
|
client: client,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Exchange) Name() types.ExchangeName {
|
|
|
|
|
return types.ExchangeKucoin
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Exchange) PlatformFeeCurrency() string {
|
|
|
|
|
return KCS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
|
2021-12-31 18:04:20 +00:00
|
|
|
|
req := e.client.AccountService.NewListAccountsRequest()
|
|
|
|
|
accounts, err := req.Do(ctx)
|
2021-12-21 16:30:16 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for now, we only return the trading account
|
|
|
|
|
a := types.NewAccount()
|
|
|
|
|
balances := toGlobalBalanceMap(accounts)
|
|
|
|
|
a.UpdateBalances(balances)
|
|
|
|
|
return a, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
|
2021-12-31 18:04:20 +00:00
|
|
|
|
req := e.client.AccountService.NewListAccountsRequest()
|
|
|
|
|
accounts, err := req.Do(ctx)
|
2021-12-21 16:30:16 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return toGlobalBalanceMap(accounts), nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 13:43:40 +00:00
|
|
|
|
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
|
2021-12-21 16:30:16 +00:00
|
|
|
|
markets, err := e.client.MarketDataService.ListSymbols()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marketMap := types.MarketMap{}
|
2021-12-22 13:06:21 +00:00
|
|
|
|
for _, s := range markets {
|
|
|
|
|
market := toGlobalMarket(s)
|
|
|
|
|
marketMap.Add(market)
|
2021-12-21 16:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return marketMap, nil
|
2021-12-10 13:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) {
|
2021-12-21 17:26:00 +00:00
|
|
|
|
s, err := e.client.MarketDataService.GetTicker24HStat(symbol)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-22 13:06:21 +00:00
|
|
|
|
ticker := toGlobalTicker(*s)
|
|
|
|
|
return &ticker, nil
|
2021-12-10 13:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 17:26:00 +00:00
|
|
|
|
func (e *Exchange) QueryTickers(ctx context.Context, symbols ...string) (map[string]types.Ticker, error) {
|
|
|
|
|
tickers := map[string]types.Ticker{}
|
|
|
|
|
if len(symbols) > 0 {
|
|
|
|
|
for _, s := range symbols {
|
|
|
|
|
t, err := e.QueryTicker(ctx, s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tickers[s] = *t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tickers, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 17:34:24 +00:00
|
|
|
|
allTickers, err := e.client.MarketDataService.ListTickers()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, s := range allTickers.Ticker {
|
2021-12-25 16:27:52 +00:00
|
|
|
|
tickers[s.Symbol] = toGlobalTicker(s)
|
2021-12-21 17:34:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 17:26:00 +00:00
|
|
|
|
return tickers, nil
|
2021-12-10 13:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 19:19:03 +00:00
|
|
|
|
// From the doc
|
|
|
|
|
// Type of candlestick patterns: 1min, 3min, 5min, 15min, 30min, 1hour, 2hour, 4hour, 6hour, 8hour, 12hour, 1day, 1week
|
2021-12-25 18:23:06 +00:00
|
|
|
|
var supportedIntervals = map[types.Interval]int{
|
2022-10-04 07:15:07 +00:00
|
|
|
|
types.Interval1m: 1 * 60,
|
|
|
|
|
types.Interval5m: 5 * 60,
|
|
|
|
|
types.Interval15m: 15 * 60,
|
|
|
|
|
types.Interval30m: 30 * 60,
|
2021-12-25 19:04:21 +00:00
|
|
|
|
types.Interval1h: 60 * 60,
|
|
|
|
|
types.Interval2h: 60 * 60 * 2,
|
|
|
|
|
types.Interval4h: 60 * 60 * 4,
|
|
|
|
|
types.Interval6h: 60 * 60 * 6,
|
2021-12-25 18:23:06 +00:00
|
|
|
|
// types.Interval8h: 60 * 60 * 8,
|
|
|
|
|
types.Interval12h: 60 * 60 * 12,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Exchange) SupportedInterval() map[types.Interval]int {
|
|
|
|
|
return supportedIntervals
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Exchange) IsSupportedInterval(interval types.Interval) bool {
|
|
|
|
|
_, ok := supportedIntervals[interval]
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 13:43:40 +00:00
|
|
|
|
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
|
2022-05-03 04:11:02 +00:00
|
|
|
|
if err := marketDataLimiter.Wait(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-12-25 19:04:21 +00:00
|
|
|
|
|
2021-12-25 18:23:06 +00:00
|
|
|
|
req := e.client.MarketDataService.NewGetKLinesRequest()
|
2023-11-02 17:03:16 +00:00
|
|
|
|
req.Symbol(ToLocalSymbol(symbol))
|
2021-12-25 18:23:06 +00:00
|
|
|
|
req.Interval(toLocalInterval(interval))
|
|
|
|
|
if options.StartTime != nil {
|
|
|
|
|
req.StartAt(*options.StartTime)
|
2021-12-25 19:19:03 +00:00
|
|
|
|
// For each query, the system would return at most **1500** pieces of data. To obtain more data, please page the data by time.
|
2021-12-25 19:14:19 +00:00
|
|
|
|
req.EndAt(options.StartTime.Add(1500 * interval.Duration()))
|
|
|
|
|
} else if options.EndTime != nil {
|
2021-12-25 19:04:21 +00:00
|
|
|
|
req.EndAt(*options.EndTime)
|
2021-12-25 18:23:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ks, err := req.Do(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var klines []types.KLine
|
|
|
|
|
for _, k := range ks {
|
|
|
|
|
gi := toGlobalInterval(k.Interval)
|
|
|
|
|
klines = append(klines, types.KLine{
|
2021-12-25 19:04:21 +00:00
|
|
|
|
Exchange: types.ExchangeKucoin,
|
|
|
|
|
Symbol: toGlobalSymbol(k.Symbol),
|
|
|
|
|
StartTime: types.Time(k.StartTime),
|
|
|
|
|
EndTime: types.Time(k.StartTime.Add(gi.Duration() - time.Millisecond)),
|
|
|
|
|
Interval: gi,
|
2022-02-03 04:55:25 +00:00
|
|
|
|
Open: k.Open,
|
|
|
|
|
Close: k.Close,
|
|
|
|
|
High: k.High,
|
|
|
|
|
Low: k.Low,
|
|
|
|
|
Volume: k.Volume,
|
|
|
|
|
QuoteVolume: k.QuoteVolume,
|
2021-12-25 19:04:21 +00:00
|
|
|
|
Closed: true,
|
2021-12-25 18:23:06 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 10:33:54 +00:00
|
|
|
|
sort.Slice(klines, func(i, j int) bool {
|
|
|
|
|
return klines[i].StartTime.Before(klines[j].StartTime.Time())
|
|
|
|
|
})
|
|
|
|
|
|
2021-12-25 18:23:06 +00:00
|
|
|
|
return klines, nil
|
2021-12-10 13:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
|
func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (createdOrder *types.Order, err error) {
|
|
|
|
|
req := e.client.TradeService.NewPlaceOrderRequest()
|
2023-11-02 17:03:16 +00:00
|
|
|
|
req.Symbol(ToLocalSymbol(order.Symbol))
|
2022-09-09 10:41:06 +00:00
|
|
|
|
req.Side(toLocalSide(order.Side))
|
|
|
|
|
|
|
|
|
|
if order.ClientOrderID != "" {
|
|
|
|
|
req.ClientOrderID(order.ClientOrderID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if order.Market.Symbol != "" {
|
|
|
|
|
req.Size(order.Market.FormatQuantity(order.Quantity))
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: report error?
|
|
|
|
|
req.Size(order.Quantity.FormatString(8))
|
|
|
|
|
}
|
2021-12-25 16:27:52 +00:00
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
|
// set price field for limit orders
|
|
|
|
|
switch order.Type {
|
|
|
|
|
case types.OrderTypeStopLimit, types.OrderTypeLimit, types.OrderTypeLimitMaker:
|
2022-01-10 17:36:19 +00:00
|
|
|
|
if order.Market.Symbol != "" {
|
2022-09-09 10:41:06 +00:00
|
|
|
|
req.Price(order.Market.FormatPrice(order.Price))
|
2021-12-25 16:27:52 +00:00
|
|
|
|
} else {
|
2022-02-03 04:55:25 +00:00
|
|
|
|
// TODO: report error?
|
2022-09-09 10:41:06 +00:00
|
|
|
|
req.Price(order.Price.FormatString(8))
|
2022-03-28 09:09:00 +00:00
|
|
|
|
}
|
2022-09-09 10:41:06 +00:00
|
|
|
|
}
|
2022-03-28 09:09:00 +00:00
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
|
if order.Type == types.OrderTypeLimitMaker {
|
|
|
|
|
req.PostOnly(true)
|
|
|
|
|
}
|
2021-12-25 16:27:52 +00:00
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
|
switch order.TimeInForce {
|
|
|
|
|
case "FOK":
|
|
|
|
|
req.TimeInForce(kucoinapi.TimeInForceFOK)
|
|
|
|
|
case "IOC":
|
|
|
|
|
req.TimeInForce(kucoinapi.TimeInForceIOC)
|
|
|
|
|
default:
|
|
|
|
|
// default to GTC
|
|
|
|
|
req.TimeInForce(kucoinapi.TimeInForceGTC)
|
|
|
|
|
}
|
2022-03-28 09:09:00 +00:00
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
|
switch order.Type {
|
|
|
|
|
case types.OrderTypeStopLimit:
|
|
|
|
|
req.OrderType(kucoinapi.OrderTypeStopLimit)
|
2022-03-28 09:09:00 +00:00
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
|
case types.OrderTypeLimit, types.OrderTypeLimitMaker:
|
|
|
|
|
req.OrderType(kucoinapi.OrderTypeLimit)
|
2022-03-28 09:09:00 +00:00
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
|
case types.OrderTypeMarket:
|
|
|
|
|
req.OrderType(kucoinapi.OrderTypeMarket)
|
|
|
|
|
}
|
2021-12-25 16:27:52 +00:00
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
|
orderResponse, err := req.Do(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return createdOrder, err
|
2021-12-25 16:27:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 10:41:06 +00:00
|
|
|
|
return &types.Order{
|
|
|
|
|
SubmitOrder: order,
|
|
|
|
|
Exchange: types.ExchangeKucoin,
|
|
|
|
|
OrderID: hashStringID(orderResponse.OrderID),
|
|
|
|
|
UUID: orderResponse.OrderID,
|
|
|
|
|
Status: types.OrderStatusNew,
|
|
|
|
|
ExecutedQuantity: fixedpoint.Zero,
|
|
|
|
|
IsWorking: true,
|
|
|
|
|
CreationTime: types.Time(time.Now()),
|
|
|
|
|
UpdateTime: types.Time(time.Now()),
|
|
|
|
|
}, nil
|
2021-12-21 16:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 17:27:22 +00:00
|
|
|
|
// QueryOpenOrders
|
|
|
|
|
/*
|
|
|
|
|
Documentation from the Kucoin API page
|
|
|
|
|
|
|
|
|
|
Any order on the exchange order book is in active status.
|
|
|
|
|
Orders removed from the order book will be marked with done status.
|
|
|
|
|
After an order becomes done, there may be a few milliseconds latency before it’s fully settled.
|
|
|
|
|
|
|
|
|
|
You can check the orders in any status.
|
|
|
|
|
If the status parameter is not specified, orders of done status will be returned by default.
|
|
|
|
|
|
|
|
|
|
When you query orders in active status, there is no time limit.
|
|
|
|
|
However, when you query orders in done status, the start and end time range cannot exceed 7* 24 hours.
|
|
|
|
|
An error will occur if the specified time window exceeds the range.
|
|
|
|
|
|
|
|
|
|
If you specify the end time only, the system will automatically calculate the start time as end time minus 7*24 hours, and vice versa.
|
|
|
|
|
|
|
|
|
|
The history for cancelled orders is only kept for one month.
|
|
|
|
|
You will not be able to query for cancelled orders that have happened more than a month ago.
|
|
|
|
|
*/
|
2021-12-21 16:30:16 +00:00
|
|
|
|
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
|
2021-12-25 16:27:52 +00:00
|
|
|
|
req := e.client.TradeService.NewListOrdersRequest()
|
2023-11-02 17:03:16 +00:00
|
|
|
|
req.Symbol(ToLocalSymbol(symbol))
|
2021-12-25 17:27:22 +00:00
|
|
|
|
req.Status("active")
|
|
|
|
|
orderList, err := req.Do(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-12-25 16:27:52 +00:00
|
|
|
|
|
2021-12-25 17:27:22 +00:00
|
|
|
|
// TODO: support pagination (right now we can only get 50 items from the first page)
|
|
|
|
|
for _, o := range orderList.Items {
|
|
|
|
|
order := toGlobalOrder(o)
|
|
|
|
|
orders = append(orders, order)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return orders, err
|
2021-12-21 16:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 17:34:03 +00:00
|
|
|
|
func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error) {
|
|
|
|
|
req := e.client.TradeService.NewListOrdersRequest()
|
2023-11-02 17:03:16 +00:00
|
|
|
|
req.Symbol(ToLocalSymbol(symbol))
|
2021-12-25 17:34:03 +00:00
|
|
|
|
req.Status("done")
|
|
|
|
|
req.StartAt(since)
|
|
|
|
|
|
2021-12-31 17:28:29 +00:00
|
|
|
|
// kucoin:
|
|
|
|
|
// When you query orders in active status, there is no time limit.
|
|
|
|
|
// However, when you query orders in done status, the start and end time range cannot exceed 7* 24 hours.
|
|
|
|
|
// An error will occur if the specified time window exceeds the range.
|
|
|
|
|
// If you specify the end time only, the system will automatically calculate the start time as end time minus 7*24 hours, and vice versa.
|
2021-12-31 18:43:08 +00:00
|
|
|
|
if until.Sub(since) < 7*24*time.Hour {
|
2021-12-31 17:28:29 +00:00
|
|
|
|
req.EndAt(until)
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 09:33:10 +00:00
|
|
|
|
if err := queryOrderLimiter.Wait(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 17:34:03 +00:00
|
|
|
|
orderList, err := req.Do(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return orders, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, o := range orderList.Items {
|
|
|
|
|
order := toGlobalOrder(o)
|
|
|
|
|
orders = append(orders, order)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return orders, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 06:06:22 +00:00
|
|
|
|
var launchDate = time.Date(2017, 9, 0, 0, 0, 0, 0, time.UTC)
|
2021-12-31 18:43:08 +00:00
|
|
|
|
|
2021-12-25 17:44:05 +00:00
|
|
|
|
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
|
|
|
|
|
req := e.client.TradeService.NewGetFillsRequest()
|
2023-11-02 17:03:16 +00:00
|
|
|
|
req.Symbol(ToLocalSymbol(symbol))
|
2021-12-31 05:52:16 +00:00
|
|
|
|
|
2021-12-31 18:43:08 +00:00
|
|
|
|
// we always sync trades in the ascending order, and kucoin does not support last trade ID query
|
|
|
|
|
// hence we need to set the start time here
|
|
|
|
|
if options.StartTime != nil && options.StartTime.Before(launchDate) {
|
|
|
|
|
// copy the time data object
|
|
|
|
|
t := launchDate
|
|
|
|
|
options.StartTime = &t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if options.StartTime != nil && options.EndTime != nil {
|
|
|
|
|
req.StartAt(*options.StartTime)
|
2022-04-12 15:25:41 +00:00
|
|
|
|
|
2021-12-31 18:43:08 +00:00
|
|
|
|
if options.EndTime.Sub(*options.StartTime) < 7*24*time.Hour {
|
|
|
|
|
req.EndAt(*options.EndTime)
|
|
|
|
|
}
|
|
|
|
|
} else if options.StartTime != nil {
|
2021-12-25 17:44:05 +00:00
|
|
|
|
req.StartAt(*options.StartTime)
|
|
|
|
|
} else if options.EndTime != nil {
|
|
|
|
|
req.EndAt(*options.EndTime)
|
|
|
|
|
}
|
2021-12-25 17:34:03 +00:00
|
|
|
|
|
2022-03-18 09:33:10 +00:00
|
|
|
|
if err := queryTradeLimiter.Wait(ctx); err != nil {
|
|
|
|
|
return trades, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 17:44:05 +00:00
|
|
|
|
response, err := req.Do(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return trades, err
|
|
|
|
|
}
|
2022-04-12 15:25:41 +00:00
|
|
|
|
|
2021-12-25 17:44:05 +00:00
|
|
|
|
for _, fill := range response.Items {
|
|
|
|
|
trade := toGlobalTrade(fill)
|
|
|
|
|
trades = append(trades, trade)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return trades, nil
|
|
|
|
|
}
|
2021-12-25 17:34:03 +00:00
|
|
|
|
|
2021-12-25 17:27:22 +00:00
|
|
|
|
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (errs error) {
|
|
|
|
|
for _, o := range orders {
|
|
|
|
|
req := e.client.TradeService.NewCancelOrderRequest()
|
|
|
|
|
|
|
|
|
|
if o.UUID != "" {
|
|
|
|
|
req.OrderID(o.UUID)
|
|
|
|
|
} else if o.ClientOrderID != "" {
|
|
|
|
|
req.ClientOrderID(o.ClientOrderID)
|
|
|
|
|
} else {
|
2021-12-30 14:02:50 +00:00
|
|
|
|
errs = multierr.Append(
|
|
|
|
|
errs,
|
|
|
|
|
fmt.Errorf("the order uuid or client order id is empty, order: %#v", o),
|
|
|
|
|
)
|
2021-12-25 17:27:22 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response, err := req.Do(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errs = multierr.Append(errs, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Infof("cancelled orders: %v", response.CancelledOrderIDs)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 14:02:50 +00:00
|
|
|
|
return errors.Wrap(errs, "order cancel error")
|
2021-12-21 16:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Exchange) NewStream() types.Stream {
|
2021-12-25 15:53:34 +00:00
|
|
|
|
return NewStream(e.client, e)
|
2021-12-10 13:43:40 +00:00
|
|
|
|
}
|
2021-12-25 15:53:34 +00:00
|
|
|
|
|
|
|
|
|
func (e *Exchange) QueryDepth(ctx context.Context, symbol string) (types.SliceOrderBook, int64, error) {
|
2023-11-02 17:03:16 +00:00
|
|
|
|
orderBook, err := e.client.MarketDataService.GetOrderBook(ToLocalSymbol(symbol), 100)
|
2021-12-25 15:53:34 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return types.SliceOrderBook{}, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(orderBook.Sequence) == 0 {
|
|
|
|
|
return types.SliceOrderBook{}, 0, ErrMissingSequence
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sequence, err := strconv.ParseInt(orderBook.Sequence, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return types.SliceOrderBook{}, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return types.SliceOrderBook{
|
|
|
|
|
Symbol: toGlobalSymbol(symbol),
|
2023-09-01 09:02:08 +00:00
|
|
|
|
Time: orderBook.Time.Time(),
|
2021-12-25 15:53:34 +00:00
|
|
|
|
Bids: orderBook.Bids,
|
|
|
|
|
Asks: orderBook.Asks,
|
|
|
|
|
}, sequence, nil
|
2021-12-25 16:27:52 +00:00
|
|
|
|
}
|