Download MetaTrader 5

Linear Regression Moving Average Algorithm

To add comments, please log in or register
Do you often publish source codes? Find out how to do that correctly!
nicolasxu
296
nicolasxu 2014.10.31 10:07 
//+------------------------------------------------------------------+\\
// Calculate LRMA
//+------------------------------------------------------------------+\\

double LRMA(const int pos,const int period,const double  &price[]) {
  
  double Res=0;
  
  double tmpS=0,tmpW=0,wsum=0;
  
  for(int i=0;i<period;i++) {

    tmpS+=price[pos+i];
    tmpW+=price[pos+i]*(period-i);
    wsum+=(period-i);
  }
   
  tmpS/=period;
  tmpW/=wsum;
  Res=3.0*tmpW-2.0*tmpS;

  return(Res);
}

This is part of the code I downloaded from MT5 indicator code base.  Link is here.

I understand linear regression from this wiki page. If pos = 1 and period = 14, how the Res is calculated? Which formula does the LRMA() use? 

 

  for(int i=0;i<period;i++) {

    tmpS+=price[pos+i];
    tmpW+=price[pos+i]*(period-i);
    wsum+=(period-i);
  }
   
  tmpS/=period;
  tmpW/=wsum;
  Res=3.0*tmpW-2.0*tmpS;

Can you explain the above code? My challenge is to understand the mathematical foundation of the above code. 

Thanks! 

Color Linear Regression
Color Linear Regression
  • votes: 19
  • 2014.10.02
  • Victor Nikolaev
  • www.mql5.com
Color version of the Linear Regression indicator.
Uwe Goetzke
1446
Uwe Goetzke 2014.10.31 12:05  

Check this article:

https://www.mql5.com/en/articles/270

3 Methods of Indicators Acceleration by the Example of the Linear Regression
3 Methods of Indicators Acceleration by the Example of the Linear Regression
  • 2011.08.04
  • Andrew
  • www.mql5.com
The article deals with the methods of indicators computational algorithms optimization. Everyone will find a method that suits his/her needs best. Three methods are described here.One of them is quite simple, the next one requires solid knowledge of Math and the last one requires some wit. Indicators or MetaTrader5 terminal design features are used to realize most of the described methods. The methods are quite universal and can be used not only for acceleration of the linear regression calculation, but also for many other indicators.
nicolasxu
296
nicolasxu 2014.10.31 16:49  
good! I'll study it! Thank you!
To add comments, please log in or register