Prices to returns: private API
Functions
PortfolioOptimisers.append_price_block! — Function
append_price_block!(P::DataFrames.DataFrame, A::Nothing, ts, sym::Symbol) -> Vector{String}
append_price_block!(P::DataFrames.DataFrame, A::TimeSeries.TimeArray, ts, sym::Symbol) -> Vector{String}Write one price block of the PricesResult beside the asset block, on the asset clock.
The PricesResult states one clock. So this function reads a factor or benchmark series at the asset timestamps, and does not join it onto them. A join adds or drops observations, and price_ingestion owns every change of the clock. The function writes the columns one at a time, so the blocks do not need the one value type that TimeSeries.merge needs. A Float32 factor table beside a Float64 asset table converts, and the conversion promotes the two types.
Algorithm
- A block that is
nothingadds no column and no name. - Otherwise, check that the block states the asset clock. Throw an error that names the block if it does not.
- Write every absent price as
NaNwithunify_gaps, whichprice_ingestionalso runs. It changes nothing on aPricesResultthat the ingestion layer built. On aPricesResultbuilt by hand, it makes amissingconvert as aNaNdoes. - Write each column of the block into
Punder its own name, and return the names in order.
Arguments
P: The table the conversion is assembling, already carrying the clock and the asset columns.A: The factor, benchmark or Exogenous Series, ornothing.ts: The asset timestamps, which are the clock of thePricesResult.sym: The block's name in the refusal,:F,:Bor:E.
Validation
TimeSeries.timestamp(A) == ts. The function throws aConflictingArgumentErrorotherwise. The message namesprice_ingestion, which puts two series on one clock.
Returns
n::Vector{String}: The column names of the block, empty when the block isnothing.
Related
PortfolioOptimisers.gap_return_writable — Function
gap_return_writable(
p::AbstractVector,
r::AbstractVector
) -> BitVector
Find the cells of one column that a Gap Return algorithm can write.
Every algorithm of the family obeys this one rule, and no algorithm states it again. apply_gap_return restores every cell outside the returned set. So no algorithm can change a return computed from two observed prices, write a return before an asset's first price, or write one after its last price.
The bounds are the Span Rule and its projection, which listing_span and PortfolioOptimisers.project_span state for a whole panel. This function reads them off one price column, for two reasons. The writable set is per column. And the table that reaches the conversion step of prices_to_returns can be a filtered table, and not the caller's table.
Algorithm
- Read the offset between the two clocks as
length(p) - length(r), which is0whenpaddingkept the first observation and1when it did not. Return celljis then the change onto price rowt = j + off, and it reads the prices at rowst - 1andt. - Find the column's Listing Span on the price clock, from the first observed price to the last. A column with no observed price admits no cell.
- Admit return cell
jwhentlies in[first + 1, last]and one of the prices at rowst - 1andtis gapped. The interval is the span projected onto the returns clock, because a return reads the earlier price of its pair. A cell that reads a gapped price is always non-finite under the default rule. The function does not admit a cell that reads two observed prices, even a non-finite one. A zero price gives a return of-1or-Infon its own observation andInfon the next, and those cells keep the values of the default rule.
Arguments
p: One column's prices along the observation axis, gaps included.r: The returns that the default rule computed fromp.
Returns
w::BitVector: The same length asr, true on the cells that an algorithm can write.
Related
PortfolioOptimisers.apply_gap_return — Function
apply_gap_return(alg::Nothing, R::DataFrames.DataFrame, P::DataFrames.DataFrame, ret_method::Symbol) -> DataFrames.DataFrame
apply_gap_return(alg::AbstractGapReturnAlgorithm, R::DataFrames.DataFrame, P::DataFrames.DataFrame, ret_method::Symbol) -> DataFrames.DataFrameApply the gap_return_alg of prices_to_returns to the converted table.
The method for nothing is the default rule. It returns the table unchanged, so the returns are bit for bit the ones that step 3 of prices_to_returns computed. The method for an algorithm writes only the cells that gap_return_writable admits.
A Gap Return reads one column and no asset axis. So this function applies it to every series of the converted table, which holds the assets, the factors and the benchmarks.
Algorithm
- For each series column of
R, read the prices of the same name fromP. - Find the writable cells with
gap_return_writable. Go to the next column when no cell is writable. - Call
gap_returnon the column, and copy back only the writable cells. Every other cell keeps its value, whatever the algorithm returned. - Log an
@infowhen no column admits a cell. A table with no gap admits none, and so does a table whose gaps are all at the ends of the Listing Spans. The returns are then correct, so the function does not throw. It does not warn either, because it cannot tell this table from one where the caller expected a gap.
Arguments
alg: The Gap Return algorithm, ornothingfor the default rule.R: The converted table,:timestampfirst and one column per series.P: The price table that reaches the conversion, with the same series columns.ret_method::simpleor:log.
Validation
gap_returnreturns one value for each cell of the column. The function throws aDimensionMismatchotherwise.
Returns
R::DataFrames.DataFrame: The converted table, with the writable cells resolved.
Related
PortfolioOptimisers.carry_cash_index — Function
carry_cash_index(K::AbstractMatrix) -> AbstractMatrixHold the last value of each cash total-return index of K across each gap inside its span.
A deposit accrues interest on every day, so a cash index has a value at every observation from its first value to its last one, and a gap inside that span is a value that was not published. The function finds the first and the last value of each column, and writes onto each gap between them the value of the observation before it, which the walk has already filled. That is the rule of CarriedPrice on one listed run. A gap before the first value or after the last value stays a gap. The function changes a copy, so the caller's array does not change, also when unify_gaps returned it unchanged.
Arguments
K: Cash total-return indices,observations × currencies, with every gap written asNaN.
Returns
K̃::AbstractMatrix: A copy ofK, with each gap inside the span of its column filled.
Related