pybroker.indicator module

PyBroker reimplements standard indicators with volatility normalization and a robust non-linear rescaling so their values are comparable across symbols and market regimes.

Contains indicator related functionality.

class Indicator(name: str, fn: Callable[[...], ndarray[tuple[Any, ...], dtype[float64]]], kwargs: dict[str, Any])[source]

Bases: object

Class representing an indicator.

Parameters:
  • name – Name of indicator.

  • fnCallable used to compute the series of indicator values.

  • kwargsdict of kwargs to pass to fn.

__call__(data: BarData | DataFrame, hyperparams: dict[str, Any] | None = None) Series[source]

Computes indicator values.

property hyperparam_names: frozenset[str]
intervals(*intervals: int | Literal['daily', 'weekly', 'monthly', 'quarterly', 'yearly'] | str) IntervalBoundIndicator[source]

Binds this indicator to one or more compression intervals for use with pybroker.strategy.Strategy.add_execution().

A bound indicator is computed on exactly the listed intervals, where its values are read with pybroker.context.IntervalContext.indicator(). Binding replaces the default base-timeframe computation; include the literal 'base' in intervals to also compute the indicator on the base timeframe. Bound intervals are automatically made available through pybroker.context.ExecContext.interval() without also declaring them in the intervals parameter of add_execution():

sma_10 = pybroker.indicator("sma_10", sma_fn)
strategy.add_execution(
    fn, "SPY", indicators=sma_10.intervals("base", "weekly")
)
Parameters:

intervals – One or more TimeframeIntervals to compute this indicator on, each strictly coarser than the base bar spacing of the backtest data, or the literal 'base' for the base timeframe.

Returns:

IntervalBoundIndicator binding this indicator to intervals.

iqr(data: BarData | DataFrame) float[source]

Generates indicator data with data and computes its interquartile range (IQR).

relative_entropy(data: BarData | DataFrame) float[source]

Generates indicator data with data and computes its relative entropy.

class IndicatorSet[source]

Bases: IndicatorsMixin

Computes data for multiple indicators.

__call__(df: DataFrame, parallel_indicators: bool = False) DataFrame[source]

Computes indicator data.

Parameters:
  • dfpandas.DataFrame of input data.

  • parallel_indicators – If True, indicator data is computed in parallel using multiple processes. If False, indicator data is computed serially. Defaults to False.

Returns:

pandas.DataFrame containing the computed indicator data.

add(indicators: Indicator | Iterable[Indicator], *args)[source]

Adds indicators.

clear()[source]

Removes all indicators.

remove(indicators: Indicator | Iterable[Indicator], *args)[source]

Removes indicators.

class IndicatorsMixin[source]

Bases: object

Mixin implementing indicator related functionality.

compute_indicators(df: DataFrame, indicator_syms: Iterable[IndicatorSymbol], cache_date_fields: CacheDateFields | None, parallel_indicators: bool, interval_data: IntervalData | None = None, symbol_store: SymbolArrayStore | None = None, hyperparams: dict[str, Any] | None = None) dict[IndicatorSymbol, Series][source]

Computes indicator data for the provided pybroker.common.IndicatorSymbol pairs.

Parameters:
  • dfpandas.DataFrame used to compute the indicator values.

  • indicator_symsIterable of pybroker.common.IndicatorSymbol pairs of indicators to compute.

  • cache_date_fields – Date fields used to key cache data. Pass None to disable caching.

  • parallel_indicators – If True, indicator data is computed in parallel using multiple processes. If False, indicator data is computed serially for all pybroker.common.IndicatorSymbol pairs.

  • interval_data – Optional compressed interval data.

  • symbol_store – Optional pre-built pybroker.scope.SymbolArrayStore to avoid rebuilding per-symbol arrays from df.

  • hyperparams – Optional hyperparameter overrides for indicators that declare hyperparameters. During pybroker.optimize.OptimizeMixin.optimize(), results are memoized in memory.

Returns:

dict mapping each pybroker.common.IndicatorSymbol pair to a computed pandas.Series of indicator values.

adx(name: str, lookback: int) Indicator[source]

Average Directional Movement Index.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

Returns:

Average Directional Movement Index Indicator.

aroon_diff(name: str, lookback: int) Indicator[source]

Aroon Upward Trend minus Aroon Downward Trend.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

Returns:

