Problem with indicators buffers not erasing when out of the period supposed to show + not updating parameters

 

Hello,

I am working on a polynomial regression indicator ( just did order 1 for the moment ).

But I've  two problems I can't solve:

1. The indicator leave a trail after passing ( instead of erasing)

2. The parameters seems to not update correctly. ( When I reload the indicator everything is fine then when the time move, there is like an unwanted memory making the indicator fail.

After reset: (wanted result)

Before reset: (unwanted result) ( from strategy tester, but I saw the same problem in M1 )

And here is my script:

//+------------------------------------------------------------------+
//|                                         PolynomialRegression.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property indicator_chart_window    // Indicator is drawn in the main window
//#property indicator_separate_window
#property indicator_buffers 3       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Yellow     // Color of the 2st line
#property indicator_color3 Yellow     // Color of the 3st line
  
extern int Period  = 50;
extern int Degree = 1;
extern double Deviation = 1;

 
//double Buf_0[51], Buf_1[51], Buf_2[51], A[51][10], A_trans[10][51], ATA[10][10], ATB[10], INV[10][10], det, x[10], b[51], temp, poly[51], std;
double Buf_0[], Buf_1[], Buf_2[], A[100][10], A_trans[10][51], ATA[10][10], ATB[10], INV[10][10], det, x[10], b[51], temp, poly[51], std;  
//--------------------------------------------------------------------
int init()                          // Special function init()
  {
   SetIndexBuffer(0,Buf_0);
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(1,Buf_1);
   SetIndexStyle (1,DRAW_LINE,STYLE_DASHDOTDOT,2);
   SetIndexBuffer(2,Buf_2);
   SetIndexStyle (2,DRAW_LINE,STYLE_DASHDOTDOT,2);
   return(0);                          // Exit the special funct. init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
   int i,j,k,Counted_bars;                // Number of counted bars
//--------------------------------------------------------------------
   Counted_bars = IndicatorCounted(); // Number of counted bars
   i = Bars-Counted_bars-1;           // Index of the first uncounted
   if (i > Period - 1){
      i = Period - 1;
   }
   while(i>=0){
      
      b[i] = Close[i];
      for(j=Degree;j>=0;j--){
         A[i][Degree-j] = MathPow(i, j);
      }
      
      i--; // Calculating index of the next bar
   }
   
   // To solve x = (A^T*A)^-1 *A^T*b
   
   // A^T
   for(i=Period;i>=0;i--){
      for(j=Degree;j>=0;j--){
         A_trans[j][i] = A[i][j];
      }
   }
   
   // A^T*A
   for(i=0;i<=Degree;i++){
      for(j=0;j<=Degree;j++){
         temp = 0;
         for(k=0;k<=Period;k++){
            temp += A_trans[i][k]*A[k][j];
         }
         ATA[i][j] = temp;
      }
   }
   
   // INV
   if(Degree==1){
      det = ATA[0][0]*ATA[1][1]-ATA[1][0]*ATA[0][1];
      
      INV[0][0] = 1/det*ATA[1][1];
      INV[0][1] = -1/det*ATA[0][1];
      INV[1][0] = -1/det*ATA[1][0];
      INV[1][1] = 1/det*ATA[0][0];
   }
   
   //ATB
   for(i=0;i<=Degree;i++){
      temp = 0;
      for(k=0;k<=Period;k++){
         temp += A_trans[i][k]*b[k];
      }
      ATB[i] = temp;
      
   }
   
   // x
   for(i=0;i<=Degree;i++){
      temp = 0;
      for(k=0;k<=Degree;k++){
         temp += INV[i][k]*ATB[k];
      }
      x[i] = temp;
      
   }
   
   // Polynome
   for(i=0;i<=Period;i++){
      temp = 0;
      for(j=0;j<=Degree;j++){
         temp += x[j]*MathPow(i, Degree-j);
      }
      
      poly[i] = temp;
   }
   
   // Ecart type
   temp = 0;
   for(i=0;i<=Period;i++){
      temp += MathPow(Close[i]-poly[i], 2);
   }
   std = MathSqrt(temp/(Period+1));
   
   // Buffers
   for(i=0;i<=Period;i++){
      Buf_0[i] = poly[i];
      Buf_1[i] = poly[i] + Deviation*std;
      Buf_2[i] = poly[i] - Deviation*std;
   }
//--------------------------------------------------------------------
   return(0);                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------
Chart AUDNZD, H1, 2019.08.07 10:51 UTC, OANDA DIVISION7, MetaTrader 4, Real
Chart AUDNZD, H1, 2019.08.07 10:51 UTC, OANDA DIVISION7, MetaTrader 4, Real
  • www.mql5.com
Symbol: AUDNZD. Periodicity: H1. Broker: OANDA DIVISION7. Trading Platform: MetaTrader 4. Trading Mode: Real. Date: 2019.08.07 10:51 UTC.
 
benoitfon:

I am working on a polynomial regression indicator ( just did order 1 for the moment ).

But I've  two problems I can't solve:

1. The indicator leave a trail after passing ( instead of erasing)

2. The parameters seems to not update correctly. ( When I reload the indicator everything is fine then when the time move, there is like an unwanted memory making the indicator fail.

After reset: (wanted result)

Before reset: (unwanted result) ( from strategy tester, but I saw the same problem in M1 )

And here is my script:

Just add these lines in your init():

   SetIndexEmptyValue(0,0);
   SetIndexEmptyValue(1,0);
   SetIndexEmptyValue(2,0);

And these lines before your last 'for' loop:

   ArrayInitialize(Buf_0,0);
   ArrayInitialize(Buf_1,0);
   ArrayInitialize(Buf_2,0);