MACDdtLSMA_v1

Download script
Author: Copyright � 2005, David W. Thomas
MACDdtLSMA_v1

This technical tool is designed to provide traders with a sophisticated visual representation of market momentum by utilizing a specific mathematical approach called the Least Squares Moving Average (LSMA). Unlike standard moving averages that might lag significantly behind current price action, this script focuses on providing a more responsive look at trend direction and strength.

Core Logic and Methodology

The indicator operates by calculating three distinct visual components based on different timeframes of price data:

1. The Moving Average Foundation (LSMA) At its core, the script uses "Least Squares" logic. Instead of simply averaging past prices, it calculates a linear regression—essentially finding the "line of best fit" for a specific period of historical data. This allows the tool to identify the underlying trend more accurately than traditional averages.

2. The MACD Line (The Momentum Tracker) The script establishes two different LSMA periods (typically a shorter and a longer timeframe). By subtracting the value of the slower average from the faster average, the tool generates the MACD Line. This line tells you the distance between short-term price movement and long-term trends; when this gap widens, it indicates increasing momentum.

3. The Signal Line (The Smoothing Effect) To make the data easier to read, the script takes the MACD Line and applies a smoothing calculation. It uses a weighted formula (derived from the Signal Period) to create the Signal Line. This line acts as a secondary reference point, helping traders identify when the momentum is beginning to shift or slow down.

4. The Histogram (The Visual Gap) Finally, the script calculates the difference between the MACD Line and the Signal Line. This result is displayed as a Histogram.

  • When the histogram bars are tall, there is a significant divergence between current momentum and the signal.
  • When the bars shrink or cross the zero midline, it suggests that the trend may be losing steam or reaching a point of reversal.

Summary of User Experience

For a trader, this means the tool provides a three-layered view of the market:

  • The MACD Line shows you the current "velocity" of the price.
  • The Signal Line tells you what that velocity has been doing over a slightly longer period.
  • The Histogram provides an immediate visual cue of whether the trend is accelerating, decelerating, or potentially turning around.
Miscellaneous
Implements a curve of type %1
16 Views
0 Downloads
0 Favorites
MACDdtLSMA_v1
/*-----------------------------+
|			       |
| 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_LSMAdt("+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);
}

//+------------------------------------------------------------------------+
//| 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 = sum*6/(length*(length+1));
    
    return(wt);
}
//+------------------------------------------------------------------+
//| 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] = LSMA(FastMAPeriod,i) - LSMA(SlowMAPeriod,i);
      SignalLineBuffer[i] = alpha*MACDLineBuffer[i] + alpha_1*SignalLineBuffer[i+1];
      HistogramBuffer[i] = MACDLineBuffer[i] - SignalLineBuffer[i];
   }
   
   //----
   return(0);
}
//+------------------------------------------------------------------+

Comments