mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
support different time format for backtesting
This commit is contained in:
parent
0472b7f21e
commit
aeeecba8dc
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user