This tool is designed as a technical analysis indicator for trading platforms, specifically used to identify trends and momentum in financial markets. It works by calculating the relationship between two different moving averages of a security's price—one that reacts quickly to new information and one that reacts more slowly.
Core Logic and Visual Components
The script generates three distinct visual elements to help traders interpret market movement:
1. The MACD Line The foundation of this tool is the difference between two Exponential Moving Averages (EMAs). By subtracting a slower-moving average from a faster-moving one, the script identifies the "velocity" of the price. When the fast average is significantly higher than the slow average, it suggests upward momentum; conversely, if it falls below, it suggests downward momentum.
2. The Signal Line To smooth out the fluctuations of the MACD Line, the script calculates a "Signal Line." This is essentially a moving average of the MACD Line itself. By looking at the distance between the MACD Line and this Signal Line, traders can identify potential turning points: when the MACD Line crosses above the Signal Line, it is often interpreted as a bullish signal, while crossing below is seen as bearish.
3. The Histogram The final visual element is a bar graph (the Histogram) situated at the bottom of the indicator window. This represents the mathematical difference between the MACD Line and the Signal Line. The height and direction of these bars provide an immediate visual cue regarding the strength of the current trend—larger bars indicate accelerating momentum, while shrinking bars suggest that the trend may be losing steam.
Key Parameters
The tool allows for customization through three primary settings:
- Fast MA Period: Determines how quickly the first average reacts to price changes.
- Slow MA Period: Determines the timeframe for the secondary, smoother average.
- Signal MA Period: Sets the sensitivity of the smoothing calculation for the Signal Line.
By processing these calculations in real-time, the script provides a multi-layered view of market behavior, helping to filter out "noise" and highlight the underlying direction of price action.
//+------------------------------------------------------------------+
//| MACD.mq4 |
//| Copyright © 2005, David W. Thomas |
//| mailto:davidwt@usa.net |
//+------------------------------------------------------------------+
// This is the correct computation and display of MACD.
#property copyright "Copyright © 2005, David W. Thomas"
#property link "mailto:davidwt@usa.net"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Red
#property indicator_color3 Blue
//---- input parameters
extern int FastMAPeriod=12;
extern int SlowMAPeriod=26;
extern int SignalMAPeriod=9;
//---- buffers
double MACDLineBuffer[];
double SignalLineBuffer[];
double HistogramBuffer[];
//---- variables
double alpha = 0;
double alpha_1 = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
//---- indicators
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(2,MACDLineBuffer);
SetIndexDrawBegin(0,SlowMAPeriod);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(1,SignalLineBuffer);
SetIndexDrawBegin(1,SlowMAPeriod+SignalMAPeriod);
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexBuffer(0,HistogramBuffer);
SetIndexDrawBegin(2,SlowMAPeriod+SignalMAPeriod);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MACD("+FastMAPeriod+","+SlowMAPeriod+","+SignalMAPeriod+")");
SetIndexLabel(0,"MACD");
SetIndexLabel(1,"Signal");
//----
alpha = 2.0 / (SignalMAPeriod + 1.0);
alpha_1 = 1.0 - alpha;
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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;
for(int i=limit; i>=0; i--)
{
MACDLineBuffer[i] = iMA(NULL,0,FastMAPeriod,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
SignalLineBuffer[i] = alpha*MACDLineBuffer[i] + alpha_1*SignalLineBuffer[i+1];
HistogramBuffer[i] = (MACDLineBuffer[i] - SignalLineBuffer[i]) * 2;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments