The Donchian channel is a volatility indicator forming a channel between the highest high and the lowest low of the chosen period.
Remarks
The Donchian channel is mainly used for providing entry signals. A long is established when the price closes above the Donchian Channel. Conversely, if it closes below, then a short is established.
Signature
1
publicabstractinterfaceDonchianChannel
Namespace
cAlgo.API.Indicators
Examples
1 2 3 4 5 6 7 8 91011121314
//...privateDonchianChanneldonchian;//...protectedoverridevoidOnStart(){donchian=Indicators.DonchianChannel(Period);}protectedoverridevoidOnBar(){Print("Top Value = {0}",donchian.Top.LastValue);Print("Middle Value = {0}",donchian.Middle.LastValue);Print("Bottom Value = {0}",donchian.Bottom.LastValue);//...}
usingcAlgo.API;usingcAlgo.API.Indicators;namespacecAlgo.Robots{/// This sample cBot shows how to use the Donchian Channel indicator[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]publicclassDonchianChannelSample:Robot{privatedouble_volumeInUnits;privateDonchianChannel_donchianChannel;[Parameter("Volume (Lots)", DefaultValue = 0.01)]publicdoubleVolumeInLots{get;set;}[Parameter("Label", DefaultValue = "Sample")]publicstringLabel{get;set;}[Parameter("Source")]publicDataSeriesSource{get;set;}publicPosition[]BotPositions{get{returnPositions.FindAll(Label);}}protectedoverridevoidOnStart(){_volumeInUnits=Symbol.QuantityToVolumeInUnits(VolumeInLots);_donchianChannel=Indicators.DonchianChannel(20);}protectedoverridevoidOnBar(){if(Bars.LowPrices.Last(1)<=_donchianChannel.Bottom.Last(1)&&Bars.LowPrices.Last(2)>_donchianChannel.Bottom.Last(2)){ClosePositions(TradeType.Sell);ExecuteMarketOrder(TradeType.Buy,SymbolName,_volumeInUnits,Label);}elseif(Bars.HighPrices.Last(1)>=_donchianChannel.Top.Last(1)&&Bars.HighPrices.Last(2)<_donchianChannel.Top.Last(2)){ClosePositions(TradeType.Buy);ExecuteMarketOrder(TradeType.Sell,SymbolName,_volumeInUnits,Label);}}privatevoidClosePositions(TradeTypetradeType){foreach(varpositioninBotPositions){if(position.TradeType!=tradeType)continue;ClosePosition(position);}}}}
1 2 3 4 5 6 7 8 91011121314
importclrclr.AddReference("cAlgo.API")# Import cAlgo API typesfromcAlgo.APIimport*# Import trading wrapper functionsfromrobot_wrapperimport*classTest():defon_start(self):# Periods is parameter defined in C# file of cBotself.donchian=api.Indicators.DonchianChannel(api.Period)defon_bar(self):print(f"Top Value = {self.donchian.Top.LastValue}")print(f"Middle Value = {self.donchian.Middle.LastValue}")print(f"Bottom Value = {self.donchian.Bottom.LastValue}")
""" VolumeInLots, Periods, and Label are parameters defined in C# file of cBot. """importclrclr.AddReference("cAlgo.API")# Import cAlgo API typesfromcAlgo.APIimport*# Import trading wrapper functionsfromrobot_wrapperimport*classDonchianChannelSample():defon_start(self):self.volumeInUnits=api.Symbol.QuantityToVolumeInUnits(api.VolumeInLots)self.donchianChannel=api.Indicators.DonchianChannel(api.Periods)defon_bar_closed(self):ifapi.Bars.LowPrices.Last(0)<=self.donchianChannel.Bottom.Last(0)andapi.Bars.LowPrices.Last(1)>self.donchianChannel.Bottom.Last(1):self.close_positions(TradeType.Sell)api.ExecuteMarketOrder(TradeType.Buy,api.SymbolName,self.volumeInUnits,api.Label)elifapi.Bars.HighPrices.Last(0)>=self.donchianChannel.Top.Last(0)andapi.Bars.HighPrices.Last(1)<self.donchianChannel.Top.Last(1):self.close_positions(TradeType.Buy)api.ExecuteMarketOrder(TradeType.Sell,api.SymbolName,self.volumeInUnits,api.Label)defget_bot_positions(self):returnapi.Positions.FindAll(api.Label)defclose_positions(self,tradeType):forpositioninself.get_bot_positions():ifposition.TradeType!=tradeType:continueapi.ClosePosition(position)
Properties
Top
Summary
Gets or sets the highest high of the period.
Signature
1
publicabstractIndicatorDataSeriesTop{get;set;}
Return Value
IndicatorDataSeries
Examples
1234
//...privateDonchianChanneldonchian;//...Print("Top Value = {0}",donchian.Top.LastValue);
123456
classTest():defon_start(self):# Periods is parameter defined in C# file of cBotself.donchian=api.Indicators.DonchianChannel(api.Period)defon_bar(self):print(f"Top Value = {self.donchian.Top.LastValue}")
Middle
Summary
Gets or sets the middle of the highest high and the lowest low of the period.
Signature
1
publicabstractIndicatorDataSeriesMiddle{get;set;}
Return Value
IndicatorDataSeries
Examples
1234
//...privateDonchianChanneldonchian;//...Print("Middle Value = {0}",donchian.Middle.LastValue);
123456
classTest():defon_start(self):# Periods is parameter defined in C# file of cBotself.donchian=api.Indicators.DonchianChannel(api.Period)defon_bar(self):print(f"Middle Value = {self.donchian.Middle.LastValue}")
Bottom
Summary
Gets or sets the lowest low of the period.
Signature
1
publicabstractIndicatorDataSeriesBottom{get;set;}
Return Value
IndicatorDataSeries
Examples
1234
//...privateDonchianChanneldonchian;//...Print("Bottom Value = {0}",donchian.Bottom.LastValue);
123456
classTest():defon_start(self):# Periods is parameter defined in C# file of cBotself.donchian=api.Indicators.DonchianChannel(api.Period)defon_bar(self):print(f"Bottom Value = {self.donchian.Bottom.LastValue}")