sequence tweaks, removing market hours anything
This commit is contained in:
parent
1a09989055
commit
0e849b11bb
17 changed files with 413 additions and 877 deletions
127
seq/time.go
127
seq/time.go
|
|
@ -20,95 +20,6 @@ func (ts timeSequence) Days(days int) []time.Time {
|
|||
return values
|
||||
}
|
||||
|
||||
func (ts timeSequence) MarketHours(from, to time.Time, marketOpen, marketClose time.Time, isHoliday util.HolidayProvider) []time.Time {
|
||||
var times []time.Time
|
||||
cursor := util.Date.On(marketOpen, from)
|
||||
toClose := util.Date.On(marketClose, to)
|
||||
for cursor.Before(toClose) || cursor.Equal(toClose) {
|
||||
todayOpen := util.Date.On(marketOpen, cursor)
|
||||
todayClose := util.Date.On(marketClose, cursor)
|
||||
isValidTradingDay := !isHoliday(cursor) && util.Date.IsWeekDay(cursor.Weekday())
|
||||
|
||||
if (cursor.Equal(todayOpen) || cursor.After(todayOpen)) && (cursor.Equal(todayClose) || cursor.Before(todayClose)) && isValidTradingDay {
|
||||
times = append(times, cursor)
|
||||
}
|
||||
if cursor.After(todayClose) {
|
||||
cursor = util.Date.NextMarketOpen(cursor, marketOpen, isHoliday)
|
||||
} else {
|
||||
cursor = util.Date.NextHour(cursor)
|
||||
}
|
||||
}
|
||||
return times
|
||||
}
|
||||
|
||||
func (ts timeSequence) MarketHourQuarters(from, to time.Time, marketOpen, marketClose time.Time, isHoliday util.HolidayProvider) []time.Time {
|
||||
var times []time.Time
|
||||
cursor := util.Date.On(marketOpen, from)
|
||||
toClose := util.Date.On(marketClose, to)
|
||||
for cursor.Before(toClose) || cursor.Equal(toClose) {
|
||||
|
||||
isValidTradingDay := !isHoliday(cursor) && util.Date.IsWeekDay(cursor.Weekday())
|
||||
|
||||
if isValidTradingDay {
|
||||
todayOpen := util.Date.On(marketOpen, cursor)
|
||||
todayNoon := util.Date.NoonOn(cursor)
|
||||
today2pm := util.Date.On(util.Date.Time(14, 0, 0, 0, cursor.Location()), cursor)
|
||||
todayClose := util.Date.On(marketClose, cursor)
|
||||
times = append(times, todayOpen, todayNoon, today2pm, todayClose)
|
||||
}
|
||||
|
||||
cursor = util.Date.NextDay(cursor)
|
||||
}
|
||||
return times
|
||||
}
|
||||
|
||||
func (ts timeSequence) MarketDayCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday util.HolidayProvider) []time.Time {
|
||||
var times []time.Time
|
||||
cursor := util.Date.On(marketOpen, from)
|
||||
toClose := util.Date.On(marketClose, to)
|
||||
for cursor.Before(toClose) || cursor.Equal(toClose) {
|
||||
isValidTradingDay := !isHoliday(cursor) && util.Date.IsWeekDay(cursor.Weekday())
|
||||
if isValidTradingDay {
|
||||
todayClose := util.Date.On(marketClose, cursor)
|
||||
times = append(times, todayClose)
|
||||
}
|
||||
|
||||
cursor = util.Date.NextDay(cursor)
|
||||
}
|
||||
return times
|
||||
}
|
||||
|
||||
func (ts timeSequence) MarketDayAlternateCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday util.HolidayProvider) []time.Time {
|
||||
var times []time.Time
|
||||
cursor := util.Date.On(marketOpen, from)
|
||||
toClose := util.Date.On(marketClose, to)
|
||||
for cursor.Before(toClose) || cursor.Equal(toClose) {
|
||||
isValidTradingDay := !isHoliday(cursor) && util.Date.IsWeekDay(cursor.Weekday())
|
||||
if isValidTradingDay {
|
||||
todayClose := util.Date.On(marketClose, cursor)
|
||||
times = append(times, todayClose)
|
||||
}
|
||||
|
||||
cursor = cursor.AddDate(0, 0, 2)
|
||||
}
|
||||
return times
|
||||
}
|
||||
|
||||
func (ts timeSequence) MarketDayMondayCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday util.HolidayProvider) []time.Time {
|
||||
var times []time.Time
|
||||
cursor := util.Date.On(marketClose, from)
|
||||
toClose := util.Date.On(marketClose, to)
|
||||
|
||||
for cursor.Equal(toClose) || cursor.Before(toClose) {
|
||||
isValidTradingDay := !isHoliday(cursor) && util.Date.IsWeekDay(cursor.Weekday())
|
||||
if isValidTradingDay {
|
||||
times = append(times, cursor)
|
||||
}
|
||||
cursor = util.Date.NextDayOfWeek(cursor, time.Monday)
|
||||
}
|
||||
return times
|
||||
}
|
||||
|
||||
func (ts timeSequence) Hours(start time.Time, totalHours int) []time.Time {
|
||||
times := make([]time.Time, totalHours)
|
||||
|
||||
|
|
@ -123,49 +34,17 @@ func (ts timeSequence) Hours(start time.Time, totalHours int) []time.Time {
|
|||
|
||||
// HoursFilled adds zero values for the data bounded by the start and end of the xdata array.
|
||||
func (ts timeSequence) HoursFilled(xdata []time.Time, ydata []float64) ([]time.Time, []float64) {
|
||||
start := Time.Start(xdata)
|
||||
end := Time.End(xdata)
|
||||
|
||||
totalHours := util.Math.AbsInt(util.Date.DiffHours(start, end))
|
||||
start, end := util.Time.StartAndEnd(xdata...)
|
||||
totalHours := util.Time.DiffHours(start, end)
|
||||
|
||||
finalTimes := ts.Hours(start, totalHours+1)
|
||||
finalValues := make([]float64, totalHours+1)
|
||||
|
||||
var hoursFromStart int
|
||||
for i, xd := range xdata {
|
||||
hoursFromStart = util.Date.DiffHours(start, xd)
|
||||
hoursFromStart = util.Time.DiffHours(start, xd)
|
||||
finalValues[hoursFromStart] = ydata[i]
|
||||
}
|
||||
|
||||
return finalTimes, finalValues
|
||||
}
|
||||
|
||||
// Start returns the earliest (min) time in a list of times.
|
||||
func (ts timeSequence) Start(times []time.Time) time.Time {
|
||||
if len(times) == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
start := times[0]
|
||||
for _, t := range times[1:] {
|
||||
if t.Before(start) {
|
||||
start = t
|
||||
}
|
||||
}
|
||||
return start
|
||||
}
|
||||
|
||||
// Start returns the earliest (min) time in a list of times.
|
||||
func (ts timeSequence) End(times []time.Time) time.Time {
|
||||
if len(times) == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
end := times[0]
|
||||
for _, t := range times[1:] {
|
||||
if t.After(end) {
|
||||
end = t
|
||||
}
|
||||
}
|
||||
return end
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,40 +8,13 @@ import (
|
|||
"github.com/wcharczuk/go-chart/util"
|
||||
)
|
||||
|
||||
func TestTimeMarketHours(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
today := time.Date(2016, 07, 01, 12, 0, 0, 0, util.Date.Eastern())
|
||||
mh := Time.MarketHours(today, today, util.NYSEOpen(), util.NYSEClose(), util.Date.IsNYSEHoliday)
|
||||
assert.Len(mh, 8)
|
||||
assert.Equal(util.Date.Eastern(), mh[0].Location())
|
||||
}
|
||||
|
||||
func TestTimeMarketHourQuarters(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
today := time.Date(2016, 07, 01, 12, 0, 0, 0, util.Date.Eastern())
|
||||
mh := Time.MarketHourQuarters(today, today, util.NYSEOpen(), util.NYSEClose(), util.Date.IsNYSEHoliday)
|
||||
assert.Len(mh, 4)
|
||||
assert.Equal(9, mh[0].Hour())
|
||||
assert.Equal(30, mh[0].Minute())
|
||||
assert.Equal(util.Date.Eastern(), mh[0].Location())
|
||||
|
||||
assert.Equal(12, mh[1].Hour())
|
||||
assert.Equal(00, mh[1].Minute())
|
||||
assert.Equal(util.Date.Eastern(), mh[1].Location())
|
||||
|
||||
assert.Equal(14, mh[2].Hour())
|
||||
assert.Equal(00, mh[2].Minute())
|
||||
assert.Equal(util.Date.Eastern(), mh[2].Location())
|
||||
}
|
||||
|
||||
func TestTimeHours(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
today := time.Date(2016, 07, 01, 12, 0, 0, 0, time.UTC)
|
||||
seq := Time.Hours(today, 24)
|
||||
|
||||
end := Time.End(seq)
|
||||
end := util.Time.End(seq...)
|
||||
assert.Len(seq, 24)
|
||||
assert.Equal(2016, end.Year())
|
||||
assert.Equal(07, int(end.Month()))
|
||||
|
|
@ -73,7 +46,7 @@ func TestSequenceHoursFill(t *testing.T) {
|
|||
}
|
||||
|
||||
filledTimes, filledValues := Time.HoursFilled(xdata, ydata)
|
||||
expected := util.Date.DiffHours(Time.Start(xdata), Time.End(xdata)) + 1
|
||||
expected := util.Time.DiffHours(util.Time.Start(xdata...), util.Time.End(xdata...)) + 1
|
||||
assert.Len(filledTimes, expected)
|
||||
assert.Equal(len(filledValues), len(filledTimes))
|
||||
|
||||
|
|
@ -94,7 +67,7 @@ func TestTimeStart(t *testing.T) {
|
|||
time.Now().AddDate(0, 0, -5),
|
||||
}
|
||||
|
||||
assert.InTimeDelta(Time.Start(times), times[4], time.Millisecond)
|
||||
assert.InTimeDelta(util.Time.Start(times...), times[4], time.Millisecond)
|
||||
}
|
||||
|
||||
func TestTimeEnd(t *testing.T) {
|
||||
|
|
@ -108,5 +81,5 @@ func TestTimeEnd(t *testing.T) {
|
|||
time.Now().AddDate(0, 0, -5),
|
||||
}
|
||||
|
||||
assert.InTimeDelta(Time.End(times), times[2], time.Millisecond)
|
||||
assert.InTimeDelta(util.Time.End(times...), times[2], time.Millisecond)
|
||||
}
|
||||
|
|
|
|||
31
seq/times.go
Normal file
31
seq/times.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package seq
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/wcharczuk/go-chart/util"
|
||||
)
|
||||
|
||||
// Assert types implement interfaces.
|
||||
var (
|
||||
_ Provider = (*Times)(nil)
|
||||
)
|
||||
|
||||
// Times are an array of times.
|
||||
// It wraps the array with methods that implement `seq.Provider`.
|
||||
type Times []time.Time
|
||||
|
||||
// Array returns the times to an array.
|
||||
func (t Times) Array() []time.Time {
|
||||
return []time.Time(t)
|
||||
}
|
||||
|
||||
// Len returns the length of the array.
|
||||
func (t Times) Len() int {
|
||||
return len(t)
|
||||
}
|
||||
|
||||
// GetValue returns a value at an index as a time.
|
||||
func (t Times) GetValue(index int) float64 {
|
||||
return util.Time.ToFloat64(t[index])
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue