Help write a linear regression

 
Long time ago I wrote linear regression indicators in MQL3, started rewriting them in 4, they don't work. Specialists please help me to write or correct a mistake.
//+------------------------------------------------------------------+
//|                                          KVNLinearRegression.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                        https://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net"
 
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 MediumBlue
//---- input parameters
extern int       nn=21;
//---- buffers
double LR1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   //SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(0,LR1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| пользовательский индикатор                            |
//+------------------------------------------------------------------+
int start()
  {
  int  n,n1,n2;
      double ssm1,ssm2,ssm3,ssm4,a,b,LR;
                  ssm1=0;
                ssm2=0;    
                ssm3=0;
                 ssm4=0;
 // for(n=0;n<=Bars;n++)
 for(n=0;n<=100;n++)
{
    for(n1=1;n1<=nn;n1++)    
    {
n2=n+n1-1;
ssm1=ssm1+n1*Close[n2];
ssm2=ssm2+n1;
ssm3=ssm3+Close[n2];
ssm4=ssm4+n1*n1;
    }
//Индикатор строится по формуле:LR = at+b
//где LR - прогнозируемая "средняя" цена закрытия,
//t - момент времени,Pt  - цены закрытия за n последних периодов.
//a = (n*СУММА (t*Pt) - СУММА(t)*CУММА(Pt))/(n*СУММА(t^2) - (СУММА(t))^2) - тангенс угла наклона линии регрессии,
//b = 1/n*(СУММА(Pt) - a*СУММА(t)), - смещение по горизонтали}
 
a=(nn*ssm1-ssm2*ssm3)/(nn*ssm4-ssm2*ssm2);
b=(1/nn)*(ssm3-a*ssm2);
LR=a*nn+b;
                ssm1=0;
                ssm2=0;    
                ssm3=0;
                 ssm4=0;
  LR1[n]=LR;                 
 }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
And a similar one: hyperbolic regression (can't find the error either)
//+------------------------------------------------------------------+
//|                                    гиперболическая регрессия.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
 
//#property indicator_chart_window
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int       nn=21;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   double barr, as, bs,cs,ds,e, f,k,LR,ExtMapBuffer1[];
   int n,n1;
// Индикатор Гиперболической Регресии
for (barr=0;barr<=100;barr++){
    for (n=1;n<=nn;n++){
        n1=barr+n-1;
as=as+1/n;
bs=bs+1/(n*n);
cs=cs+Close[n1];
ds=ds+Close[n1]/n;
}
 
e=nn*bs-as*as;
f=cs*bs-ds*as;
k=nn*ds-as*cs;
f=f/e;
k=k/e;                                                                                                                                                                                                                                     
LR = f+k/nn;
    as=0;                                                 
    bs=0;
    cs=0;
    ds=0;
    ExtMapBuffer1[n]=LR;
}
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
 
https://forum.mql4.com/ru/10446/page13
 
Thanks, interesting, but where did I mess up the indicator?
 
kvn:
Thanks, interesting, but where did I mess up the indicator?
That's right -messed up.
 
Rosh:
kvn:

Thanks, interesting, but where did I mess up the indicator?

That's right -I messed up.

You're literate, aren't you? Then tell me where. And thank you very much.
 
kvn:
Rosh:
kvn:

Thanks, interesting, but where did I mess up the indicator?

That's right -messed up.

Literate, eh? Then tell me where, and thank you very much.
There has been a lot of information on this topic here. I can only give you a general idea: the equation of the sum of standard deviations from the approximating curve with as many given parameters as you want. Then the partial derivatives of each parameter are found and equated to zero. From the resulting system of linear equations all necessary parameters are found. The algorithm, as you can see, is simple, and not at all creative to do it.
 
What you say is not exactly linear regression. The LR technique is described in my indicator.

//Indicator is built using the formula:LR = at+b
//where LR - forecasted "average" price of closing,
//t - point in time,Pt - closing price of the last n periods.
//a = (n*SUMM(t*Pt) -SUMM(t)*SUMM(Pt))/(n*SUMM(t^2) - (SUMM(t))^2) - angle tangent of the regression line,
//b = 1/n*(SUMM(Pt) - a*SUMM(t)), - horizontal shift}

But when I execute it, first I get wrong data (when n=1-100,) and then n=22 and correct values come out. There is a small error somewhere and I can't find it.
I suspect the error is in the loop operator.
 
Although I don't like wikipedia, but I am providing a link to it on the subject of linear regression. I also found this on a certain blog - http://cmacfm.mazoo.net/archives/000936.html
 
I won't argue about LR. SO WHERE IS THE ERROR IN THE INDICATOR CODE???????
Reason: