Linear regression channel - page 17

 

I willingly believe that it is possible to do a lot of functions with accelerated calculation. Like subtract a value from the end of a period and add a new value to the beginning, without a cycle.

In particular, here is an example of how you can calculate several types of filter from simple waving to linear regression without a cycle.

#property indicator_chart_window 
#property indicator_buffers 2 
#property indicator_plots   1  
#property indicator_type1   DRAW_COLOR_LINE 
#property indicator_color1  clrDeepSkyBlue,clrBisque 
#property indicator_width1  2 
//********************************************************************
input int    p = 24;
input double N = 3;
//********************************************************************
double ss[],col[];
double ci,sum1,sum2,c1,c2,mai,lwi,fxi;
 int w,fs;
//********************************************************************
int OnInit()
{
   SetIndexBuffer(0,ss,INDICATOR_DATA); 
   SetIndexBuffer(1,col,INDICATOR_COLOR_INDEX); 
   //------------------------------------------
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,clrDeepSkyBlue);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,clrBisque);
   //------------------------------------------
   
   return(INIT_SUCCEEDED);
}
//********************************************************************
int OnCalculate(const int rates_total,const int prev_calculated,
                const datetime &time[],const double &open[],const double &high[],const double &low[],const double &close[],
                const long &tick_volume[],const long &volume[],const int &spread[])
{
   //--------------------------------------------------------
   if (prev_calculated==rates_total) return(rates_total);
   int start=prev_calculated; 
   if (prev_calculated==0) 
   {
      start=p; 
      
      int k=0; w=0; sum1=0; sum2=0;
      for(int j=p-1; j>=0; j--)
      {
         k++;
         ci=open[start-j];
         sum1+=ci; 
         sum2+=k*ci; 
         w+=k; 
      }
      mai=sum2/w; ss[start]=mai; 
      ci=open[start]; 
      sum1-=ci; sum2-=ci*p;
      start++;
   }
   //--------------------------------------------------------
   for(int i=start; i<rates_total; i++)
   {
      c1=open[i-1];  
      c2=open[i-p]; 
      
      sum1+=c1-c2;
      sum2+=c1*p-c2-sum1;
         
      ci=open[i]; 
      
      lwi=(sum2+ci*p)/w;
      mai=(sum1+ci)/p;
      
      fxi=mai+(lwi-mai)*N;
      
      ss[i]=fxi;
      
      if (ss[i]>ss[i-1]) fs=0; else  
      if (ss[i]<ss[i-1]) fs=1; 
      
      if (fs==0) col[i]=0; else col[i]=1;   
   }
   //--------------------------------------------------------
   return(rates_total);
}
//********************************************************************

Here at N=0 - normal SMA, N=1 - linear-weighted, N=3 - linear regression. And you can also get intermediate values as N is fractional. I also calculated the RMS in a similar way. I think polynomial regression can be done the same way. It would be a practical necessity. At least, I have not managed to use polynomial regression in profitable Expert Advisors yet. Channels on EMAs are simpler and work well in practice.

Here is a slightly contrived variant of linear regression on mq4 with RMS without cycle. There is a cycle once at initiation, or more precisely, when calculating the first set of values, and that's all, - then there are only sums and differences. Of course, it is much faster than with cycles. One subtlety is that the period is specified in hours and is recalculated when switching timeframes.

