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).

示例

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[源代码]

基类:Indicator

Measures 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)
next()[源代码]

Calculate momentum for the current bar.

Momentum = current_price - price_period_ago

once(start, end)[源代码]

Calculate momentum in runonce mode.

Computes price difference across all bars.

frompackages = ()
packages = ()
class backtrader.indicators.momentum.MomentumOscillator[源代码]

基类:Indicator

Measures the ratio of change in prices over a period

Formula:
  • mosc = 100 * (data / data_period)

See:
alias = ('MomentumOsc',)
__init__(*args, **kwargs)
next()[源代码]

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)[源代码]

Calculate momentum oscillator in runonce mode.

Computes 100 * ratio across all bars.

frompackages = ()
packages = ()
class backtrader.indicators.momentum.RateOfChange[源代码]

基类:Indicator

Measures the ratio of change in prices over a period

Formula:
  • roc = (data - data_period) / data_period

See:
alias = ('ROC',)
__init__(*args, **kwargs)
next()[源代码]

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)[源代码]

Calculate ROC in runonce mode.

Computes rate of change ratio across all bars.

frompackages = ()
packages = ()
class backtrader.indicators.momentum.RateOfChange100[源代码]

基类:Indicator

Measures 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()[源代码]

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)[源代码]

Calculate ROC100 in runonce mode.

Computes rate of change with base 100 across all bars.

frompackages = ()
packages = ()
backtrader.indicators.momentum.ROC

RateOfChange 的别名

backtrader.indicators.momentum.ROC100

RateOfChange100 的别名