MACD_alpha
Download scriptThis tool is designed to provide a more precise way of visualizing market momentum by calculating and displaying a modified version of the Moving Average Convergence Divergence (MACD) indicator. It helps traders identify the strength, direction, and changes in price momentum by analyzing the relationship between two different moving averages.
Core Logic and Components
The script functions by processing three distinct visual elements on your chart:
1. The MACD Line (Fast vs. Slow Momentum) The primary calculation begins by comparing two different "speeds" of price movement. It takes a fast-moving average (which reacts quickly to recent price changes) and subtracts a slower-moving average (which represents the longer-term trend). The difference between these two values tells you how much the current price is pulling away from or returning to its long-term average. This is displayed as a primary line on your screen.
2. The Signal Line (Smoothed Momentum) To filter out "noise" and make the data easier to read, the script calculates a Signal Line. Instead of just showing raw momentum, it uses a specific mathematical weighting factor (Alpha) to blend the current MACD value with the previous signal's value. This creates a smoother trend line that helps identify where the momentum is actually heading without reacting too wildly to every minor price fluctuation.
3. The Histogram (The Gap Analysis) Finally, the script produces a histogram. This represents the mathematical distance between the MACD Line and the Signal Line. When the histogram bars are tall, it indicates a significant divergence between the immediate momentum and the smoothed trend; when they shrink, it suggests that the momentum is stabilizing or reaching a turning point.
How it Works for the User
The script automatically processes all historical price data to populate these three visual elements. By looking at the interaction between the two lines and the histogram:
- Trend Direction: You can see if the fast momentum is leading the trend or lagging behind.
- Strength: The height of the histogram provides a visual cue on how strong the current move is.
- Timing: Because it uses specific smoothing periods (Fast, Slow, and Signal periods), it allows you to customize how sensitive the indicator is to price changes based on your specific trading style.
//+------------------------------------------------------------------+
//| MACD_alpha.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 FireBrick
#property indicator_color3 SlateGray
#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_alpha("+FastMAPeriod+","+SlowMAPeriod+","+SignalMAPeriod+")");
SetIndexLabel(0,"MACD_alpha");
SetIndexLabel(1,"MACD_alphaSignal");
//----
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