diff --git a/date/util.go b/date/util.go index 0432681..6646146 100644 --- a/date/util.go +++ b/date/util.go @@ -73,6 +73,8 @@ var ( // HolidayProvider is a function that returns if a given time falls on a holiday. type HolidayProvider func(time.Time) bool +func DefaultHolidayProvider(_ time.Time) bool { return false } + // IsNYSEHoliday returns if a date was/is on a nyse holiday day. func IsNYSEHoliday(t time.Time) bool { te := t.In(Eastern()) @@ -241,6 +243,10 @@ func NextMarketOpen(after, openTime time.Time, isHoliday HolidayProvider) time.T afterEastern := after.In(Eastern()) todaysOpen := On(openTime, afterEastern) + if isHoliday == nil { + isHoliday = DefaultHolidayProvider + } + if afterEastern.Before(todaysOpen) && IsWeekDay(todaysOpen.Weekday()) && !isHoliday(todaysOpen) { return todaysOpen } @@ -262,6 +268,10 @@ func NextMarketOpen(after, openTime time.Time, isHoliday HolidayProvider) time.T func NextMarketClose(after, closeTime time.Time, isHoliday HolidayProvider) time.Time { afterEastern := after.In(Eastern()) + if isHoliday == nil { + isHoliday = DefaultHolidayProvider + } + todaysClose := On(closeTime, afterEastern) if afterEastern.Before(todaysClose) && IsWeekDay(todaysClose.Weekday()) && !isHoliday(todaysClose) { return todaysClose @@ -292,7 +302,7 @@ func CalculateMarketSecondsBetween(start, end, marketOpen, marketClose time.Time seconds += int64(startMarketClose.Sub(se) / time.Second) } - cursor := NextMarketOpen(startMarketClose, marketClose, isHoliday) + cursor := NextMarketOpen(startMarketClose, marketOpen, isHoliday) for BeforeDate(cursor, ee) { if IsWeekDay(cursor.Weekday()) && !isHoliday(cursor) { close := NextMarketClose(cursor, marketClose, isHoliday) diff --git a/date/util_test.go b/date/util_test.go index 9f9da52..7310cec 100644 --- a/date/util_test.go +++ b/date/util_test.go @@ -34,15 +34,15 @@ func TestNextMarketOpen(t *testing.T) { weekend := time.Date(2016, 07, 23, 9, 31, 0, 0, Eastern()) - assert.True(todayOpen.Equal(NextMarketOpen(beforeOpen))) - assert.True(tomorrowOpen.Equal(NextMarketOpen(afterOpen))) - assert.True(mondayOpen.Equal(NextMarketOpen(afterFriday))) - assert.True(mondayOpen.Equal(NextMarketOpen(weekend))) + assert.True(todayOpen.Equal(NextMarketOpen(beforeOpen, NYSEOpen, IsNYSEHoliday))) + assert.True(tomorrowOpen.Equal(NextMarketOpen(afterOpen, NYSEOpen, IsNYSEHoliday))) + assert.True(mondayOpen.Equal(NextMarketOpen(afterFriday, NYSEOpen, IsNYSEHoliday))) + assert.True(mondayOpen.Equal(NextMarketOpen(weekend, NYSEOpen, IsNYSEHoliday))) testRegression := time.Date(2016, 07, 18, 16, 0, 0, 0, Eastern()) shouldbe := time.Date(2016, 07, 19, 9, 30, 0, 0, Eastern()) - assert.True(shouldbe.Equal(NextMarketOpen(testRegression))) + assert.True(shouldbe.Equal(NextMarketOpen(testRegression, NYSEOpen, IsNYSEHoliday))) } func TestNextMarketClose(t *testing.T) { @@ -59,10 +59,10 @@ func TestNextMarketClose(t *testing.T) { weekend := time.Date(2016, 07, 23, 9, 31, 0, 0, Eastern()) - assert.True(todayClose.Equal(NextMarketClose(beforeClose))) - assert.True(tomorrowClose.Equal(NextMarketClose(afterClose))) - assert.True(mondayClose.Equal(NextMarketClose(afterFriday))) - assert.True(mondayClose.Equal(NextMarketClose(weekend))) + assert.True(todayClose.Equal(NextMarketClose(beforeClose, NYSEClose, IsNYSEHoliday))) + assert.True(tomorrowClose.Equal(NextMarketClose(afterClose, NYSEClose, IsNYSEHoliday))) + assert.True(mondayClose.Equal(NextMarketClose(afterFriday, NYSEClose, IsNYSEHoliday))) + assert.True(mondayClose.Equal(NextMarketClose(weekend, NYSEClose, IsNYSEHoliday))) } func TestCalculateMarketSecondsBetween(t *testing.T) { @@ -73,7 +73,7 @@ func TestCalculateMarketSecondsBetween(t *testing.T) { shouldbe := 5 * 6.5 * 60 * 60 - assert.Equal(shouldbe, CalculateMarketSecondsBetween(start, end)) + assert.Equal(shouldbe, CalculateMarketSecondsBetween(start, end, NYSEOpen, NYSEClose, IsNYSEHoliday)) } func TestCalculateMarketSecondsBetweenLTM(t *testing.T) { @@ -83,5 +83,5 @@ func TestCalculateMarketSecondsBetweenLTM(t *testing.T) { end := time.Date(2016, 07, 01, 9, 30, 0, 0, Eastern()) shouldbe := 253 * 6.5 * 60 * 60 //253 full market days since this date last year. - assert.Equal(shouldbe, CalculateMarketSecondsBetween(start, end)) + assert.Equal(shouldbe, CalculateMarketSecondsBetween(start, end, NYSEOpen, NYSEClose, IsNYSEHoliday)) } diff --git a/examples/stock_analysis/main.go b/examples/stock_analysis/main.go index 89fc527..69a4501 100644 --- a/examples/stock_analysis/main.go +++ b/examples/stock_analysis/main.go @@ -47,7 +47,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, YAxis: chart.YAxis{ Style: chart.Style{Show: true}, - Range: chart.Range{ + Range: &chart.ContinuousRange{ Max: 220.0, Min: 180.0, },