Good_Macd_v1
Download scriptThis script is a specialized technical analysis tool designed to help traders identify momentum and trend reversals in the financial markets. It functions by calculating and displaying three distinct visual elements on a separate sub-window below the main price chart.
Core Logic and Components
The script operates by processing historical price data to calculate the Moving Average Convergence Divergence (MACD). It breaks down the information into three specific components:
1. The MACD Line The foundation of this tool is the relationship between two different types of averages: a "Fast" moving average and a "Slow" moving average.
- The script looks at the closing price of assets over specific periods (defaulting to 12 and 26 units).
- By subtracting the Slow average from the Fast average, the tool determines the distance between short-term price momentum and long-term trends. This results in a primary line that helps visualize whether the current price trend is accelerating or decelerating.
2. The Signal Line To make the data more readable and less "noisy," the script calculates a second line called the Signal Line.
- This is essentially a smoothed version of the MACD Line itself.
- It uses a specific mathematical weighting (an "alpha" calculation) to track the average direction of the MACD over a secondary period.
- Traders typically look for the intersection of the MACD Line and this Signal Line to identify potential entry or exit points.
3. The Histogram The third visual element is a bar graph (histogram) located at the bottom of the indicator window.
- This represents the "gap" or difference between the MACD Line and the Signal Line.
- When the histogram bars are above zero, it suggests bullish momentum; when they dip below zero, it indicates bearish momentum.
- The height of these bars provides a quick visual cue regarding the strength of the current move.
How it Processes Data
The script is designed to work dynamically with live market data. Every time the price updates, the logic automatically recalculates these three values across all available historical bars on your chart. It ensures that the "Slow" components have enough prior data to provide an accurate reading before displaying the visual output, ensuring that the trend analysis is stable and reliable for the user.
/*-----------------------------+
| |
| 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 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_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