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,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)
}
}
}

View file

@ -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)

View file

@ -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.

View file

@ -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)
}

View file

@ -18,6 +18,8 @@ type MarketHoursRange struct {
HolidayProvider date.HolidayProvider
ValueFormatter ValueFormatter
Domain int
}
@ -83,12 +85,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{
@ -111,15 +119,15 @@ 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.
func (mhr MarketHoursRange) Translate(value float64) int {
valueTime := Float64ToTime(value)
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)
translated := int((float64(valueDelta) / float64(deltaSeconds)) * float64(mhr.Domain))
translated := int((float64(valueDelta) / float64(totalSeconds)) * float64(mhr.Domain))
return translated
}