2021-05-27 06:42:14 +00:00
|
|
|
package okex
|
2021-05-27 07:48:51 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-27 17:14:11 +00:00
|
|
|
"encoding/json"
|
2021-05-27 07:48:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-01-16 07:29:12 +00:00
|
|
|
"strconv"
|
2021-05-27 09:55:23 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-05-27 07:48:51 +00:00
|
|
|
|
2021-05-27 17:14:11 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
|
2021-05-27 07:48:51 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2021-05-27 08:01:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2021-05-27 07:48:51 +00:00
|
|
|
)
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
type Channel string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ChannelBooks Channel = "books"
|
|
|
|
ChannelBook5 Channel = "book5"
|
|
|
|
ChannelCandlePrefix Channel = "candle"
|
|
|
|
ChannelAccount Channel = "account"
|
2024-01-08 07:05:32 +00:00
|
|
|
ChannelMarketTrades Channel = "trades"
|
2024-01-16 07:29:12 +00:00
|
|
|
ChannelOrderTrades Channel = "orders"
|
2024-01-02 15:18:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ActionType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ActionTypeSnapshot ActionType = "snapshot"
|
|
|
|
ActionTypeUpdate ActionType = "update"
|
|
|
|
)
|
|
|
|
|
|
|
|
func parseWebSocketEvent(in []byte) (interface{}, error) {
|
|
|
|
var event WebSocketEvent
|
2024-01-16 07:29:12 +00:00
|
|
|
err := json.Unmarshal(in, &event)
|
2024-01-02 15:18:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if event.Event != "" {
|
2024-01-09 02:57:33 +00:00
|
|
|
return &event, nil
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
switch event.Arg.Channel {
|
|
|
|
case ChannelAccount:
|
2024-01-09 02:57:33 +00:00
|
|
|
return parseAccount(event.Data)
|
2024-01-02 15:18:57 +00:00
|
|
|
|
|
|
|
case ChannelBooks, ChannelBook5:
|
|
|
|
var bookEvent BookEvent
|
|
|
|
err = json.Unmarshal(event.Data, &bookEvent.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal data into BookEvent, Arg: %+v Data: %s, err: %w", event.Arg, string(event.Data), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
instId := event.Arg.InstId
|
|
|
|
bookEvent.InstrumentID = instId
|
|
|
|
bookEvent.Symbol = toGlobalSymbol(instId)
|
|
|
|
bookEvent.channel = event.Arg.Channel
|
|
|
|
bookEvent.Action = event.ActionType
|
|
|
|
return &bookEvent, nil
|
|
|
|
|
2024-01-08 07:05:32 +00:00
|
|
|
case ChannelMarketTrades:
|
|
|
|
var trade []MarketTradeEvent
|
|
|
|
err = json.Unmarshal(event.Data, &trade)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal data into MarketTradeEvent: %+v, err: %w", string(event.Data), err)
|
|
|
|
}
|
|
|
|
return trade, nil
|
|
|
|
|
2024-01-16 07:29:12 +00:00
|
|
|
case ChannelOrderTrades:
|
|
|
|
var orderTrade []OrderTradeEvent
|
|
|
|
err := json.Unmarshal(event.Data, &orderTrade)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return orderTrade, nil
|
2024-01-02 15:18:57 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
if strings.HasPrefix(string(event.Arg.Channel), string(ChannelCandlePrefix)) {
|
|
|
|
// TODO: Support kline subscription. The kline requires another URL to subscribe, which is why we cannot
|
|
|
|
// support it at this time.
|
|
|
|
var kLineEvt KLineEvent
|
|
|
|
err = json.Unmarshal(event.Data, &kLineEvt.Events)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal data into KLineEvent, Arg: %+v Data: %s, err: %w", event.Arg, string(event.Data), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kLineEvt.Channel = event.Arg.Channel
|
|
|
|
kLineEvt.InstrumentID = event.Arg.InstId
|
|
|
|
kLineEvt.Symbol = toGlobalSymbol(event.Arg.InstId)
|
|
|
|
kLineEvt.Interval = strings.ToLower(strings.TrimPrefix(string(event.Arg.Channel), string(ChannelCandlePrefix)))
|
|
|
|
return &kLineEvt, nil
|
|
|
|
}
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-01-09 02:57:33 +00:00
|
|
|
type WsEventType string
|
|
|
|
|
|
|
|
const (
|
2024-01-10 05:56:17 +00:00
|
|
|
WsEventTypeLogin = "login"
|
|
|
|
WsEventTypeError = "error"
|
|
|
|
WsEventTypeSubscribe = "subscribe"
|
|
|
|
WsEventTypeUnsubscribe = "unsubscribe"
|
2024-01-09 02:57:33 +00:00
|
|
|
)
|
|
|
|
|
2021-05-27 07:48:51 +00:00
|
|
|
type WebSocketEvent struct {
|
2024-01-09 02:57:33 +00:00
|
|
|
Event WsEventType `json:"event"`
|
|
|
|
Code string `json:"code,omitempty"`
|
|
|
|
Message string `json:"msg,omitempty"`
|
2024-01-02 15:18:57 +00:00
|
|
|
Arg struct {
|
|
|
|
Channel Channel `json:"channel"`
|
|
|
|
InstId string `json:"instId"`
|
|
|
|
} `json:"arg,omitempty"`
|
|
|
|
Data json.RawMessage `json:"data"`
|
|
|
|
ActionType ActionType `json:"action"`
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 02:57:33 +00:00
|
|
|
func (w *WebSocketEvent) IsValid() error {
|
|
|
|
switch w.Event {
|
|
|
|
case WsEventTypeError:
|
|
|
|
return fmt.Errorf("websocket request error, code: %s, msg: %s", w.Code, w.Message)
|
|
|
|
|
2024-01-10 05:56:17 +00:00
|
|
|
case WsEventTypeSubscribe, WsEventTypeUnsubscribe:
|
|
|
|
return nil
|
|
|
|
|
2024-01-09 02:57:33 +00:00
|
|
|
case WsEventTypeLogin:
|
|
|
|
// Actually, this code is unnecessary because the events are either `Subscribe` or `Unsubscribe`, But to avoid bugs
|
|
|
|
// in the exchange, we still check.
|
|
|
|
if w.Code != "0" || len(w.Message) != 0 {
|
|
|
|
return fmt.Errorf("websocket request error, code: %s, msg: %s", w.Code, w.Message)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected event type: %+v", w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WebSocketEvent) IsAuthenticated() bool {
|
|
|
|
return w.Event == WsEventTypeLogin && w.Code == "0"
|
|
|
|
}
|
|
|
|
|
2022-01-01 18:37:33 +00:00
|
|
|
type BookEvent struct {
|
2024-01-02 15:18:57 +00:00
|
|
|
InstrumentID string
|
|
|
|
Symbol string
|
|
|
|
Action ActionType
|
|
|
|
channel Channel
|
|
|
|
|
|
|
|
Data []struct {
|
|
|
|
Bids PriceVolumeOrderSlice `json:"bids"`
|
|
|
|
Asks PriceVolumeOrderSlice `json:"asks"`
|
|
|
|
MillisecondTimestamp types.MillisecondTimestamp `json:"ts"`
|
|
|
|
Checksum int `json:"checksum"`
|
|
|
|
}
|
2021-12-22 16:42:25 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
func (event *BookEvent) BookTicker() types.BookTicker {
|
2023-09-01 09:12:32 +00:00
|
|
|
ticker := types.BookTicker{
|
2024-01-02 15:18:57 +00:00
|
|
|
Symbol: event.Symbol,
|
2023-09-01 09:12:32 +00:00
|
|
|
}
|
2021-12-22 16:42:25 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
if len(event.Data) > 0 {
|
|
|
|
if len(event.Data[0].Bids) > 0 {
|
|
|
|
ticker.Buy = event.Data[0].Bids[0].Price
|
|
|
|
ticker.BuySize = event.Data[0].Bids[0].Volume
|
|
|
|
}
|
2021-12-22 16:42:25 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
if len(event.Data[0].Asks) > 0 {
|
|
|
|
ticker.Sell = event.Data[0].Asks[0].Price
|
|
|
|
ticker.SellSize = event.Data[0].Asks[0].Volume
|
|
|
|
}
|
2021-12-22 16:42:25 +00:00
|
|
|
}
|
2023-09-01 09:12:32 +00:00
|
|
|
|
|
|
|
return ticker
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
func (event *BookEvent) Book() types.SliceOrderBook {
|
2021-05-27 08:01:15 +00:00
|
|
|
book := types.SliceOrderBook{
|
2024-01-02 15:18:57 +00:00
|
|
|
Symbol: event.Symbol,
|
2021-05-27 08:01:15 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
if len(event.Data) > 0 {
|
|
|
|
book.Time = event.Data[0].MillisecondTimestamp.Time()
|
2021-05-27 08:01:15 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
for _, data := range event.Data {
|
|
|
|
for _, bid := range data.Bids {
|
|
|
|
book.Bids = append(book.Bids, types.PriceVolume{Price: bid.Price, Volume: bid.Volume})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ask := range data.Asks {
|
|
|
|
book.Asks = append(book.Asks, types.PriceVolume{Price: ask.Price, Volume: ask.Volume})
|
|
|
|
}
|
2021-05-27 08:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return book
|
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
type PriceVolumeOrder struct {
|
|
|
|
types.PriceVolume
|
|
|
|
// NumLiquidated is part of a deprecated feature and it is always "0"
|
2021-05-27 07:48:51 +00:00
|
|
|
NumLiquidated int
|
2024-01-02 15:18:57 +00:00
|
|
|
// NumOrders is the number of orders at the price.
|
|
|
|
NumOrders int
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
type PriceVolumeOrderSlice []PriceVolumeOrder
|
2021-05-27 07:48:51 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
func (slice *PriceVolumeOrderSlice) UnmarshalJSON(b []byte) error {
|
|
|
|
s, err := ParsePriceVolumeOrderSliceJSON(b)
|
2021-05-27 07:48:51 +00:00
|
|
|
if err != nil {
|
2024-01-02 15:18:57 +00:00
|
|
|
return err
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
*slice = s
|
|
|
|
return nil
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
// ParsePriceVolumeOrderSliceJSON tries to parse a 2 dimensional string array into a PriceVolumeOrderSlice
|
|
|
|
//
|
|
|
|
// [["8476.98", "415", "0", "13"], ["8477", "7", "0", "2"], ... ]
|
|
|
|
func ParsePriceVolumeOrderSliceJSON(b []byte) (slice PriceVolumeOrderSlice, err error) {
|
|
|
|
var as [][]fixedpoint.Value
|
2021-05-27 07:48:51 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
err = json.Unmarshal(b, &as)
|
2021-05-27 07:48:51 +00:00
|
|
|
if err != nil {
|
2024-01-02 15:18:57 +00:00
|
|
|
return slice, fmt.Errorf("failed to unmarshal price volume order slice: %w", err)
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
for _, a := range as {
|
|
|
|
var pv PriceVolumeOrder
|
|
|
|
pv.Price = a[0]
|
|
|
|
pv.Volume = a[1]
|
|
|
|
pv.NumLiquidated = a[2].Int()
|
|
|
|
pv.NumOrders = a[3].Int()
|
2021-05-27 07:48:51 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
slice = append(slice, pv)
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
return slice, nil
|
2021-05-27 07:48:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
type KLine struct {
|
|
|
|
StartTime types.MillisecondTimestamp
|
|
|
|
OpenPrice fixedpoint.Value
|
|
|
|
HighestPrice fixedpoint.Value
|
|
|
|
LowestPrice fixedpoint.Value
|
|
|
|
ClosePrice fixedpoint.Value
|
|
|
|
// Volume trading volume, with a unit of contract.cccccbcvefkeibbhtrebbfklrbetukhrgjgkiilufbde
|
2021-05-27 09:55:23 +00:00
|
|
|
|
|
|
|
// If it is a derivatives contract, the value is the number of contracts.
|
2024-01-02 15:18:57 +00:00
|
|
|
// If it is SPOT/MARGIN, the value is the quantity in base currency.
|
2021-05-27 09:55:23 +00:00
|
|
|
Volume fixedpoint.Value
|
2024-01-02 15:18:57 +00:00
|
|
|
// VolumeCcy trading volume, with a unit of currency.
|
|
|
|
// If it is a derivatives contract, the value is the number of base currency.
|
|
|
|
// If it is SPOT/MARGIN, the value is the quantity in quote currency.
|
|
|
|
VolumeCcy fixedpoint.Value
|
|
|
|
// VolumeCcyQuote Trading volume, the value is the quantity in quote currency
|
|
|
|
// e.g. The unit is USDT for BTC-USDT and BTC-USDT-SWAP;
|
|
|
|
// The unit is USD for BTC-USD-SWAP
|
|
|
|
VolumeCcyQuote fixedpoint.Value
|
|
|
|
// The state of candlesticks.
|
|
|
|
// 0 represents that it is uncompleted, 1 represents that it is completed.
|
|
|
|
Confirm fixedpoint.Value
|
2021-05-27 09:55:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
func (k KLine) ToGlobal(interval types.Interval, symbol string) types.KLine {
|
|
|
|
startTime := k.StartTime.Time()
|
|
|
|
|
2021-05-27 10:35:34 +00:00
|
|
|
return types.KLine{
|
2024-01-02 15:18:57 +00:00
|
|
|
Exchange: types.ExchangeOKEx,
|
|
|
|
Symbol: symbol,
|
|
|
|
StartTime: types.Time(startTime),
|
|
|
|
EndTime: types.Time(startTime.Add(interval.Duration() - time.Millisecond)),
|
|
|
|
Interval: interval,
|
|
|
|
Open: k.OpenPrice,
|
|
|
|
Close: k.ClosePrice,
|
|
|
|
High: k.HighestPrice,
|
|
|
|
Low: k.LowestPrice,
|
|
|
|
Volume: k.Volume,
|
|
|
|
QuoteVolume: k.VolumeCcy, // not supported
|
|
|
|
TakerBuyBaseAssetVolume: fixedpoint.Zero, // not supported
|
|
|
|
TakerBuyQuoteAssetVolume: fixedpoint.Zero, // not supported
|
|
|
|
LastTradeID: 0, // not supported
|
|
|
|
NumberOfTrades: 0, // not supported
|
|
|
|
Closed: !k.Confirm.IsZero(),
|
2021-05-27 10:35:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
type KLineSlice []KLine
|
2021-05-27 09:55:23 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
func (m *KLineSlice) UnmarshalJSON(b []byte) error {
|
|
|
|
if m == nil {
|
|
|
|
return errors.New("nil pointer of kline slice")
|
2021-05-27 09:55:23 +00:00
|
|
|
}
|
2024-01-02 15:18:57 +00:00
|
|
|
s, err := parseKLineSliceJSON(b)
|
2021-05-27 09:55:23 +00:00
|
|
|
if err != nil {
|
2024-01-02 15:18:57 +00:00
|
|
|
return err
|
2021-05-27 09:55:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
*m = s
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-27 09:55:23 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
// parseKLineSliceJSON tries to parse a 2 dimensional string array into a KLineSlice
|
|
|
|
//
|
|
|
|
// [
|
|
|
|
// [
|
|
|
|
// "1597026383085",
|
|
|
|
// "8533.02",
|
|
|
|
// "8553.74",
|
|
|
|
// "8527.17",
|
|
|
|
// "8548.26",
|
|
|
|
// "45247",
|
|
|
|
// "529.5858061",
|
|
|
|
// "5529.5858061",
|
|
|
|
// "0"
|
|
|
|
// ]
|
|
|
|
// ]
|
|
|
|
func parseKLineSliceJSON(in []byte) (slice KLineSlice, err error) {
|
|
|
|
var rawKLines [][]json.RawMessage
|
|
|
|
|
|
|
|
err = json.Unmarshal(in, &rawKLines)
|
2021-05-27 09:55:23 +00:00
|
|
|
if err != nil {
|
2024-01-02 15:18:57 +00:00
|
|
|
return slice, err
|
2021-05-27 09:55:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
for _, raw := range rawKLines {
|
|
|
|
if len(raw) != 9 {
|
|
|
|
return nil, fmt.Errorf("unexpected kline length: %d, data: %q", len(raw), raw)
|
|
|
|
}
|
|
|
|
var kline KLine
|
|
|
|
if err = json.Unmarshal(raw[0], &kline.StartTime); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal into timestamp: %q", raw[0])
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(raw[1], &kline.OpenPrice); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal into open price: %q", raw[1])
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(raw[2], &kline.HighestPrice); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal into highest price: %q", raw[2])
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(raw[3], &kline.LowestPrice); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal into lowest price: %q", raw[3])
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(raw[4], &kline.ClosePrice); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal into close price: %q", raw[4])
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(raw[5], &kline.Volume); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal into volume: %q", raw[5])
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(raw[6], &kline.VolumeCcy); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal into volume currency: %q", raw[6])
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(raw[7], &kline.VolumeCcyQuote); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal into trading currency quote: %q", raw[7])
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(raw[8], &kline.Confirm); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal into confirm: %q", raw[8])
|
|
|
|
}
|
2021-05-27 09:55:23 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
slice = append(slice, kline)
|
2021-05-27 09:55:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
return slice, nil
|
|
|
|
}
|
2021-05-27 09:55:23 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
type KLineEvent struct {
|
|
|
|
Events KLineSlice
|
2021-05-27 09:55:23 +00:00
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
InstrumentID string
|
|
|
|
Symbol string
|
|
|
|
Interval string
|
|
|
|
Channel Channel
|
2021-05-27 09:55:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 02:57:33 +00:00
|
|
|
func parseAccount(v []byte) (*okexapi.Account, error) {
|
2021-05-27 17:14:11 +00:00
|
|
|
var accounts []okexapi.Account
|
2024-01-09 02:57:33 +00:00
|
|
|
err := json.Unmarshal(v, &accounts)
|
2021-05-27 17:14:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(accounts) == 0 {
|
2024-01-09 02:57:33 +00:00
|
|
|
return &okexapi.Account{}, nil
|
2021-05-27 17:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &accounts[0], nil
|
|
|
|
}
|
|
|
|
|
2024-01-16 07:29:12 +00:00
|
|
|
type OrderTradeEvent struct {
|
|
|
|
okexapi.OrderDetail
|
|
|
|
|
|
|
|
Code types.StrInt64 `json:"code"`
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
AmendResult string `json:"amendResult"`
|
|
|
|
ExecutionType okexapi.LiquidityType `json:"execType"`
|
|
|
|
// FillFee last filled fee amount or rebate amount:
|
|
|
|
// Negative number represents the user transaction fee charged by the platform;
|
|
|
|
// Positive number represents rebate
|
|
|
|
FillFee fixedpoint.Value `json:"fillFee"`
|
|
|
|
// FillFeeCurrency last filled fee currency or rebate currency.
|
|
|
|
// It is fee currency when fillFee is less than 0; It is rebate currency when fillFee>=0.
|
|
|
|
FillFeeCurrency string `json:"fillFeeCcy"`
|
|
|
|
// FillNotionalUsd Filled notional value in USD of order
|
|
|
|
FillNotionalUsd fixedpoint.Value `json:"fillNotionalUsd"`
|
|
|
|
FillPnl fixedpoint.Value `json:"fillPnl"`
|
|
|
|
// NotionalUsd Estimated national value in USD of order
|
|
|
|
NotionalUsd fixedpoint.Value `json:"notionalUsd"`
|
|
|
|
// ReqId Client Request ID as assigned by the client for order amendment. "" will be returned if there is no order amendment.
|
|
|
|
ReqId string `json:"reqId"`
|
|
|
|
LastPrice fixedpoint.Value `json:"lastPx"`
|
|
|
|
// QuickMgnType Quick Margin type, Only applicable to Quick Margin Mode of isolated margin
|
|
|
|
// manual, auto_borrow, auto_repay
|
|
|
|
QuickMgnType string `json:"quickMgnType"`
|
|
|
|
// AmendSource Source of the order amendation.
|
|
|
|
AmendSource string `json:"amendSource"`
|
|
|
|
// CancelSource Source of the order cancellation.
|
|
|
|
CancelSource string `json:"cancelSource"`
|
|
|
|
|
|
|
|
// Only applicable to options; return "" for other instrument types
|
|
|
|
FillPriceVolume string `json:"fillPxVol"`
|
|
|
|
FillPriceUsd string `json:"fillPxUsd"`
|
|
|
|
FillMarkVolume string `json:"fillMarkVol"`
|
|
|
|
FillFwdPrice string `json:"fillFwdPx"`
|
|
|
|
FillMarkPrice string `json:"fillMarkPx"`
|
|
|
|
}
|
2021-05-27 18:21:35 +00:00
|
|
|
|
2024-01-16 07:29:12 +00:00
|
|
|
func (o *OrderTradeEvent) toGlobalTrade() (types.Trade, error) {
|
|
|
|
side := toGlobalSide(o.Side)
|
|
|
|
tradeId, err := strconv.ParseUint(o.TradeId, 10, 64)
|
2021-05-27 18:21:35 +00:00
|
|
|
if err != nil {
|
2024-01-16 07:29:12 +00:00
|
|
|
return types.Trade{}, fmt.Errorf("unexpected trade id [%s] format: %w", o.TradeId, err)
|
2021-05-27 18:21:35 +00:00
|
|
|
}
|
2024-01-16 07:29:12 +00:00
|
|
|
return types.Trade{
|
|
|
|
ID: tradeId,
|
|
|
|
OrderID: uint64(o.OrderId),
|
|
|
|
Exchange: types.ExchangeOKEx,
|
|
|
|
Price: o.FillPrice,
|
|
|
|
Quantity: o.FillSize,
|
|
|
|
QuoteQuantity: o.FillPrice.Mul(o.FillSize),
|
|
|
|
Symbol: toGlobalSymbol(o.InstrumentID),
|
|
|
|
Side: side,
|
|
|
|
IsBuyer: side == types.SideTypeBuy,
|
|
|
|
IsMaker: o.ExecutionType == okexapi.LiquidityTypeMaker,
|
|
|
|
Time: types.Time(o.FillTime.Time()),
|
|
|
|
// charged by the platform is positive in our design, so added the `Neg()`.
|
|
|
|
Fee: o.FillFee.Neg(),
|
|
|
|
FeeCurrency: o.FeeCurrency,
|
|
|
|
FeeDiscounted: false,
|
|
|
|
}, nil
|
2021-05-27 18:21:35 +00:00
|
|
|
}
|
2024-01-08 07:05:32 +00:00
|
|
|
|
|
|
|
func toGlobalSideType(side okexapi.SideType) (types.SideType, error) {
|
|
|
|
switch side {
|
|
|
|
case okexapi.SideTypeBuy:
|
|
|
|
return types.SideTypeBuy, nil
|
|
|
|
|
|
|
|
case okexapi.SideTypeSell:
|
|
|
|
return types.SideTypeSell, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return types.SideType(side), fmt.Errorf("unexpected side: %s", side)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MarketTradeEvent struct {
|
|
|
|
InstId string `json:"instId"`
|
|
|
|
TradeId types.StrInt64 `json:"tradeId"`
|
|
|
|
Px fixedpoint.Value `json:"px"`
|
|
|
|
Sz fixedpoint.Value `json:"sz"`
|
|
|
|
Side okexapi.SideType `json:"side"`
|
|
|
|
Timestamp types.MillisecondTimestamp `json:"ts"`
|
|
|
|
Count types.StrInt64 `json:"count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MarketTradeEvent) toGlobalTrade() (types.Trade, error) {
|
|
|
|
symbol := toGlobalSymbol(m.InstId)
|
|
|
|
if symbol == "" {
|
|
|
|
return types.Trade{}, fmt.Errorf("unexpected inst id: %s", m.InstId)
|
|
|
|
}
|
|
|
|
|
|
|
|
side, err := toGlobalSideType(m.Side)
|
|
|
|
if err != nil {
|
|
|
|
return types.Trade{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.Trade{
|
|
|
|
ID: uint64(m.TradeId),
|
|
|
|
OrderID: 0, // not supported
|
|
|
|
Exchange: types.ExchangeOKEx,
|
|
|
|
Price: m.Px,
|
|
|
|
Quantity: m.Sz,
|
|
|
|
QuoteQuantity: m.Px.Mul(m.Sz),
|
|
|
|
Symbol: symbol,
|
|
|
|
Side: side,
|
|
|
|
IsBuyer: side == types.SideTypeBuy,
|
|
|
|
IsMaker: false, // not supported
|
|
|
|
Time: types.Time(m.Timestamp.Time()),
|
|
|
|
Fee: fixedpoint.Zero, // not supported
|
|
|
|
FeeCurrency: "", // not supported
|
|
|
|
}, nil
|
|
|
|
}
|