MACD
Download scriptThis custom indicator is designed to help traders visualize market momentum and identify potential trend reversals by calculating the relationship between two different moving averages. Instead of looking at just one price point, it analyzes the "gap" between fast-moving price actions and slower-moving trends to determine the underlying strength of the market.
Core Logic and Components
The indicator functions through a three-step mathematical process:
1. The Fast and Slow Trends The script first calculates two different types of moving averages based on the closing price of the assets:
- Fast EMA: A quicker average that reacts quickly to recent price fluctuations.
- Slow EMA: A broader average that follows the overall long-term trend more smoothly.
2. The MACD Histogram (The Difference) The primary visual output—the histogram—is created by subtracting the Slow EMA from the Fast EMA.
- When the fast trend is significantly higher than the slow trend, it indicates upward momentum (bullish sentiment).
- When the fast trend falls below the slow trend, it suggests downward momentum (bearish sentiment).
- The distance between these two averages determines the height of the histogram bars; larger bars indicate stronger momentum.
3. The Signal Line (The Smoothing Filter) To prevent traders from reacting to "noise" or minor fluctuations, the script performs a final calculation. It takes all the data points from the MACD Histogram and calculates a Simple Moving Average (SMA) of those values. This creates a smooth line that flows over the histogram.
How it is Visualized
The indicator occupies a separate window on your trading chart, allowing you to observe momentum independently of the main price action:
- Histogram: Shows the raw difference between the two trends. It provides a real-time view of how "fast" the price is moving relative to its recent history.
- Signal Line: A smooth line that follows the histogram. Traders often look for the point where the histogram crosses this line as a potential signal that momentum is shifting from one direction to another.
Summary for the User
Essentially, this tool tells you how much "pull" there is in the current market direction. By comparing short-term reactions against long-term trends, it filters out minor price movements to show you whether the market is accelerating, decelerating, or potentially changing direction.
//+------------------------------------------------------------------+
//| Custom MACD.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Silver
#property indicator_color2 Red
#property indicator_width1 2
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double MacdBuffer[];
double SignalBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_LINE);
SetIndexDrawBegin(1,SignalSMA);
IndicatorDigits(Digits+1);
//---- indicator buffers mapping
SetIndexBuffer(0,MacdBuffer);
SetIndexBuffer(1,SignalBuffer);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
SetIndexLabel(0,"MACD");
SetIndexLabel(1,"Signal");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- macd counted in the 1-st buffer
for(int i=0; i<limit; i++)
MacdBuffer[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 buffer
for(i=0; i<limit; i++)
SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments