diff --git a/pkg/backtest/exchange.go b/pkg/backtest/exchange.go index d2c74df3a..d076cfba7 100644 --- a/pkg/backtest/exchange.go +++ b/pkg/backtest/exchange.go @@ -53,7 +53,7 @@ type Exchange struct { sourceName types.ExchangeName publicExchange types.Exchange srv *service.BacktestService - startTime time.Time + currentTime time.Time account *types.Account config *bbgo.Backtest @@ -99,7 +99,7 @@ func NewExchange(sourceName types.ExchangeName, sourceExchange types.Exchange, s srv: srv, config: config, account: account, - startTime: startTime, + currentTime: startTime, closedOrders: make(map[string][]types.Order), trades: make(map[string][]types.Trade), } @@ -137,7 +137,7 @@ func (e *Exchange) addMatchingBook(symbol string, market types.Market) { func (e *Exchange) _addMatchingBook(symbol string, market types.Market) { e.matchingBooks[symbol] = &SimplePriceMatching{ - CurrentTime: e.startTime, + CurrentTime: e.currentTime, Account: e.account, Market: market, closedOrders: make(map[uint64]types.Order), @@ -364,6 +364,8 @@ func (e *Exchange) SubscribeMarketData(startTime, endTime time.Time, extraInterv func (e *Exchange) ConsumeKLine(k types.KLine) { if k.Interval == types.Interval1m { + e.currentTime = k.EndTime.Time() + matching, ok := e.matchingBook(k.Symbol) if !ok { log.Errorf("matching book of %s is not initialized", k.Symbol) diff --git a/pkg/backtest/matching.go b/pkg/backtest/matching.go index b2f06d512..ad40a567a 100644 --- a/pkg/backtest/matching.go +++ b/pkg/backtest/matching.go @@ -571,6 +571,7 @@ func (m *SimplePriceMatching) getOrder(orderID uint64) (types.Order, bool) { func (m *SimplePriceMatching) processKLine(kline types.KLine) { m.CurrentTime = kline.EndTime.Time() + if m.LastPrice.IsZero() { m.LastPrice = kline.Open } else {