data

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CashAsset = "$CASH"
)

Variables

View Source
var (
	ErrSingle                 = errors.New("cannot call *Single functions with multiple securities or metrics")
	ErrBeginAfterEnd          = errors.New("invalid interval; begin after end date")
	ErrDataLargerThanCache    = errors.New("data larger than cache size")
	ErrDateLengthDoesNotMatch = errors.New("length of date and values arrays must match when using local date index")
	ErrInvalidTimeRange       = errors.New("start must be before end")
	ErrMultipleNotSupported   = errors.New("on-or-before can only return a single metric for a single security")
	ErrNoData                 = errors.New("no data available")
	ErrSecurityNotFound       = errors.New("security not found")
	ErrNoTradingDays          = errors.New("no trading days available")
	ErrOutsideCoveredTime     = errors.New("date range outside of covered time interval")
	ErrRangeDoesNotExist      = errors.New("range does not exist in cache")
	ErrUnsupportedMetric      = errors.New("unsupported metric")
)
View Source
var (
	CashSecurity = Security{
		Ticker:        CashAsset,
		CompositeFigi: CashAsset,
	}
	RiskFreeSecurity = Security{
		CompositeFigi: "PVGG06TNP6J8",
		Ticker:        "DGS3MO",
	}
)

Functions

func FilterDays added in v0.5.0

func FilterDays(frequency dataframe.Frequency, dates []time.Time) []time.Time

FilterDays takes an array of dates and filters it based on the requested frequency

func LoadSecuritiesFromDB added in v0.5.0

func LoadSecuritiesFromDB() error

Types

type ByDate added in v0.5.0

type ByDate []pair

ByDate implements sort.Interface for []pair based on `last` the time field.

func (ByDate) Len added in v0.5.0

func (a ByDate) Len() int

func (ByDate) Less added in v0.5.0

func (a ByDate) Less(i, j int) bool

func (ByDate) Swap added in v0.5.0

func (a ByDate) Swap(i, j int)

type BySecurityMetric added in v0.5.0

type BySecurityMetric []SecurityMetric

BySecurityMetric implements sort.Interface for []SecurityMetric

func (BySecurityMetric) Len added in v0.5.0

func (a BySecurityMetric) Len() int

func (BySecurityMetric) Less added in v0.5.0

func (a BySecurityMetric) Less(i, j int) bool

func (BySecurityMetric) Swap added in v0.5.0

func (a BySecurityMetric) Swap(i, j int)

type CacheItem added in v0.5.0

type CacheItem struct {
	Values        []float64
	Period        *Interval
	CoveredPeriod *Interval
	// contains filtered or unexported fields
}

func (*CacheItem) IsLocalDateIndex added in v0.5.0

func (item *CacheItem) IsLocalDateIndex() bool

IsLocalDateIndex returns true if a the CacheItem uses a local date index

func (*CacheItem) LocalDateIndex added in v0.5.0

func (item *CacheItem) LocalDateIndex() []time.Time

LocalDateIndex returns the local date index

func (*CacheItem) MarshalZerologObject added in v0.5.0

func (item *CacheItem) MarshalZerologObject(e *zerolog.Event)

type Interval added in v0.5.0

type Interval struct {
	Begin time.Time
	End   time.Time
}

Intervals store a beginning and ending time period

func (*Interval) Adjacent added in v0.5.0

func (interval *Interval) Adjacent(other *Interval) bool

Adjacent checks if offset touches the beginning or ending of interval (daily resolution) NOTE: Adjaceny implies the two intervals DO NOT overlap

func (*Interval) AdjacentLeft added in v0.5.0

func (interval *Interval) AdjacentLeft(other *Interval) bool

Adjacent checks if offset touches the beginning of interval (daily resolution) NOTE: Adjaceny implies the two intervals DO NOT overlap

func (*Interval) AdjacentRight added in v0.5.0

func (interval *Interval) AdjacentRight(other *Interval) bool

Adjacent checks if offset touches the ending of interval (daily resolution) NOTE: Adjaceny implies the two intervals DO NOT overlap

func (*Interval) Contains added in v0.5.0

func (interval *Interval) Contains(other *Interval) bool

Contains returns true if interval completely contains other

func (*Interval) Contiguous added in v0.5.0

func (interval *Interval) Contiguous(other *Interval) bool

Contiguous returns true if the other interval shares a common boarder with this interval this can mean they are adjacent or overlapping NOTE: contiguous implies that other is not a subset of interval

func (*Interval) MarshalZerologObject added in v0.5.0

func (interval *Interval) MarshalZerologObject(e *zerolog.Event)

MarshalZerologObject implement the log marshaller interface for zerolog

func (*Interval) Overlaps added in v0.5.0

func (interval *Interval) Overlaps(other *Interval) bool

Overlaps returns true if interval and other overlap

func (*Interval) Valid added in v0.5.0

