This commit is contained in:
Will Charczuk 2016-07-27 08:23:53 -07:00
commit 4f381fa4dc
5 changed files with 39 additions and 20 deletions

View File

@ -306,9 +306,10 @@ func CalculateMarketSecondsBetween(start, end, marketOpen, marketClose time.Time
startEastern := start.In(Eastern()) startEastern := start.In(Eastern())
endEastern := end.In(Eastern()) endEastern := end.In(Eastern())
startMarketOpen := NextMarketOpen(startEastern, marketOpen, isHoliday) startMarketOpen := On(marketOpen, startEastern)
startMarketClose := NextMarketClose(startEastern, marketClose, isHoliday) startMarketClose := On(marketClose, startEastern)
if !IsWeekendDay(startMarketOpen.Weekday()) && !isHoliday(startMarketOpen) {
if (startEastern.Equal(startMarketOpen) || startEastern.After(startMarketOpen)) && startEastern.Before(startMarketClose) { if (startEastern.Equal(startMarketOpen) || startEastern.After(startMarketOpen)) && startEastern.Before(startMarketClose) {
if endEastern.Before(startMarketClose) { if endEastern.Before(startMarketClose) {
seconds += int64(endEastern.Sub(startEastern) / time.Second) seconds += int64(endEastern.Sub(startEastern) / time.Second)
@ -316,6 +317,7 @@ func CalculateMarketSecondsBetween(start, end, marketOpen, marketClose time.Time
seconds += int64(startMarketClose.Sub(startEastern) / time.Second) seconds += int64(startMarketClose.Sub(startEastern) / time.Second)
} }
} }
}
cursor := NextMarketOpen(startMarketClose, marketOpen, isHoliday) cursor := NextMarketOpen(startMarketClose, marketOpen, isHoliday)
for BeforeDate(cursor, endEastern) { for BeforeDate(cursor, endEastern) {

View File

@ -76,6 +76,17 @@ func TestCalculateMarketSecondsBetween(t *testing.T) {
assert.Equal(shouldbe, CalculateMarketSecondsBetween(start, end, NYSEOpen, NYSEClose, IsNYSEHoliday)) 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) { func TestCalculateMarketSecondsBetweenLTM(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)

View File

@ -2,7 +2,6 @@ package chart
import ( import (
"sync" "sync"
"time"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/drawing" "github.com/wcharczuk/go-chart/drawing"
@ -59,7 +58,7 @@ const (
// DefaultDateHourFormat is the date format for hour timestamp formats. // DefaultDateHourFormat is the date format for hour timestamp formats.
DefaultDateHourFormat = "01-02 3PM" DefaultDateHourFormat = "01-02 3PM"
// DefaultDateMinuteFormat is the date format for minute range timestamp formats. // 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 is the default float format.
DefaultFloatFormat = "%.2f" DefaultFloatFormat = "%.2f"
// DefaultPercentValueFormat is the default percent format. // DefaultPercentValueFormat is the default percent format.

View File

@ -16,12 +16,11 @@ func TestGenerateGridLines(t *testing.T) {
{Value: 4.0, Label: "4.0"}, {Value: 4.0, Label: "4.0"},
} }
gl := GenerateGridLines(ticks, true) gl := GenerateGridLines(ticks, Style{}, Style{}, true)
assert.Len(gl, 4) assert.Len(gl, 2)
assert.Equal(1.0, gl[0].Value)
assert.Equal(2.0, gl[1].Value) assert.Equal(2.0, gl[0].Value)
assert.Equal(3.0, gl[2].Value) assert.Equal(3.0, gl[1].Value)
assert.Equal(4.0, gl[3].Value)
assert.True(gl[0].IsVertical) assert.True(gl[0].IsVertical)
} }

View File

@ -18,6 +18,8 @@ type MarketHoursRange struct {
HolidayProvider date.HolidayProvider HolidayProvider date.HolidayProvider
ValueFormatter ValueFormatter
Domain int Domain int
} }
@ -83,12 +85,18 @@ func (mhr MarketHoursRange) GetHolidayProvider() date.HolidayProvider {
// GetTicks returns the ticks for the range. // GetTicks returns the ticks for the range.
// This is to override the default continous ticks that would be generated 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 { 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 var ticks []Tick
cursor := date.On(mhr.MarketClose, mhr.Min) cursor := date.On(mhr.MarketClose, mhr.Min)
maxClose := date.On(mhr.MarketClose, mhr.Max) 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) { for date.BeforeDate(cursor, maxClose) {
if date.IsWeekDay(cursor.Weekday()) && !mhr.GetHolidayProvider()(cursor) { if date.IsWeekDay(cursor.Weekday()) && !mhr.GetHolidayProvider()(cursor) {
ticks = append(ticks, Tick{ ticks = append(ticks, Tick{
@ -111,15 +119,15 @@ func (mhr *MarketHoursRange) GetTicks(vf ValueFormatter) []Tick {
} }
func (mhr MarketHoursRange) String() string { 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. // Translate maps a given value into the ContinuousRange space.
func (mhr MarketHoursRange) Translate(value float64) int { func (mhr MarketHoursRange) Translate(value float64) int {
valueTime := Float64ToTime(value) valueTime := Float64ToTime(value)
valueTimeEastern := valueTime.In(date.Eastern()) valueTimeEastern := valueTime.In(date.Eastern())
deltaSeconds := date.CalculateMarketSecondsBetween(mhr.Min, mhr.GetEffectiveMax(), mhr.MarketOpen, mhr.MarketClose, mhr.HolidayProvider) totalSeconds := date.CalculateMarketSecondsBetween(mhr.Min, mhr.GetEffectiveMax(), mhr.MarketOpen, mhr.MarketClose, mhr.HolidayProvider)
valueDelta := date.CalculateMarketSecondsBetween(mhr.Min, valueTimeEastern, 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 return translated
} }