Aroon Upward Trend minus Aroon Downward Trend Indicator.

aroon_down(name: str, lookback: int) Indicator[source]

Aroon Downward Trend.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

Returns:

Aroon Downward Trend Indicator.

aroon_up(name: str, lookback: int) Indicator[source]

Aroon Upward Trend.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

Returns:

Aroon Upward Trend Indicator.

atr(name: str, lookback: int) Indicator[source]

Average True Range (ATR).

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

Returns:

Average True Range Indicator.

close_minus_ma(name: str, lookback: int, atr_length: int, scale: float = 1.0) Indicator[source]

Close Minus Moving Average.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • atr_length – Lookback length used for Average True Range (ATR) normalization.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 1.0.

Returns:

Close Minus Moving Average Indicator.

cubic_deviation(name: str, field: str, lookback: int, scale: float = 0.6) Indicator[source]

Deviation from Cubic Trend.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field name.

  • lookback – Number of lookback bars.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 0.6.

Returns:

Deviation from Cubic Trend Indicator.

cubic_trend(name: str, field: str, lookback: int, atr_length: int, scale: float = 1.0) Indicator[source]

Cubic Trend Strength.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field name.

  • lookback – Number of lookback bars.

  • atr_length – Lookback length used for Average True Range (ATR) normalization.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 1.0.

Returns:

Cubic Trend Strength Indicator.

delta_on_balance_volume(name: str, lookback: int, delta_length: int = 0, scale: float = 0.6) Indicator[source]

Delta On-Balance Volume.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • delta_length – Lag for differencing.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 0.6.

Returns:

Delta On-Balance Volume Indicator.

detrended_rsi(name: str, field: str, short_length: int, long_length: int, reg_length: int) Indicator[source]

Detrended Relative Strength Index (RSI).

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field name.

  • short_length – Lookback for the short-term RSI.

  • long_length – Lookback for the long-term RSI.

  • reg_length – Number of bars used for linear regressions.

Returns:

Detrended RSI Indicator.

highest(name: str, field: str, period: int) Indicator[source]

Creates a rolling high Indicator.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field for computing the rolling high.

  • period – Lookback period.

Returns:

Rolling high Indicator.

indicator(name: str, fn: Callable[[...], ndarray[tuple[Any, ...], dtype[float64]]], **kwargs) Indicator[source]

Creates an Indicator instance and registers it globally with name.

Parameters:
  • name – Name for referencing the indicator globally.

  • fnCallable[[BarData, ...], NDArray[float]] used to compute the series of indicator values.

  • **kwargs – Additional arguments to pass to fn.

Returns:

Indicator instance.

intraday_intensity(name: str, lookback: int, smoothing: float = 0.0) Indicator[source]

Intraday Intensity.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • smoothing – Amount of smoothing; <= 1 for none. Defaults to 0.

Returns:

Intraday Intensity Indicator.

laguerre_rsi(name: str, fe_length: int = 13) Indicator[source]

Laguerre Relative Strength Index (RSI).

Parameters:
  • name – Indicator name.

  • fe_length – Fractal Energy length. Defaults to 13.

Returns:

Laguerre RSI Indicator.

linear_deviation(name: str, field: str, lookback: int, scale: float = 0.6) Indicator[source]

Deviation from Linear Trend.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field name.

  • lookback – Number of lookback bars.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 0.6.

Returns:

Deviation from Linear Trend Indicator.

linear_trend(name: str, field: str, lookback: int, atr_length: int, scale: float = 1.0) Indicator[source]

Linear Trend Strength.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field name.

  • lookback – Number of lookback bars.

  • atr_length – Lookback length used for Average True Range (ATR) normalization.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 1.0.

Returns:

Linear Trend Strength Indicator.

lowest(name: str, field: str, period: int) Indicator[source]

Creates a rolling low Indicator.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field for computing the rolling low.

  • period – Lookback period.

Returns:

Rolling low Indicator.

macd(name: str, short_length: int, long_length: int, smoothing: float = 0.0, scale: float = 1.0) Indicator[source]

Moving Average Convergence Divergence.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field name.

  • short_length – Short-term lookback.

  • long_length – Long-term lookback.

  • smoothing – Compute MACD minus smoothed if >= 2.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 1.0.

Returns:

Moving Average Convergence Divergence Indicator.

