From aeeecba8dc1e19abb7ca40c46a194a6b305af9a0 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 6 Dec 2021 01:50:50 +0800 Subject: [PATCH] support different time format for backtesting --- pkg/bbgo/config.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/bbgo/config.go b/pkg/bbgo/config.go index 0bee2d364..ced6fc452 100644 --- a/pkg/bbgo/config.go +++ b/pkg/bbgo/config.go @@ -94,6 +94,13 @@ type Session struct { IsolatedMarginSymbol string `json:"isolatedMarginSymbol,omitempty" yaml:"isolatedMarginSymbol,omitempty"` } +var supportedTimeFormats = []string{ + time.RFC3339, + time.RFC822, + "2006-01-02T15:04:05", + "2006-01-02", +} + type Backtest struct { StartTime string `json:"startTime" yaml:"startTime"` EndTime string `json:"endTime" yaml:"endTime"` @@ -104,12 +111,23 @@ type Backtest struct { Symbols []string `json:"symbols" yaml:"symbols"` } +func parseTimeWithFormats(strTime string, formats []string) (time.Time, error) { + for _, format := range formats { + tt, err := time.Parse(format, strTime) + if err == nil { + return tt, nil + } + } + return time.Time{}, fmt.Errorf("failed to parse time %s, valid formats are %+v", strTime, formats) +} + + func (t Backtest) ParseEndTime() (time.Time, error) { if len(t.EndTime) == 0 { return time.Time{}, errors.New("backtest.endTime must be defined") } - return time.Parse("2006-01-02", t.EndTime) + return parseTimeWithFormats(t.EndTime, supportedTimeFormats) } func (t Backtest) ParseStartTime() (time.Time, error) { @@ -117,7 +135,7 @@ func (t Backtest) ParseStartTime() (time.Time, error) { return time.Time{}, errors.New("backtest.startTime must be defined") } - return time.Parse("2006-01-02", t.StartTime) + return parseTimeWithFormats(t.StartTime, supportedTimeFormats) } type BacktestAccount struct {