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
import (
"fmt"
"strings"
"time"
"github.com/c9s/bbgo/pkg/accounting/pnl"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
@ -15,3 +19,14 @@ type Report struct {
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
}
backtestSessionName, err := cmd.Flags().GetString("session")
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
@ -172,24 +172,18 @@ var BacktestCmd = &cobra.Command{
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
environ.BacktestService = backtestService
if len(backtestSessionName) > 0 {
userConfig.Backtest.Sessions = []string{backtestSessionName}
if len(sessionName) > 0 {
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)
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 {
exName, err := types.ValidExchangeName(name)
if err != nil {
@ -202,16 +196,6 @@ var BacktestCmd = &cobra.Command{
}
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 {
var syncFromTime time.Time
@ -342,10 +326,19 @@ var BacktestCmd = &cobra.Command{
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)
if generatingReport {
dumpDir := outputDirectory
if reportFileInSubDir {
dumpDir = filepath.Join(dumpDir, backtestSessionName)
dumpDir = filepath.Join(dumpDir, uuid.NewString())
}