Linear_Regression_Slope_v1

Download script
Author: Copyright 2018, MetaQuotes Software Corp.

This script is a technical analysis tool designed for the MetaTrader 5 platform. Its primary purpose is to measure the "momentum" or the rate of change in price movement by calculating the Linear Regression Slope.

Here is a breakdown of how the logic works in simple terms:

Core Concept: The Trend Line

Imagine you are looking at a series of price points on a chart. If those prices were moving in a perfectly straight line, that line would represent the "trend." This script mathematically calculates the best-fit straight line through a specific number of recent price candles (the "Period").

How it Works

  1. Data Collection: The user defines a Period (for example, 14 candles). The script looks at the closing prices of these candles to understand how the market has behaved over that specific timeframe.
  2. Mathematical Mapping: Instead of just looking at the current price, the script performs a calculation known as "Least Squares Regression." It looks at every candle within your chosen period and determines how much the price deviates from a theoretical straight line.
  3. Calculating the Slope: The final result shown on your chart is the Slope.
    • If the slope is positive, it indicates that the general trend of the price is moving upward over that period.
    • If the slope is negative, it indicates that the general trend is moving downward.
    • The higher the numerical value, the steeper the incline; the lower (more negative) the value, the steeper the decline.

Visual Output

The script operates in a separate window below your main price chart. It plots a single line that fluctuates based on these slope calculations.

Key Benefits for the User

  • Smoothing the Noise: Because it looks at a "Period" of candles rather than just the last single tick, it helps filter out minor market "noise" to show you the underlying direction of the trend.
  • Trend Intensity: It doesn't just tell you if the price is up or down; by looking at the steepness of the slope, it helps you see how aggressively the price is moving in that direction.
  • Dynamic Updating: The calculation happens automatically with every new price update, ensuring the "slope" always reflects the most current trend based on your chosen settings.
Price Data Components
9 Views
0 Downloads
0 Favorites
Linear_Regression_Slope_v1
ÿþ//+------------------------------------------------------------------+

//|                                      Linear_Regression_Slope.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property description "Linear regression slope oscillator"

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_plots   1

//--- plot LRS

#property indicator_label1  "LReg Slope"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrSteelBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input uint     InpPeriod=14;   // Period

//--- indicator buffers

double         BufferLRS[];

//--- global variables

int            period;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- set global variables

   period=int(InpPeriod<1 ? 1 : InpPeriod);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferLRS,INDICATOR_DATA);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Linear regression slope ("+(string)period+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferLRS,true);

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(close,true);

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   if(rates_total<fmax(period,4)) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-period-1;

      ArrayInitialize(BufferLRS,EMPTY_VALUE);

     }



//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      double ii=0,y=0,xy=0,x=0,x2=0;

      for(int j=i+period; j>=i; j--)

        {

         ii++;

         y+=close[j];

         xy+=close[j]*ii;

         x+=ii;

         x2+=ii*ii;

        }

      double c=x2*ii-x*x;

      BufferLRS[i]=(c!=0 ? (xy*ii-x*y)/c : 0);

     }



//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments