WalkForward: private API

PortfolioOptimisers.WalkForward_Onl — Type
const WalkForward_Onl = Union{WalkForwardEstimator, Online{<:WalkForwardEstimator}}

Alias for a walk-forward scheme, plain or an Online Scheme.

An Online Scheme is an Online around a walk-forward, built by the scheme's function constructor — OnlineIndexWalkForward, OnlineDateWalkForward, OnlineHindsightSplit. It is not a WalkForwardEstimator by supertype, because a struct has one, so every bound that admits a walk-forward is written on this alias and the wrapped form reaches the same methods as the plain one. split, n_splits and fold_evaluation forward to the wrapped scheme; folds_are_stepped is where the two differ.

Related

source
PortfolioOptimisers.OptCVE_Onl — Type
const OptCVE_Onl = Union{OptimisationCrossValidationEstimator, Online{<:WalkForwardEstimator}}

Alias for the schemes that the cv field of a meta-optimiser takes: an optimisation cross-validation scheme, plain or an Online Scheme.

NestedClustered and Stacking bind their cv field to it, and predict_outer_returns dispatches on it. It takes no split result, because an estimator holds no result. An Online Scheme is an Online around a walk-forward, so it is not an OptimisationCrossValidationEstimator by supertype, and the alias names it.

Related

source
PortfolioOptimisers.DatesUnionPeriod — Type
const DatesUnionPeriod = Union{Dates.Period, Dates.CompoundPeriod}

Alias for a Dates period or compound period.

Used internally to accept either simple date periods (e.g., Dates.Month(1)) or compound periods (e.g., Dates.Month(1) + Dates.Day(1)) as date offsets in walk-forward cross-validation.

Related

source
PortfolioOptimisers.assert_positive_period — Function
assert_positive_period(
    p::Union{Dates.CompoundPeriod, Period},
    sym::Union{AbstractString, Symbol}
)

Assert that the period p moves a date forward.

A Dates.Period must be greater than zero. A Dates.CompoundPeriod must hold at least one positive part, so a form such as Month(1) - Day(1) passes. A compound period whose parts are all zero or negative moves no date forward.

Arguments

  • p: The period to check (DatesUnionPeriod).
  • sym: Symbolic name used in the error message.

Validation

  • A positive period, as above, which raises a DomainError that names sym and p.

Returns

  • nothing.

Related

source
PortfolioOptimisers.walk_forward_date_range — Function
walk_forward_date_range(ts::AbstractVector, period::DatesUnionPeriod,
                        period_offset::Option{<:DatesUnionPeriod}, adjuster::DateAdjType)

Build the date range that a DateWalkForward estimator walks over the timestamps ts.

The range runs from the first to the last timestamp with a step of period. If period_offset is not nothing, the start moves to min(ts[1], ts[1] - period_offset) and the end moves to max(ts[end], ts[end] - period_offset). Then the adjuster is applied, and the offset is added to every date. This shifts each date by the offset and loses neither end of ts. A negative offset moves a date after ts[end] back into the data, so that date stays in the range.

Arguments

  • ts: Sorted timestamp vector (cv_timestamps).
  • period: Step of the date range.
  • period_offset: Offset applied to every date of the range, or nothing.
  • adjuster: Function applied to the unshifted range, e.g. a business-day adjuster.

Returns

Related

source
PortfolioOptimisers.date_index_positions — Function
date_index_positions(ts::AbstractVector, date_range, previous::Bool,
                     ::Type{T} = Int) where {T <: Integer}

Map every date of date_range to a position in the sorted timestamp vector ts.

A date that falls between two timestamps maps to the previous timestamp if previous is true, and to the next timestamp if previous is false. A date before ts[1] maps to the first timestamp. The mapping stops at the first date that falls after ts[end], so the result is not longer than date_range.

Arguments

  • ts: Sorted timestamp vector (cv_timestamps).
  • date_range: Date range from walk_forward_date_range.
  • previous: If true, a date between two timestamps takes the previous timestamp.
  • T: Element type of the returned vector.

Returns

  • Vector{T}: Positions in ts, in the order of date_range.

Related

source
PortfolioOptimisers.date_walk_forward_train_starts — Function
date_walk_forward_train_starts(train_size, ts::AbstractVector, date_range,
                               idx::AbstractVector{<:Integer})

Return the row where the training window of each test start begins, one entry per position of idx, and zero where no training window fits inside the data.

An Integer train_size counts dates of the range, so the training window of the test that starts at the date in position p starts at the row of the date in position p - train_size. A Period train_size is subtracted from the date in position p, and the training window starts at the last timestamp on or before the result.

Arguments

Returns

  • Vector{<:Integer}: The first training row for each position of idx, or zero.

Related

source
PortfolioOptimisers.date_walk_forward_bounds — Function
date_walk_forward_bounds(dwf::DateWalkForward, rd::Prices_RR)

Compute the bounds of every fold that the date walk-forward dwf makes over the returns data rd.

Fold k trains on train_lo[k]:(test_lo[k] - purged_size - 1) and tests on test_lo[k]:test_hi[k]. train_lo already holds the start that expand_train sets. The first test starts at the first position whose training window starts inside the data (date_walk_forward_train_starts).

Two dates of the range can map to one timestamp, for example the two days of a weekend under a daily period over business-day data. A fold whose test window holds no observation for this reason is dropped. A fold that is kept must have at least one training observation after the purge.

Arguments

  • dwf: Date-based walk-forward estimator.
  • rd: Returns-level or price-level data with timestamps (Prices_RR).

Validation

  • The timestamp vector (cv_timestamps) must not be nothing.
  • Every fold that is kept must have a training window that is not empty after expand_train and purged_size, else an IsEmptyError is thrown before a fold is returned.

Returns

  • (train_lo, test_lo, test_hi): Three vectors with one entry per fold.

Related

source
PortfolioOptimisers.resolve_previous_weights_source — Function
resolve_previous_weights_source(pws::Nothing, wd::Nothing)
resolve_previous_weights_source(pws::Nothing, wd::AbstractWeightDrift)
resolve_previous_weights_source(pws::TargetWeights, wd::Option{<:AbstractWeightDrift})
resolve_previous_weights_source(pws::AbstractPreviousWeightsSource, wd::Option{<:AbstractWeightDrift})

Resolve the Previous-Weights Source a fold loop runs, from the two switches of a walk-forward scheme.

An unset pws follows wd. If the series drifts, the fund holds the drifted weights at the end of the fold, so the trade it places next is the distance from them. A turnover charged against the targets while the series drifts, or the reverse, prices a trade that no one placed. So the two consistent pairs are no drift with the target weights, and a drift with the drifted weights. An explicit pws overrides the rule, for the two mixed pairs.

The fold loop reads nothing as the target weights. So the resolution maps TargetWeights to nothing, and every reader of the source keeps its two arms.

Algorithm

  1. pws and wd are both unset: give nothing, the target weights.
  2. pws is unset and wd is set: give DriftedWeights, and held_weights_drift runs wd.
  3. pws is a TargetWeights: give nothing, the target weights, whatever wd is.
  4. Any other pws: give it unchanged.

Arguments

  • pws: Previous-weights source of the scheme, or nothing.
  • wd: Weight drift of the scheme, or nothing.

Returns

  • Option{<:AbstractPreviousWeightsSource}: The source the fold loop reads, where nothing is the target weights.

Related

source