bbgo: fix margin order/trade sync

This commit is contained in:
c9s 2022-04-27 13:25:42 +08:00
parent 8ef5fe3a53
commit 5edaa9708c
No known key found for this signature in database
GPG Key ID: F0A7E4490F2EBC8C

View File

@ -464,12 +464,32 @@ func (environ *Environment) BindSync(config *SyncConfig) {
environ.syncConfig = config environ.syncConfig = config
tradeWriter := func(trade types.Trade) { tradeWriterCreator := func(session *ExchangeSession) func(trade types.Trade) {
return func(trade types.Trade) {
trade.IsMargin = session.Margin
trade.IsFutures = session.Futures
if session.Margin {
trade.IsIsolated = session.IsolatedMargin
} else if session.Futures {
trade.IsIsolated = session.IsolatedFutures
}
if err := environ.TradeService.Insert(trade); err != nil { if err := environ.TradeService.Insert(trade); err != nil {
log.WithError(err).Errorf("trade insert error: %+v", trade) log.WithError(err).Errorf("trade insert error: %+v", trade)
} }
} }
orderWriter := func(order types.Order) { }
orderWriterCreator := func(session *ExchangeSession) func(order types.Order) {
return func(order types.Order) {
order.IsMargin = session.Margin
order.IsFutures = session.Futures
if session.Margin {
order.IsIsolated = session.IsolatedMargin
} else if session.Futures {
order.IsIsolated = session.IsolatedFutures
}
switch order.Status { switch order.Status {
case types.OrderStatusFilled, types.OrderStatusCanceled: case types.OrderStatusFilled, types.OrderStatusCanceled:
if order.ExecutedQuantity.Sign() > 0 { if order.ExecutedQuantity.Sign() > 0 {
@ -479,13 +499,17 @@ func (environ *Environment) BindSync(config *SyncConfig) {
} }
} }
} }
}
for _, session := range environ.sessions { for _, session := range environ.sessions {
// if trade sync is on, we will write all received trades // if trade sync is on, we will write all received trades
if config.UserDataStream.Trades { if config.UserDataStream.Trades {
tradeWriter := tradeWriterCreator(session)
session.UserDataStream.OnTradeUpdate(tradeWriter) session.UserDataStream.OnTradeUpdate(tradeWriter)
} }
if config.UserDataStream.FilledOrders { if config.UserDataStream.FilledOrders {
orderWriter := orderWriterCreator(session)
session.UserDataStream.OnOrderUpdate(orderWriter) session.UserDataStream.OnOrderUpdate(orderWriter)
} }
} }