The Commodity Channel Index is used to determine overbought and oversold conditions relating to a symbol. The Commodity Channel Index can be used to forecast changes in price direction.
usingcAlgo.API;usingcAlgo.API.Indicators;namespacecAlgo.Indicator{[Indicator]publicclassCommodityChannelIndexReferenceExample:Indicator{privateCommodityChannelIndex_commodityChannelIndex;[Parameter("Periods", DefaultValue = 14)]publicintPeriods{get;set;}[Output("Main")]publicIndicatorDataSeriesResult{get;set;}protectedoverridevoidInitialize(){_commodityChannelIndex=Indicators.CommodityChannelIndex(Periods);}publicoverridevoidCalculate(intindex){// Display Result of IndicatorResult[index]=_commodityChannelIndex.Result[index];}}}
usingcAlgo.API;usingcAlgo.API.Indicators;namespacecAlgo.Robots{// This sample cBot shows how to use the Commodity Channel Index indicator[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]publicclassCommodityChannelIndexSample:Robot{privatedouble_volumeInUnits;privateCommodityChannelIndex_commodityChannelIndex;[Parameter("Volume (Lots)", DefaultValue = 0.01)]publicdoubleVolumeInLots{get;set;}[Parameter("Stop Loss (Pips)", DefaultValue = 10)]publicdoubleStopLossInPips{get;set;}[Parameter("Take Profit (Pips)", DefaultValue = 10)]publicdoubleTakeProfitInPips{get;set;}[Parameter("Label", DefaultValue = "Sample")]publicstringLabel{get;set;}publicPosition[]BotPositions{get{returnPositions.FindAll(Label);}}protectedoverridevoidOnStart(){_volumeInUnits=Symbol.QuantityToVolumeInUnits(VolumeInLots);_commodityChannelIndex=Indicators.CommodityChannelIndex(14);}protectedoverridevoidOnBar(){if(_commodityChannelIndex.Result.Last(1)>100&&_commodityChannelIndex.Result.Last(2)<=100){ClosePositions(TradeType.Sell);ExecuteMarketOrder(TradeType.Buy,SymbolName,_volumeInUnits,Label,StopLossInPips,TakeProfitInPips);}elseif(_commodityChannelIndex.Result.Last(1)<-100&&_commodityChannelIndex.Result.Last(2)>=-100){ClosePositions(TradeType.Buy);ExecuteMarketOrder(TradeType.Sell,SymbolName,_volumeInUnits,Label,StopLossInPips,TakeProfitInPips);}}privatevoidClosePositions(TradeTypetradeType){foreach(varpositioninBotPositions){if(position.TradeType!=tradeType)continue;ClosePosition(position);}}}}
//...privateCommodityChannelIndex_commodityChannelIndex;//...protectedoverridevoidOnStart(){_commodityChannelIndex=Indicators.CommodityChannelIndex(Periods);}protectedoverridevoidOnBar(){// Print to logPrint("The Current Commodity Channel Index is: {0}",_commodityChannelIndex.Result.LastValue);}//...