Skip to content

Commit

Permalink
backtest: apply default symbols from the config when there is no subs…
Browse files Browse the repository at this point in the history
…cription
  • Loading branch information
c9s committed Jan 20, 2025
1 parent 14a5818 commit d424a50
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
11 changes: 9 additions & 2 deletions pkg/backtest/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ func (e *Exchange) BindUserData(userDataStream types.StandardStreamEmitter) {
}

func (e *Exchange) SubscribeMarketData(
startTime, endTime time.Time, requiredInterval types.Interval, extraIntervals ...types.Interval,
startTime, endTime time.Time,
defaultSymbols []string,
requiredInterval types.Interval,
extraIntervals ...types.Interval,
) (chan types.KLine, error) {
log.Infof("collecting backtest configurations...")

Expand Down Expand Up @@ -370,6 +373,10 @@ func (e *Exchange) SubscribeMarketData(
symbols = append(symbols, symbol)
}

if len(symbols) == 0 {
symbols = defaultSymbols
}

var intervals []types.Interval
for interval := range loadedIntervals {
intervals = append(intervals, interval)
Expand All @@ -389,7 +396,7 @@ func (e *Exchange) SubscribeMarketData(
}

if len(symbols) == 0 {
log.Warnf("empty symbols, will not query kline data from the database")
log.Warnf("empty symbols, will not query kline data from the database, default symbols = %v", defaultSymbols)

c := make(chan types.KLine)
close(c)
Expand Down
9 changes: 7 additions & 2 deletions pkg/backtest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ func CollectSubscriptionIntervals(environ *bbgo.Environment) (allKLineIntervals
return allKLineIntervals, requiredInterval, backTestIntervals
}

func InitializeExchangeSources(sessions map[string]*bbgo.ExchangeSession, startTime, endTime time.Time, requiredInterval types.Interval, extraIntervals ...types.Interval) (exchangeSources []*ExchangeDataSource, err error) {
func InitializeExchangeSources(
sessions map[string]*bbgo.ExchangeSession,
startTime, endTime time.Time,
defaultSymbols []string, requiredInterval types.Interval,
extraIntervals ...types.Interval,
) (exchangeSources []*ExchangeDataSource, err error) {
for _, session := range sessions {
backtestEx := session.Exchange.(*Exchange)

c, err := backtestEx.SubscribeMarketData(startTime, endTime, requiredInterval, extraIntervals...)
c, err := backtestEx.SubscribeMarketData(startTime, endTime, defaultSymbols, requiredInterval, extraIntervals...)
if err != nil {
return exchangeSources, err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/backtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ var BacktestCmd = &cobra.Command{
}

allKLineIntervals, requiredInterval, backTestIntervals := backtest.CollectSubscriptionIntervals(environ)
exchangeSources, err := backtest.InitializeExchangeSources(environ.Sessions(), startTime, endTime, requiredInterval, backTestIntervals...)
exchangeSources, err := backtest.InitializeExchangeSources(environ.Sessions(),
startTime, endTime,
userConfig.Backtest.Symbols,
requiredInterval, backTestIntervals...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/grid2/backtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func RunBacktest(t *testing.T, strategy bbgo.SingleExchangeStrategy) {
t.Logf("requiredInterval: %s backTestIntervals: %v", requiredInterval, backTestIntervals)

_ = allKLineIntervals
exchangeSources, err := backtest.InitializeExchangeSources(environ.Sessions(), startTime.Time(), endTime.Time(), requiredInterval, backTestIntervals...)
exchangeSources, err := backtest.InitializeExchangeSources(environ.Sessions(), startTime.Time(), endTime.Time(), nil, requiredInterval, backTestIntervals...)
if !assert.NoError(t, err) {
return
}
Expand Down

0 comments on commit d424a50

Please sign in to comment.