MACD_4_v2
Download scriptThis tool is a technical analysis indicator designed to help traders identify trends and momentum in the financial markets. It simplifies complex price movements into three visual components: a primary trend line, a secondary signal line, and a visual histogram.
Core Logic Overview
The script functions by comparing two different types of moving averages based on the closing prices of assets. By calculating the difference between a "fast" moving average (which reacts quickly to recent price changes) and a "slow" moving average (which tracks the broader trend), the tool reveals whether the current market momentum is accelerating or decelerating.
Key Components of the Analysis
The indicator processes data through three distinct layers to provide a comprehensive view of market sentiment:
- The MACD Line: This represents the direct difference between the fast and slow moving averages. When this line moves upward, it suggests that short-term price momentum is strengthening relative to the long-term trend. Conversely, a downward move indicates weakening short-term strength.
- The Signal Line: This is a smoothed version of the MACD line. By applying a mathematical smoothing filter (calculated based on a specific period), the script filters out "market noise" to create a clearer path. Traders often look for instances where the MACD line crosses this signal line as a potential confirmation of a trend change.
- The Histogram: This is the visual representation of the gap between the MACD Line and the Signal Line. It acts as a momentum gauge; when the histogram bars grow taller, it signifies increasing momentum in the current direction. When the bars begin to shrink toward the zero mark, it suggests that the current move may be losing steam.
How it Works for the User
The script automatically scans historical price data and calculates these values in real-time. By looking at the relationship between these three elements—specifically where the lines cross or where the histogram changes direction—users can better visualize whether a market is entering a bullish (upward) phase, a bearish (downward) phase, or a period of consolidation.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| 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 DeepSkyBlue
//---- 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_DOT);
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