cci_on_stepchannel

Download script
Author: Copyright 2015, fxborg
cci_on_stepchannel

Technical Logic Overview: CCI on StepChannel

This script is a sophisticated technical analysis indicator designed to help traders visualize price momentum and volatility by combining two distinct mathematical concepts: Dynamic Step Channels and a modified Commodity Channel Index (CCI).

Instead of simply plotting standard price lines, this tool creates a structured environment that identifies "levels" or "steps" in the market, allowing for a clearer view of trend persistence and potential exhaustion points.

1. The Step Channel Mechanism

The primary logic of the script revolves around building a "Step Channel." This is not a standard moving average; rather, it is a dynamic boundary that calculates market volatility to create a visual corridor.

  • Volatility Assessment: The script looks at historical price data to determine the current volatility (standard deviation). It then applies a "Scale Factor" to this volatility to establish a buffer zone.
  • The "Step" Logic: Unlike most indicators that change smoothly every second, this logic compares current price movements against the previous boundaries. It only updates the "High," "Low," and "Middle" boundaries when the price moves significantly enough to break the existing threshold.
  • Hybrid Smoothing: By using this approach, the indicator filters out minor market "noise" while locking onto major structural shifts. This results in a "stepped" appearance where the channel remains stable until a meaningful trend change occurs.

2. Modified CCI Calculation

The script then takes the data generated by the Step Channel and processes it through a customized version of the Commodity Channel Index (CCI).

  • Baseline Comparison: Rather than comparing the price to a simple moving average, the script compares the current "Price Buffer" (an average of high, low, and close prices) against the Middle Ma Buffer derived from the Step Channel.
  • Relative Momentum: It calculates how far the current price has deviated from this dynamic middle step. This provides a measure of whether the market is overextended in one direction or consolidating.
  • Normalized Output: The final result is a value that indicates the strength of the movement relative to the historical volatility of that specific "step."

3. Visual Representation

The script outputs three primary visual components to a separate window on your trading platform:

  • The Main CCI Line: This represents the core momentum calculation. It oscillates around a zero line, showing how aggressively the price is pushing against the established Step Channel.
  • Upper and Lower Histograms: These provide a visual "weight" to the momentum. When the CCI moves into positive or negative territory, these histograms highlight the intensity of the movement, making it easier to identify when a trend is gaining strength or beginning to weaken.

Summary for the Trader

In essence, this tool doesn't just tell you if the price is going up or down; it tells you how much the price is moving relative to its current structural boundaries. By combining Step Channels (which define the market's structure) with a modified CCI (which measures momentum), it helps identify when a trend is becoming "overstretched" compared to the recent volatility.

Indicators Used
Standard Deviation indicator
7 Views
0 Downloads
0 Favorites
cci_on_stepchannel
//+------------------------------------------------------------------+
//|                                            CCI_OnStepChannel.mq4 |
//| CCI on StepChannel v1.00                  Copyright 2015, fxborg |
//|                                   http://fxborg-labo.hateblo.jp/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, fxborg"
#property link      "http://fxborg-labo.hateblo.jp/"
#property version   "1.00"
//---
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_level1    -80.0
#property indicator_level2     80.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//---
#property indicator_color1 DarkGray
#property indicator_color2 DodgerBlue
#property indicator_color3 DeepPink
#property indicator_label1 "CCI"
#property indicator_label2 "CCI Upper"
#property indicator_label3 "CCI Lower"
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1 
#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_SOLID
#property indicator_style3 STYLE_SOLID
//--- input parameters
input string Description1="--- Step Channel Settings---";
input double InpScaleFactor=2.25; // Scale factor
input int    InpMaPeriod=3;       // Smooth Period
input int    InpVolatilityPeriod=70; //  Volatility Period
input string Description2="--- CCI Settings ---";
input int InpCCIPeriod=32; // CCI Period
//---
int    InpFastPeriod=(int)MathRound(InpVolatilityPeriod/7); //  Fast Period
int    InpMode=2; //  1:Simple Mode 2:hybrid Mode
ENUM_MA_METHOD InpMaMethod=MODE_SMMA; // Ma Method 
//---- will be used as indicator buffers
double UpperBuffer[];
double MiddleBuffer[];
double LowerBuffer[];
//---
double UpperMaBuffer[];
double MiddleMaBuffer[];
double LowerMaBuffer[];
//---
double CCI_Buffer[];
double CCI_H_Buffer[];
double CCI_L_Buffer[];
double PriceBuffer[];
double BaseBuffer[];
//---
double HighBuffer[];
double LowBuffer[];
double CloseBuffer[];
double StdDevBuffer[];
//---- declaration of global variables
int min_rates_total;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---- Initialization of variables of data calculation starting point
   min_rates_total=1+InpFastPeriod+InpVolatilityPeriod+InpMaPeriod+InpMaPeriod+1+InpCCIPeriod+1;
