CCI_MA_Smoothed
Download scriptTechnical Overview: Custom Commodity Channel Index (CCI) Indicator
This script is designed to provide a more sophisticated analysis of market price momentum and volatility compared to the standard Commodity Channel Index. It achieves this by offering high levels of customization regarding how price data is gathered, how averages are calculated, and how the final output is smoothed.
Core Calculation Logic
The indicator functions by measuring how much the current price deviates from its recent historical average. The process follows a multi-step logical flow:
-
Moving Average Calculation: The script first establishes a baseline for the "average" price over a specific period. Unlike standard indicators, this script allows users to choose between several different mathematical models for this average (such as Simple, Exponential, Smoothed, Linear Weighted, or Least Squares Moving Averages). It also lets the user decide which price point to prioritize—whether it be the open, high, low, close, or a calculation of those figures.
-
Standard Deviation and Relation: Once the baseline average is established, the script calculates two key values:
- Relative Difference: It determines how far the current market price sits from the baseline average.
- Volatility (Deviation): It calculates the "spread" or variance of prices over the same period. This tells the script whether a price movement is normal for the current market conditions or if it is an extreme outlier.
-
CCI Generation: The final CCI value is produced by dividing the Relative Difference by the Volatility. Essentially, this provides a ratio: "How many standard deviations is the current price away from its average?" This helps identify overbought or oversold conditions.
-
Smoothing Mechanism: To prevent the indicator from becoming too erratic or "noisy" due to sudden market spikes, the script includes a secondary smoothing layer. It applies a final moving average to the calculated CCI values, resulting in a cleaner, more readable line that highlights primary trends while filtering out minor fluctuations.
Key Features for Users
- Versatile Baseline Models: Users can switch between different types of Moving Averages (MA) to change how the script interprets "normal" price behavior.
- Dynamic Price Inputs: The ability to choose specific price types (e.g., Median, Typical, or Weighted prices) allows for a more nuanced reflection of market activity beyond just the closing price.
- Customizable Smoothing: A secondary smoothing period is included, allowing users to fine-tune the visual output to match their preferred trading speed—choosing between a "fast" reactive line or a "slow" stable trend line.
//+------------------------------------------------------------------+
//| CCI.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
//----
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Yellow
#property indicator_color2 Aqua
//---- input parameters
//---- indicator parameters
extern string m = "--Moving Average Types--";
extern string m0 = " 0 = SMA";
extern string m1 = " 1 = EMA";
extern string m2 = " 2 = SMMA";
extern string m3 = " 3 = LWMA";
extern string m4 = " 4 = LSMA";
extern int MA_Type=0;
extern string p = "--Applied Price Types--";
extern string p0 = " 0 = close";
extern string p1 = " 1 = open";
extern string p2 = " 2 = high";
extern string p3 = " 3 = low";
extern string p4 = " 4 = median(high+low)/2";
extern string p5 = " 5 = typical(high+low+close)/3";
extern string p6 = " 6 = weighted(high+low+close+close)/4";
extern int MA_AppliedPrice = 0;
extern string c = "--CCI Inputs--";
extern int CCIPeriod = 14;
extern int SmoothingMA_Type = 2;
extern int SmoothingMA_Period = 3;
//---- buffers
double CCIBuffer[];
double CCIBufferSmoothed[];
double RelBuffer[];
double DevBuffer[];
double MovBuffer[];
int MA_Mode;
string strMAType;
int SmoothingMode;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- 3 additional buffers are used for counting.
IndicatorBuffers(5);
SetIndexBuffer(1, RelBuffer);
SetIndexBuffer(2, DevBuffer);
SetIndexBuffer(3, MovBuffer);
//---- indicator lines
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, CCIBuffer);
SetIndexStyle(1, DRAW_LINE);
SetIndexBuffer(1, CCIBufferSmoothed);
//----
if(CCIPeriod <= 0)
CCIPeriod = 14;
SmoothingMode = SmoothingMA_Type;
if (SmoothingMA_Type > 3) SmoothingMode = 2;
//----
SetIndexDrawBegin(0, CCIPeriod);
//---- name for DataWindow and indicator subwindow label
switch (MA_Type)
{
case 1: strMAType="EMA"; MA_Mode=MODE_EMA; break;
case 2: strMAType="SMMA"; MA_Mode=MODE_SMMA; break;
case 3: strMAType="LWMA"; MA_Mode=MODE_LWMA; break;
case 4: strMAType="LSMA"; break;
default: strMAType="SMA"; MA_Mode=MODE_SMA; break;
}
short_name = "CCI-" +strMAType+ " (" +CCIPeriod + ") ";
IndicatorShortName(short_name);
SetIndexLabel(0, short_name);
//----
return(0);
}
//+------------------------------------------------------------------------+
//| LSMA - Least Squares Moving Average function calculation |
//| LSMA_In_Color Indicator plots the end of the linear regression line |
//+------------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| LSMA with PriceMode |
//| PrMode 0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, |
//| 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4 |
//+------------------------------------------------------------------+
double LSMA(int TimeFrame, int Rperiod, int prMode, int shift)
{
int i, myShift;
double sum, pr;
int length;
double lengthvar;
double tmp;
double wt;
length = Rperiod;
sum = 0;
for(i = length; i >= 1 ; i--)
{
lengthvar = length + 1;
lengthvar /= 3;
tmp = 0;
myShift = length - i + shift;
switch (prMode)
{
case 0: pr = iClose(NULL,TimeFrame,myShift);break;
case 1: pr = iOpen(NULL,TimeFrame,myShift);break;
case 2: pr = iHigh(NULL,TimeFrame,myShift);break;
case 3: pr = iLow(NULL,TimeFrame,myShift);break;
case 4: pr = (iHigh(NULL,TimeFrame,myShift) + iLow(NULL,TimeFrame,myShift))/2;break;
case 5: pr = (iHigh(NULL,TimeFrame,myShift) + iLow(NULL,TimeFrame,myShift) + iClose(NULL,TimeFrame,myShift))/3;break;
case 6: pr = (iHigh(NULL,TimeFrame,myShift) + iLow(NULL,TimeFrame,myShift) + iClose(NULL,TimeFrame,myShift) + iClose(NULL,TimeFrame,myShift))/4;break;
}
tmp = ( i - lengthvar)*pr;
sum+=tmp;
}
wt = sum*6/(length*(length+1));
wt = MathFloor(wt/Point)*Point;
return(wt);
}
//+------------------------------------------------------------------+
//| Commodity Channel Index |
//+------------------------------------------------------------------+
int start()
{
int i, k, counted_bars = IndicatorCounted();
double price, sum, mul;
if(CCIPeriod <= 1)
return(0);
if(Bars <= CCIPeriod)
return(0);
//---- initial zero
if(counted_bars < 1)
{
for(i = 1; i <= CCIPeriod; i++)
CCIBuffer[Bars-i] = 0.0;
for(i = 1; i <= CCIPeriod; i++)
DevBuffer[Bars-i] = 0.0;
for(i = 1; i <= CCIPeriod; i++)
MovBuffer[Bars-i] =0.0;
}
//---- last counted bar will be recounted
int limit = Bars - counted_bars;
if(counted_bars > 0)
limit++;
//---- moving average
for(i = 0; i < limit; i++)
{
if (MA_Type == 4)
MovBuffer[i]=LSMA(0, CCIPeriod, MA_AppliedPrice,i);
else
MovBuffer[i]=iMA(NULL,0,CCIPeriod,0,MA_Mode,MA_AppliedPrice,i);
}
//---- standard deviations
i = Bars - CCIPeriod + 1;
if(counted_bars > CCIPeriod - 1)
i = Bars - counted_bars - 1;
mul = 0.015 / CCIPeriod;
while(i >= 0)
{
sum = 0.0;
k = i + CCIPeriod - 1;
while(k >= i)
{
price =(High[k] + Low[k] + Close[k]) / 3;
sum += MathAbs(price - MovBuffer[i]);
k--;
}
DevBuffer[i] = sum*mul;
i--;
}
i = Bars - CCIPeriod + 1;
if(counted_bars > CCIPeriod - 1)
i = Bars - counted_bars - 1;
while(i >= 0)
{
price = (High[i] + Low[i] + Close[i]) / 3;
RelBuffer[i] = price - MovBuffer[i];
i--;
}
//---- cci counting
i = Bars - CCIPeriod + 1;
if(counted_bars > CCIPeriod - 1)
i = Bars - counted_bars - 1;
while(i >= 0)
{
if(DevBuffer[i] == 0.0)
CCIBuffer[i] = 0.0;
else
CCIBuffer[i] = RelBuffer[i] / DevBuffer[i];
i--;
}
//---- cci smoothing
i = Bars - CCIPeriod + 1;
if(counted_bars > CCIPeriod - 1)
i = Bars - counted_bars - 1;
while(i >= 0)
{
CCIBufferSmoothed[i] = iMAOnArray(CCIBuffer, 0, SmoothingMA_Period, 0, SmoothingMode, i);
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments