handle max order update message convertion

This commit is contained in:
c9s 2020-10-29 22:53:04 +08:00
parent 64286bf198
commit 8174b64e21
2 changed files with 59 additions and 5 deletions

View File

@ -17,14 +17,14 @@ type OrderUpdate struct {
Event string `json:"e"`
ID uint64 `json:"i"`
Side string `json:"sd"`
OrderType string `json:"ot"`
OrderType OrderType `json:"ot"`
Price string `json:"p"`
StopPrice string `json:"sp"`
Volume string `json:"v"`
AveragePrice string `json:"ap"`
State string `json:"S"`
State OrderState `json:"S"`
Market string `json:"M"`
RemainingVolume string `json:"rv"`
@ -37,6 +37,7 @@ type OrderUpdate struct {
CreatedAtMs int64 `json:"T"`
}
type OrderUpdateEvent struct {
BaseEvent
@ -49,8 +50,8 @@ func parserOrderUpdate(v *fastjson.Value) OrderUpdate {
ID: v.GetUint64("i"),
Side: string(v.GetStringBytes("sd")),
Market: string(v.GetStringBytes("M")),
OrderType: string(v.GetStringBytes("ot")),
State: string(v.GetStringBytes("S")),
OrderType: OrderType(v.GetStringBytes("ot")),
State: OrderState(v.GetStringBytes("S")),
Price: string(v.GetStringBytes("p")),
StopPrice: string(v.GetStringBytes("sp")),
AveragePrice: string(v.GetStringBytes("ap")),

View File

@ -6,7 +6,9 @@ import (
"time"
max "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
)
var logger = log.WithField("exchange", "max")
@ -28,7 +30,29 @@ func NewStream(key, secret string) *Stream {
logger.Infof("M: %s", message)
})
// wss.OnTradeEvent(func(e max.PublicTradeEvent) { })
wss.OnOrderSnapshotEvent(func(e max.OrderSnapshotEvent) {
for _, o := range e.Orders {
globalOrder, err := toGlobalOrderUpdate(o)
if err != nil {
log.WithError(err).Error("websocket order snapshot convert error")
continue
}
stream.EmitOrderUpdate(*globalOrder)
}
})
wss.OnOrderUpdateEvent(func(e max.OrderUpdateEvent) {
for _, o := range e.Orders {
globalOrder, err := toGlobalOrderUpdate(o)
if err != nil {
log.WithError(err).Error("websocket order update convert error")
continue
}
stream.EmitOrderUpdate(*globalOrder)
}
})
wss.OnTradeUpdateEvent(func(e max.TradeUpdateEvent) {
for _, tradeUpdate := range e.Trades {
@ -141,3 +165,32 @@ func convertWebSocketTrade(t max.TradeUpdate) (*types.Trade, error) {
Time: mts,
}, nil
}
func toGlobalOrderUpdate(u max.OrderUpdate) (*types.Order, error) {
executedVolume, err := fixedpoint.NewFromString(u.ExecutedVolume)
if err != nil {
return nil, err
}
remainingVolume, err := fixedpoint.NewFromString(u.RemainingVolume)
if err != nil {
return nil, err
}
return &types.Order{
SubmitOrder: types.SubmitOrder{
ClientOrderID: u.ClientOID,
Symbol: u.Market,
Side: toGlobalSideType(u.Side),
Type: toGlobalOrderType(u.OrderType),
Quantity: util.MustParseFloat(u.Volume),
Price: util.MustParseFloat(u.Price),
StopPrice: util.MustParseFloat(u.StopPrice),
TimeInForce: "GTC", // MAX only supports GTC
},
OrderID: u.ID,
Status: toGlobalOrderStatus(u.State, executedVolume, remainingVolume),
ExecutedQuantity: executedVolume.Float64(),
CreationTime: time.Unix(0, u.CreatedAtMs*int64(time.Millisecond)),
}, nil
}