MACD_LSMA
Download scriptThis tool is a specialized technical analysis indicator designed to help traders identify momentum and trend changes in financial markets. It enhances the classic Moving Average Convergence Divergence (MACD) by replacing standard smoothing techniques with a more advanced mathematical approach known as Least Squares Moving Average (LSMA).
Core Logic and Functionality
The script functions by calculating the relationship between two different price speeds to determine whether a market's momentum is accelerating or decelerating. Here is the breakdown of how it works:
1. The Foundation: LSMA vs. Standard Averages Unlike standard moving averages that simply average past prices, this script uses the "Least Squares" method. In non-technical terms, this means it acts more like a linear regression; it attempts to find the "best-fit" line for a specific period of price data. This makes the indicator more responsive to recent price changes while still filtering out minor market noise.
2. The MACD Calculation The script tracks two distinct versions of this LSMA:
- A Fast version (defaulting to 12 periods).
- A Slow version (defaulting to 26 periods).
By subtracting the Slow value from the Fast value, the tool generates the primary MACD line. This difference tells you how far apart the short-term price momentum is from the long-term trend. If the gap is widening, momentum is increasing; if it is shrinking, momentum is cooling off.
3. The Signal Line To make the data easier to read, the script takes the resulting MACD values and applies a Simple Moving Average (SMA) over them (defaulting to 9 periods). This creates a "Signal" line, which acts as a smoothed baseline. Traders often look for the moment the main MACD line crosses this Signal line as a potential entry or exit point.
4. The Visual Histogram Finally, the script visualizes the difference between the MACD line and the Signal line as a vertical histogram at the bottom of your chart:
- When the value is positive (above the zero line), it appears as an upward-pointing bar, indicating bullish momentum.
- When the value is negative (below the zero line), it appears as a downward-pointing bar, indicating bearish momentum.
Summary for the User
In short, this script provides a more "intelligent" version of a standard MACD. By using Least Squares regression instead of standard smoothing, it aims to provide a clearer picture of market direction and momentum, helping you visualize whether the current price trend is gaining strength or starting to exhaust.
//+------------------------------------------------------------------+
//| MACD_LSMA_EMA.mq4 |
//| Copyright © 2007, Robert Hill |
//| Standard MACD modified to use LSMA instead of EMA |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Aqua
#property indicator_color2 Red
#property indicator_color3 Green
#property indicator_color4 Red
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double MACD_buffer[];
double Signal_buffer[];
double HistogramBufferUp[];
double HistogramBufferDown[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
SetIndexBuffer(0,MACD_buffer);
SetIndexDrawBegin(0,SlowEMA);
SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
SetIndexBuffer(1,Signal_buffer);
SetIndexDrawBegin(1,SignalSMA);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexBuffer(2,HistogramBufferUp);
SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexBuffer(3,HistogramBufferDown);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("MACD_LSMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
SetIndexLabel(0,"MACD");
SetIndexLabel(1,"Signal");
SetIndexLabel(2,"Histogram");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------------+
//| LSMA - Least Squares Moving Average function calculation |
//| LSMA_In_Color Indicator plots the end of the linear regression line |
//+------------------------------------------------------------------------+
double LSMA(int Rperiod, int shift)
{
int i;
double sum;
int length;
double lengthvar;
double tmp;
double wt;
length = Rperiod;
sum = 0;
for(i = length; i >= 1 ; i--)
{
lengthvar = length + 1;
lengthvar /= 3;
tmp = 0;
tmp = ( i - lengthvar)*Close[length-i+shift];
sum+=tmp;
}
wt = MathFloor(sum*6/(length*(length+1))/Point)*Point;
return(wt);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start()
{
double temp;
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;
//---- macd counted in the 1-st buffer
for(int i=0; i<limit; i++)
MACD_buffer[i]=LSMA(FastEMA,i)-LSMA(SlowEMA,i);
//---- signal line counted in the 2-nd buffer
for(i=0; i<limit; i++)
Signal_buffer[i]=iMAOnArray(MACD_buffer,Bars,SignalSMA,0,MODE_SMA,i);
for(i=0; i<limit; i++)
{
HistogramBufferUp[i] = 0;
HistogramBufferDown[i] = 0;
temp = MACD_buffer[i] - Signal_buffer[i];
if (temp >= 0)
HistogramBufferUp[i] = temp;
else
HistogramBufferDown[i] = temp;
}
//---- done
return(0);
}
Comments