From 428e208120ca89e2173c8caef7f486adfc971e54 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 9 May 2022 19:14:09 +0800 Subject: [PATCH] cmd: add backtest --session option to make it backward compatible --- pkg/cmd/backtest.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/backtest.go b/pkg/cmd/backtest.go index 69caa647c..dbf14027c 100644 --- a/pkg/cmd/backtest.go +++ b/pkg/cmd/backtest.go @@ -32,6 +32,8 @@ func init() { 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-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("base-asset-baseline", false, "use base asset performance as the competitive baseline performance") @@ -81,6 +83,11 @@ var BacktestCmd = &cobra.Command{ return err } + backtestSessionName, err := cmd.Flags().GetString("session") + if err != nil { + return err + } + force, err := cmd.Flags().GetBool("force") if err != nil { return err @@ -165,6 +172,10 @@ var BacktestCmd = &cobra.Command{ backtestService := &service.BacktestService{DB: environ.DatabaseService.DB} environ.BacktestService = backtestService + if len(backtestSessionName) > 0 { + userConfig.Backtest.Sessions = []string{backtestSessionName} + } + var sourceExchanges = make(map[types.ExchangeName]types.Exchange) if len(syncExchangeName) > 0 { exName, err := types.ValidExchangeName(syncExchangeName) @@ -331,7 +342,6 @@ var BacktestCmd = &cobra.Command{ exchangeSources = append(exchangeSources, backtest.ExchangeDataSource{C: c, Exchange: exchange}) } - runCtx, cancelRun := context.WithCancel(ctx) var kLineHandlers []func(k types.KLine) if generatingReport { dumpDir := outputDirectory @@ -341,9 +351,15 @@ var BacktestCmd = &cobra.Command{ 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) defer func() { - if err := dumper.Close() ; err != nil { + if err := dumper.Close(); err != nil { 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() { defer cancelRun()