go-chart/market_hours_range.go

80 lines
2.1 KiB
Go
Raw Normal View History

2016-07-22 01:09:09 -04:00
package chart
import (
"fmt"
"time"
"github.com/wcharczuk/go-chart/date"
)
2016-07-23 14:50:30 -04:00
// MarketHoursRange is a special type of range that compresses a time range into just the
2016-07-22 01:09:09 -04:00
// market (i.e. NYSE operating hours and days) range.
2016-07-23 14:50:30 -04:00
type MarketHoursRange struct {
Min time.Time
Max time.Time
MarketOpen time.Time
MarketClose time.Time
HolidayProvider date.HolidayProvider
2016-07-22 01:09:09 -04:00
Domain int
}
2016-07-22 01:22:22 -04:00
// IsZero returns if the range is setup or not.
2016-07-23 14:50:30 -04:00
func (mhr MarketHoursRange) IsZero() bool {
2016-07-22 01:22:22 -04:00
return mhr.Min.IsZero() && mhr.Max.IsZero()
}
2016-07-22 01:09:09 -04:00
// GetMin returns the min value.
2016-07-23 14:50:30 -04:00
func (mhr MarketHoursRange) GetMin() float64 {
2016-07-22 01:09:09 -04:00
return TimeToFloat64(mhr.Min)
}
// GetMax returns the max value.
2016-07-23 14:50:30 -04:00
func (mhr MarketHoursRange) GetMax() float64 {
2016-07-22 01:09:09 -04:00
return TimeToFloat64(mhr.Max)
}
// SetMin sets the min value.
2016-07-23 14:50:30 -04:00
func (mhr *MarketHoursRange) SetMin(min float64) {
2016-07-22 01:09:09 -04:00
mhr.Min = Float64ToTime(min)
}
// SetMax sets the max value.
2016-07-23 14:50:30 -04:00
func (mhr *MarketHoursRange) SetMax(max float64) {
2016-07-22 01:09:09 -04:00
mhr.Max = Float64ToTime(max)
}
// GetDelta gets the delta.
2016-07-23 14:50:30 -04:00
func (mhr MarketHoursRange) GetDelta() float64 {
2016-07-22 01:09:09 -04:00
min := TimeToFloat64(mhr.Min)
2016-07-23 01:43:27 -04:00
max := TimeToFloat64(mhr.Max)
2016-07-22 01:09:09 -04:00
return max - min
}
// GetDomain gets the domain.
2016-07-23 14:50:30 -04:00
func (mhr MarketHoursRange) GetDomain() int {
2016-07-22 01:09:09 -04:00
return mhr.Domain
}
// SetDomain sets the domain.
2016-07-23 14:50:30 -04:00
func (mhr *MarketHoursRange) SetDomain(domain int) {
2016-07-22 01:09:09 -04:00
mhr.Domain = domain
}
2016-07-23 14:50:30 -04:00
func (mhr MarketHoursRange) String() string {
2016-07-22 01:09:09 -04:00
return fmt.Sprintf("MarketHoursRange [%s, %s] => %d", mhr.Min.Format(DefaultDateFormat), mhr.Max.Format(DefaultDateFormat), mhr.Domain)
}
// Translate maps a given value into the ContinuousRange space.
2016-07-23 14:50:30 -04:00
func (mhr MarketHoursRange) Translate(value float64) int {
2016-07-22 01:09:09 -04:00
valueTime := Float64ToTime(value)
2016-07-23 14:50:30 -04:00
deltaSeconds := date.CalculateMarketSecondsBetween(mhr.Min, mhr.Max, mhr.MarketOpen, mhr.MarketClose, mhr.HolidayProvider)
valueDelta := date.CalculateMarketSecondsBetween(mhr.Min, valueTime, mhr.MarketOpen, mhr.MarketClose, mhr.HolidayProvider)
2016-07-23 01:43:27 -04:00
translated := int((float64(valueDelta) / float64(deltaSeconds)) * float64(mhr.Domain))
fmt.Printf("nyse translating: %s to %d ~= %d", valueTime.Format(time.RFC3339), deltaSeconds, valueDelta)
return translated
2016-07-22 01:09:09 -04:00
}