refactor backtest, add BootstrapBacktestEnvironment

This commit is contained in:
c9s 2021-05-08 00:14:25 +08:00
parent b9aa182727
commit 3501e8f5fd
4 changed files with 32 additions and 16 deletions

View File

@ -26,7 +26,7 @@ backtest:
# see here for more details
# https://www.investopedia.com/terms/m/maximum-drawdown-mdd.asp
startTime: "2021-01-01"
endTime: "2021-01-30"
endTime: "2021-01-03"
symbols:
- USDTTWD
account:

View File

@ -4,11 +4,13 @@ import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/types"
)
var log = logrus.WithField("cmd", "backtest")
type Stream struct {
types.StandardStream
@ -53,13 +55,17 @@ func (s *Stream) Connect(ctx context.Context) error {
s.EmitConnect()
klineC, errC := s.exchange.srv.QueryKLinesCh(s.exchange.startTime, s.exchange.endTime, s.exchange, symbols, intervals)
numKlines := 0
for k := range klineC {
if k.Interval == types.Interval1m {
matching, ok := s.exchange.matchingBooks[k.Symbol]
if !ok {
log.Errorf("matching book of %s is not initialized", k.Symbol)
continue
}
matching.processKLine(k)
numKlines++
}
s.EmitKLineClosed(k)
@ -69,6 +75,10 @@ func (s *Stream) Connect(ctx context.Context) error {
log.WithError(err).Error("backtest data feed error")
}
if numKlines == 0 {
log.Error("kline data is empty, make sure you have sync the exchange market data")
}
if err := s.Close(); err != nil {
log.WithError(err).Error("stream close error")
}

View File

@ -192,14 +192,10 @@ var BacktestCmd = &cobra.Command{
}
backtestExchange := backtest.NewExchange(exchangeName, backtestService, userConfig.Backtest)
environ.SetStartTime(startTime)
environ.AddExchange(exchangeName.String(), backtestExchange)
environ.Notifiability = bbgo.Notifiability{
SymbolChannelRouter: bbgo.NewPatternChannelRouter(nil),
SessionChannelRouter: bbgo.NewPatternChannelRouter(nil),
ObjectChannelRouter: bbgo.NewObjectChannelRouter(),
if err := BootstrapBacktestEnvironment(ctx, environ, userConfig) ; err != nil {
return err
}
trader := bbgo.NewTrader(environ)
@ -214,14 +210,8 @@ var BacktestCmd = &cobra.Command{
trader.DisableLogging()
}
if userConfig.RiskControls != nil {
log.Infof("setting risk controls: %+v", userConfig.RiskControls)
trader.SetRiskControls(userConfig.RiskControls)
}
for _, entry := range userConfig.ExchangeStrategies {
log.Infof("attaching strategy %T on %s instead of %v", entry.Strategy, exchangeName.String(), entry.Mounts)
trader.AttachStrategyOn(exchangeName.String(), entry.Strategy)
if err := trader.Configure(userConfig) ; err != nil {
return err
}
if len(userConfig.CrossExchangeStrategies) > 0 {

View File

@ -79,6 +79,22 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
return nil
}
func BootstrapBacktestEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
if err := environ.ConfigureDatabase(ctx); err != nil {
return err
}
environ.Notifiability = bbgo.Notifiability{
SymbolChannelRouter: bbgo.NewPatternChannelRouter(nil),
SessionChannelRouter: bbgo.NewPatternChannelRouter(nil),
ObjectChannelRouter: bbgo.NewObjectChannelRouter(),
}
return nil
}
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
if err := environ.ConfigureDatabase(ctx); err != nil {
return err