bbgo_origin/pkg/exchange/max/maxapi/userdata.go

273 lines
6.1 KiB
Go
Raw Normal View History

2020-10-01 08:48:08 +00:00
package max
import (
"github.com/pkg/errors"
"github.com/valyala/fastjson"
2020-10-03 03:11:59 +00:00
2020-10-11 08:46:15 +00:00
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
2020-10-01 08:48:08 +00:00
)
2020-10-01 09:05:46 +00:00
type BaseEvent struct {
2020-10-02 13:29:56 +00:00
Event string `json:"e"`
Timestamp int64 `json:"T"`
2020-10-01 08:48:08 +00:00
}
type OrderUpdate struct {
2020-10-02 13:29:56 +00:00
Event string `json:"e"`
ID uint64 `json:"i"`
Side string `json:"sd"`
OrderType OrderType `json:"ot"`
2020-10-01 09:08:12 +00:00
2020-10-02 13:29:56 +00:00
Price string `json:"p"`
StopPrice string `json:"sp"`
2020-10-01 09:08:12 +00:00
2020-10-01 08:48:08 +00:00
Volume string `json:"v"`
AveragePrice string `json:"ap"`
State OrderState `json:"S"`
2020-10-01 08:48:08 +00:00
Market string `json:"M"`
RemainingVolume string `json:"rv"`
ExecutedVolume string `json:"ev"`
TradesCount int64 `json:"tc"`
GroupID int64 `json:"gi"`
ClientOID string `json:"ci"`
CreatedAtMs int64 `json:"T"`
}
2020-10-01 09:05:46 +00:00
type OrderUpdateEvent struct {
BaseEvent
Orders []OrderUpdate `json:"o"`
}
func parserOrderUpdate(v *fastjson.Value) OrderUpdate {
2020-10-01 08:48:08 +00:00
return OrderUpdate{
Event: string(v.GetStringBytes("e")),
ID: v.GetUint64("i"),
Side: string(v.GetStringBytes("sd")),
Market: string(v.GetStringBytes("M")),
OrderType: OrderType(v.GetStringBytes("ot")),
State: OrderState(v.GetStringBytes("S")),
2020-10-01 08:48:08 +00:00
Price: string(v.GetStringBytes("p")),
2020-10-01 09:08:12 +00:00
StopPrice: string(v.GetStringBytes("sp")),
2020-10-01 08:48:08 +00:00
AveragePrice: string(v.GetStringBytes("ap")),
Volume: string(v.GetStringBytes("v")),
RemainingVolume: string(v.GetStringBytes("rv")),
ExecutedVolume: string(v.GetStringBytes("ev")),
TradesCount: v.GetInt64("tc"),
GroupID: v.GetInt64("gi"),
ClientOID: string(v.GetStringBytes("ci")),
CreatedAtMs: v.GetInt64("T"),
2020-10-01 09:05:46 +00:00
}
2020-10-01 08:48:08 +00:00
}
2020-10-02 13:29:56 +00:00
func parseOrderUpdateEvent(v *fastjson.Value) *OrderUpdateEvent {
var e OrderUpdateEvent
2020-10-01 09:05:46 +00:00
e.Event = string(v.GetStringBytes("e"))
e.Timestamp = v.GetInt64("T")
for _, ov := range v.GetArray("o") {
o := parserOrderUpdate(ov)
e.Orders = append(e.Orders, o)
2020-10-01 08:48:08 +00:00
}
2020-10-02 13:29:56 +00:00
return &e
2020-10-01 08:48:08 +00:00
}
2020-10-01 09:05:46 +00:00
type OrderSnapshotEvent struct {
BaseEvent
2020-10-01 08:48:08 +00:00
2020-10-02 13:29:56 +00:00
Orders []OrderUpdate `json:"o"`
2020-10-01 09:05:46 +00:00
}
2020-10-01 08:48:08 +00:00
2020-10-02 13:29:56 +00:00
func parserOrderSnapshotEvent(v *fastjson.Value) *OrderSnapshotEvent {
var e OrderSnapshotEvent
2020-10-01 09:05:46 +00:00
e.Event = string(v.GetStringBytes("e"))
e.Timestamp = v.GetInt64("T")
2020-10-01 08:48:08 +00:00
2020-10-01 09:05:46 +00:00
for _, ov := range v.GetArray("o") {
o := parserOrderUpdate(ov)
e.Orders = append(e.Orders, o)
2020-10-01 08:48:08 +00:00
}
2020-10-02 13:29:56 +00:00
return &e
2020-10-01 08:48:08 +00:00
}
type TradeUpdate struct {
ID uint64 `json:"i"`
Side string `json:"sd"`
Price string `json:"p"`
Volume string `json:"v"`
Market string `json:"M"`
Fee string `json:"f"`
FeeCurrency string `json:"fc"`
Timestamp int64 `json:"T"`
OrderID uint64 `json:"oi"`
2020-10-19 14:23:49 +00:00
Maker bool `json:"m"`
2020-10-01 08:48:08 +00:00
}
2020-10-01 09:05:46 +00:00
func parseTradeUpdate(v *fastjson.Value) TradeUpdate {
2020-10-01 08:48:08 +00:00
return TradeUpdate{
ID: v.GetUint64("i"),
Side: string(v.GetStringBytes("sd")),
Price: string(v.GetStringBytes("p")),
Volume: string(v.GetStringBytes("v")),
Market: string(v.GetStringBytes("M")),
Fee: string(v.GetStringBytes("f")),
FeeCurrency: string(v.GetStringBytes("fc")),
Timestamp: v.GetInt64("T"),
OrderID: v.GetUint64("oi"),
2020-10-19 14:23:49 +00:00
Maker: v.GetBool("m"),
2020-10-01 09:05:46 +00:00
}
2020-10-01 08:48:08 +00:00
}
2020-10-01 09:05:46 +00:00
type TradeUpdateEvent struct {
BaseEvent
Trades []TradeUpdate `json:"t"`
}
2020-10-02 13:29:56 +00:00
func parseTradeUpdateEvent(v *fastjson.Value) *TradeUpdateEvent {
var e TradeUpdateEvent
2020-10-01 09:05:46 +00:00
e.Event = string(v.GetStringBytes("e"))
e.Timestamp = v.GetInt64("T")
for _, tv := range v.GetArray("t") {
e.Trades = append(e.Trades, parseTradeUpdate(tv))
2020-10-01 08:48:08 +00:00
}
2020-10-02 13:29:56 +00:00
return &e
2020-10-01 08:48:08 +00:00
}
type TradeSnapshot []TradeUpdate
2020-10-01 09:05:46 +00:00
type TradeSnapshotEvent struct {
BaseEvent
2020-10-01 08:48:08 +00:00
2020-10-01 09:05:46 +00:00
Trades []TradeUpdate `json:"t"`
}
2020-10-02 13:29:56 +00:00
func parseTradeSnapshotEvent(v *fastjson.Value) *TradeSnapshotEvent {
var e TradeSnapshotEvent
2020-10-01 09:05:46 +00:00
e.Event = string(v.GetStringBytes("e"))
e.Timestamp = v.GetInt64("T")
for _, tv := range v.GetArray("t") {
e.Trades = append(e.Trades, parseTradeUpdate(tv))
2020-10-01 08:48:08 +00:00
}
2020-10-02 13:29:56 +00:00
return &e
2020-10-01 08:48:08 +00:00
}
2020-10-02 02:10:59 +00:00
type BalanceMessage struct {
2020-10-01 08:48:08 +00:00
Currency string `json:"cu"`
Available string `json:"av"`
Locked string `json:"l"`
}
2020-10-03 03:11:59 +00:00
func (m *BalanceMessage) Balance() (*types.Balance, error) {
available, err := util.ParseFloat(m.Available)
if err != nil {
return nil, err
}
locked, err := util.ParseFloat(m.Locked)
if err != nil {
return nil, err
}
return &types.Balance{
2020-10-09 05:21:42 +00:00
Currency: m.Currency,
Locked: locked,
2020-10-03 03:11:59 +00:00
Available: available,
}, nil
}
2020-10-02 02:10:59 +00:00
func parseBalance(v *fastjson.Value) BalanceMessage {
return BalanceMessage{
2020-10-01 08:48:08 +00:00
Currency: string(v.GetStringBytes("cu")),
Available: string(v.GetStringBytes("av")),
Locked: string(v.GetStringBytes("l")),
2020-10-01 09:05:46 +00:00
}
2020-10-01 08:48:08 +00:00
}
2020-10-01 09:05:46 +00:00
type AccountUpdateEvent struct {
BaseEvent
2020-10-02 02:10:59 +00:00
Balances []BalanceMessage `json:"B"`
2020-10-01 08:48:08 +00:00
}
2020-10-02 13:29:56 +00:00
func parserAccountUpdateEvent(v *fastjson.Value) *AccountUpdateEvent {
var e AccountUpdateEvent
2020-10-01 09:05:46 +00:00
e.Event = string(v.GetStringBytes("e"))
e.Timestamp = v.GetInt64("T")
2020-10-01 08:48:08 +00:00
2020-10-01 09:05:46 +00:00
for _, bv := range v.GetArray("B") {
e.Balances = append(e.Balances, parseBalance(bv))
2020-10-01 08:48:08 +00:00
}
2020-10-02 13:29:56 +00:00
return &e
2020-10-01 09:05:46 +00:00
}
type AccountSnapshotEvent struct {
BaseEvent
2020-10-02 02:10:59 +00:00
Balances []BalanceMessage `json:"B"`
2020-10-01 09:05:46 +00:00
}
2020-10-02 13:29:56 +00:00
func parserAccountSnapshotEvent(v *fastjson.Value) *AccountSnapshotEvent {
var e AccountSnapshotEvent
2020-10-01 09:05:46 +00:00
e.Event = string(v.GetStringBytes("e"))
e.Timestamp = v.GetInt64("T")
for _, bv := range v.GetArray("B") {
e.Balances = append(e.Balances, parseBalance(bv))
2020-10-01 08:48:08 +00:00
}
2020-10-02 13:29:56 +00:00
return &e
2020-10-01 08:48:08 +00:00
}
2020-10-02 13:29:56 +00:00
func parseAuthEvent(v *fastjson.Value) *AuthEvent {
return &AuthEvent{
2020-10-01 08:48:08 +00:00
Event: string(v.GetStringBytes("e")),
ID: string(v.GetStringBytes("i")),
Timestamp: v.GetInt64("T"),
2020-10-01 09:05:46 +00:00
}
}
2020-10-02 04:43:14 +00:00
func ParseUserEvent(v *fastjson.Value) (interface{}, error) {
2020-10-01 09:05:46 +00:00
eventType := string(v.GetStringBytes("e"))
switch eventType {
case "order_snapshot":
return parserOrderSnapshotEvent(v), nil
case "order_update":
return parseOrderUpdateEvent(v), nil
case "trade_snapshot":
return parseTradeSnapshotEvent(v), nil
case "trade_update":
return parseTradeUpdateEvent(v), nil
case "account_snapshot":
return parserAccountSnapshotEvent(v), nil
case "account_update":
return parserAccountUpdateEvent(v), nil
case "authenticated":
2020-10-02 04:43:14 +00:00
return parseAuthEvent(v), nil
2020-10-01 09:05:46 +00:00
case "error":
2020-10-02 04:43:14 +00:00
logger.Errorf("error %s", v.MarshalTo(nil))
2020-10-01 09:05:46 +00:00
}
2020-10-02 04:43:14 +00:00
return nil, errors.Wrapf(ErrMessageTypeNotSupported, "private message %s", v.MarshalTo(nil))
2020-10-01 08:48:08 +00:00
}