tests pass post refactor.

This commit is contained in:
Will Charczuk 2016-07-23 12:58:37 -07:00
parent fd2bfe14f0
commit d41c9313aa
3 changed files with 23 additions and 13 deletions

View File

@ -73,6 +73,8 @@ var (
// HolidayProvider is a function that returns if a given time falls on a holiday. // HolidayProvider is a function that returns if a given time falls on a holiday.
type HolidayProvider func(time.Time) bool 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. // IsNYSEHoliday returns if a date was/is on a nyse holiday day.
func IsNYSEHoliday(t time.Time) bool { func IsNYSEHoliday(t time.Time) bool {
te := t.In(Eastern()) te := t.In(Eastern())
@ -241,6 +243,10 @@ func NextMarketOpen(after, openTime time.Time, isHoliday HolidayProvider) time.T
afterEastern := after.In(Eastern()) afterEastern := after.In(Eastern())
todaysOpen := On(openTime, afterEastern) todaysOpen := On(openTime, afterEastern)
if isHoliday == nil {
isHoliday = DefaultHolidayProvider
}
if afterEastern.Before(todaysOpen) && IsWeekDay(todaysOpen.Weekday()) && !isHoliday(todaysOpen) { if afterEastern.Before(todaysOpen) && IsWeekDay(todaysOpen.Weekday()) && !isHoliday(todaysOpen) {
return 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 { func NextMarketClose(after, closeTime time.Time, isHoliday HolidayProvider) time.Time {
afterEastern := after.In(Eastern()) afterEastern := after.In(Eastern())
if isHoliday == nil {
isHoliday = DefaultHolidayProvider
}
todaysClose := On(closeTime, afterEastern) todaysClose := On(closeTime, afterEastern)
if afterEastern.Before(todaysClose) && IsWeekDay(todaysClose.Weekday()) && !isHoliday(todaysClose) { if afterEastern.Before(todaysClose) && IsWeekDay(todaysClose.Weekday()) && !isHoliday(todaysClose) {
return todaysClose return todaysClose
@ -292,7 +302,7 @@ func CalculateMarketSecondsBetween(start, end, marketOpen, marketClose time.Time
seconds += int64(startMarketClose.Sub(se) / time.Second) seconds += int64(startMarketClose.Sub(se) / time.Second)
} }
cursor := NextMarketOpen(startMarketClose, marketClose, isHoliday) cursor := NextMarketOpen(startMarketClose, marketOpen, isHoliday)
for BeforeDate(cursor, ee) { for BeforeDate(cursor, ee) {
if IsWeekDay(cursor.Weekday()) && !isHoliday(cursor) { if IsWeekDay(cursor.Weekday()) && !isHoliday(cursor) {
close := NextMarketClose(cursor, marketClose, isHoliday) close := NextMarketClose(cursor, marketClose, isHoliday)

View File

@ -34,15 +34,15 @@ func TestNextMarketOpen(t *testing.T) {
weekend := time.Date(2016, 07, 23, 9, 31, 0, 0, Eastern()) weekend := time.Date(2016, 07, 23, 9, 31, 0, 0, Eastern())
assert.True(todayOpen.Equal(NextMarketOpen(beforeOpen))) assert.True(todayOpen.Equal(NextMarketOpen(beforeOpen, NYSEOpen, IsNYSEHoliday)))
assert.True(tomorrowOpen.Equal(NextMarketOpen(afterOpen))) assert.True(tomorrowOpen.Equal(NextMarketOpen(afterOpen, NYSEOpen, IsNYSEHoliday)))
assert.True(mondayOpen.Equal(NextMarketOpen(afterFriday))) assert.True(mondayOpen.Equal(NextMarketOpen(afterFriday, NYSEOpen, IsNYSEHoliday)))
assert.True(mondayOpen.Equal(NextMarketOpen(weekend))) assert.True(mondayOpen.Equal(NextMarketOpen(weekend, NYSEOpen, IsNYSEHoliday)))
testRegression := time.Date(2016, 07, 18, 16, 0, 0, 0, Eastern()) testRegression := time.Date(2016, 07, 18, 16, 0, 0, 0, Eastern())
shouldbe := time.Date(2016, 07, 19, 9, 30, 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) { 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()) weekend := time.Date(2016, 07, 23, 9, 31, 0, 0, Eastern())
assert.True(todayClose.Equal(NextMarketClose(beforeClose))) assert.True(todayClose.Equal(NextMarketClose(beforeClose, NYSEClose, IsNYSEHoliday)))
assert.True(tomorrowClose.Equal(NextMarketClose(afterClose))) assert.True(tomorrowClose.Equal(NextMarketClose(afterClose, NYSEClose, IsNYSEHoliday)))
assert.True(mondayClose.Equal(NextMarketClose(afterFriday))) assert.True(mondayClose.Equal(NextMarketClose(afterFriday, NYSEClose, IsNYSEHoliday)))
assert.True(mondayClose.Equal(NextMarketClose(weekend))) assert.True(mondayClose.Equal(NextMarketClose(weekend, NYSEClose, IsNYSEHoliday)))
} }
func TestCalculateMarketSecondsBetween(t *testing.T) { func TestCalculateMarketSecondsBetween(t *testing.T) {
@ -73,7 +73,7 @@ func TestCalculateMarketSecondsBetween(t *testing.T) {
shouldbe := 5 * 6.5 * 60 * 60 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) { 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()) 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. 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))
} }

View File

@ -47,7 +47,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
}, },
YAxis: chart.YAxis{ YAxis: chart.YAxis{
Style: chart.Style{Show: true}, Style: chart.Style{Show: true},
Range: chart.Range{ Range: &chart.ContinuousRange{
Max: 220.0, Max: 220.0,
Min: 180.0, Min: 180.0,
}, },