support different time format for backtesting

This commit is contained in:
c9s 2021-12-06 01:50:50 +08:00
parent 0472b7f21e
commit aeeecba8dc

View File

@ -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 {