wtf
This commit is contained in:
parent
376a8efa36
commit
255390c710
129
util/generate.go
129
util/generate.go
|
@ -59,132 +59,3 @@ func (g generate) RandomValuesWithAverage(samples int, average, scale float64) S
|
||||||
|
|
||||||
return Sequence{Array(values)}
|
return Sequence{Array(values)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Days generates a sequence of timestamps by day, from -days to today.
|
|
||||||
func (g generate) Days(days int) []time.Time {
|
|
||||||
var values []time.Time
|
|
||||||
for day := days; day >= 0; day-- {
|
|
||||||
values = append(values, time.Now().AddDate(0, 0, -day))
|
|
||||||
}
|
|
||||||
return values
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g generate) MarketHours(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
|
|
||||||
var times []time.Time
|
|
||||||
cursor := Date.On(marketOpen, from)
|
|
||||||
toClose := Date.On(marketClose, to)
|
|
||||||
for cursor.Before(toClose) || cursor.Equal(toClose) {
|
|
||||||
todayOpen := Date.On(marketOpen, cursor)
|
|
||||||
todayClose := Date.On(marketClose, cursor)
|
|
||||||
isValidTradingDay := !isHoliday(cursor) && 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 = Date.NextMarketOpen(cursor, marketOpen, isHoliday)
|
|
||||||
} else {
|
|
||||||
cursor = Date.NextHour(cursor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return times
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g generate) MarketHourQuarters(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
|
|
||||||
var times []time.Time
|
|
||||||
cursor := Date.On(marketOpen, from)
|
|
||||||
toClose := Date.On(marketClose, to)
|
|
||||||
for cursor.Before(toClose) || cursor.Equal(toClose) {
|
|
||||||
|
|
||||||
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
|
|
||||||
|
|
||||||
if isValidTradingDay {
|
|
||||||
todayOpen := Date.On(marketOpen, cursor)
|
|
||||||
todayNoon := Date.NoonOn(cursor)
|
|
||||||
today2pm := Date.On(Date.Time(14, 0, 0, 0, cursor.Location()), cursor)
|
|
||||||
todayClose := Date.On(marketClose, cursor)
|
|
||||||
times = append(times, todayOpen, todayNoon, today2pm, todayClose)
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor = Date.NextDay(cursor)
|
|
||||||
}
|
|
||||||
return times
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g generate) MarketDayCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
|
|
||||||
var times []time.Time
|
|
||||||
cursor := Date.On(marketOpen, from)
|
|
||||||
toClose := Date.On(marketClose, to)
|
|
||||||
for cursor.Before(toClose) || cursor.Equal(toClose) {
|
|
||||||
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
|
|
||||||
if isValidTradingDay {
|
|
||||||
todayClose := Date.On(marketClose, cursor)
|
|
||||||
times = append(times, todayClose)
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor = Date.NextDay(cursor)
|
|
||||||
}
|
|
||||||
return times
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g generate) MarketDayAlternateCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
|
|
||||||
var times []time.Time
|
|
||||||
cursor := Date.On(marketOpen, from)
|
|
||||||
toClose := Date.On(marketClose, to)
|
|
||||||
for cursor.Before(toClose) || cursor.Equal(toClose) {
|
|
||||||
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
|
|
||||||
if isValidTradingDay {
|
|
||||||
todayClose := Date.On(marketClose, cursor)
|
|
||||||
times = append(times, todayClose)
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor = cursor.AddDate(0, 0, 2)
|
|
||||||
}
|
|
||||||
return times
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g generate) MarketDayMondayCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
|
|
||||||
var times []time.Time
|
|
||||||
cursor := Date.On(marketClose, from)
|
|
||||||
toClose := Date.On(marketClose, to)
|
|
||||||
|
|
||||||
for cursor.Equal(toClose) || cursor.Before(toClose) {
|
|
||||||
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
|
|
||||||
if isValidTradingDay {
|
|
||||||
times = append(times, cursor)
|
|
||||||
}
|
|
||||||
cursor = Date.NextDayOfWeek(cursor, time.Monday)
|
|
||||||
}
|
|
||||||
return times
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g generate) Hours(start time.Time, totalHours int) []time.Time {
|
|
||||||
times := make([]time.Time, totalHours)
|
|
||||||
|
|
||||||
last := start
|
|
||||||
for i := 0; i < totalHours; i++ {
|
|
||||||
times[i] = last
|
|
||||||
last = last.Add(time.Hour)
|
|
||||||
}
|
|
||||||
|
|
||||||
return times
|
|
||||||
}
|
|
||||||
|
|
||||||
// HoursFilled adds zero values for the data bounded by the start and end of the xdata array.
|
|
||||||
func (g generate) HoursFilled(xdata []time.Time, ydata []float64) ([]time.Time, []float64) {
|
|
||||||
start := Date.Start(xdata)
|
|
||||||
end := Date.End(xdata)
|
|
||||||
|
|
||||||
totalHours := Math.AbsInt(Date.DiffHours(start, end))
|
|
||||||
|
|
||||||
finalTimes := g.Hours(start, totalHours+1)
|
|
||||||
finalValues := make([]float64, totalHours+1)
|
|
||||||
|
|
||||||
var hoursFromStart int
|
|
||||||
for i, xd := range xdata {
|
|
||||||
hoursFromStart = Date.DiffHours(start, xd)
|
|
||||||
finalValues[hoursFromStart] = ydata[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
return finalTimes, finalValues
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user