Skip to content

Commit

Permalink
add backtestconfig.Interval.StartDate
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Jan 20, 2024
1 parent 2d7f847 commit ac13e95
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
36 changes: 36 additions & 0 deletions backtest/backtestconfig/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,39 @@ func Annually() func(t time.Time, _ []float64) bool {
return monthly(current, nil) && current.Month() == time.January
}
}

func (t Interval) StartDate(now time.Time) time.Time {
switch t {
case IntervalWeekly:
wd := now.Weekday()
return now.AddDate(0, 0, -int(wd)+1)
case IntervalMonthly:
y, m, _ := now.Date()
return time.Date(y, m, 1, 0, 0, 0, 0, now.Location())
case IntervalQuarterly:
y, m, _ := now.Date()
m = quarterMonth(m)
return time.Date(y, m, 1, 0, 0, 0, 0, now.Location())
case IntervalAnnually:
return time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
case IntervalNever, IntervalDaily, "":
fallthrough
default:
return now
}
}

func quarterMonth(m time.Month) time.Month {
switch m {
case time.January, time.February, time.March:
fallthrough
default:
return time.January
case time.April, time.May, time.June:
return time.April
case time.July, time.August, time.September:
return time.July
case time.October, time.November, time.December:
return time.October
}
}
93 changes: 93 additions & 0 deletions backtest/backtestconfig/interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,96 @@ func date(str string) time.Time {
d, _ := time.Parse(time.DateOnly, str)
return d
}

func TestInterval_StartDate(t *testing.T) {
for _, tt := range []struct {
Name string
Interval backtestconfig.Interval
Expected time.Time
Now time.Time
}{
{
Name: "never gives the same date",
Interval: backtestconfig.IntervalNever,
Expected: date("2020-03-01"),
Now: date("2020-03-01"),
},
{
Name: "daily gives the same day",
Interval: backtestconfig.IntervalDaily,
Expected: date("2020-03-01"),
Now: date("2020-03-01"),
},
{
Name: "weekly gives the most recent monday",
Interval: backtestconfig.IntervalWeekly,
Expected: date("2024-01-01"),
Now: date("2024-01-03"),
},
{
Name: "monthly gives the 1st of a month",
Interval: backtestconfig.IntervalMonthly,
Expected: date("2024-02-01"),
Now: date("2024-02-07"),
},
{
Name: "q1 gives the most recent 1st quarter day",
Interval: backtestconfig.IntervalQuarterly,
Expected: date("2024-01-01"),
Now: date("2024-02-23"),
},
{
Name: "q2 gives the most recent 1st quarter day",
Interval: backtestconfig.IntervalQuarterly,
Expected: date("2024-04-01"),
Now: date("2024-05-23"),
},
{
Name: "q3 gives the most recent 1st quarter day",
Interval: backtestconfig.IntervalQuarterly,
Expected: date("2024-07-01"),
Now: date("2024-09-23"),
},
{
Name: "q4 gives the most recent 1st quarter day",
Interval: backtestconfig.IntervalQuarterly,
Expected: date("2024-10-01"),
Now: date("2024-12-03"),
},
{
Name: "weekly gives the most recent monday",
Interval: backtestconfig.IntervalAnnually,
Expected: date("2024-01-01"),
Now: date("2024-04-23"),
},
{
Name: "weekly on monday gives the same day",
Interval: backtestconfig.IntervalWeekly,
Expected: date("2024-01-01"),
Now: date("2024-01-01"),
},
{
Name: "monthly on the first gives the same day",
Interval: backtestconfig.IntervalMonthly,
Expected: date("2024-01-01"),
Now: date("2024-01-01"),
},
{
Name: "monthly on the first gives the same day",
Interval: backtestconfig.IntervalQuarterly,
Expected: date("2024-01-01"),
Now: date("2024-01-01"),
},
{
Name: "annually on jan 1st gives the same day",
Interval: backtestconfig.IntervalAnnually,
Expected: date("2024-01-01"),
Now: date("2024-01-01"),
},
} {
t.Run(tt.Name, func(t *testing.T) {
out := tt.Interval.StartDate(tt.Now)
assert.Equal(t, tt.Expected, out)
})
}
}

0 comments on commit ac13e95

Please sign in to comment.