2021-05-25 18:11:02 +00:00
|
|
|
package okex
|
|
|
|
|
2021-05-25 19:04:49 +00:00
|
|
|
import (
|
2021-05-27 06:42:14 +00:00
|
|
|
"fmt"
|
2022-05-03 03:14:53 +00:00
|
|
|
"regexp"
|
2021-05-27 18:21:35 +00:00
|
|
|
"strconv"
|
2021-05-25 19:04:49 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-02-18 05:52:13 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-05-25 19:04:49 +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-02-18 05:52:13 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2021-05-25 19:04:49 +00:00
|
|
|
)
|
2021-05-25 18:11:02 +00:00
|
|
|
|
|
|
|
func toGlobalSymbol(symbol string) string {
|
|
|
|
return strings.ReplaceAll(symbol, "-", "")
|
|
|
|
}
|
2021-05-25 18:37:28 +00:00
|
|
|
|
2022-05-19 01:48:36 +00:00
|
|
|
// //go:generate sh -c "echo \"package okex\nvar spotSymbolMap = map[string]string{\n\" $(curl -s -L 'https://okex.com/api/v5/public/instruments?instType=SPOT' | jq -r '.data[] | \"\\(.instId | sub(\"-\" ; \"\") | tojson ): \\( .instId | tojson),\n\"') \"\n}\" > symbols.go"
|
2021-05-25 18:53:55 +00:00
|
|
|
//go:generate go run gensymbols.go
|
2021-05-25 18:37:28 +00:00
|
|
|
func toLocalSymbol(symbol string) string {
|
2021-05-25 18:53:55 +00:00
|
|
|
if s, ok := spotSymbolMap[symbol]; ok {
|
2021-05-25 18:37:28 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf("failed to look up local symbol from %s", symbol)
|
|
|
|
return symbol
|
|
|
|
}
|
2021-05-25 19:04:49 +00:00
|
|
|
|
|
|
|
func toGlobalTicker(marketTicker okexapi.MarketTicker) *types.Ticker {
|
|
|
|
return &types.Ticker{
|
|
|
|
Time: marketTicker.Timestamp.Time(),
|
2022-02-03 04:55:25 +00:00
|
|
|
Volume: marketTicker.Volume24H,
|
|
|
|
Last: marketTicker.Last,
|
|
|
|
Open: marketTicker.Open24H,
|
|
|
|
High: marketTicker.High24H,
|
|
|
|
Low: marketTicker.Low24H,
|
|
|
|
Buy: marketTicker.BidPrice,
|
|
|
|
Sell: marketTicker.AskPrice,
|
2021-05-25 19:04:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-26 16:24:16 +00:00
|
|
|
|
2021-05-27 17:14:11 +00:00
|
|
|
func toGlobalBalance(account *okexapi.Account) types.BalanceMap {
|
2021-05-26 16:24:16 +00:00
|
|
|
var balanceMap = types.BalanceMap{}
|
2021-05-27 17:14:11 +00:00
|
|
|
for _, balanceDetail := range account.Details {
|
|
|
|
balanceMap[balanceDetail.Currency] = types.Balance{
|
|
|
|
Currency: balanceDetail.Currency,
|
|
|
|
Available: balanceDetail.CashBalance,
|
|
|
|
Locked: balanceDetail.Frozen,
|
2021-05-26 16:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return balanceMap
|
|
|
|
}
|
2021-05-27 06:42:14 +00:00
|
|
|
|
|
|
|
type WebsocketSubscription struct {
|
2021-05-27 17:14:11 +00:00
|
|
|
Channel string `json:"channel"`
|
|
|
|
InstrumentID string `json:"instId,omitempty"`
|
2021-05-27 16:47:34 +00:00
|
|
|
InstrumentType string `json:"instType,omitempty"`
|
2021-05-27 06:42:14 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 09:40:24 +00:00
|
|
|
var CandleChannels = []string{
|
|
|
|
"candle1Y",
|
|
|
|
"candle6M", "candle3M", "candle1M",
|
|
|
|
"candle1W",
|
|
|
|
"candle1D", "candle2D", "candle3D", "candle5D",
|
|
|
|
"candle12H", "candle6H", "candle4H", "candle2H", "candle1H",
|
|
|
|
"candle30m", "candle15m", "candle5m", "candle3m", "candle1m",
|
|
|
|
}
|
|
|
|
|
2022-05-19 01:48:36 +00:00
|
|
|
func convertIntervalToCandle(interval types.Interval) string {
|
|
|
|
s := interval.String()
|
|
|
|
switch s {
|
2021-05-27 09:40:24 +00:00
|
|
|
|
|
|
|
case "1h", "2h", "4h", "6h", "12h", "1d", "3d":
|
2022-05-19 01:48:36 +00:00
|
|
|
return "candle" + strings.ToUpper(s)
|
2021-05-27 09:40:24 +00:00
|
|
|
|
|
|
|
case "1m", "5m", "15m", "30m":
|
2022-05-19 01:48:36 +00:00
|
|
|
return "candle" + s
|
2021-05-27 09:40:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-19 01:48:36 +00:00
|
|
|
return "candle" + s
|
2021-05-27 09:40:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 06:42:14 +00:00
|
|
|
func convertSubscription(s types.Subscription) (WebsocketSubscription, error) {
|
|
|
|
// binance uses lower case symbol name,
|
|
|
|
// for kline, it's "<symbol>@kline_<interval>"
|
|
|
|
// for depth, it's "<symbol>@depth OR <symbol>@depth@100ms"
|
|
|
|
switch s.Channel {
|
|
|
|
case types.KLineChannel:
|
2021-05-27 09:40:24 +00:00
|
|
|
// Channel names are:
|
2021-05-27 06:42:14 +00:00
|
|
|
return WebsocketSubscription{
|
2021-05-27 09:40:24 +00:00
|
|
|
Channel: convertIntervalToCandle(s.Options.Interval),
|
2021-05-27 06:42:14 +00:00
|
|
|
InstrumentID: toLocalSymbol(s.Symbol),
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
case types.BookChannel:
|
|
|
|
return WebsocketSubscription{
|
|
|
|
Channel: "books",
|
|
|
|
InstrumentID: toLocalSymbol(s.Symbol),
|
|
|
|
}, nil
|
2021-12-22 16:42:25 +00:00
|
|
|
case types.BookTickerChannel:
|
|
|
|
return WebsocketSubscription{
|
|
|
|
Channel: "books5",
|
|
|
|
InstrumentID: toLocalSymbol(s.Symbol),
|
|
|
|
}, nil
|
2021-05-27 06:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return WebsocketSubscription{}, fmt.Errorf("unsupported public stream channel %s", s.Channel)
|
|
|
|
}
|
2021-05-27 18:21:35 +00:00
|
|
|
|
2021-05-27 18:45:09 +00:00
|
|
|
func toLocalSideType(side types.SideType) okexapi.SideType {
|
|
|
|
return okexapi.SideType(strings.ToLower(string(side)))
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:21:35 +00:00
|
|
|
func segmentOrderDetails(orderDetails []okexapi.OrderDetails) (trades, orders []okexapi.OrderDetails) {
|
|
|
|
for _, orderDetail := range orderDetails {
|
|
|
|
if len(orderDetail.LastTradeID) > 0 {
|
|
|
|
trades = append(trades, orderDetail)
|
|
|
|
}
|
2021-05-28 16:27:05 +00:00
|
|
|
orders = append(orders, orderDetail)
|
2021-05-27 18:21:35 +00:00
|
|
|
}
|
|
|
|
return trades, orders
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalTrades(orderDetails []okexapi.OrderDetails) ([]types.Trade, error) {
|
|
|
|
var trades []types.Trade
|
|
|
|
for _, orderDetail := range orderDetails {
|
|
|
|
tradeID, err := strconv.ParseInt(orderDetail.LastTradeID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return trades, errors.Wrapf(err, "error parsing tradeId value: %s", orderDetail.LastTradeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
orderID, err := strconv.ParseInt(orderDetail.OrderID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return trades, errors.Wrapf(err, "error parsing ordId value: %s", orderDetail.OrderID)
|
|
|
|
}
|
|
|
|
|
|
|
|
side := types.SideType(strings.ToUpper(string(orderDetail.Side)))
|
|
|
|
|
|
|
|
trades = append(trades, types.Trade{
|
2021-12-23 05:15:27 +00:00
|
|
|
ID: uint64(tradeID),
|
2021-05-27 18:21:35 +00:00
|
|
|
OrderID: uint64(orderID),
|
|
|
|
Exchange: types.ExchangeOKEx,
|
2022-02-03 04:55:25 +00:00
|
|
|
Price: orderDetail.LastFilledPrice,
|
|
|
|
Quantity: orderDetail.LastFilledQuantity,
|
|
|
|
QuoteQuantity: orderDetail.LastFilledPrice.Mul(orderDetail.LastFilledQuantity),
|
2021-05-27 18:21:35 +00:00
|
|
|
Symbol: toGlobalSymbol(orderDetail.InstrumentID),
|
|
|
|
Side: side,
|
|
|
|
IsBuyer: side == types.SideTypeBuy,
|
|
|
|
IsMaker: orderDetail.ExecutionType == "M",
|
|
|
|
Time: types.Time(orderDetail.LastFilledTime),
|
2022-02-03 04:55:25 +00:00
|
|
|
Fee: orderDetail.LastFilledFee,
|
2021-05-27 18:21:35 +00:00
|
|
|
FeeCurrency: orderDetail.LastFilledFeeCurrency,
|
|
|
|
IsMargin: false,
|
|
|
|
IsIsolated: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return trades, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalOrders(orderDetails []okexapi.OrderDetails) ([]types.Order, error) {
|
|
|
|
var orders []types.Order
|
|
|
|
for _, orderDetail := range orderDetails {
|
|
|
|
orderID, err := strconv.ParseInt(orderDetail.OrderID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return orders, err
|
|
|
|
}
|
|
|
|
|
|
|
|
side := types.SideType(strings.ToUpper(string(orderDetail.Side)))
|
|
|
|
|
|
|
|
orderType, err := toGlobalOrderType(orderDetail.OrderType)
|
|
|
|
if err != nil {
|
|
|
|
return orders, err
|
|
|
|
}
|
|
|
|
|
2022-02-18 05:52:13 +00:00
|
|
|
timeInForce := types.TimeInForceGTC
|
2021-05-27 18:21:35 +00:00
|
|
|
switch orderDetail.OrderType {
|
|
|
|
case okexapi.OrderTypeFOK:
|
2022-02-18 05:52:13 +00:00
|
|
|
timeInForce = types.TimeInForceFOK
|
2021-05-27 18:21:35 +00:00
|
|
|
case okexapi.OrderTypeIOC:
|
2022-02-18 05:52:13 +00:00
|
|
|
timeInForce = types.TimeInForceIOC
|
2021-05-27 18:21:35 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
orderStatus, err := toGlobalOrderStatus(orderDetail.State)
|
|
|
|
if err != nil {
|
|
|
|
return orders, err
|
|
|
|
}
|
|
|
|
|
|
|
|
isWorking := false
|
|
|
|
switch orderStatus {
|
|
|
|
case types.OrderStatusNew, types.OrderStatusPartiallyFilled:
|
|
|
|
isWorking = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
orders = append(orders, types.Order{
|
|
|
|
SubmitOrder: types.SubmitOrder{
|
|
|
|
ClientOrderID: orderDetail.ClientOrderID,
|
|
|
|
Symbol: toGlobalSymbol(orderDetail.InstrumentID),
|
|
|
|
Side: side,
|
|
|
|
Type: orderType,
|
2022-02-03 04:55:25 +00:00
|
|
|
Price: orderDetail.Price,
|
|
|
|
Quantity: orderDetail.Quantity,
|
|
|
|
StopPrice: fixedpoint.Zero, // not supported yet
|
2021-05-27 18:21:35 +00:00
|
|
|
TimeInForce: timeInForce,
|
|
|
|
},
|
|
|
|
Exchange: types.ExchangeOKEx,
|
|
|
|
OrderID: uint64(orderID),
|
|
|
|
Status: orderStatus,
|
2022-02-03 04:55:25 +00:00
|
|
|
ExecutedQuantity: orderDetail.FilledQuantity,
|
2021-05-27 18:21:35 +00:00
|
|
|
IsWorking: isWorking,
|
|
|
|
CreationTime: types.Time(orderDetail.CreationTime),
|
|
|
|
UpdateTime: types.Time(orderDetail.UpdateTime),
|
|
|
|
IsMargin: false,
|
|
|
|
IsIsolated: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return orders, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGlobalOrderStatus(state okexapi.OrderState) (types.OrderStatus, error) {
|
|
|
|
switch state {
|
|
|
|
case okexapi.OrderStateCanceled:
|
|
|
|
return types.OrderStatusCanceled, nil
|
|
|
|
case okexapi.OrderStateLive:
|
|
|
|
return types.OrderStatusNew, nil
|
|
|
|
case okexapi.OrderStatePartiallyFilled:
|
|
|
|
return types.OrderStatusPartiallyFilled, nil
|
|
|
|
case okexapi.OrderStateFilled:
|
|
|
|
return types.OrderStatusFilled, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("unknown or unsupported okex order state: %s", state)
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:45:09 +00:00
|
|
|
func toLocalOrderType(orderType types.OrderType) (okexapi.OrderType, error) {
|
|
|
|
switch orderType {
|
|
|
|
case types.OrderTypeMarket:
|
|
|
|
return okexapi.OrderTypeMarket, nil
|
|
|
|
|
|
|
|
case types.OrderTypeLimit:
|
|
|
|
return okexapi.OrderTypeLimit, nil
|
|
|
|
|
|
|
|
case types.OrderTypeLimitMaker:
|
|
|
|
return okexapi.OrderTypePostOnly, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("unknown or unsupported okex order type: %s", orderType)
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:21:35 +00:00
|
|
|
func toGlobalOrderType(orderType okexapi.OrderType) (types.OrderType, error) {
|
|
|
|
switch orderType {
|
|
|
|
case okexapi.OrderTypeMarket:
|
|
|
|
return types.OrderTypeMarket, nil
|
|
|
|
case okexapi.OrderTypeLimit:
|
|
|
|
return types.OrderTypeLimit, nil
|
|
|
|
case okexapi.OrderTypePostOnly:
|
|
|
|
return types.OrderTypeLimitMaker, nil
|
|
|
|
|
|
|
|
case okexapi.OrderTypeFOK:
|
|
|
|
case okexapi.OrderTypeIOC:
|
|
|
|
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("unknown or unsupported okex order type: %s", orderType)
|
|
|
|
}
|
2022-05-03 03:14:53 +00:00
|
|
|
|
2022-05-19 01:48:36 +00:00
|
|
|
func toLocalInterval(src string) string {
|
2022-06-17 11:19:51 +00:00
|
|
|
var re = regexp.MustCompile(`\d+[hdw]`)
|
2022-05-03 03:14:53 +00:00
|
|
|
return re.ReplaceAllStringFunc(src, func(w string) string {
|
|
|
|
return strings.ToUpper(w)
|
|
|
|
})
|
|
|
|
}
|