Contents
Indicator Reference
Average Directional Index
Introduction
This indicator computes Average Directional Index which measures trend strength without regard to trend direction. Firstly, it calculates the Directional Movement and the True Range value, and then the values are accumulated and smoothed using a custom smoothing method proposed by Wilder. For an n period smoothing, 1/n of each period's value is added to the total period. From these accumulated values we are therefore able to derived the 'Positive Directional Index' (+DI) and 'Negative Directional Index' (-DI) which is used to calculate the Average Directional Index. Computation source: https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_directional_index_adx
Create Manual Indicators
You can manually create a AverageDirectionalIndex
indicator, so it doesn’t automatically update. Manual indicators let you update their values with any data you choose. The following reference table describes the AverageDirectionalIndex
constructor.
AverageDirectionalIndex()1/2
AverageDirectionalIndex QuantConnect.Indicators.AverageDirectionalIndex (
int
period
)
Initializes a new instance of the AverageDirectionalIndex
class.
AverageDirectionalIndex()2/2
AverageDirectionalIndex QuantConnect.Indicators.AverageDirectionalIndex (string
name,int
period )
Initializes a new instance of the AverageDirectionalIndex
class.
Update Manual Indicators
You can update the indicator automatically or manually.
Automatic Update
To register a manual indicator for automatic updates with the security data, call the RegisterIndicator
method.
private AverageDirectionalIndex _adx; // In Initialize() _adx = new AverageDirectionalIndex(name, period); _adx.Updated += IndicatorUpdateMethod; RegisterIndicator(symbol, _adx, Resolution.Daily); // In IndicatorUpdateMethod() if (_adx.IsReady) { var indicatorValue = _adx.Current.Value; }
# In Initialize() self.adx = AverageDirectionalIndex(name, period) self.adx.Updated += self.IndicatorUpdateMethod self.RegisterIndicator(symbol, self.adx, Resolution.Daily) # In IndicatorUpdateMethod() if self.adx.IsReady: indicator_value = self.adx.Current.Value
To customize the data that automatically updates the indicator, see Custom Indicator Periods and Custom Indicator Values.
Manual Update
Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Update
method with a TradeBar
, QuoteBar
, or an IndicatorDataPoint
. The indicator will only be ready after you prime it with enough data.
private AverageDirectionalIndex _adx; private Symbol symbol; // In Initialize() _adx = new AverageDirectionalIndex(period); symbol = AddEquity("SPY").Symbol; // In OnData() if (data.QuoteBars.ContainsKey(_symbol)) { _adx.Update(data.QuoteBars[symbol]); } if (_adx.IsReady) { var indicatorValue = _adx.Current.Value; }
# In Initialize() self.adx = AverageDirectionalIndex(period) self.symbol = self.AddEquity("SPY").Symbol # In OnData() if data.QuoteBars.ContainsKey(self.symbol): self.adx.Update(data.QuoteBars[self.symbol]) if self.adx.IsReady: indicator_value = self.adx.Current.Value
Create Automatic Indicators
The ADX method creates an AverageDirectionalIndex indicator, sets up a consolidator to update the indicator, and then returns the indicator so you can use it in your algorithm.
The following reference table describes the ADX
method:
ADX()1/1
AverageDirectionalIndex QuantConnect.Algorithm.QCAlgorithm.ADX (Symbol
symbol,Int32
period,*Nullable<Resolution>
resolution,*Func<IBaseData, IBaseDataBar>
selector )
Creates a new Average Directional Index indicator. The indicator will be automatically updated on the given resolution.
If you don't provide a resolution, it defauls to the security resolution. If you provide a resolution, it must be greater than or equal to the resolution of the security. For instance, if you subscribe to hourly data for a security, you should update its indicator with data that spans 1 hour or longer.
For more information about the selector argument, see Alternative Price Fields.
Get Indicator Values
To get the value of the indicator, use its Current.Value
attribute.
private AverageDirectionalIndex _adx; // In Initialize() var symbol = AddEquity("SPY").Symbol; _adx = ADX(symbol, period); // In OnData() if (_adx.IsReady) { var positiveDirectionalIndex = _adx.PositiveDirectionalIndex.Current.Value; var negativeDirectionalIndex = _adx.NegativeDirectionalIndex.Current.Value; var current = _adx.Current.Value; }
# In Initialize() symbol = self.AddEquity("SPY").Symbol self.adx = self.ADX(symbol, period) # In OnData() if self.adx.IsReady: positive_directional_index = self.adx.PositiveDirectionalIndex.Current.Value negative_directional_index = self.adx.NegativeDirectionalIndex.Current.Value current = self.adx.Current.Value
Visualization
To plot indicator values, in the OnData
event handler, call the Plot
method.
private AverageDirectionalIndex _adx; // In Initialize() var symbol = AddEquity("SPY").Symbol; _adx = ADX(symbol, period); // In OnData() if (_adx.IsReady) { Plot("My Indicators", "positivedirectionalindex", _adx.PositiveDirectionalIndex); Plot("My Indicators", "negativedirectionalindex", _adx.NegativeDirectionalIndex); Plot("My Indicators", "averagedirectionalindex", _adx.Current); }
# In Initialize() symbol = self.AddEquity("SPY").Symbol self.adx = self.ADX(symbol, period) # In OnData() if self.adx.IsReady: self.Plot("My Indicators", "positivedirectionalindex", self.adx.PositiveDirectionalIndex) self.Plot("My Indicators", "negativedirectionalindex", self.adx.NegativeDirectionalIndex) self.Plot("My Indicators", "averagedirectionalindex", self.adx.Current)
For more information about plotting indicators, see Plotting Indicators.