go-chart/market_hours_range.go

80 lines
2.1 KiB
Go
Raw Normal View History

2016-07-22 07:09:09 +02:00
package chart
import (
"fmt"
"time"
"github.com/wcharczuk/go-chart/date"
)
2016-07-23 20:50:30 +02:00
// MarketHoursRange is a special type of range that compresses a time range into just the
2016-07-22 07:09:09 +02:00
// market (i.e. NYSE operating hours and days) range.
2016-07-23 20:50:30 +02:00
type MarketHoursRange struct {
Min time.Time
Max time.Time
MarketOpen time.Time
MarketClose time.Time
HolidayProvider date.HolidayProvider
2016-07-22 07:09:09 +02:00
Domain int
}
2016-07-22 07:22:22 +02:00
// IsZero returns if the range is setup or not.
2016-07-23 20:50:30 +02:00
func (mhr MarketHoursRange) IsZero() bool {
2016-07-22 07:22:22 +02:00
return mhr.Min.IsZero() && mhr.Max.IsZero()
}
2016-07-22 07:09:09 +02:00
// GetMin returns the min value.
2016-07-23 20:50:30 +02:00
func (mhr MarketHoursRange) GetMin() float64 {
2016-07-22 07:09:09 +02:00
return TimeToFloat64(mhr.Min)
}
// GetMax returns the max value.
2016-07-23 20:50:30 +02:00
func (mhr MarketHoursRange) GetMax() float64 {
2016-07-22 07:09:09 +02:00
return TimeToFloat64(mhr.Max)
}
// SetMin sets the min value.
2016-07-23 20:50:30 +02:00
func (mhr *MarketHoursRange) SetMin(min float64) {
2016-07-22 07:09:09 +02:00
mhr.Min = Float64ToTime(min)
}
// SetMax sets the max value.
2016-07-23 20:50:30 +02:00
func (mhr *MarketHoursRange) SetMax(max float64) {
2016-07-22 07:09:09 +02:00
mhr.Max = Float64ToTime(max)
}
// GetDelta gets the delta.
2016-07-23 20:50:30 +02:00
func (mhr MarketHoursRange) GetDelta() float64 {
2016-07-22 07:09:09 +02:00
min := TimeToFloat64(mhr.Min)
2016-07-23 07:43:27 +02:00
max := TimeToFloat64(mhr.Max)
2016-07-22 07:09:09 +02:00
return max - min
}
// GetDomain gets the domain.
2016-07-23 20:50:30 +02:00
func (mhr MarketHoursRange) GetDomain() int {
2016-07-22 07:09:09 +02:00
return mhr.Domain
}
// SetDomain sets the domain.
2016-07-23 20:50:30 +02:00
func (mhr *MarketHoursRange) SetDomain(domain int) {
2016-07-22 07:09:09 +02:00
mhr.Domain = domain
}
2016-07-23 20:50:30 +02:00
func (mhr MarketHoursRange) String() string {
2016-07-22 07:09:09 +02: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 20:50:30 +02:00
func (mhr MarketHoursRange) Translate(value float64) int {
2016-07-22 07:09:09 +02:00
valueTime := Float64ToTime(value)
2016-07-23 20:50:30 +02: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 07:43:27 +02: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 07:09:09 +02:00
}