CCI_Histogram
Download scriptThis script is a custom trading indicator designed for the MetaTrader platform. It provides a visual tool that analyzes price momentum using the Commodity Channel Index (CCI) and displays the results as a color-coded histogram on your chart.
Core Logic and Functionality
The primary purpose of this tool is to help traders identify market momentum and "neutral zones" by comparing different timeframes of the CCI indicator. Here is a breakdown of how it works:
1. Dual Momentum Analysis The script calculates two distinct versions of the CCI:
- Fast CCI: A shorter-term measurement that reacts quickly to price changes.
- Slow CCI: A longer-term measurement that reflects broader market trends.
2. Dynamic Color Coding Instead of a standard line, this script uses a histogram system where the colors change based on whether the market momentum is positive or negative:
- Blue Histogram: Appears when the momentum is positive (the price is moving above the established average).
- Red Histogram: Appears when the momentum is negative (the price is moving below the established average).
3. The "Neutral Territory" Logic The most unique feature of this script is its ability to identify a "neutral zone." When the indicator detects that the momentum has crossed from positive to negative (or vice versa), it doesn't immediately flip the color.
Instead, it enters a counting phase. It monitors how many bars the price stays near the zero line (the neutral point). If the price remains in this state for a specific amount of time—defined by the "neutral" setting—the script recognizes it as a period of indecision or consolidation. This prevents the colors from flickering rapidly during small, insignificant price movements.
4. Visual Output By processing these rules, the script gives you a much clearer picture of the market's direction:
- Clear Blue blocks indicate sustained upward momentum.
- Clear Red blocks indicate sustained downward momentum.
- The transition zones help highlight where the market is shifting from one trend to another or pausing to rest.
Summary for Traders
This tool simplifies complex momentum data into a visual map. It allows you to see at a glance whether the current trend is strong, weak, or currently in a "neutral" state of equilibrium, helping you make more informed decisions on when to enter or exit trades based on price direction.
//+------------------------------------------------------------------+
//| CCI_Histogram.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright "histogram colour update by thor@gmx.co.uk Moneytec chat: thorr; created by Luis Damiani; converted by Rosh"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Yellow
#property indicator_width1 2
#property indicator_color2 Maroon
#property indicator_width2 2
#property indicator_color3 DarkGray
#property indicator_width3 2
#property indicator_color4 Red
#property indicator_width4 2
#property indicator_color5 RoyalBlue
#property indicator_width5 2
//---- input parameters
extern double A_period=50;
extern int B_period=12;
extern int neutral = 6;
extern int num_bars=5000;
// parameters
int i=0;
bool initDone=true;
int bar=0;
int prevbars=0;
int startpar=0;
int cs=0;
int prevcs=0;
string commodt="nonono";
int frame=0;
int bars=0;
int count = 0, thisbar = 0;
double now, prev;
bool flag;
//---- buffers
double FastWoodieCCI[];
double SlowWoodieCCI[];
double HistoWoodieCCI[];
double HistoRed[];
double HistoBlue[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
SetIndexBuffer(0,FastWoodieCCI);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID);
SetIndexBuffer(1,SlowWoodieCCI);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexBuffer(2,HistoWoodieCCI);
SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexBuffer(3,HistoRed);
SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexBuffer(4,HistoBlue);
IndicatorShortName("CCI Histogram ("+A_period+")");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
cs = A_period + B_period + num_bars; //checksum used to see if parameters have been changed
if ((cs==prevcs) && (commodt == Symbol()) &&
(frame == (Time[4] - Time[5])) &&
((Bars - prevbars) < 2))
startpar = Bars - prevbars;
else
startpar = -1; //params haven't changed only need to calculate new bar
commodt = Symbol();
frame= Time[4] - Time[5];
prevbars = Bars;
prevcs = cs;
if (startpar == 1 | startpar == 0)
bar = startpar;
else
initDone = true;
if (initDone) {
//FastWoodieCCI[num_bars-1]=0;
SlowWoodieCCI[num_bars-1]=0;
HistoWoodieCCI[num_bars-1]=0;
HistoBlue[num_bars-1]=0;
HistoRed[num_bars-1]=0;
bar=num_bars-2;
initDone=false;
}
for (i = bar; i >= 0; i--) {
//FastWoodieCCI[i]=iCCI(NULL,0,B_period,PRICE_TYPICAL,i);
SlowWoodieCCI[i]=iCCI(NULL,0,A_period,PRICE_TYPICAL,i);
now = iCCI(NULL,0,A_period,PRICE_TYPICAL,i);
if ((prev >= 0 && now < 0) || (prev < 0 && now >= 0)) { // change of sign detected
flag = true;
count = 0;
}
if (flag) { // neutral territory
if (thisbar != i)
count++;
prev = now;
HistoWoodieCCI[i] = now;
if (count >= neutral)
flag = false;
thisbar = i;
continue;
}
// Colour it!
if (now > 0)
HistoBlue[i] = now;
if (now < 0)
HistoRed[i] = now;
prev = now;
}
return(0);
}
Comments