Contents
Indicator Reference
Donchian Channel
Introduction
This indicator computes the upper and lower band of the Donchian Channel. The upper band is computed by finding the highest high over the given period. The lower band is computed by finding the lowest low over the given period. The primary output value of the indicator is the mean of the upper and lower band for the given timeframe.
Create Manual Indicators
You can manually create a DonchianChannel
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 DonchianChannel
constructor.
DonchianChannel()1/4
DonchianChannel QuantConnect.Indicators.DonchianChannel (
int
period
)
Initializes a new instance of the DonchianChannel
class.
DonchianChannel()2/4
DonchianChannel QuantConnect.Indicators.DonchianChannel (
int
lowerPeriod
)
Initializes a new instance of the DonchianChannel
class.
DonchianChannel()3/4
DonchianChannel QuantConnect.Indicators.DonchianChannel (string
name,int
period )
Initializes a new instance of the DonchianChannel
class.
DonchianChannel()4/4
DonchianChannel QuantConnect.Indicators.DonchianChannel (string
name,int
lowerPeriod )
Initializes a new instance of the DonchianChannel
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 DonchianChannel _dch; // In Initialize() _dch = new DonchianChannel(name, upperPeriod, lowerPeriod, period); _dch.Updated += IndicatorUpdateMethod; RegisterIndicator(symbol, _dch, Resolution.Daily); // In IndicatorUpdateMethod() if (_dch.IsReady) { var indicatorValue = _dch.Current.Value; }
# In Initialize() self.dch = DonchianChannel(name, upperPeriod, lowerPeriod, period) self.dch.Updated += self.IndicatorUpdateMethod self.RegisterIndicator(symbol, self.dch, Resolution.Daily) # In IndicatorUpdateMethod() if self.dch.IsReady: indicator_value = self.dch.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 DonchianChannel _dch; private Symbol symbol; // In Initialize() _dch = new DonchianChannel(upperPeriod, lowerPeriod, period); symbol = AddEquity("SPY").Symbol; // In OnData() if (data.QuoteBars.ContainsKey(_symbol)) { _dch.Update(data.QuoteBars[symbol]); } if (_dch.IsReady) { var indicatorValue = _dch.Current.Value; }
# In Initialize() self.dch = DonchianChannel(upperPeriod, lowerPeriod, period) self.symbol = self.AddEquity("SPY").Symbol # In OnData() if data.QuoteBars.ContainsKey(self.symbol): self.dch.Update(data.QuoteBars[self.symbol]) if self.dch.IsReady: indicator_value = self.dch.Current.Value
Create Automatic Indicators
The DCH method creates an DonchianChannel 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 DCH
method:
DCH()1/2
DonchianChannel QuantConnect.Algorithm.QCAlgorithm.DCH (Symbol
symbol,Int32
upperPeriod,Int32
lowerPeriod,*Nullable<Resolution>
resolution,*Func<IBaseData, IBaseDataBar>
selector )
Creates a new Donchian Channel indicator which will compute the Upper Band and Lower Band. The indicator will be automatically updated on the given resolution.
DCH()2/2
DonchianChannel QuantConnect.Algorithm.QCAlgorithm.DCH (Symbol
symbol,Int32
period,*Nullable<Resolution>
resolution,*Func<IBaseData, IBaseDataBar>
selector )
Overload shorthand to create a new symmetric Donchian Channel indicator which has the upper and lower channels set to the same period length.
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 DonchianChannel _dch; // In Initialize() var symbol = AddEquity("SPY").Symbol; _dch = DCH(symbol, period); // In OnData() if (_dch.IsReady) { var upperBand = _dch.UpperBand.Current.Value; var lowerBand = _dch.LowerBand.Current.Value; var current = _dch.Current.Value; }
# In Initialize() symbol = self.AddEquity("SPY").Symbol self.dch = self.DCH(symbol, period) # In OnData() if self.dch.IsReady: upper_band = self.dch.UpperBand.Current.Value lower_band = self.dch.LowerBand.Current.Value current = self.dch.Current.Value
Visualization
To plot indicator values, in the OnData
event handler, call the Plot
method.
private DonchianChannel _dch; // In Initialize() var symbol = AddEquity("SPY").Symbol; _dch = DCH(symbol, period); // In OnData() if (_dch.IsReady) { Plot("My Indicators", "upperband", _dch.UpperBand); Plot("My Indicators", "lowerband", _dch.LowerBand); Plot("My Indicators", "donchianchannel", _dch.Current); }
# In Initialize() symbol = self.AddEquity("SPY").Symbol self.dch = self.DCH(symbol, period) # In OnData() if self.dch.IsReady: self.Plot("My Indicators", "upperband", self.dch.UpperBand) self.Plot("My Indicators", "lowerband", self.dch.LowerBand) self.Plot("My Indicators", "donchianchannel", self.dch.Current)
For more information about plotting indicators, see Plotting Indicators.