backtest: assign user data stream to backtest.Exchange before we call EmitStart

This commit is contained in:
c9s 2022-07-04 02:21:14 +08:00
parent 449b2d8220
commit ecd4df86f9
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 12 additions and 9 deletions

View File

@ -99,7 +99,6 @@ func (m *SimplePriceMatching) CancelOrder(o types.Order) (types.Order, error) {
} }
m.askOrders = orders m.askOrders = orders
m.mu.Unlock() m.mu.Unlock()
} }
if !found { if !found {
@ -191,6 +190,8 @@ func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (*types.Order, *ty
order2.ExecutedQuantity = order2.Quantity order2.ExecutedQuantity = order2.Quantity
order2.IsWorking = false order2.IsWorking = false
m.EmitOrderUpdate(order2)
// let the exchange emit the "FILLED" order update (we need the closed order) // let the exchange emit the "FILLED" order update (we need the closed order)
// m.EmitOrderUpdate(order2) // m.EmitOrderUpdate(order2)
return &order2, &trade, nil return &order2, &trade, nil

View File

@ -131,8 +131,6 @@ func (b *ActiveOrderBook) orderUpdateHandler(order types.Order) {
return return
} }
log.Debugf("[ActiveOrderBook] received order update: %+v", order)
switch order.Status { switch order.Status {
case types.OrderStatusFilled: case types.OrderStatusFilled:
// make sure we have the order and we remove it // make sure we have the order and we remove it

View File

@ -267,6 +267,12 @@ var BacktestCmd = &cobra.Command{
return err 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) trader := bbgo.NewTrader(environ)
if verboseCnt == 0 { if verboseCnt == 0 {
trader.DisableLogging() 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) { func toExchangeSources(sessions map[string]*bbgo.ExchangeSession, extraIntervals ...types.Interval) (exchangeSources []backtest.ExchangeDataSource, err error) {
for _, session := range sessions { for _, session := range sessions {
exchange := session.Exchange.(*backtest.Exchange) backtestEx := session.Exchange.(*backtest.Exchange)
exchange.UserDataStream = session.UserDataStream.(types.StandardStreamEmitter) backtestEx.InitMarketData()
exchange.MarketDataStream = session.MarketDataStream.(types.StandardStreamEmitter)
exchange.InitMarketData()
c, err := exchange.SubscribeMarketData(extraIntervals...) c, err := backtestEx.SubscribeMarketData(extraIntervals...)
if err != nil { if err != nil {
return exchangeSources, err return exchangeSources, err
} }
@ -655,7 +659,7 @@ func toExchangeSources(sessions map[string]*bbgo.ExchangeSession, extraIntervals
sessionCopy := session sessionCopy := session
exchangeSources = append(exchangeSources, backtest.ExchangeDataSource{ exchangeSources = append(exchangeSources, backtest.ExchangeDataSource{
C: c, C: c,
Exchange: exchange, Exchange: backtestEx,
Session: sessionCopy, Session: sessionCopy,
}) })
} }