//--- indicator buffers mapping
   IndicatorBuffers(14);
//--- indicator buffers
   SetIndexBuffer(0,CCI_Buffer);
   SetIndexBuffer(1,CCI_H_Buffer);
   SetIndexBuffer(2,CCI_L_Buffer);
   SetIndexBuffer(3,UpperMaBuffer);
   SetIndexBuffer(4,MiddleMaBuffer);
   SetIndexBuffer(5,LowerMaBuffer);
   SetIndexBuffer(6,UpperBuffer);
   SetIndexBuffer(7,MiddleBuffer);
   SetIndexBuffer(8,LowerBuffer);
   SetIndexBuffer(9,HighBuffer);
   SetIndexBuffer(10,LowBuffer);
   SetIndexBuffer(11,CloseBuffer);
   SetIndexBuffer(12,StdDevBuffer);
   SetIndexBuffer(13,PriceBuffer);
//---
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexEmptyValue(2,EMPTY_VALUE);
   SetIndexEmptyValue(3,0);
   SetIndexEmptyValue(4,0);
   SetIndexEmptyValue(5,0);
   SetIndexEmptyValue(6,0);
   SetIndexEmptyValue(7,0);
   SetIndexEmptyValue(8,0);
   SetIndexEmptyValue(9,0);
   SetIndexEmptyValue(10,0);
   SetIndexEmptyValue(11,0);
   SetIndexEmptyValue(12,0);
   SetIndexEmptyValue(13,0);
//---
   SetIndexDrawBegin(0,min_rates_total);
   SetIndexDrawBegin(1,min_rates_total);
   SetIndexDrawBegin(2,min_rates_total);
//---
   string short_name="CCI on Step Channel ("
                     +IntegerToString(InpCCIPeriod)+","
                     +IntegerToString(InpScaleFactor)+","
                     +IntegerToString(InpMaPeriod)+","
                     +IntegerToString(InpVolatilityPeriod)+")";
   IndicatorShortName(short_name);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int i,j,first;
//--- check for bars count
   if(rates_total<=min_rates_total)
      return(0);
//---
   MathSrand(int(TimeLocal()));
//--- indicator buffers
   ArraySetAsSeries(CCI_Buffer,false);
   ArraySetAsSeries(CCI_H_Buffer,false);
   ArraySetAsSeries(CCI_L_Buffer,false);
   ArraySetAsSeries(UpperMaBuffer,false);
   ArraySetAsSeries(LowerMaBuffer,false);
   ArraySetAsSeries(MiddleMaBuffer,false);
   ArraySetAsSeries(UpperBuffer,false);
   ArraySetAsSeries(LowerBuffer,false);
   ArraySetAsSeries(MiddleBuffer,false);
   ArraySetAsSeries(HighBuffer,false);
   ArraySetAsSeries(LowBuffer,false);
   ArraySetAsSeries(CloseBuffer,false);
   ArraySetAsSeries(StdDevBuffer,false);
   ArraySetAsSeries(PriceBuffer,false);
   ArraySetAsSeries(BaseBuffer,false);
//--- rate data
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);
   ArraySetAsSeries(time,false);
//+----------------------------------------------------+
//| Set StdDev Buffeer                                 |
//+----------------------------------------------------+
   first=InpFastPeriod+1-1;
   if(first+1<prev_calculated)
      first=prev_calculated-2;
   else
     {
      for(i=0; i<first; i++)
        {
         PriceBuffer[i]=(high[i]+low[i]+close[i])/3;
         StdDevBuffer[i]=EMPTY_VALUE;
        }
     }
//---
   for(i=first; i<rates_total-1 && !IsStopped(); i++)
     {
      StdDevBuffer[i]=iStdDev(Symbol(),PERIOD_CURRENT,InpFastPeriod,0,InpMaMethod,PRICE_CLOSE,rates_total-i);
      PriceBuffer[i]=(high[i]+low[i]+close[i])/3;
     }
