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

244 lines
5.5 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-01 09:05:46 +00:00
type BaseEvent struct {
Event string `json:"e"`
Timestamp int64 `json:"T"`
2020-10-01 08:48:08 +00:00
}
type OrderUpdate struct {
Event string `json:"e"`
ID uint64 `json:"i"`
Side string `json:"sd"`
OrderType string `json:"ot"`
2020-10-01 09:08:12 +00:00
2020-10-01 08:48:08 +00:00
Price string `json:"p"`
2020-10-01 09:08:12 +00:00
StopPrice string `json:"sp"`
2020-10-01 08:48:08 +00:00
Volume string `json:"v"`
AveragePrice string `json:"ap"`
State string `json:"S"`
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: string(v.GetStringBytes("ot")),
State: string(v.GetStringBytes("S")),
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-01 09:05:46 +00:00
func parseOrderUpdateEvent(v *fastjson.Value) (e OrderUpdateEvent) {
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-01 09:05:46 +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-01 09:05:46 +00:00
Orders []OrderUpdate `json:"o"`
}
2020-10-01 08:48:08 +00:00
2020-10-01 09:05:46 +00:00
func parserOrderSnapshotEvent(v *fastjson.Value) (e OrderSnapshotEvent) {
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-01 09:05:46 +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-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-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"`
}
func parseTradeUpdateEvent(v *fastjson.Value) (e TradeUpdateEvent) {
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-01 09:05:46 +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"`
}
func parseTradeSnapshotEvent(v *fastjson.Value) (e TradeSnapshotEvent) {
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-01 09:05:46 +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-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-01 09:05:46 +00:00
func parserAccountUpdateEvent(v *fastjson.Value) (e AccountUpdateEvent) {
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-01 09:05:46 +00:00
return e
}
type AccountSnapshotEvent struct {
BaseEvent
2020-10-02 02:10:59 +00:00
Balances []BalanceMessage `json:"B"`
2020-10-01 09:05:46 +00:00
}
func parserAccountSnapshotEvent(v *fastjson.Value) (e AccountSnapshotEvent) {
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-01 09:05:46 +00:00
return e
2020-10-01 08:48:08 +00:00
}
2020-10-02 04:43:14 +00:00
func parseAuthEvent(v *fastjson.Value) AuthEvent {
2020-10-01 08:48:08 +00:00
return AuthEvent{
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
}