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().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,6 +351,12 @@ 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 {
@ -355,6 +371,7 @@ var BacktestCmd = &cobra.Command{
})
}
runCtx, cancelRun := context.WithCancel(ctx)
go func() {
defer cancelRun()