Page 137 of 158

Regularized Linear Regression MA

Posted: Sat Sep 10, 2022 2:33 pm
by yoake
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);
}



Re: Regularized Linear Regression MA

Posted: Sat Sep 10, 2022 3:12 pm
by ChuChu Rocket
yoake wrote: Sat Sep 10, 2022 2:33 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
Following.

Thank you for posting your file progress so far. I'm keen on this, please hang tight one of our coders will sort it out for you later today 👍

Re: Regularized Linear Regression MA

Posted: Mon Sep 12, 2022 2:10 am
by mrtools
yoake wrote: Sat Sep 10, 2022 2:33 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

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       = 26;
extern int    Price        = 0;
extern double Lambda       = 1.0;

//
//
//
//
//

double RegLR[];
double line[];


string  indicatorFileName;
bool    returnBars;


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

int init()
{
   SetIndexBuffer(0,RegLR);
   SetIndexBuffer(1,line);
         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;
       
       //double prc = iMA(NULL,0,1,0,MODE_SMA,Price,i);
       
       for(int z=1; z<=Length; z++){
           
           suma +=(z)*(z);
           sumb +=(z);
           sumc +=(z)*Close[i+Length-z];
           sumd +=Close[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);
}


Image

Image

Image
Not sure, tried a version using Regularized ema as an example and it doesn't seem right either.

Re: Regularized Linear Regression MA

Posted: Mon Sep 12, 2022 3:14 am
by yoake
mrtools wrote: Mon Sep 12, 2022 2:10 am Not sure, tried a version using Regularized ema as an example and it doesn't seem right either.

Thank you for your reply.
I am a bit confused to be honest.
I will go back and read the book and test code again.

By the way.
I would like to take this opportunity to thank you.

A few years ago I had very little programming knowledge, but I learnt a lot from reading your code.
I have been reading the TSD Forum and Forex-Station for a long time.
Getting a response from you is like a dream come true.
Thank you so much, I really appreciate it.

Yoake

Re: Coding Help

Posted: Mon Sep 12, 2022 4:07 am
by alpha24
Dear Mrtools Sir,
Got this code idea from the internet if it is possible please make this indicator
-------------------------------------------------------------------------------------------------------------------
1. Calculate the moving average (EMA)

The formula for the 12-day EMA is
EMA (12) = EMA (12) of the previous day × 11/13 + today’s closing price × 2/13
The formula for the 26-day EMA is
EMA (26) = the previous day’s EMA (26) × 25/27 + today’s closing price × 2/27
2. Calculate the deviation value (DIF)
DIF = Today’s EMA (12)-Today’s EMA (26)
3. Calculate the 9-day EMA of DIF
Calculate the 9-day EMA based on the deviation value, that is, the average value of the deviation, which is the required MACD value. In order not to be confused with the original name of the indicator, this value is also known as
DEA or DEM.
Today’s DEA (MACD)=Previous day’s DEA×8/10+today’s DIF×2/10.
The calculated values ​​of DIF and DEA are both positive or negative.
Using (DIF-DEA)×2 is the MACD histogram.

Re: Coding Help

Posted: Mon Sep 12, 2022 7:01 am
by mrtools
alpha24 wrote: Mon Sep 12, 2022 4:07 am Dear Mrtools Sir,
Got this code idea from the internet if it is possible please make this indicator
-------------------------------------------------------------------------------------------------------------------
1. Calculate the moving average (EMA)

The formula for the 12-day EMA is
EMA (12) = EMA (12) of the previous day × 11/13 + today’s closing price × 2/13
The formula for the 26-day EMA is
EMA (26) = the previous day’s EMA (26) × 25/27 + today’s closing price × 2/27
2. Calculate the deviation value (DIF)
DIF = Today’s EMA (12)-Today’s EMA (26)
3. Calculate the 9-day EMA of DIF
Calculate the 9-day EMA based on the deviation value, that is, the average value of the deviation, which is the required MACD value. In order not to be confused with the original name of the indicator, this value is also known as
DEA or DEM.
Today’s DEA (MACD)=Previous day’s DEA×8/10+today’s DIF×2/10.
The calculated values ​​of DIF and DEA are both positive or negative.
Using (DIF-DEA)×2 is the MACD histogram.
Not sure by any chance do you have more information on it?

Re: Coding Help

Posted: Mon Sep 12, 2022 3:26 pm
by yoake
Regularized Linear Regression MA

I have tested again with some corrections. There was no change, but I noticed something.
If the period exceeds 25, the amount of correction seems to be significantly less. It is no different from normal Linear regression. Conversely, if the period is shortened, the correction amount becomes larger.

However, it is not clear whether this is the correct behaviour.
I have corrected the uploaded file just in case.

Thanks in advance.
Yoake

Re: Coding Help

Posted: Tue Sep 13, 2022 12:38 am
by alpha24
mrtools wrote: Mon Sep 12, 2022 7:01 am Not sure by any chance do you have more information on it?
No sir,
just I know about it is "Chinese style MACD".
and below image.

Re: Coding Help

Posted: Tue Sep 13, 2022 3:03 am
by wojtek
Is it possible to place a histogram (which is usually in the separate window)
at the bottom of the main chart? In particular, I think about something
like the Heiken Ashi histo.

Re: Coding Help

Posted: Tue Sep 13, 2022 3:45 am
by mrtools
wojtek wrote: Tue Sep 13, 2022 3:03 am Is it possible to place a histogram (which is usually in the separate window)
at the bottom of the main chart? In particular, I think about something
like the Heiken Ashi histo.
Know Mladen has done a lot of on the bottom of the main chart indicators, only version I've seen the code was for a volume histogram, that isn't a zero cross type or anything.