#property strict
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 clrDodgerBlue
#property indicator_color2 clrOrangeRed
#property indicator_color3 clrLavender
#property indicator_color4 clrLavender
#property indicator_color5 clrMediumAquamarine
#property indicator_color6 clrMagenta
#property indicator_width1 2
#property indicator_width2 2
#property indicator_style5 2
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_LINE
#property indicator_type3 DRAW_LINE
#property indicator_type4 DRAW_LINE
#property indicator_type5 DRAW_LINE
#property indicator_type6 DRAW_LINE
//===========================
extern double hrLR = 12;
extern double ksq  = 1;
extern int    i0   = 1;
extern int    SPR  = 1;
//===========================
double ss[],ssL[],sH[],sL[],ma[],sU[];
double lri,sq,mai,ui,spr2;
int p,fs;
//******************************************************************
int init() 
{
   SetIndexBuffer(0,ss);
   SetIndexBuffer(1,ssL); SetIndexEmptyValue(1,0);
   SetIndexBuffer(2,sH);
   SetIndexBuffer(3,sL);
   SetIndexBuffer(4,ma);
   SetIndexBuffer(5,sU);
   //--------------------
   p=int(hrLR*60/Period()); if (p<3) p=3;
   if (SPR<0) spr2=-SPR*Point/2;
   
   return(0);
}
//******************************************************************
int start() 
{
   int cbi=Bars-IndicatorCounted()-1; if (cbi<0) return(-1);
   if (i0==1 && cbi==0) return(0); if (cbi==1) cbi=0;
   if (SPR>0) spr2=NormalizeDouble(Ask-Bid,Digits)/2;
   if (cbi>1)
   { 
      cbi=Bars-p-1; 
   
      af_LR0(0,cbi); ui=mai; 
   }
   //------------------------------
   for(int i=cbi; i>=0; i--) 
   { 
      lri=af_LR0(1,i);
      
      ss[i]=lri;
      sH[i]=lri+sq;
      sL[i]=lri-sq;
      ma[i]=mai;
      
      if (sL[i]>ui) ui=sL[i]; else if (sH[i]<ui) ui=sH[i];
      
      sU[i]=ui;
      
      if (ss[i]>ss[i+1]) fs=1;
      if (ss[i]<ss[i+1]) {if (fs==1) ssL[i+1]=ss[i+1]; fs=2;}
      if (fs==2) ssL[i]=ss[i]; else if (fs==1) ssL[i]=0.0; 
   }
   return(0);
}
//********************************************************************
double af_LR0(int index, int i)
{
   static double sx,syp,sxy,syyp,S;
   double ci=0,c1=0,cp=0,sy,syy,aa,bb;
   static int ti;
   
   if (index==1)
   {
      if (ti!=Time[i]) 
      {
         if (i0==0) {c1=Close[i+1]+spr2; cp=Close[i+p]+spr2;} else
         if (i0==1) {c1=Open[i+1]+spr2; cp=Open[i+p]+spr2;} else
         if (i0==2) {c1=(High[i+1]+Low[i+1])/2+spr2; cp=(High[i+p]+Low[i+p])/2+spr2;}
         
         sxy+=syp+c1-p*cp; 
         syp+=c1-cp;
         syyp+=c1*c1-cp*cp; 
         ti=int(Time[i]);
      }
   }
   else
   {
      int j;
      double sxx;
      
      sx=0.0; sxx=0.0; 
      for (j=0; j<p; j++) {sx+=j; sxx+=j*j;} 
      S=sx*sx-p*sxx;
      
      syp=0.0; sxy=0.0; syyp=0.0;
      for (j=1; j<p; j++) 
      {
         if (i0==0) ci=Close[i+j]+spr2; else
         if (i0==1) ci=Open[i+j]+spr2; else
         if (i0==2) ci=(High[i+j]+Low[i+j])/2+spr2;
         
         syp+=ci; 
         sxy+=j*ci;
         syyp+=ci*ci;
      }
      
      ti=int(Time[i]);
   }
   
   if (i0==0) ci=Close[i]+spr2; else
   if (i0==1) ci=Open[i]+spr2; else
   if (i0==2) ci=(High[i]+Low[i])/2+spr2;
         
   sy=syp+ci; 
   syy=syyp+ci*ci;
   
   aa=(sx*sy-p*sxy)/S; 
   bb=(sy-aa*sx)/p;
   
   sq = (syy - aa*sxy - bb*sy)/(p-2); 
   if (sq>=0) sq = MathSqrt(sq)*ksq;
   
   mai=sy/p;
      
   return(bb);
}
//********************************************************************

 
Yousufkhodja Sultonov:

Why did Fedoseyev self-depart, reasoning correctly?

Because he reasoned incorrectly.
Read the thread.

 
What does "no loop" do for you?

How much does it speed up code execution?
 
danminin:
What does "no loop" do for you?

How much faster does it make your code run?
A lot, actually. I used to measure it with GetTickCount(). But it's well noticeable when you need to do a lot of optimizations. One thing is when you have to count a lot of variants for several hours, and another thing is when you have to do it in several dozens or units of minutes. But it's rarely needed. That's why you may not bother with it too much.
 
ANG3110:

I willingly believe that it is possible to do a lot of functions with accelerated calculation. Like subtract a value from the end of a period and add a new value to the beginning, without a cycle.

In particular, here is an example of how you can calculate several kinds of filter from simple waving to linear regression, without cycle.

Here at N=0 - normal SMA, N=1 - linear-weighted, N=3 - linear regression. And you can also get intermediate values as N is fractional. I also calculated the RMS in a similar way. I think polynomial regression can be done the same way. It would be a practical necessity. At least, I have not managed to use polynomial regression in profitable Expert Advisors yet. Channels on EMAs are simpler and work well in practice.

Here is a slightly complex variant of the linear regression in mq4 with RMS without loops. There is a cycle once at initiation, or more precisely, when calculating the first reading, and that's it - then there are only sums and differences. Of course it calculates many times faster than with cycles.

Yes. That is correct. This is a real example of cycle-free RMS calculation in linear regression.
True, there is a small error somewhere in the algorithm, leading to all three lines (centre, upper and lower limits of the channel) being shifted upwards.


 
Nikolai Semko:

Yes. That is correct. This is a real example of cycle-free RMS calculation in linear regression.
True, there is a small error somewhere in the algorithm, leading to all three lines (centre, upper and lower limits of the channel) being shifted upwards.


And there's just half of the spread added up. I did this once, to test a trading Expert Advisor to have alignment relative to - Ask-Bid. If you set SPR=0, there will be no offset. It will count purely on bid prices.
 
danminin:
What does this "no loop" do for you?

How much faster does it make your code run?

The RMS calculation alone can give a gain of about 10 to 1,000 times, depending on the period.

 
ANG3110:
And there's just half of the spread added in. I did this once to test a trading Expert Advisor to have alignment relative to - Ask-Bid. If we set SPR=0, there will be no offset. It will count purely on bid prices.

Yes, exactly. A complete match to my linear regression implementation.

 
Yousufkhodja Sultonov:


How is the Sultonov indicator doing? Has it broken the back of the forex already or is it in progress?
 
Vladimir Baskakov:
How is the Sultonov indicator doing? Has it broken its back to forex yet or is it in the process of doing so?

The indicator is working on one cent real account at UPU on a "run and forget" basis with a deposit of 48 c.u. The second month it has been hovering around 50, apparently, waiting for its time. It is impossible to receive more than 10 % per annum on Forex, stably and without risk, provided reinvestment of profits for many years - these are my conclusions on the backbone of Forex. The hasty conclusions of 8 years ago are shattered by the reality of the market. The powerful universal regression model has failed miserably, producing bankable profits. The URM does a great job on all the technical, social, mining (extracting gold from poor ores) and other processeso except the market element. The conclusion is that regression models have limited potential to extract profits from the market.