FXBay_-_MACD
Download scriptThis custom tool is designed to provide traders with a visual representation of market momentum by calculating and displaying three specific components based on the Moving Averages Convergence Divergence (MACD) methodology. Instead of relying on standard price action alone, it helps identify the strength, direction, and speed of price movements through three distinct layers of data.
Core Logic Overview
The tool functions by analyzing the relationship between two different moving averages—essentially comparing a "fast" trend against a "slow" trend to see if the market is accelerating or slowing down.
1. The MACD Line (Trend Identification) The script first identifies the gap between a fast-moving average and a slower-moving average. By subtracting the slow value from the fast value, it produces the primary MACD line.
- When this line is above zero, it suggests that short-term momentum is stronger than long-term momentum (bullish).
- When it drops below zero, it indicates that short-term momentum is weaker (bearish).
2. The Signal Line (Smoothing) To make the data easier to read and filter out "noise," the script takes the resulting MACD line and applies a secondary smoothing calculation over it. This creates the "Signal" line. Traders use this as a reference point; when the primary MACD line crosses over this signal line, it often indicates a potential change in market direction.
3. The Histogram (Momentum Gap) The final visual element is the histogram. This represents the mathematical distance between the main MACD line and the Signal line. It provides a clear, at-a-glance view of the "velocity" of the price:
- Tall bars indicate significant momentum or a widening gap between trends.
- Shrinking bars suggest that the current trend is losing steam and may be nearing a reversal.
Visual Presentation
The tool organizes these calculations into a dedicated sub-window below the main price chart. It uses a three-part visual system:
- A solid line for the primary MACD value.
- A second line for the Signal to track trend shifts.
- Vertical bars (the Histogram) to highlight the immediate strength of the market movement.
By combining these three elements, the script allows a visitor to see not just where the price is going, but how fast it is moving and whether that movement has enough energy to sustain itself.
//+---------------------------------------------------------------------------+
//| Custom MACD-Histogram.mq4 |
//| Copyright © 2010, FXBay - Vietnamese FX Trading Team |
//| http://www.fxbay.net |
//+---------------------------------------------------------------------------+
#property copyright "Copyright © 2010, FXBay - Vietnamese FX Trading Team"
#property link "http://www.fxbay.net/"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 DarkTurquoise
#property indicator_color2 Red
#property indicator_color3 Gray
#property indicator_width1 1
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalEMA=9;
//---- indicator buffers
double MACD_Buffer[];
double SIGNAL_Buffer[];
double HISTOGRAM_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexDrawBegin(1,SignalEMA);
IndicatorDigits(Digits+1);
//---- indicator buffers mapping
SetIndexBuffer(0,MACD_Buffer);
SetIndexBuffer(1,SIGNAL_Buffer);
SetIndexBuffer(2,HISTOGRAM_Buffer);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("FXBay - MACD ("+FastEMA+","+SlowEMA+","+SignalEMA+")");
SetIndexLabel(0,"MACD");
SetIndexLabel(1,"Signal");
SetIndexLabel(2,"Histogram");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start() {
int i;
int limit;
int counted_bars = IndicatorCounted();
if(counted_bars < 0) return(-1); //---- check for possible errors
if(counted_bars > 0) counted_bars--; //---- the last counted bar will be recounted
limit = Bars - counted_bars;
//---- Draw MACD line
for(i=0; i<limit; i++) { //-- loop from the current bar to the first bar
MACD_Buffer[i] = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
}
//---- Draw Signal line and Histogram
for(i=0; i<limit; i++) { //-- same loop above
SIGNAL_Buffer[i] = iMAOnArray(MACD_Buffer,Bars,SignalEMA,0,MODE_EMA,i);
HISTOGRAM_Buffer[i] = MACD_Buffer[i] - SIGNAL_Buffer[i];
}
//---- Done
return(0);
}
//+-----------------------------------------------------------------+
Comments