//+----------------------------------------------------+
//| Set Median Buffeer                                 |
//+----------------------------------------------------+
   first=InpFastPeriod+InpVolatilityPeriod+InpMaPeriod+InpMaPeriod+1;
   if(first+1<prev_calculated)
      first=prev_calculated-2;
   else
     {
      for(i=0; i<first; i++)
        {
         MiddleBuffer[i]=close[i];
         HighBuffer[i]=high[i];
         LowBuffer[i]=low[i];
        }
     }
//---
   for(i=first; i<rates_total-1 && !IsStopped(); i++)
     {
      //---
      if(!random(20) && rates_total-i>min_rates_total*2 && MiddleBuffer[i]!=0) continue;
      //---
      double h,l,c,hsum=0.0,lsum=0.0,csum=0.0;
      //---
      for(j=0;j<InpMaPeriod;j++)
        {
         hsum += high[i-j];
         lsum += low[i-j];
         csum += close[i-j];
        }
      //---
      h=hsum/InpMaPeriod;
      l=lsum/InpMaPeriod;
      c=csum/InpMaPeriod;
      //--- Base Volatility
      double sd=0.0;
      for(j=(i-InpVolatilityPeriod+1);j<=i;j++)
         sd+=StdDevBuffer[j];
      //--- Ma Buffer
      double v=sd/InpVolatilityPeriod;
      double base=v*InpScaleFactor;
      //--- Hybrid Mode
      if((h-base)>HighBuffer[i-1]) HighBuffer[i]=h;
      else if(h+base<HighBuffer[i-1]) HighBuffer[i]=h+base;
      else HighBuffer[i]=HighBuffer[i-1];
      //---
      if(l+base<LowBuffer[i-1]) LowBuffer[i]=l;
      else if((l-base)>LowBuffer[i-1]) LowBuffer[i]=l-base;
      else LowBuffer[i]=LowBuffer[i-1];
      //---
      if((c-base/2)>CloseBuffer[i-1]) CloseBuffer[i]=c-base/2;
      else if(c+base/2<CloseBuffer[i-1]) CloseBuffer[i]=c+base/2;
      else CloseBuffer[i]=CloseBuffer[i-1];
      //---
      MiddleBuffer[i]=(HighBuffer[i]+LowBuffer[i]+CloseBuffer[i]*2)/4;
      UpperBuffer[i]=HighBuffer[i] + base/2;
      LowerBuffer[i]=LowBuffer[i]  - base/2;
      //---
      hsum=0.0;
      lsum=0.0;
      csum=0.0;
      //---
      for(j=0;j<InpMaPeriod;j++)
        {
         hsum += UpperBuffer[i-j];
         lsum += LowerBuffer[i-j];
         csum += MiddleBuffer[i-j];
        }
      //---
      UpperMaBuffer[i]=hsum/InpMaPeriod;
      LowerMaBuffer[i]=lsum/InpMaPeriod;
      MiddleMaBuffer[i]=csum/InpMaPeriod;
     }
//+----------------------------------------------------+
//| Set CCI Buffeer                                    |
//+----------------------------------------------------+
   first=InpCCIPeriod+1-1;
   if(first+1<prev_calculated)
      first=prev_calculated-2;
   else
     {
      for(i=0; i<first; i++)
         CCI_Buffer[i]=EMPTY_VALUE;
     }
//---      
   for(i=first; i<rates_total-1 && !IsStopped(); i++)
     {
      double sum=0;
      for(j=0;j<InpCCIPeriod;j++)
         sum+=MathAbs(PriceBuffer[i-j]-MiddleMaBuffer[i]);
      //---
      sum*=0.015/InpCCIPeriod;
      if(sum==0.0)
         CCI_Buffer[i]=0.0;
      else
         CCI_Buffer[i]=(PriceBuffer[i]-MiddleMaBuffer[i])/sum;
      //---
      if(0<CCI_Buffer[i]) CCI_H_Buffer[i]=CCI_Buffer[i];
      if(0>CCI_Buffer[i]) CCI_L_Buffer[i]=CCI_Buffer[i];
     }
//----    
   return(rates_total);
  }
//+----------------------------------------------------+
//| random func                                        |
//+----------------------------------------------------+
bool random(int x)
  {
   int ran=MathRand();
   bool res=(MathMod(ran,x)==0);
   return(res);
  }
//+------------------------------------------------------------------+

Comments