CCI_v2
Download scriptThis script functions as a technical analysis tool designed to measure the price behavior of a financial asset relative to its recent historical average. Its primary objective is to identify whether the current market price is overextended—meaning it has moved significantly higher or lower than what is considered "normal" for the given timeframe.
The logic operates through a four-step mathematical process:
1. Establishing the Baseline (Moving Average) The tool first calculates a "Typical Price" for every period, which is derived by averaging the high, low, and closing prices of the candles. It then creates a moving average of these typical prices over a specific number of bars (the user-defined period). This serves as the anchor point or the "center" of the market's current movement.
2. Measuring Price Variance (Standard Deviation) To understand how much the price normally fluctuates, the script calculates the difference between each individual candle's typical price and the moving average established in the first step. By aggregating these differences over the specified period, the tool determines the "volatility" or the standard deviation of the asset. This helps the indicator distinguish between normal market noise and significant price swings.
3. Determining Relative Position The script then calculates the specific distance between the current market price and its moving average. Essentially, it asks: "How far away is the current price from the recent average?"
4. Calculating the Final Index (CCI) In the final step, the tool combines these elements to produce the Commodity Channel Index (CCI). It takes the relative position (distance from the average) and divides it by the standard deviation (volatility).
The Resulting Insight By dividing the distance by the volatility, the tool provides a standardized score. This allows traders to see how "extreme" the current price movement is relative to its own history. If the resulting value is very high, it suggests the price may be overbought; if it is very low, it may suggest the asset is oversold. Because it scales based on volatility, the tool remains effective even when market conditions change from calm to turbulent.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| 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);
//---- name for DataWindow and indicator subwindow label
short_name="CCI("+CCIPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,CCIPeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Commodity Channel Index |
//+------------------------------------------------------------------+
int start()
{
int i,k,counted_bars=IndicatorCounted();
double price,sum,mul;
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