Accelerator_LSMA1

Download script
Accelerator_LSMA1

The Accelerator_LSMA is a technical analysis tool designed to help traders visualize the momentum and speed of price movements. Unlike standard moving averages that simply smooth out price data, this script focuses on the "acceleration" of the market—essentially measuring how fast the trend is speeding up or slowing down.

How the Logic Works

The indicator operates through a three-step calculation process:

1. The Foundation: Least Squares Moving Average (LSMA) At its core, the tool uses a specific type of moving average called the "Least Squares." While a standard moving average treats all recent price points equally, the LSMA uses linear regression to find the "line of best fit" for a specific period. This allows the indicator to react more accurately to price trends by identifying the underlying trajectory of the market rather than just trailing behind it.

2. The Momentum Calculation The script compares two different LSMA settings: a short-term average (set to 5 periods) and a long-term average (set to 34 periods). By subtracting the long-term value from the short-term value, the tool calculates the distance between the current price trend and its historical baseline. This difference represents the market's "velocity."

3. The Signal and Visualization To make this data easy to read, the script performs a final layer of processing:

  • Smoothing: It applies a secondary moving average to the momentum data to create a "signal line," which helps filter out minor market noise.
  • Dynamic Coloring: The tool calculates the difference between the momentum and its signal line. If the momentum is increasing, it highlights the movement in one color (Green); if the momentum is decreasing, it switches to another (Red).
  • Histogram Display: These results are plotted as a histogram. This allows traders to see at a glance not just whether the price is moving, but whether that move is gaining strength (accelerating) or losing steam (decelerating).

Summary for the User

Essentially, this tool acts as a speedometer for the market. It tells you:

  • Direction: Is the price currently trending up or down?
  • Strength: How powerful is that trend right now?
  • Change: Is the trend gaining momentum (accelerating) or beginning to tire out (decelerating)?
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
Accelerator_LSMA1
//+------------------------------------------------------------------+
//|                                             Accelerator_LSMA.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2005, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  Yellow
#property  indicator_color2  Green
#property  indicator_color3  Red

#property indicator_level1 0.0
//---- indicator buffers
double     ExtBuffer0[];
double     ExtBuffer1[];
double     ExtBuffer2[];
double     ExtBuffer3[];
double     ExtBuffer4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,2);
   IndicatorDigits(Digits+2);
   SetIndexDrawBegin(0,38);
   SetIndexDrawBegin(1,38);
   SetIndexDrawBegin(2,38);
//---- 4 indicator buffers mapping
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,ExtBuffer3);
   SetIndexBuffer(4,ExtBuffer4);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("AC");
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
//---- 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 = sum*6/(length*(length+1));
    
    return(wt);
}


//+------------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator                               |
//+------------------------------------------------------------------+
int start()
  {
   int    limit;
   int    counted_bars=IndicatorCounted();
   double prev,current;
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   //---- macd counted in the 1-st additional buffer
   for(int i=0; i<limit; i++)
      ExtBuffer3[i]=LSMA(5,i)-LSMA(34,i);
   //---- signal line counted in the 2-nd additional buffer
   for(i=0; i<limit; i++)
      ExtBuffer4[i]=iMAOnArray(ExtBuffer3,Bars,5,0,MODE_SMA,i);
   //---- dispatch values between 2 buffers
   bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current=ExtBuffer3[i]-ExtBuffer4[i];
      prev=ExtBuffer3[i+1]-ExtBuffer4[i+1];
      if(current>prev) up=true;
      if(current<prev) up=false;
      if(!up)
        {
         ExtBuffer2[i]=current*100;
         ExtBuffer1[i]=0.0;
        }
      else
        {
         ExtBuffer1[i]=current*100;
         ExtBuffer2[i]=0.0;
        }
       ExtBuffer0[i]=current*100;
     }
   //---- done
   return(0);
  }

Comments