MACD_correct2
Download scriptThis tool is designed to provide a visual representation of market momentum by analyzing the relationship between short-term and long-term price movements. It helps traders identify trends and potential shifts in market direction by calculating three distinct data points displayed in a separate window on your trading chart.
How it Works
The logic operates by comparing two different types of "moving averages"—which are essentially smoothed versions of price data that filter out minor fluctuations to reveal the broader trend.
-
The MACD Line (Trend Strength): The tool first calculates a fast-moving average and a slow-moving average based on the closing prices of the assets. By subtracting the slow average from the fast average, it produces the MACD Line.
- When this line is above zero, it generally suggests upward momentum.
- When it is below zero, it suggests downward momentum.
-
The Signal Line (Trend Smoothing): To make the data easier to read, the tool takes the MACD Line and applies a secondary smoothing process (a weighted average). This creates the Signal Line, which moves more slowly than the standard MACD line. Traders often look for the "crossover"—when the MACD line crosses above or below this Signal Line—as a potential indicator of a change in market direction.
-
The Histogram (Momentum Gap): Finally, the tool calculates the difference between the MACD Line and the Signal Line. This is displayed as a Histogram.
- The height of the bars shows the distance between the current momentum and the smoothed signal.
- Increasing bar heights suggest accelerating momentum, while shrinking bars may indicate that a trend is losing steam.
Summary for Traders
By using these three components together, the script provides a comprehensive view of market health. It allows you to see not just where the price is going, but how fast it is moving and whether the current strength of the trend is beginning to weaken or strengthen.
//+------------------------------------------------------------------+
//| 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 Gold
#property indicator_color2 Black
#property indicator_color3 DarkGreen
#property indicator_level1 0
//---- input parameters
extern int FastMAPeriod=6;
extern int SlowMAPeriod=13;
extern int SignalMAPeriod=5;
//---- 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(0,DRAW_LINE);
SetIndexBuffer(0,MACDLineBuffer);
SetIndexDrawBegin(0,SlowMAPeriod);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,SignalLineBuffer);
SetIndexDrawBegin(1,SlowMAPeriod+SignalMAPeriod);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexBuffer(2,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];
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments