cmd: add backtest --session option to make it backward compatible

This commit is contained in:
c9s 2022-05-09 19:14:09 +08:00
parent 0780dafdc3
commit 428e208120
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -32,6 +32,8 @@ func init() {
BacktestCmd.Flags().Bool("sync-only", false, "sync backtest data only, do not run backtest") BacktestCmd.Flags().Bool("sync-only", false, "sync backtest data only, do not run backtest")
BacktestCmd.Flags().String("sync-from", "", "sync backtest data from the given time, which will override the time range in the backtest config") BacktestCmd.Flags().String("sync-from", "", "sync backtest data from the given time, which will override the time range in the backtest config")
BacktestCmd.Flags().String("sync-exchange", "", "specify only one exchange to sync backtest data") BacktestCmd.Flags().String("sync-exchange", "", "specify only one exchange to sync backtest data")
BacktestCmd.Flags().String("session", "", "specify only one exchange session to run backtest")
BacktestCmd.Flags().Bool("verify", false, "verify the kline back-test data") BacktestCmd.Flags().Bool("verify", false, "verify the kline back-test data")
BacktestCmd.Flags().Bool("base-asset-baseline", false, "use base asset performance as the competitive baseline performance") BacktestCmd.Flags().Bool("base-asset-baseline", false, "use base asset performance as the competitive baseline performance")
@ -81,6 +83,11 @@ var BacktestCmd = &cobra.Command{
return err return err
} }
backtestSessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
force, err := cmd.Flags().GetBool("force") force, err := cmd.Flags().GetBool("force")
if err != nil { if err != nil {
return err return err
@ -165,6 +172,10 @@ var BacktestCmd = &cobra.Command{
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB} backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
environ.BacktestService = backtestService environ.BacktestService = backtestService
if len(backtestSessionName) > 0 {
userConfig.Backtest.Sessions = []string{backtestSessionName}
}
var sourceExchanges = make(map[types.ExchangeName]types.Exchange) var sourceExchanges = make(map[types.ExchangeName]types.Exchange)
if len(syncExchangeName) > 0 { if len(syncExchangeName) > 0 {
exName, err := types.ValidExchangeName(syncExchangeName) exName, err := types.ValidExchangeName(syncExchangeName)
@ -331,7 +342,6 @@ var BacktestCmd = &cobra.Command{
exchangeSources = append(exchangeSources, backtest.ExchangeDataSource{C: c, Exchange: exchange}) exchangeSources = append(exchangeSources, backtest.ExchangeDataSource{C: c, Exchange: exchange})
} }
runCtx, cancelRun := context.WithCancel(ctx)
var kLineHandlers []func(k types.KLine) var kLineHandlers []func(k types.KLine)
if generatingReport { if generatingReport {
dumpDir := outputDirectory dumpDir := outputDirectory
@ -341,9 +351,15 @@ var BacktestCmd = &cobra.Command{
dumpDir = filepath.Join(dumpDir, "klines") dumpDir = filepath.Join(dumpDir, "klines")
if _, err := os.Stat(dumpDir) ; os.IsNotExist(err) {
if err2 := os.MkdirAll(dumpDir, 0755) ; err2 != nil {
return err2
}
}
dumper := backtest.NewKLineDumper(dumpDir) dumper := backtest.NewKLineDumper(dumpDir)
defer func() { defer func() {
if err := dumper.Close() ; err != nil { if err := dumper.Close(); err != nil {
log.WithError(err).Errorf("kline dumper can not close files") log.WithError(err).Errorf("kline dumper can not close files")
} }
}() }()
@ -355,6 +371,7 @@ var BacktestCmd = &cobra.Command{
}) })
} }
runCtx, cancelRun := context.WithCancel(ctx)
go func() { go func() {
defer cancelRun() defer cancelRun()