Understanding Linear Regression Slope: A Key Indicator for MetaTrader 5

Mike 2010.07.05 23:14 27 0 0
Attachments

When it comes to analyzing price data, linear regression is a powerful tool that traders can’t overlook. This method fits a straight line to the price data, helping us identify trends and make informed trading decisions. The equation for this line is:

y[x] = y0 + b*x

In this equation:

  • x is the bar number (x = 1..n);
  • y[x] refers to the corresponding price (like open, close, median, etc.);
  • b is the proportionality coefficient;
  • y0 is the bias.

The slope of the linear regression, which this indicator provides, is actually a normalized version of the coefficient b.

To calculate b, the formula is as follows:

b = (n*Sxy - Sx*Sy)/(n*Sxx - Sx*Sx)

Where:

  • Sx = Sum(x, x = 1..n) = n*(n + 1)/2;
  • Sy = Sum(y[x], x = 1..n);
  • Sxx = Sum(x*x, x = 1..n) = n*(n+1)*(2*n+1)/6;
  • Sxy = Sum(x*y[x], x = 1..n);
  • n is the period of LRS (input parameter Per).

The denominator of b can be simplified to:

n*Sxx - Sx*Sx = n*n*(n-1)*(n+1)/12

Ultimately, we can simplify the entire equation for b to:

b = 6*(2*Sxy/(n + 1) - Sy)/n/(n - 1)

Now, it’s important to note that the coefficient b isn’t normalized. To make the LRS (Linear Regression Slope) comparable across different currency pairs, we need to normalize it. A common approach is to divide b by either a simple moving average (SMA) or a linear weighted moving average (LWMA). These are defined as:

SMA = Sy/n
LWMA = 2*Sxy/n/(n + 1)

Thus, the corresponding versions of LRS are:

LRS_SMA = b/SMA = 6*(2*Sxy/Sy/(n + 1) - 1)/(n + 1)

LRS_LWMA = b/LWMA = 6*(1 - (n + 1)*Sy/Sxy/2)/(n + 1)

In practice, these two normalization methods yield very similar results. For this reason, the SMA normalization was selected for the indicator. Additionally, because the LRS values can be quite small, the indicator’s values are calculated and plotted in parts per 100 thousand, which helps keep them within a range of -100 to +100.

Linear regression slope

List
Comments 0