format backtest report session name

This commit is contained in:
c9s 2022-05-09 19:27:02 +08:00
parent 428e208120
commit bff73a3a80
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 38 additions and 30 deletions

View File

@ -1,6 +1,10 @@
package backtest package backtest
import ( import (
"fmt"
"strings"
"time"
"github.com/c9s/bbgo/pkg/accounting/pnl" "github.com/c9s/bbgo/pkg/accounting/pnl"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
@ -15,3 +19,14 @@ type Report struct {
FinalBalances types.BalanceMap `json:"finalBalances,omitempty"` FinalBalances types.BalanceMap `json:"finalBalances,omitempty"`
} }
const SessionTimeFormat = "2006-01-02T15_04"
// FormatSessionName returns the back-test session name
func FormatSessionName(sessions []string, symbols []string, startTime, endTime time.Time) string {
return fmt.Sprintf("%s_%s_%s-%s",
strings.Join(sessions, "-"),
strings.Join(symbols, "-"),
startTime.Format(SessionTimeFormat),
endTime.Format(SessionTimeFormat),
)
}

View File

@ -83,7 +83,7 @@ var BacktestCmd = &cobra.Command{
return err return err
} }
backtestSessionName, err := cmd.Flags().GetString("session") sessionName, err := cmd.Flags().GetString("session")
if err != nil { if err != nil {
return err return err
} }
@ -172,24 +172,18 @@ 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 { if len(sessionName) > 0 {
userConfig.Backtest.Sessions = []string{backtestSessionName} userConfig.Backtest.Sessions = []string{sessionName}
} else if len(syncExchangeName) > 0 {
userConfig.Backtest.Sessions = []string{syncExchangeName}
} else if len(userConfig.Backtest.Sessions) == 0 {
log.Infof("backtest.sessions is not defined, using all supported exchanges: %v", types.SupportedExchanges)
for _, exName := range types.SupportedExchanges {
userConfig.Backtest.Sessions = append(userConfig.Backtest.Sessions, exName.String())
}
} }
var sourceExchanges = make(map[types.ExchangeName]types.Exchange) var sourceExchanges = make(map[types.ExchangeName]types.Exchange)
if len(syncExchangeName) > 0 {
exName, err := types.ValidExchangeName(syncExchangeName)
if err != nil {
return err
}
publicExchange, err := cmdutil.NewExchangePublic(exName)
if err != nil {
return err
}
sourceExchanges[exName] = publicExchange
} else if len(userConfig.Backtest.Sessions) > 0 {
for _, name := range userConfig.Backtest.Sessions { for _, name := range userConfig.Backtest.Sessions {
exName, err := types.ValidExchangeName(name) exName, err := types.ValidExchangeName(name)
if err != nil { if err != nil {
@ -202,16 +196,6 @@ var BacktestCmd = &cobra.Command{
} }
sourceExchanges[exName] = publicExchange sourceExchanges[exName] = publicExchange
} }
} else {
log.Infof("backtest.sessions is not defined, loading all supported exchanges: %v", types.SupportedExchanges)
for _, exName := range types.SupportedExchanges {
publicExchange, err := cmdutil.NewExchangePublic(exName)
if err != nil {
return err
}
sourceExchanges[exName] = publicExchange
}
}
if wantSync { if wantSync {
var syncFromTime time.Time var syncFromTime time.Time
@ -342,17 +326,26 @@ var BacktestCmd = &cobra.Command{
exchangeSources = append(exchangeSources, backtest.ExchangeDataSource{C: c, Exchange: exchange}) exchangeSources = append(exchangeSources, backtest.ExchangeDataSource{C: c, Exchange: exchange})
} }
// back-test session report name
var backtestSessionName = backtest.FormatSessionName(
userConfig.Backtest.Sessions,
userConfig.Backtest.Symbols,
userConfig.Backtest.StartTime.Time(),
userConfig.Backtest.EndTime.Time(),
)
var kLineHandlers []func(k types.KLine) var kLineHandlers []func(k types.KLine)
if generatingReport { if generatingReport {
dumpDir := outputDirectory dumpDir := outputDirectory
if reportFileInSubDir { if reportFileInSubDir {
dumpDir = filepath.Join(dumpDir, backtestSessionName)
dumpDir = filepath.Join(dumpDir, uuid.NewString()) dumpDir = filepath.Join(dumpDir, uuid.NewString())
} }
dumpDir = filepath.Join(dumpDir, "klines") dumpDir = filepath.Join(dumpDir, "klines")
if _, err := os.Stat(dumpDir) ; os.IsNotExist(err) { if _, err := os.Stat(dumpDir); os.IsNotExist(err) {
if err2 := os.MkdirAll(dumpDir, 0755) ; err2 != nil { if err2 := os.MkdirAll(dumpDir, 0755); err2 != nil {
return err2 return err2
} }
} }