forked from wcharczuk/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
market_hours_range.go
133 lines (108 loc) · 3.56 KB
/
market_hours_range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package chart
import (
"fmt"
"time"
"github.com/wcharczuk/go-chart/date"
)
// MarketHoursRange is a special type of range that compresses a time range into just the
// market (i.e. NYSE operating hours and days) range.
type MarketHoursRange struct {
Min time.Time
Max time.Time
MarketOpen time.Time
MarketClose time.Time
HolidayProvider date.HolidayProvider
ValueFormatter ValueFormatter
Domain int
}
// IsZero returns if the range is setup or not.
func (mhr MarketHoursRange) IsZero() bool {
return mhr.Min.IsZero() && mhr.Max.IsZero()
}
// GetMin returns the min value.
func (mhr MarketHoursRange) GetMin() float64 {
return TimeToFloat64(mhr.Min)
}
// GetMax returns the max value.
func (mhr MarketHoursRange) GetMax() float64 {
return TimeToFloat64(mhr.GetEffectiveMax())
}
// GetEffectiveMax gets either the close on the max, or the max itself.
func (mhr MarketHoursRange) GetEffectiveMax() time.Time {
maxClose := date.On(mhr.MarketClose, mhr.Max)
if maxClose.After(mhr.Max) {
return maxClose
}
return mhr.Max
}
// SetMin sets the min value.
func (mhr *MarketHoursRange) SetMin(min float64) {
mhr.Min = Float64ToTime(min)
}
// SetMax sets the max value.
func (mhr *MarketHoursRange) SetMax(max float64) {
mhr.Max = Float64ToTime(max)
}
// GetDelta gets the delta.
func (mhr MarketHoursRange) GetDelta() float64 {
min := mhr.GetMin()
max := mhr.GetMax()
return max - min
}
// GetDomain gets the domain.
func (mhr MarketHoursRange) GetDomain() int {
return mhr.Domain
}
// SetDomain sets the domain.
func (mhr *MarketHoursRange) SetDomain(domain int) {
mhr.Domain = domain
}
// GetHolidayProvider coalesces a userprovided holiday provider and the date.DefaultHolidayProvider.
func (mhr MarketHoursRange) GetHolidayProvider() date.HolidayProvider {
if mhr.HolidayProvider == nil {
return date.DefaultHolidayProvider
}
return mhr.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 {
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{
Value: TimeToFloat64(cursor),
Label: vf(cursor),
})
}
cursor = cursor.AddDate(0, 0, 1)
}
endMarketClose := date.On(mhr.MarketClose, cursor)
if date.IsWeekDay(endMarketClose.Weekday()) && !mhr.GetHolidayProvider()(endMarketClose) {
ticks = append(ticks, Tick{
Value: TimeToFloat64(endMarketClose),
Label: vf(endMarketClose),
})
}
return ticks
}
func (mhr MarketHoursRange) String() string {
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())
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(totalSeconds)) * float64(mhr.Domain))
return translated
}