iMACD
Download scriptThis tool is designed to provide a visual representation of market momentum by analyzing price changes over specific timeframes. It calculates three distinct components to help traders identify the strength, direction, and potential reversal points of a financial asset's price movement.
Core Components
The logic operates by comparing two different moving averages—one that reacts quickly to recent price changes (the "Fast" average) and one that tracks the broader trend more slowly (the "Slow" average). By calculating the difference between these two, the script creates a primary data line that reveals whether the asset's momentum is accelerating or decelerating.
Visual Data Elements
The script translates this mathematical logic into three distinct visual elements on your chart:
- The MACD Line: This represents the raw difference between the fast and slow averages. When this line moves above zero, it suggests bullish momentum; when it drops below zero, it indicates bearish momentum.
- The Signal Line: This is a smoothed version of the MACD Line. It acts as a secondary reference point, filtering out minor "noise" in the market to show the underlying trend more clearly.
- The Histogram: This is the visual difference between the MACD Line and the Signal Line. It provides an immediate, at-a-glance view of momentum strength. When the bars grow taller or deeper, it signals increasing pressure in a specific direction; when they shrink toward the zero line, it may indicate that the current trend is losing steam.
How the Logic Functions
The script processes historical price data to determine these values dynamically. For every new price point, it:
- Calculates the gap between the fast and slow trends.
- Smooths that gap to create the Signal Line using a weighted calculation.
- Subtracts the Signal Line from the MACD Line to generate the Histogram bars.
By observing where these three elements intersect or diverge, users can better understand the relationship between short-term price fluctuations and long-term trends, helping to identify potential entry points or trend shifts in real-time.
//+------------------------------------------------------------------+
//| 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 PaleGreen
#property indicator_color2 Blue
#property indicator_color3 Red
//---- 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(1,DRAW_LINE);
SetIndexBuffer(1,MACDLineBuffer);
SetIndexDrawBegin(1,SlowMAPeriod);
SetIndexStyle(2,DRAW_LINE,STYLE_DOT);
SetIndexBuffer(2,SignalLineBuffer);
SetIndexDrawBegin(2,SlowMAPeriod+SignalMAPeriod);
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,HistogramBuffer);
SetIndexDrawBegin(0,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