CCI
Download scriptThis script is designed to provide traders with a visual tool called the Commodity Channel Index (CCI). Its primary purpose is to help identify whether an asset's price is currently trading at an extreme level relative to its recent average, which can signal potential trend reversals or overbought/oversold conditions.
How the Logic Works
The script processes historical market data to create a smoothed line on your chart. It achieves this through a multi-step mathematical calculation:
1. Establishing a Baseline (Moving Average) First, the script looks at a specific timeframe of price action—determined by the "CCIPeriod" setting (defaulting to 14 bars). It calculates the "Typical Price," which is the average of the high, low, and closing prices for those periods. By averaging these together, it creates a stable baseline that represents the "normal" price path.
2. Measuring Volatility (Standard Deviation) The script then measures how much the price fluctuates away from that baseline. It calculates the difference between the typical price and the moving average across the entire period. By identifying the average intensity of these fluctuations, the script determines the "normal" level of volatility for the asset at any given moment.
3. Calculating Relative Position Once it knows the average price and the average volatility, it compares the current price to the baseline. This reveals how far the price has strayed from its average—whether it is pushing significantly higher or dipping significantly lower than usual.
4. Final Calculation (The CCI Value) Finally, the script combines these elements into a single ratio: it takes the difference between the current price and the moving average and divides it by the volatility measurement.
Practical Application
By performing these calculations in real-time, the script produces a line that oscillates. When the line moves significantly away from the zero center point, it suggests that the price is deviating from its recent historical norm. This allows traders to visually monitor market momentum and identify areas where the price might be overextended or entering a new trend.
//+------------------------------------------------------------------+
//| 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 1
#property indicator_color1 LightSeaGreen
//---- input parameters
extern int CCIPeriod = 14;
//---- buffers
double CCIBuffer[];
double RelBuffer[];
double DevBuffer[];
double MovBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- 3 additional buffers are used for counting.
IndicatorBuffers(4);
SetIndexBuffer(1, RelBuffer);
SetIndexBuffer(2, DevBuffer);
SetIndexBuffer(3, MovBuffer);
//---- indicator lines
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, CCIBuffer);
//----
if(CCIPeriod <= 0)
CCIPeriod = 14;
//----
SetIndexDrawBegin(0, CCIPeriod);
//---- name for DataWindow and indicator subwindow label
short_name="CCI(" + CCIPeriod + ")";
IndicatorShortName(short_name);
SetIndexLabel(0, short_name);
//----
return(0);
}
//+------------------------------------------------------------------+
//| 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++)
MovBuffer[i] = iMA(NULL, 0, CCIPeriod, 0, MODE_SMA, PRICE_TYPICAL, 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--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments