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

  1. A block that is nothing adds no column and no name.
  2. Otherwise, check that the block states the asset clock. Throw an error that names the block if it does not.
  3. Write every absent price as NaN with unify_gaps, which price_ingestion also runs. It changes nothing on a PricesResult that the ingestion layer built. On a PricesResult built by hand, it makes a missing convert as a NaN does.
  4. Write each column of the block into P under 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, or nothing.
  • ts: The asset timestamps, which are the clock of the PricesResult.
  • sym: The block's name in the refusal, :F, :B or :E.

Validation

Returns

  • n::Vector{String}: The column names of the block, empty when the block is nothing.

Related

source
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

  1. Read the offset between the two clocks as length(p) - length(r), which is 0 when padding kept the first observation and 1 when it did not. Return cell j is then the change onto price row t = j + off, and it reads the prices at rows t - 1 and t.
  2. 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.
  3. Admit return cell j when t lies in [first + 1, last] and one of the prices at rows t - 1 and t is 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 -1 or -Inf on its own observation and Inf on 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 from p.

Returns

  • w::BitVector: The same length as r, true on the cells that an algorithm can write.

Related

source
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.DataFrame

Apply 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

  1. For each series column of R, read the prices of the same name from P.
  2. Find the writable cells with gap_return_writable. Go to the next column when no cell is writable.
  3. Call gap_return on the column, and copy back only the writable cells. Every other cell keeps its value, whatever the algorithm returned.
  4. Log an @info when 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, or nothing for the default rule.
  • R: The converted table, :timestamp first and one column per series.
  • P: The price table that reaches the conversion, with the same series columns.
  • ret_method: :simple or :log.

Validation

  • gap_return returns one value for each cell of the column. The function throws a DimensionMismatch otherwise.

Returns

  • R::DataFrames.DataFrame: The converted table, with the writable cells resolved.

Related

source
PortfolioOptimisers.carry_cash_index — Function
carry_cash_index(K::AbstractMatrix) -> AbstractMatrix

Hold 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 as NaN.

Returns

  • K̃::AbstractMatrix: A copy of K, with each gap inside the span of its column filled.

Related

source