This script is designed to function as a technical analysis tool, specifically creating a visual representation of market momentum on a trading platform. It calculates three distinct components based on price history to help traders identify trends and potential shifts in market direction.
Core Logic Overview
The tool operates by comparing two different types of moving averages. A "Fast" moving average reacts quickly to recent price changes, while a "Slow" moving average provides a broader view of the long-term trend. By subtracting the Slow average from the Fast average, the script determines the current strength and direction of the market momentum.
Visual Components
The script generates three specific visual elements on your chart:
- The MACD Line: This represents the raw difference between the fast and slow moving averages. When this line is above zero, it suggests that short-term price action is stronger than long-term trends; when below zero, it suggests the opposite.
- The Signal Line: This is a smoothed version of the MACD Line. It acts as a "trend of a trend," filtering out minor price fluctuations to provide a clearer picture of whether the momentum is accelerating or slowing down.
- The Histogram: This is a visual representation of the distance between the MACD Line and the Signal Line. It provides a quick reference for the intensity of the current movement—the taller the bars, the greater the divergence between short-term and smoothed momentum.
Technical Behavior
To ensure accuracy, the script calculates these values starting from the oldest data points in the history and works forward to the present moment. It uses a specific mathematical weighting system (an exponential moving average logic) to ensure that more recent price data has a greater influence on the current reading than older data. This allows the indicator to remain responsive to new market information while maintaining a stable view of established trends.
//+------------------------------------------------------------------+
//| 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 Blue
#property indicator_color2 Red
#property indicator_color3 Green
//---- 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(0,DRAW_LINE);
SetIndexBuffer(0,MACDLineBuffer);
SetIndexDrawBegin(0,SlowMAPeriod);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID);
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