backtrader.indicators.directionalmove module¶
Directional Movement Indicator Module - ADX and DI indicators.
This module provides the ADX (Average Directional Index) and Directional Indicators developed by J. Welles Wilder, Jr. for measuring trend strength.
- Classes:
UpMove: Upward move calculation. DownMove: Downward move calculation. _DirectionalIndicator: Base class for DI calculations. DirectionalIndicator: DI indicator (alias: DI). PlusDirectionalIndicator: +DI indicator (aliases: PlusDI, +DI). MinusDirectionalIndicator: -DI indicator (aliases: MinusDI, -DI). AverageDirectionalMovementIndex: ADX indicator (alias: ADX). AverageDirectionalMovementIndexRating: ADXR indicator (alias: ADXR). DirectionalMovementIndex: DMI with ADX and DI (alias: DMI). DirectionalMovement: Complete DM system (alias: DM).
Example
- class MyStrategy(bt.Strategy):
- def __init__(self):
# Calculate ADX to measure trend strength self.adx = bt.indicators.ADX(self.data, period=14)
# Or use DI for +DI and -DI self.di = bt.indicators.DI(self.data, period=14)
- def next(self):
# Buy when trend is strong (ADX > 25) and +DI crosses above -DI if self.adx[0] > 25 and self.di.plusDI[0] > self.di.minusDI[0]:
self.buy()
- class backtrader.indicators.directionalmove.UpMove[source]¶
Bases:
IndicatorDefined by J. Welles Wilder, Jr. in 1978 in his book “New Concepts in Technical Trading Systems” as part of the Directional Move System to calculate Directional Indicators.
Positive if the given data has moved higher than the previous day
- Formula:
upmove = data - data(-1)
- See:
- __init__(*args, **kwargs)¶
- next()[source]¶
Calculate up move for the current bar.
Returns data - data(-1), the positive price change.
- once(start, end)[source]¶
Calculate up moves in runonce mode.
Computes data[i] - data[i-1] for each bar.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.DownMove[source]¶
Bases:
IndicatorDefined by J. Welles Wilder, Jr. in 1978 in his book “New Concepts in Technical Trading Systems” as part of the Directional Move System to calculate Directional Indicators.
Positive if the given data has moved lower than the previous day
- Formula:
downmove = data(-1) - data
- See:
- __init__(*args, **kwargs)¶
- next()[source]¶
Calculate down move for the current bar.
Returns data(-1) - data, the negative price change as positive value.
- once(start, end)[source]¶
Calculate down moves in runonce mode.
Computes data[i-1] - data[i] for each bar.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.DirectionalIndicator[source]¶
Bases:
_DirectionalIndicatorDefined by J. Welles Wilder, Jr. in 1978 in his book “New Concepts in Technical Trading Systems”.
Intended to measure trend strength
- This indicator shows +DI, -DI:
Use PlusDirectionalIndicator (PlusDI) to get +DI
Use MinusDirectionalIndicator (MinusDI) to get -DI
Use AverageDirectionalIndex (ADX) to get ADX
Use AverageDirectionalIndexRating (ADXR) to get ADX, ADXR
Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI
- Formula:
upmove = high - high(-1)
downmove = low(-1) - low
+dm = upmove if upmove > downmove and upmove > 0 else 0
-dm = downmove if downmove > upmove and downmove > 0 else 0
+di = 100 * MovingAverage(+dm, period) / atr(period)
-di = 100 * MovingAverage(-dm, period) / atr(period)
The moving average used is the one originally defined by Wilder, the SmoothedMovingAverage
- alias = ('DI',)¶
- __init__(*args, **kwargs)¶
- next()[source]¶
Calculate +DI and -DI for the current bar.
Copies DI values from parent calculation.
- once(start, end)[source]¶
Calculate DI values in runonce mode.
Copies +DI and -DI values across all bars.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.PlusDirectionalIndicator[source]¶
Bases:
_DirectionalIndicatorDefined by J. Welles Wilder, Jr. in 1978 in his book “New Concepts in Technical Trading Systems”.
Intended to measure trend strength
- This indicator shows +DI:
Use MinusDirectionalIndicator (MinusDI) to get -DI
Use Directional Indicator (DI) to get +DI, -DI
Use AverageDirectionalIndex (ADX) to get ADX
Use AverageDirectionalIndexRating (ADXR) to get ADX, ADXR
Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI
- Formula:
upmove = high - high(-1)
downmove = low(-1) - low
+dm = upmove if upmove > downmove and upmove > 0 else 0
+di = 100 * MovingAverage(+dm, period) / atr(period)
The moving average used is the one originally defined by Wilder, the SmoothedMovingAverage
- alias = (('PlusDI', '+DI'),)¶
- plotinfo = <backtrader.metabase.plotinfo_obj object>¶
- __init__(*args, **kwargs)¶
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.MinusDirectionalIndicator[source]¶
Bases:
_DirectionalIndicatorDefined by J. Welles Wilder, Jr. in 1978 in his book “New Concepts in Technical Trading Systems”.
Intended to measure trend strength
- This indicator shows -DI:
Use PlusDirectionalIndicator (PlusDI) to get +DI
Use Directional Indicator (DI) to get +DI, -DI
Use AverageDirectionalIndex (ADX) to get ADX
Use AverageDirectionalIndexRating (ADXR) to get ADX, ADXR
Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI
- Formula:
upmove = high - high(-1)
downmove = low(-1) - low
-dm = downmove if downmove > upmove and downmove > 0 else 0
-di = 100 * MovingAverage(-dm, period) / atr(period)
The moving average used is the one originally defined by Wilder, the SmoothedMovingAverage
- alias = (('MinusDI', '-DI'),)¶
- plotinfo = <backtrader.metabase.plotinfo_obj object>¶
- __init__(*args, **kwargs)¶
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.AverageDirectionalMovementIndex[source]¶
Bases:
IndicatorDefined by J. Welles Wilder, Jr. in 1978 in his book “New Concepts in Technical Trading Systems”.
Intended to measure trend strength. Rewritten following MACD pattern with explicit next()/once() methods for reliable calculation.
- Formula:
upmove = high - high(-1)
downmove = low(-1) - low
+dm = upmove if upmove > downmove and upmove > 0 else 0
-dm = downmove if downmove > upmove and downmove > 0 else 0
+di = 100 * MovingAverage(+dm, period) / atr(period)
-di = 100 * MovingAverage(-dm, period) / atr(period)
dx = 100 * abs(+di - -di) / (+di + -di)
adx = MovingAverage(dx, period)
- alias = ('ADX',)¶
- plotlines = <backtrader.metabase.plotlines_obj object>¶
- __init__(*args, **kwargs)¶
- prenext()[source]¶
Track previous high/low during warmup.
Stores high and low values for directional move calculation.
- nextstart()[source]¶
Seed ADX calculation on first valid bar.
Calculates initial DM, DI, DX, and ADX values.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.AverageDirectionalMovementIndexRating[source]¶
Bases:
AverageDirectionalMovementIndexDefined by J. Welles Wilder, Jr. in 1978 in his book “New Concepts in Technical Trading Systems”.
Intended to measure trend strength.
ADXR is the average of ADX with a value period bars ago
- This indicator shows the ADX and ADXR:
Use PlusDirectionalIndicator (PlusDI) to get +DI
Use MinusDirectionalIndicator (MinusDI) to get -DI
Use Directional Indicator (DI) to get +DI, -DI
Use AverageDirectionalIndex (ADX) to get ADX
Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI
- Formula:
upmove = high - high(-1)
downmove = low(-1) - low
+dm = upmove if upmove > downmove and upmove > 0 else 0
-dm = downmove if downmove > upmove and downmove > 0 else 0
+di = 100 * MovingAverage(+dm, period) / atr(period)
-di = 100 * MovingAverage(-dm, period) / atr(period)
dx = 100 * abs(+di - -di) / (+di + -di)
adx = MovingAverage(dx, period)
adxr = (adx + adx(-period)) / 2
The moving average used is the one originally defined by Wilder, the SmoothedMovingAverage
- alias = ('ADXR',)¶
- plotlines = <backtrader.metabase.plotlines_obj object>¶
- __init__(*args, **kwargs)¶
- once(start, end)[source]¶
Calculate ADXR in runonce mode.
Computes ADXR as average of current ADX and ADX from period ago.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.DirectionalMovementIndex[source]¶
Bases:
AverageDirectionalMovementIndex,DirectionalIndicatorDefined by J. Welles Wilder, Jr. in 1978 in his book “New Concepts in Technical Trading Systems”.
Intended to measure trend strength
- This indicator shows the ADX, +DI, -DI:
Use PlusDirectionalIndicator (PlusDI) to get +DI
Use MinusDirectionalIndicator (MinusDI) to get -DI
Use Directional Indicator (DI) to get +DI, -DI
Use AverageDirectionalIndex (ADX) to get ADX
Use AverageDirectionalIndexRating (ADXRating) to get ADX, ADXR
Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI
- Formula:
upmove = high - high(-1)
downmove = low(-1) - low
+dm = upmove if upmove > downmove and upmove > 0 else 0
-dm = downmove if downmove > upmove and downmove > 0 else 0
+di = 100 * MovingAverage(+dm, period) / atr(period)
-di = 100 * MovingAverage(-dm, period) / atr(period)
dx = 100 * abs(+di - -di) / (+di + -di)
adx = MovingAverage(dx, period)
The moving average used is the one originally defined by Wilder, the SmoothedMovingAverage
- alias = ('DMI',)¶
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.DirectionalMovement[source]¶
Bases:
AverageDirectionalMovementIndexRating,DirectionalIndicatorDefined by J. Welles Wilder, Jr. in 1978 in his book “New Concepts in Technical Trading Systems”.
Intended to measure trend strength
This indicator shows ADX, ADXR, +DI, -DI.
Use PlusDirectionalIndicator (PlusDI) to get +DI
Use MinusDirectionalIndicator (MinusDI) to get -DI
Use Directional Indicator (DI) to get +DI, -DI
Use AverageDirectionalIndex (ADX) to get ADX
Use AverageDirectionalIndexRating (ADXR) to get ADX, ADXR
Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
- Formula:
upmove = high - high(-1)
downmove = low(-1) - low
+dm = upmove if upmove > downmove and upmove > 0 else 0
-dm = downmove if downmove > upmove and downmove > 0 else 0
+di = 100 * MovingAverage(+dm, period) / atr(period)
-di = 100 * MovingAverage(-dm, period) / atr(period)
dx = 100 * abs(+di - -di) / (+di + -di)
adx = MovingAverage(dx, period)
The moving average used is the one originally defined by Wilder, the SmoothedMovingAverage
- alias = ('DM',)¶
- frompackages = ()¶
- packages = ()¶