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).
示例
- 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[源代码]¶
基类:
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()[源代码]¶
Calculate up move for the current bar.
Returns data - data(-1), the positive price change.
- once(start, end)[源代码]¶
Calculate up moves in runonce mode.
Computes data[i] - data[i-1] for each bar.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.DownMove[源代码]¶
基类:
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()[源代码]¶
Calculate down move for the current bar.
Returns data(-1) - data, the negative price change as positive value.
- once(start, end)[源代码]¶
Calculate down moves in runonce mode.
Computes data[i-1] - data[i] for each bar.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.DirectionalIndicator[源代码]¶
基类:
_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)¶
- once(start, end)[源代码]¶
Calculate DI values in runonce mode.
Copies +DI and -DI values across all bars.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.PlusDirectionalIndicator[源代码]¶
基类:
_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[源代码]¶
基类:
_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[源代码]¶
基类:
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()[源代码]¶
Track previous high/low during warmup.
Stores high and low values for directional move calculation.
- nextstart()[源代码]¶
Seed ADX calculation on first valid bar.
Calculates initial DM, DI, DX, and ADX values.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.AverageDirectionalMovementIndexRating[源代码]¶
基类:
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)[源代码]¶
Calculate ADXR in runonce mode.
Computes ADXR as average of current ADX and ADX from period ago.
- frompackages = ()¶
- packages = ()¶
- class backtrader.indicators.directionalmove.DirectionalMovementIndex[源代码]¶
基类:
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[源代码]¶
基类:
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 = ()¶