money_flow(name: str, lookback: int, smoothing: float = 0.0) Indicator[source]

Chaikin’s Money Flow.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • smoothing – Amount of smoothing; <= 1 for none. Defaults to 0.

Returns:

Chaikin’s Money Flow Indicator.

normalized_negative_volume_index(name: str, lookback: int, scale: float = 0.5) Indicator[source]

Normalized Negative Volume Index.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 0.5.

Returns:

Normalized Negative Volume Index Indicator.

normalized_on_balance_volume(name: str, lookback: int, scale: float = 0.6) Indicator[source]

Normalized On-Balance Volume.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 0.6.

Returns:

Normalized On-Balance Volume Indicator.

normalized_positive_volume_index(name: str, lookback: int, scale: float = 0.5) Indicator[source]

Normalized Positive Volume Index.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 0.5.

Returns:

Normalized Positive Volume Index Indicator.

price_change_oscillator(name: str, short_length: int, multiplier: int, scale: float = 4.0) Indicator[source]

Price Change Oscillator.

Parameters:
  • name – Indicator name.

  • short_length – Number of short lookback bars.

  • multiplier – Multiplier used to compute number of long lookback bars = multiplier * short_length.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 4.0.

Returns:

Price Change Oscillator Indicator.

price_intensity(name: str, smoothing: float = 0.0, scale: float = 0.8) Indicator[source]

Price Intensity.

Parameters:
  • name – Indicator name.

  • smoothing – Amount of smoothing. Defaults to 0.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 0.8.

Returns:

Price Intensity Indicator.

price_volume_fit(name: str, lookback: int, scale: float = 9.0) Indicator[source]

Price Volume Fit.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 9.0.

Returns:

Price Volume Fit Indicator.

quadratic_deviation(name: str, field: str, lookback: int, scale: float = 0.6) Indicator[source]

Deviation from Quadratic Trend.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field name.

  • lookback – Number of lookback bars.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 0.6.

Returns:

Deviation from Quadratic Trend Indicator.

quadratic_trend(name: str, field: str, lookback: int, atr_length: int, scale: float = 1.0) Indicator[source]

Quadratic Trend Strength.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field name.

  • lookback – Number of lookback bars.

  • atr_length – Lookback length used for Average True Range (ATR) normalization.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 1.0.

Returns:

Quadratic Trend Strength Indicator.

reactivity(name: str, lookback: int, smoothing: float = 1.0, scale: float = 0.6) Indicator[source]

Reactivity.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • smoothing – Smoothing multiplier.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 0.6.

Returns:

Reactivity Indicator.

returns(name: str, field: str, period: int = 1) Indicator[source]

Creates a rolling returns Indicator.

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field for computing the rolling returns.

  • period – Returns period. Defaults to 1.

Returns:

Rolling returns Indicator.

stochastic(name: str, lookback: int, smoothing: int = 0) Indicator[source]

Stochastic.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • smoothing – Number of times the raw stochastic is smoothed, either 0, 1, or 2 times. Defaults to 0.

Returns:

Stochastic Indicator.

stochastic_rsi(name: str, field: str, rsi_lookback: int, sto_lookback: int, smoothing: float = 0.0) Indicator[source]

Stochastic Relative Strength Index (RSI).

Parameters:
  • name – Indicator name.

  • fieldpybroker.common.BarData field name.

  • rsi_lookback – Lookback length for RSI calculation.

  • sto_lookback – Lookback length for Stochastic calculation.

  • smoothing – Amount of smoothing; <= 1 for none. Defaults to 0.

Returns:

Stochastic RSI Indicator.

volume_momentum(name: str, short_length: int, multiplier: int = 2, scale: float = 3.0) Indicator[source]

Volume Momentum.

Parameters:
  • name – Indicator name.

  • short_length – Number of short lookback bars.

  • multiplier – Lookback multiplier. Defaults to 2.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 3.0.

Returns:

Volume Momentum Indicator.

volume_weighted_ma_ratio(name: str, lookback: int, scale: float = 1.0) Indicator[source]

Volume-Weighted Moving Average Ratio.

Parameters:
  • name – Indicator name.

  • lookback – Number of lookback bars.

  • scale – Increase > 1.0 for more compression of return values, decrease < 1.0 for less. Defaults to 1.0.

Returns:

Volume-Weighted Moving Average Ratio Indicator.