backtrader.indicators.momentum module¶
Momentum Indicator Module - Momentum and Rate of Change.
This module provides momentum-based indicators for measuring the rate of price change over a given period.
- Classes:
Momentum: Measures price change (difference). MomentumOscillator: Momentum as ratio (alias: MomentumOsc). RateOfChange: Rate of change indicator (alias: ROC). RateOfChange100: ROC with base 100 (alias: ROC100).
Example
- class MyStrategy(bt.Strategy):
- def __init__(self):
self.mom = bt.indicators.Momentum(self.data.close, period=12) self.roc = bt.indicators.ROC(self.data.close, period=10) self.roc100 = bt.indicators.ROC100(self.data.close, period=12)
- def next(self):
# Momentum-based strategy if self.mom.momentum[0] > 0:
self.buy()
- elif self.mom.momentum[0] < 0:
self.sell()
- class backtrader.indicators.momentum.Momentum[source]¶
Bases:
IndicatorMeasures the change in price by calculating the difference between the current price and the price from a given period ago
- Formula:
momentum = data - data_period
- See:
- plotinfo = <backtrader.metabase.plotinfo_obj object>¶
- __init__(*args, **kwargs)¶
- once(start, end)[source]¶
Calculate momentum in runonce mode.
Computes price difference across all bars.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.momentum.MomentumOscillator[source]¶
Bases:
IndicatorMeasures the ratio of change in prices over a period
- Formula:
mosc = 100 * (data / data_period)
- See:
- alias = ('MomentumOsc',)¶
- __init__(*args, **kwargs)¶
- next()[source]¶
Calculate momentum oscillator for the current bar.
Oscillator = 100 * (current_price / price_period_ago) Returns 0.0 if previous value is 0 to avoid division by zero.
- once(start, end)[source]¶
Calculate momentum oscillator in runonce mode.
Computes 100 * ratio across all bars.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.momentum.RateOfChange[source]¶
Bases:
IndicatorMeasures the ratio of change in prices over a period
- Formula:
roc = (data - data_period) / data_period
- See:
- alias = ('ROC',)¶
- __init__(*args, **kwargs)¶
- next()[source]¶
Calculate ROC for the current bar.
ROC = (current_price - price_period_ago) / price_period_ago Returns 0.0 if previous value is 0 to avoid division by zero.
- once(start, end)[source]¶
Calculate ROC in runonce mode.
Computes rate of change ratio across all bars.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.momentum.RateOfChange100[source]¶
Bases:
IndicatorMeasures the ratio of change in prices over a period with base 100
This is, for example, how ROC is defined in stockcharts
- Formula:
roc = 100 * (data - data_period) / data_period
- See:
- alias = ('ROC100',)¶
- __init__(*args, **kwargs)¶
- next()[source]¶
Calculate ROC100 for the current bar.
ROC100 = 100 * (current_price - price_period_ago) / price_period_ago Returns 0.0 if previous value is 0 to avoid division by zero.
- once(start, end)[source]¶
Calculate ROC100 in runonce mode.
Computes rate of change with base 100 across all bars.
- frompackages = ()¶
- packages = ()¶
- backtrader.indicators.momentum.ROC¶
alias of
RateOfChange
- backtrader.indicators.momentum.ROC100¶
alias of
RateOfChange100