MACD_2_Color_HISTORAM
Download scriptThis custom indicator is designed to enhance the visual representation of the MACD (Moving Average Convergence Divergence) tool, a popular momentum oscillator used by traders to identify trends and potential reversal points.
While the standard MACD provides a single histogram to show the relationship between two moving averages, this specific script introduces a dynamic coloring system to make market sentiment easier to read at a glance.
Core Functionality
The logic operates through three primary stages:
1. Calculating the Base Values First, the script calculates the core components of the MACD. It looks at the price action over a "Fast" period and a "Slow" period to create two separate averages. By subtracting the slow average from the fast average, it determines the immediate momentum of the market.
2. Creating the Signal Line The script then takes those calculated results and applies a third moving average to them. This creates a "Signal Line," which helps smooth out the fluctuations in the data, allowing traders to see the broader direction of the momentum rather than just short-term noise.
3. Visual Differentiation (Color Coding) The most distinct feature of this script is how it handles the final histogram display. Instead of showing one uniform color for all values, the logic separates the results into two categories:
- Positive Momentum: When the difference between the main MACD value and the Signal Line is above zero, the indicator highlights these bars in one specific color (Blue).
- Negative Momentum: When that difference falls below zero, the indicator switches the bars to a different color (Red).
Summary for Traders
By using this logic, the chart provides an immediate visual cue regarding market strength. Because the histogram changes color based on whether the momentum is currently positive or negative relative to its signal line, it allows you to quickly identify trend shifts and stay aligned with the market's current direction without having to manually interpret complex overlapping lines.
//+------------------------------------------------------------------+
//| MACD_2_Color_HISTORAM.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property link "2 colour Histogram added by cja"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 2
#property indicator_width4 2
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double ind_Buffer1[];
double ind_Buffer2[];
double ind_buffer3a[];
double ind_buffer3b[];
double ind_buffer4[];
double ind_buffer5[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(6);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexDrawBegin(0,SignalSMA);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexDrawBegin(1,SignalSMA);
SetIndexStyle(4,DRAW_LINE);
SetIndexBuffer(4,ind_Buffer1);
SetIndexStyle(5,DRAW_LINE);
SetIndexBuffer(5,ind_Buffer2);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 5 indicator buffers mapping
if(!SetIndexBuffer(0,ind_buffer3a) &&
!SetIndexBuffer(1,ind_buffer3b) &&
!SetIndexBuffer(2,ind_buffer4) &&
!SetIndexBuffer(3,ind_buffer5) &&
!SetIndexBuffer(4,ind_Buffer1) &&
!SetIndexBuffer(5,ind_Buffer2))
Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MACD ("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Average of Oscillator |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- macd counted in the 1-st additional buffer
for(int i=0; i<limit; i++)
ind_buffer4[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)
-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd additional buffer
for(i=0; i<limit; i++)
ind_buffer5[i]=iMAOnArray(ind_buffer4,Bars,SignalSMA,0,MODE_EMA,i);
//---- main loop
double value=0;
for(i=0; i<limit; i++)
{
ind_buffer3a[i]=0.0;
ind_buffer3b[i]=0.0;
value=ind_buffer4[i]-ind_buffer5[i];
if (value>0) ind_buffer3a[i]=value;
if (value<0) ind_buffer3b[i]=value;
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments