# Linear Regression Moving Average Algorithm

Do you often publish source codes? Find out how to do that correctly!
296
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);
}
```

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
• 2014.10.02
• Victor Nikolaev
• www.mql5.com
Color version of the Linear Regression indicator.
1446
2014.10.31 12:05