func (interval *Interval) Valid() error

Valid checks if the given interval is valid range and returns an error if not

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func GetManagerInstance added in v0.5.0

func GetManagerInstance() *Manager

func (*Manager) GetLRU added in v0.5.0

func (manager *Manager) GetLRU(key string) []byte

GetPortfolio returns a cached portfolio

func (*Manager) GetMetricOnOrBefore added in v0.5.0

func (manager *Manager) GetMetricOnOrBefore(security *Security, metric Metric, date time.Time) (float64, time.Time, error)

GetMetricsOnOrBefore finds a single metric value on the requested date or the closest date available prior to the requested date

func (*Manager) GetMetrics added in v0.5.0

func (manager *Manager) GetMetrics(securities []*Security, metrics []Metric, begin, end time.Time) (dataframe.Map, error)

GetMetrics returns metrics for the requested securities over the specified time range

func (*Manager) PreloadMetrics added in v0.5.0

func (manager *Manager) PreloadMetrics(ctx context.Context, plan PortfolioPlan)

PreloadMetrics pre-emptively loads metrics into the data cache

func (*Manager) Reset added in v0.5.0

func (manager *Manager) Reset()

Reset restores the data manager to its initial state; clearing all cache - this is mostly used in testing

func (*Manager) SetLRU added in v0.5.0

func (manager *Manager) SetLRU(key string, val []byte)

SetLRU saves the portfolio in the online cache

func (*Manager) TradingDays

func (manager *Manager) TradingDays(begin, end time.Time) *dataframe.DataFrame

TradingDays returns a dataframe over the specified date period, all values in the dataframe are 0

func (*Manager) TradingDaysAtFrequency added in v0.5.0

func (manager *Manager) TradingDaysAtFrequency(frequency dataframe.Frequency, begin, end time.Time) []time.Time

type Metric

type Metric string
const (
	MetricOpen          Metric = "Open"
	MetricLow           Metric = "Low"
	MetricHigh          Metric = "High"
	MetricClose         Metric = "Close"
	MetricVolume        Metric = "Volume"
	MetricAdjustedClose Metric = "AdjustedClose"
	MetricDividendCash  Metric = "DividendCash"
	MetricSplitFactor   Metric = "SplitFactor"
)

type PortfolioPlan added in v0.5.0

type PortfolioPlan []*SecurityAllocation

func (PortfolioPlan) EndDate added in v0.5.0

func (pl PortfolioPlan) EndDate() time.Time

EndDate returns the ending date of the pie history

func (PortfolioPlan) Last added in v0.5.0

func (pl PortfolioPlan) Last() *SecurityAllocation

Last returns the last item in the PieHistory

func (PortfolioPlan) StartDate added in v0.5.0

func (pl PortfolioPlan) StartDate() time.Time

StartDate returns the starting date of the pie history

func (PortfolioPlan) Table added in v0.5.0

func (pl PortfolioPlan) Table()

Table prints an ASCII formated table to stdout

func (PortfolioPlan) Trim added in v0.5.0

func (pl PortfolioPlan) Trim(begin, end time.Time) PortfolioPlan

Trim the PieHistory to only cover the time period between begin and end (inclusive)

type PvDb added in v0.5.0

type PvDb struct {
}

func NewPvDb added in v0.5.0

func NewPvDb() *PvDb

NewPVDB Create a new PVDB data provider

func (*PvDb) GetEOD added in v0.5.0

func (p *PvDb) GetEOD(ctx context.Context, securities []*Security, metrics []Metric, begin, end time.Time) (dataframe.Map, error)

GetEOD fetches EOD metrics from the database

func (*PvDb) GetEODOnOrBefore added in v0.5.0

func (p *PvDb) GetEODOnOrBefore(ctx context.Context, security *Security, metric Metric, date time.Time) (float64, time.Time, error)

GetEODOnOrBefore queries the database for the last value on or before the specified date. It returns the metric value and the date of the measurement as well as any errors that occur.

func (*PvDb) TradingDays added in v0.5.0

func (p *PvDb) TradingDays(ctx context.Context, begin time.Time, end time.Time) ([]time.Time, error)

TradingDays returns a list of trading days between begin and end at the desired frequency

type Request added in v0.5.0

type Request struct {
	// contains filtered or unexported fields
}

func NewDataRequest added in v0.5.0

func NewDataRequest(securities ...*Security) *Request

NewDataRequest creates a new data request object for the requested securities. The frequency is defaulted to Monthly and the metric defaults to Adjusted Close

func (*Request) Between added in v0.5.0

func (req *Request) Between(a, b time.Time) (dataframe.Map, error)

Between queries the metric data store and returns a dataframe with metrics between the specified dates columns are named `CompositeFigi:MetricName`

func (*Request) Metrics added in v0.5.0

func (req *Request) Metrics(metrics ...Metric) *Request

