My code is working but slow on a Min/Max, please help

 

Hi Everyone,

I wrote the below code. The idea is:

From a given bar i (say when MA5 cut MA10), lookback 15 bar, find Min Close bar (call min and nmin)

From bar i to Min Close bar, find Max Close bar (call max2 and nmax2)

From bar i to Max Close bar, find Min Close bar (call min2 and nmin2)

I wrote 3 for() to get it. Yes it works but it take quite long time.  

 

Do I make any logic mistake and if you could tell me if there is a way to make it runs faster?

 

Thank you,

HHC

 

      //lookback 15 bars to find Min and nmin or barmin
      int nmin=0;
      double min=10000;
      for(int j=i;j<=i+15;j++)
      {  if(Close[j]<min)     {min=Close[j]; nmin=j;}
      }
      
      //from i to Min, find Max2
      int nmax2=0;
      double max2=0;
      for(int k0=i;k0<=nmin;k0++)
      {  if(Close[k0]>max2)     {max2=Close[k0]; nmax2=k0;}
      }
      
      //from i to nMax2, find Min2 and Nmin2------------------------------------------------------------
      int nmin2=0;
      double min2=100000;
      for(int k=i;k<=nmax2;k++)
      {  if(Close[k]<min2)    {min2=Close[k]; nmin2=k;}
      }
 
      //lookback 15 bars to find Min and nmin or barmin
      int look_back_size = 15;
      int nmin = iLowest(_Symbol, _Period, MODE_CLOSE, look_back_size + 1, i);
      double min = Close[nmin];

      
      //from i to Min, find Max2
      int nmax2 = iHighest(_Symbol, _Period, MODE_CLOSE, nmin - i + 1, i);
      double max2 = Close[nmax2];
      
      //from i to nMax2, find Min2 and Nmin2------------------------------------------------------------
      int nmin2 = iLowest(_Symbol, _Period, MODE_CLOSE, nmax2 - i + 1, i);
      double min2 = Close[nmin2];
On my mind, something like this will be better (sorry, I didn't test the code).
 
Sergey Dzyublik:
On my mind, something like this will be better (sorry, I didn't test the code).

Thanks Sergey,

I do a speed test, calculating for 2000 bar.

The original take about 3 second. the new method take about 12 second.

HHC 

 
hhchenfx:

Thanks Sergey,

I do a speed test, calculating for 2000 bar.

The original take about 3 second. the new method take about 12 second.

HHC 

Try with ArrayMinimum() and ArrayMaximum().
Reason: