backtrader.indicators.crossover module¶
Crossover Indicator Module - Crossover detection indicators.
This module provides indicators for detecting when two data series cross each other (upward or downward).
- Classes:
NonZeroDifference: Tracks difference, memorizing last non-zero value (alias: NZD). CrossUp: Detects upward crossover. CrossDown: Detects downward crossover. CrossOver: Detects both directional crossovers.
Example
- class MyStrategy(bt.Strategy):
- def __init__(self):
self.sma_fast = bt.indicators.SMA(self.data, period=10) self.sma_slow = bt.indicators.SMA(self.data, period=20) self.crossover = bt.indicators.CrossOver(self.sma_fast, self.sma_slow)
- def next(self):
- if self.crossover[0] > 0:
self.buy()
- elif self.crossover[0] < 0:
self.sell()
- class backtrader.indicators.crossover.NonZeroDifference[source]¶
Bases:
IndicatorKeeps track of the difference between two data inputs, memorizing the last non-zero value if the current difference is zero
- alias = ('NZD',)¶
- __init__(*args, **kwargs)¶
- next()[source]¶
Calculate NZD for the current bar.
Memorizes last non-zero difference when current difference is zero.
- frompackages = ()¶
- once(start, end)¶
Implement once using next for batch calculation.
This is used when next is overridden but once is not. It loops through the range and calls next for each step.
- Parameters:
start – Starting index
end – Ending index
- oncestart(start, end)¶
Implement oncestart using nextstart for batch calculation.
This is used when nextstart is overridden but oncestart is not.
- Parameters:
start – Starting index
end – Ending index
- packages = ()¶
- preonce(start, end)¶
Implement preonce using prenext for batch calculation.
This is a generic implementation if prenext is overridden but preonce is not. It loops through the range and calls prenext for each step.
- Parameters:
start – Starting index
end – Ending index
- class backtrader.indicators.crossover.CrossUp[source]¶
Bases:
_CrossBaseUpward cross indicator
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.crossover.CrossDown[source]¶
Bases:
_CrossBaseDownward cross indicator
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.crossover.CrossOver[source]¶
Bases:
IndicatorGives signal for data crossover: 1.0 for upward cross, -1.0 for downward cross, 0.0 otherwise
- plotinfo = <backtrader.metabase.plotinfo_obj object>¶
- __init__(*args, **kwargs)¶
- prenext()[source]¶
Track difference during warmup period.
Updates _last_nzd for use in nextstart/next crossover detection.
- nextstart()[source]¶
Calculate crossover on first valid bar.
Handles replay mode special case and calculates initial crossover.
- next()[source]¶
Calculate crossover for the current bar.
Returns 1.0 for upward cross, -1.0 for downward cross, 0.0 otherwise. Handles replay mode correctly by deferring calculation when bars are updating.
- once(start, end)[source]¶
Calculate crossover in runonce mode.
Vectorized implementation that processes all bars at once.
- frompackages = ()¶
- packages = ()¶