func (*Request) On added in v0.5.0

func (req *Request) On(a time.Time) (map[SecurityMetric]float64, error)

On returns the price for the requested date

func (*Request) OnOrBefore added in v0.5.0

func (req *Request) OnOrBefore(a time.Time) (float64, time.Time, error)

func (*Request) OnSingle added in v0.5.0

func (req *Request) OnSingle(a time.Time) (float64, error)

OnSingle is a convenience function that returns the price for a single security and metric

func (*Request) Securities added in v0.5.0

func (req *Request) Securities(securities ...*Security) *Request

type Security added in v0.5.0

type Security struct {
	Ticker        string `json:"ticker"`
	CompositeFigi string `json:"compositeFigi"`
}

Security represents a tradeable asset

func MustSecurityFromFigi added in v0.6.0

func MustSecurityFromFigi(figi string) *Security

MustSecurityFromFigi loads a security from database using the Composite FIGI as the lookup key

func SecurityFromFigi added in v0.5.0

func SecurityFromFigi(figi string) (*Security, error)

SecurityFromFigi loads a security from database using the Composite FIGI as the lookup key

func SecurityFromTicker added in v0.5.0

func SecurityFromTicker(ticker string) (*Security, error)

SecurityFromTicker loads a security from database using the ticker as the lookup key

func SecurityFromTickerList added in v0.5.0

func SecurityFromTickerList(tickers []string) ([]*Security, error)

SecurityFromTickerList loads securities from database using the ticker as the lookup key

type SecurityAllocation added in v0.5.0

type SecurityAllocation struct {
	Date           time.Time
	Members        map[Security]float64
	Justifications map[string]float64
}

type SecurityMetric added in v0.5.0

type SecurityMetric struct {
	SecurityObject Security
	MetricObject   Metric
}

func NewSecurityMetricFromString added in v0.5.0

func NewSecurityMetricFromString(s string) SecurityMetric

func (SecurityMetric) String added in v0.5.0

func (sm SecurityMetric) String() string

String returns a string representation of the security metric

type SecurityMetricCache added in v0.5.0

type SecurityMetricCache struct {
	// contains filtered or unexported fields
}

func NewSecurityMetricCache added in v0.5.0

func NewSecurityMetricCache(sz int64, periods []time.Time) *SecurityMetricCache

func (*SecurityMetricCache) Check added in v0.5.0

func (cache *SecurityMetricCache) Check(security *Security, metric Metric, begin, end time.Time) (bool, []*Interval)

Check returns if the requested range is in the cache. If the range is not completely covered by the cache returns false and a list of intervals covered by the cache that partially match the requested range.

func (*SecurityMetricCache) Count added in v0.5.0

func (cache *SecurityMetricCache) Count() int

Count returns the number of securities + metrics in the cache

func (*SecurityMetricCache) Get added in v0.5.0

func (cache *SecurityMetricCache) Get(security *Security, metric Metric, begin, end time.Time) (*dataframe.DataFrame, error)

Get returns the requested data over the range. If no data matches the hash key return ErrRangeDoesNotExist

func (*SecurityMetricCache) GetPartial added in v0.5.0

func (cache *SecurityMetricCache) GetPartial(security *Security, metric Metric, begin, end time.Time) *dataframe.DataFrame

GetPartial returns the requested data over the range. If the requested period only partially exists, return what is available.

func (*SecurityMetricCache) ItemCount added in v0.5.0

func (cache *SecurityMetricCache) ItemCount(security *Security, metric Metric) int

ItemCount returns the number of non-contiguous items in the cache for the given SecurityMetric

func (*SecurityMetricCache) Items added in v0.5.0

func (cache *SecurityMetricCache) Items(security *Security, metric Metric) []*CacheItem

Items returns the items in the cache for a given SecurityMetric. This method is only intended for testing.

func (*SecurityMetricCache) Set added in v0.5.0

func (cache *SecurityMetricCache) Set(security *Security, metric Metric, begin, end time.Time, df *dataframe.DataFrame) error

Set adds the data for the specified security and metric to the cache

func (*SecurityMetricCache) SetMatched added in v0.5.0

func (cache *SecurityMetricCache) SetMatched(security *Security, metric Metric, begin, end time.Time, df *dataframe.DataFrame) error

SetMatched adds the data for the specified security and metric to the cache assuming df's date frame matches cache.periods exactly

func (*SecurityMetricCache) SetWithLocalDates added in v0.5.0

func (cache *SecurityMetricCache) SetWithLocalDates(security *Security, metric Metric, begin, end time.Time, df *dataframe.DataFrame) error

Set adds the data for the specified security and metric to the cache

func (*SecurityMetricCache) Size added in v0.5.0

func (cache *SecurityMetricCache) Size() int64

Size returns the number of bytes currently stored in the SecurityMetricCache

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL