Indicator very slow and runs in error

 

Good morning,


I am still in the learning process so please dont be offended if I ask silly questions.

I am working on an indicator and recently added some part of code which calculates a raff channel.

The code was a bit enhanced by me so it can calculate the regression on another timeframe.

This is what it actually looks like:

                           double a2,b2,c2,
                                  sumy2=0.0,
                                  sumx2=0.0,
                                  sumxy2=0.0,
                                  sumx22=0.0,
                                  h2=0.0,l2=0.0;   
                                  int x2;
   
                              // calculate linear regression
                              for(int y2=0; y2<barsToCount; y2++)
                              {
                              int _barShift2 = iBarShift(NULL, TF_3, iTime(NULL,TF_3,y2));
                                 sumy2+=iClose(NULL,TF_3,_barShift2);
                                 sumxy2+=iClose(NULL,TF_3,_barShift2)*y2;
                                 sumx2+=y2;
                                 sumx22+=y2*y2;
                              }
                              
                              c2=sumx22*barsToCount-sumx2*sumx2;
                              
                              if(c2==0.0)
                              {
                                 Alert("Error in linear regression!");
                                 return(0);
                              }

Unfortunately I am facing 2 problems. 
1) I am actually running in an error (if c2 == 0.0)

2) The indicator is very slow (2021.06.28 06:00:17.934 EURUSD.r,H1: indicator(s) working too slowly, total calculation time 8531 ms)

As TF3 I am using W1 timeframe. barsToCount is set to 200.

I tried all wekend to find the problem but could not figure it out.

Any help would be appreciated.


Thank you.

 
Could you please show the whole OnCalc... Function, maybe.
 

Yes I can do so. Its too long for adding it here (>64.000 characters).

I will attach it as file.

Files:
OnCalc.txt  28 kb
 
silverchair :

Yes I can do so. Its too long for adding it here (>64.000 characters).

I will attach it as file.

Indicator code CANNOT have *.txt* extension

Please attach the correct file.

 
Vladimir Karputov:

Indicator code CANNOT have *.txt* extension

Please attach the correct file.

This is not the whole indicator. It is just the OnCalc code.

 
silverchair :

This is not the whole indicator. It is just the OnCalc code.

We still do not understand: do you have an old code or MQL5?

 
Vladimir Karputov:

We still do not understand: do you have an old code or MQL5?

This is written in MQL5. 

Edit: Sorry for confusion. Yes its MQL4 code
 
silverchair :

This is written in MQL5. 

This is an old code (let me remind you that in MQL5 the indicator handle is CREATED ONCE and it is done in OnInit).

I will move your topic to the section for the old terminal.

 
Vladimir Karputov:

This is an old code (let me remind you that in MQL5 the indicator handle is CREATED ONCE and it is done in OnInit).

I will move your topic to the section for the old terminal.

Sorry for the confusion and thank you for moving the topic.

 
silverchair: 2) The indicator is very slow (2021.06.28 06:00:17.934 EURUSD.r,H1: indicator(s) working too slowly, total calculation time 8531 ms)
  1. iMA(NULL,CS_TIMEFRAME, …

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20

  2. Your code
          if((iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx)>iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+1)) &&
             (iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+1)>iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+2)) &&
             (iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+2)>iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+3)))
             {
             Up=true;
             }
          else
             {
             Up=false;
             }
    
    // Market Trend Down
          if((iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx)<iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+1)) &&
             (iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+1)<iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+2)) &&
             (iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+2)<iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+3)))
             {
             Down=true;
             }
          else
             {
             Down=false;
             }
    Don't dupicate (copy and paste) function calls.
          double MA0=iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx);
          double MA1=iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+1);
          double MA2=iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+2);
          double MA3=iMA(NULL,CS_TIMEFRAME,5,0,MODE_EMA,PRICE_CLOSE,_barShiftx+3);
          if((MA0>MA1) &&
             (MA1>MA2) &&
             (MA2>MA3)
             {
             Up=true;
             }
          else
             {
             Up=false;
             }
    
          if((MA0<MA1) &&
             (MA1<MA2) &&
             (MA2<MA3)
             {
             Down=true;
             }
          else
             {
             Down=false;
             }
    Simplify your code.
      Up = MA0>MA1 && MA1>MA2 && MA2>MA3;
    Down = MA0<MA1 && MA1<MA2 && MA2<MA3;
    
              Increase Order after stoploss - MQL4 programming forum #1.3 2017.05.29
  3. 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[])
    {
    
       int i = MathMax(barsToCount,FiboBars);
          while (i >= 0) 
    You are computing the max every tick. After the first tick you should only be computing bar zero of the current timeframe, and bar zero of other timeframes.

    See How to do your lookbacks correctly #9#14 & #19.

 
William Roeder:
  1. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20

  2. Your code
    Don't dupicate (copy and paste) function calls.
    Simplify your code.           Increase Order after stoploss - MQL4 programming forum #1.3 2017.05.29
  3. You are computing the max every tick. After the first tick you should only be computing bar zero of the current timeframe, and bar zero of other timeframes.

    See How to do your lookbacks correctly #9#14 & #19.

Thank you for your reply and thanks a lot for the practical suggestions to simplify the code. I can see now the difference if somebody looks at a code just from the point of the desired output or if you look at the code as a programmer.

I will read your link about the lookbacks. Thats probably the key to improve performance. 

Thanks again!