From 05caeb41eec950a3e369bfa911c1a109a9c65a61 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Tue, 26 Jul 2016 23:55:31 -0700 Subject: [PATCH 1/4] fixing test. --- grid_line_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/grid_line_test.go b/grid_line_test.go index d6e1fe6..d9b464e 100644 --- a/grid_line_test.go +++ b/grid_line_test.go @@ -16,12 +16,11 @@ func TestGenerateGridLines(t *testing.T) { {Value: 4.0, Label: "4.0"}, } - gl := GenerateGridLines(ticks, true) - assert.Len(gl, 4) - assert.Equal(1.0, gl[0].Value) - assert.Equal(2.0, gl[1].Value) - assert.Equal(3.0, gl[2].Value) - assert.Equal(4.0, gl[3].Value) + gl := GenerateGridLines(ticks, Style{}, Style{}, true) + assert.Len(gl, 2) + + assert.Equal(2.0, gl[0].Value) + assert.Equal(3.0, gl[1].Value) assert.True(gl[0].IsVertical) } From 865ba96eb5e6f17adf45f9e2e27d09f461fc1799 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Wed, 27 Jul 2016 00:20:43 -0700 Subject: [PATCH 2/4] switching the formatter --- market_hours_range.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/market_hours_range.go b/market_hours_range.go index e063435..baadc39 100644 --- a/market_hours_range.go +++ b/market_hours_range.go @@ -18,6 +18,8 @@ type MarketHoursRange struct { HolidayProvider date.HolidayProvider + ValueFormatter ValueFormatter + Domain int } @@ -102,7 +104,7 @@ func (mhr *MarketHoursRange) GetTicks(vf ValueFormatter) []Tick { } func (mhr MarketHoursRange) String() string { - return fmt.Sprintf("MarketHoursRange [%s, %s] => %d", mhr.Min.Format(DefaultDateFormat), mhr.Max.Format(DefaultDateFormat), mhr.Domain) + return fmt.Sprintf("MarketHoursRange [%s, %s] => %d", mhr.Min.Format(DefaultDateMinuteFormat), mhr.Max.Format(DefaultDateMinuteFormat), mhr.Domain) } // Translate maps a given value into the ContinuousRange space. From 0c049db317bdb76e4ea8d95d95e0b1ef90118309 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Wed, 27 Jul 2016 00:34:10 -0700 Subject: [PATCH 3/4] fixing calculate on day. --- date/util.go | 16 +++++++++------- date/util_test.go | 11 +++++++++++ defaults.go | 3 +-- market_hours_range.go | 4 ++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/date/util.go b/date/util.go index f1a524c..48fb5a4 100644 --- a/date/util.go +++ b/date/util.go @@ -306,14 +306,16 @@ func CalculateMarketSecondsBetween(start, end, marketOpen, marketClose time.Time startEastern := start.In(Eastern()) endEastern := end.In(Eastern()) - startMarketOpen := NextMarketOpen(startEastern, marketOpen, isHoliday) - startMarketClose := NextMarketClose(startEastern, marketClose, isHoliday) + startMarketOpen := On(marketOpen, startEastern) + startMarketClose := On(marketClose, startEastern) - if (startEastern.Equal(startMarketOpen) || startEastern.After(startMarketOpen)) && startEastern.Before(startMarketClose) { - if endEastern.Before(startMarketClose) { - seconds += int64(endEastern.Sub(startEastern) / time.Second) - } else { - seconds += int64(startMarketClose.Sub(startEastern) / time.Second) + if !IsWeekendDay(startMarketOpen.Weekday()) && !isHoliday(startMarketOpen) { + if (startEastern.Equal(startMarketOpen) || startEastern.After(startMarketOpen)) && startEastern.Before(startMarketClose) { + if endEastern.Before(startMarketClose) { + seconds += int64(endEastern.Sub(startEastern) / time.Second) + } else { + seconds += int64(startMarketClose.Sub(startEastern) / time.Second) + } } } diff --git a/date/util_test.go b/date/util_test.go index 7310cec..8538053 100644 --- a/date/util_test.go +++ b/date/util_test.go @@ -76,6 +76,17 @@ func TestCalculateMarketSecondsBetween(t *testing.T) { assert.Equal(shouldbe, CalculateMarketSecondsBetween(start, end, NYSEOpen, NYSEClose, IsNYSEHoliday)) } +func TestCalculateMarketSecondsBetween1D(t *testing.T) { + assert := assert.New(t) + + start := time.Date(2016, 07, 22, 9, 45, 0, 0, Eastern()) + end := time.Date(2016, 07, 22, 15, 45, 0, 0, Eastern()) + + shouldbe := 6 * 60 * 60 + + assert.Equal(shouldbe, CalculateMarketSecondsBetween(start, end, NYSEOpen, NYSEClose, IsNYSEHoliday)) +} + func TestCalculateMarketSecondsBetweenLTM(t *testing.T) { assert := assert.New(t) diff --git a/defaults.go b/defaults.go index fc69d0e..88dbd03 100644 --- a/defaults.go +++ b/defaults.go @@ -2,7 +2,6 @@ package chart import ( "sync" - "time" "github.com/golang/freetype/truetype" "github.com/wcharczuk/go-chart/drawing" @@ -59,7 +58,7 @@ const ( // DefaultDateHourFormat is the date format for hour timestamp formats. DefaultDateHourFormat = "01-02 3PM" // DefaultDateMinuteFormat is the date format for minute range timestamp formats. - DefaultDateMinuteFormat = time.Kitchen + DefaultDateMinuteFormat = "01-02 3:04PM" // DefaultFloatFormat is the default float format. DefaultFloatFormat = "%.2f" // DefaultPercentValueFormat is the default percent format. diff --git a/market_hours_range.go b/market_hours_range.go index baadc39..86a7317 100644 --- a/market_hours_range.go +++ b/market_hours_range.go @@ -111,8 +111,8 @@ func (mhr MarketHoursRange) String() string { func (mhr MarketHoursRange) Translate(value float64) int { valueTime := Float64ToTime(value) valueTimeEastern := valueTime.In(date.Eastern()) - deltaSeconds := date.CalculateMarketSecondsBetween(mhr.Min, mhr.Max, mhr.MarketOpen, mhr.MarketClose, mhr.HolidayProvider) + totalSeconds := date.CalculateMarketSecondsBetween(mhr.Min, mhr.Max, mhr.MarketOpen, mhr.MarketClose, mhr.HolidayProvider) valueDelta := date.CalculateMarketSecondsBetween(mhr.Min, valueTimeEastern, mhr.MarketOpen, mhr.MarketClose, mhr.HolidayProvider) - translated := int((float64(valueDelta) / float64(deltaSeconds)) * float64(mhr.Domain)) + translated := int((float64(valueDelta) / float64(totalSeconds)) * float64(mhr.Domain)) return translated } From 40ff878b79397f1fe57f54b4a078b1597a07ad0e Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Wed, 27 Jul 2016 00:40:22 -0700 Subject: [PATCH 4/4] things. --- market_hours_range.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/market_hours_range.go b/market_hours_range.go index 86a7317..f1777e6 100644 --- a/market_hours_range.go +++ b/market_hours_range.go @@ -76,12 +76,18 @@ func (mhr MarketHoursRange) GetHolidayProvider() date.HolidayProvider { // GetTicks returns the ticks for the range. // This is to override the default continous ticks that would be generated for the range. func (mhr *MarketHoursRange) GetTicks(vf ValueFormatter) []Tick { - // return one tick per day - // figure out how to advance one ticke per market day. var ticks []Tick cursor := date.On(mhr.MarketClose, mhr.Min) maxClose := date.On(mhr.MarketClose, mhr.Max) + + if mhr.Min.Before(cursor) { + ticks = append(ticks, Tick{ + Value: TimeToFloat64(cursor), + Label: vf(cursor), + }) + } + for date.BeforeDate(cursor, maxClose) { if date.IsWeekDay(cursor.Weekday()) && !mhr.GetHolidayProvider()(cursor) { ticks = append(ticks, Tick{