PastView#

class backtest_lib.market.PastView#

Time-fenced read-only series up to the current decision point.

This protocol can abstract over different implementations (list-backed, NumPy array-backed, mmap, etc.) that present a “fenced” slice of historical snapshots ending at “now”, with no lookahead as to reduce the risk of lookahead bias while maintaining an ergonomic interface to access the market conditions.

abstractmethod after(start: Index | str, *, inclusive: bool = True) Self#

Return a view of periods at or after start.

Parameters:
  • start – Start bound for the slice.

  • inclusive – Whether to include the start period.

Returns:

A view containing periods after the bound.

abstractmethod before(end: Index | str, *, inclusive: bool = False) Self#

Return a view of periods before end.

Parameters:
  • end – End bound for the slice.

  • inclusive – Whether to include the end period.

Returns:

A view containing periods before the bound.

abstractmethod between(start: Index | str, end: Index | str, *, closed: Closed | str = Closed.LEFT) Self#

Return a view for periods between two bounds.

Parameters:
  • start – Start bound for the slice.

  • end – End bound for the slice.

  • closed – Which side of the interval is inclusive.

Returns:

A new view containing the bounded range of periods.

abstract property by_period: ByPeriod[ValueT, Index]#

Period-axis accessor for the view.

Returns:

ByPeriod accessor that indexes snapshots by period.

abstract property by_security: BySecurity[ValueT, Index]#

Security-axis accessor for the view.

Returns:

BySecurity accessor that indexes series by security.

abstractmethod static from_dataframe(df: TypeAliasForwardRef('polars.DataFrame') | TypeAliasForwardRef('pandas.DataFrame')) PastView#

Build a view from a DataFrame.

The DataFrame must include a date column and one column per security.

Parameters:

df – Polars or pandas DataFrame of historical values.

Returns:

PastView instance containing the provided values.

abstractmethod static from_security_mappings(ms: SecurityMappings[int], periods: Sequence[Index]) PastView[int, Index]#
abstractmethod static from_security_mappings(ms: SecurityMappings[float], periods: Sequence[Index]) PastView[float, Index]
abstractmethod static from_security_mappings(ms: Sequence[Mapping[str, float | int]], periods: Sequence[Index]) PastView[float, Index]

Build a view from security mappings and periods.

Parameters:
  • ms – Sequence of per-period security mappings.

  • periods – Period labels aligned to ms.

Returns:

PastView instance containing the provided values.

abstract property periods: tuple[Index, ...]#

Periods represented by this view.

Returns:

Tuple of period labels ordered from oldest to newest.

abstract property plot: PastViewPlotAccessor#

Return the plotting accessor for this view.

Returns:

Backend-specific plot accessor.

abstract property securities: tuple[str, ...]#

Securities represented by this view.

Returns:

Tuple of security identifiers aligned to the view.

Slicing Options#

Closed(*values)