From ecd4df86f9d3cd853c57280f0dfe1795fe380738 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 4 Jul 2022 02:21:14 +0800 Subject: [PATCH] backtest: assign user data stream to backtest.Exchange before we call EmitStart --- pkg/backtest/matching.go | 3 ++- pkg/bbgo/activeorderbook.go | 2 -- pkg/cmd/backtest.go | 16 ++++++++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/backtest/matching.go b/pkg/backtest/matching.go index 6b7b75185..b2f06d512 100644 --- a/pkg/backtest/matching.go +++ b/pkg/backtest/matching.go @@ -99,7 +99,6 @@ func (m *SimplePriceMatching) CancelOrder(o types.Order) (types.Order, error) { } m.askOrders = orders m.mu.Unlock() - } if !found { @@ -191,6 +190,8 @@ func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (*types.Order, *ty order2.ExecutedQuantity = order2.Quantity order2.IsWorking = false + m.EmitOrderUpdate(order2) + // let the exchange emit the "FILLED" order update (we need the closed order) // m.EmitOrderUpdate(order2) return &order2, &trade, nil diff --git a/pkg/bbgo/activeorderbook.go b/pkg/bbgo/activeorderbook.go index e720e46fd..ba13b6293 100644 --- a/pkg/bbgo/activeorderbook.go +++ b/pkg/bbgo/activeorderbook.go @@ -131,8 +131,6 @@ func (b *ActiveOrderBook) orderUpdateHandler(order types.Order) { return } - log.Debugf("[ActiveOrderBook] received order update: %+v", order) - switch order.Status { case types.OrderStatusFilled: // make sure we have the order and we remove it diff --git a/pkg/cmd/backtest.go b/pkg/cmd/backtest.go index 1c1ac11a4..e9a567b9b 100644 --- a/pkg/cmd/backtest.go +++ b/pkg/cmd/backtest.go @@ -267,6 +267,12 @@ var BacktestCmd = &cobra.Command{ return err } + for _, session := range environ.Sessions() { + backtestEx := session.Exchange.(*backtest.Exchange) + backtestEx.UserDataStream = session.UserDataStream.(types.StandardStreamEmitter) + backtestEx.MarketDataStream = session.MarketDataStream.(types.StandardStreamEmitter) + } + trader := bbgo.NewTrader(environ) if verboseCnt == 0 { trader.DisableLogging() @@ -642,12 +648,10 @@ func confirmation(s string) bool { func toExchangeSources(sessions map[string]*bbgo.ExchangeSession, extraIntervals ...types.Interval) (exchangeSources []backtest.ExchangeDataSource, err error) { for _, session := range sessions { - exchange := session.Exchange.(*backtest.Exchange) - exchange.UserDataStream = session.UserDataStream.(types.StandardStreamEmitter) - exchange.MarketDataStream = session.MarketDataStream.(types.StandardStreamEmitter) - exchange.InitMarketData() + backtestEx := session.Exchange.(*backtest.Exchange) + backtestEx.InitMarketData() - c, err := exchange.SubscribeMarketData(extraIntervals...) + c, err := backtestEx.SubscribeMarketData(extraIntervals...) if err != nil { return exchangeSources, err } @@ -655,7 +659,7 @@ func toExchangeSources(sessions map[string]*bbgo.ExchangeSession, extraIntervals sessionCopy := session exchangeSources = append(exchangeSources, backtest.ExchangeDataSource{ C: c, - Exchange: exchange, + Exchange: backtestEx, Session: sessionCopy, }) }