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)¶
- prenext()¶
It will be called during the “minperiod” phase of an iteration.
- nextstart()¶
It will be called when the minperiod phase is over for the 1st post-minperiod value. Only called once and defaults to automatically calling next
- next()¶
Called to calculate values when the minperiod is over
- 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
- 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
- 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
- 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)¶
- prenext()¶
It will be called during the “minperiod” phase of an iteration.
- nextstart()¶
It will be called when the minperiod phase is over for the 1st post-minperiod value. Only called once and defaults to automatically calling next
- next()¶
Called to calculate values when the minperiod is over
- 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
- 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
- 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
- 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)¶
- prenext()¶
It will be called during the “minperiod” phase of an iteration.
- nextstart()¶
It will be called when the minperiod phase is over for the 1st post-minperiod value. Only called once and defaults to automatically calling next
- next()¶
Called to calculate values when the minperiod is over
- 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
- 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
- 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
- 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()¶
It will be called during the “minperiod” phase of an iteration.
- nextstart()¶
It will be called when the minperiod phase is over for the 1st post-minperiod value. Only called once and defaults to automatically calling next
- next()¶
Called to calculate values when the minperiod is over
- 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
- 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
- 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
- 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
- frompackages = ()¶
- packages = ()¶
- alias = ('ADXR',)¶
- plotlines = <backtrader.metabase.plotlines_obj object>¶
- __init__(*args, **kwargs)¶
- prenext()¶
It will be called during the “minperiod” phase of an iteration.
- nextstart()¶
It will be called when the minperiod phase is over for the 1st post-minperiod value. Only called once and defaults to automatically calling next
- next()¶
Called to calculate values when the minperiod is over
- 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
- 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
- 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
- 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
- frompackages = ()¶
- packages = ()¶
- alias = ('DMI',)¶
- 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
- frompackages = ()¶
- packages = ()¶
- alias = ('DM',)¶