backtest: update backtest.Exchange currentTime

This commit is contained in:
c9s 2022-07-04 02:38:42 +08:00
parent 82f9fc139c
commit c258d522e6
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 6 additions and 3 deletions

View File

@ -53,7 +53,7 @@ type Exchange struct {
sourceName types.ExchangeName sourceName types.ExchangeName
publicExchange types.Exchange publicExchange types.Exchange
srv *service.BacktestService srv *service.BacktestService
startTime time.Time currentTime time.Time
account *types.Account account *types.Account
config *bbgo.Backtest config *bbgo.Backtest
@ -99,7 +99,7 @@ func NewExchange(sourceName types.ExchangeName, sourceExchange types.Exchange, s
srv: srv, srv: srv,
config: config, config: config,
account: account, account: account,
startTime: startTime, currentTime: startTime,
closedOrders: make(map[string][]types.Order), closedOrders: make(map[string][]types.Order),
trades: make(map[string][]types.Trade), 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) { func (e *Exchange) _addMatchingBook(symbol string, market types.Market) {
e.matchingBooks[symbol] = &SimplePriceMatching{ e.matchingBooks[symbol] = &SimplePriceMatching{
CurrentTime: e.startTime, CurrentTime: e.currentTime,
Account: e.account, Account: e.account,
Market: market, Market: market,
closedOrders: make(map[uint64]types.Order), 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) { func (e *Exchange) ConsumeKLine(k types.KLine) {
if k.Interval == types.Interval1m { if k.Interval == types.Interval1m {
e.currentTime = k.EndTime.Time()
matching, ok := e.matchingBook(k.Symbol) matching, ok := e.matchingBook(k.Symbol)
if !ok { if !ok {
log.Errorf("matching book of %s is not initialized", k.Symbol) log.Errorf("matching book of %s is not initialized", k.Symbol)

View File

@ -571,6 +571,7 @@ func (m *SimplePriceMatching) getOrder(orderID uint64) (types.Order, bool) {
func (m *SimplePriceMatching) processKLine(kline types.KLine) { func (m *SimplePriceMatching) processKLine(kline types.KLine) {
m.CurrentTime = kline.EndTime.Time() m.CurrentTime = kline.EndTime.Time()
if m.LastPrice.IsZero() { if m.LastPrice.IsZero() {
m.LastPrice = kline.Open m.LastPrice = kline.Open
} else { } else {