Attachments forums

List of attachments posted on this forum.


All files on forums: 136852

Regularized Linear Regression MA

yoake, Sat Sep 10, 2022 2:23 pm

I made Regularized Linear Regression MA.
with reference to
Pattern Recognition and Trading Decision Chris Satchwell p193-194

But it does not seem to be working correctly.
Changing setting Lambda makes little difference, same as usual Linear regression.

Where should I fix it?
If anyone knows of a solution, could you please let me know?

Thanks in advance.
Yoake

2022/09/12 Partially corrected.

Code: Select all

#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  clrAqua
#property indicator_color2  clrDeepSkyBlue
#property indicator_width1  2





extern int    Length                      = 15;
extern ENUM_APPLIED_PRICE    Price        = PRICE_CLOSE;
extern double Lambda                      = 1.0;

//
//
//
//
//

double RegLR[];
double prc[];
double line[];


string  indicatorFileName;
bool    returnBars;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(3);
   SetIndexBuffer(0,RegLR);
   SetIndexBuffer(1,line);
   SetIndexBuffer(2,prc);
         Length = MathMax(Length,1);
   IndicatorShortName("Regularized LRMA ("+Length+")");
   
         indicatorFileName = WindowExpertName();
   
   
   return(0);
}
int deinit() { return(0); }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = Bars-counted_bars;
         

         

   //
   //
   //
   //
   //

   for(i=limit; i>=0; i--){
       
       double lambda = Lambda;
       double Ra,Rb,Aaa,Abb,Acc;
       double suma=0,sumb=0,sumc=0,sumd=0;
       int    ww =Length;
       
       prc[i] = iMA(NULL,0,1,0,MODE_SMA,Price,i);
       
       if(i > Bars-1-Length) continue;
       if(i ==Bars-1-Length) {RegLR[i+2] = prc[i+2]; RegLR[i+1] = prc[i+1];}
       
       
       
       
       for(int z=1; z<=Length; z++){
           
           suma +=(z)*(z);
           sumb +=(z);
           sumc +=(z)*prc[i+Length-z];
           sumd +=prc[i+Length-z];
       }
       
       Ra = 2.0*lambda*Length*RegLR[i+1] - lambda*Length*RegLR[i+2]+sumc;
       Rb = 2.0*lambda*RegLR[i+1] - lambda*RegLR[i+2]+sumd;
       
       Aaa = lambda*Length*Length+suma;
       Abb = lambda*Length+sumb;
       Acc = ww+lambda;
       
       
       double slope,inter;
       
       if((Aaa*Acc)-(Abb*Abb) !=0){
          slope =((Ra*Acc)-(Rb*Abb)) / ((Aaa*Acc)-(Abb*Abb));
       
          inter =((Aaa*Rb)-(Abb*Ra)) / ((Aaa*Acc)-(Abb*Abb));
       }
       
       
       RegLR[i] = slope*Length+inter;
    
   }
   
   for(i=0; i<=Length; i++){
      
      line[Length-i] = slope*i+inter;
   
   }
   
   
   return(0);
}


All files in topic