Problem in OnCalculate()

 

Wondering if someone can shed some light on an issue I'm having in my OnCalculate() function.  I'm trying to do three iterations of calculations when the function gets called, but I can't seem to get anything to happen in the third iteration.  Everything works fine in the first two iterations, but if I put any code at all in the third iteration it doesn't seem to execute.  Code looks like this:

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;
   static int limit;
   double temp;
   double rise;
   double tempSlope;

   if(rates_total <= run)
      return 0; 
        
   //--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;     
    
   for(i=0; i<limit; i++)       // FIRST ITERATION
   {
      trendTF1[i] = iCustom(NULL, 0, "Super smoothed average trend 1.2", TimeFrame1, TF1_Period, 0, TF1_MA_Method, false, false, true, true, false, false, false, clrLimeGreen, clrOrangeRed, clrDimGray, 3, true, 2, i); // Mode 2 indicates direction 
      trendTF2[i] = iCustom(NULL, 0, "Super smoothed average trend 1.2", TimeFrame2, TF2_Period, 0, TF2_MA_Method, false, false, true, true, false, false, false, clrLimeGreen, clrOrangeRed, clrDimGray, 3, true, 2, i); 
      trendTF3[i] = iCustom(NULL, 0, "Super smoothed average trend 1.2", TimeFrame3, TF3_Period, 0, TF3_MA_Method, false, false, true, true, false, false, false, clrLimeGreen, clrOrangeRed, clrDimGray, 3, true, 2, i); 
      trendTF4[i] = iCustom(NULL, 0, "Super smoothed average trend 1.2", TimeFrame4, TF4_Period, 0, TF4_MA_Method, false, false, true, true, false, false, false, clrLimeGreen, clrOrangeRed, clrDimGray, 3, true, 2, i); 
      trendTF4Raw[i] = iCustom(NULL, 0, "Super smoothed average trend 1.2", TimeFrame4, TF4_Period, 0, TF4_MA_Method, false, false, true, true, false, false, false, clrLimeGreen, clrOrangeRed, clrDimGray, 3, true, 1, i); // Mode 1 is raw value

         if(trendTF1[i] == 2147483647) // UP
         {
            if(trendTF2[i] == 2147483647) // UP
            {
               if(trendTF3[i] == 2147483647) // UP
               {
                  upTrend = true;
                  dirMult[i] = 1;
               }
            }
         }
         else if(trendTF1[i] != 2147483647) // DOWN
         {
            if(trendTF2[i] != 2147483647) // DOWN
            {
               if(trendTF3[i] != 2147483647) // DOWN
               {
                  downTrend = true;
                  dirMult[i] = -1;
               }
            }
         }
   } 
    
   ArrayCopy(tempArray, trendTF4Raw, 0, 0, WHOLE_ARRAY); 
      
   for(i=0; i<limit; i++)       // SECOND ITERATION
   {
      int Q1;
      int Q3;
      double IQR; // Interquartile range
      double median;
      static int barsCalculated = 0;

      ArraySort(tempArray, scalingSet, i, MODE_ASCEND);
      Q3 = i + MathRound(scalingSet * 0.75);
      Q1 = i + MathRound(scalingSet * 0.25);
      IQR = tempArray[Q3] - tempArray[Q1];
      median = tempArray[Q1 + ((Q3 - Q1)/2)];
      
      trendScaled[i] = (trendTF4Raw[i] - median) / IQR;  // Robust scaling method    
   }
 
   
   for(i=0; i<limit; i++)       // THIRD ITERATION
   {
        slopeScaled[i] = 3; // Even this doesn't execute....why?????????  

   }
 
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }

If I move that line.....

slopeScaled[i] = 3;

.....up into the second iteration it will execute just fine, but putting it in the third iteration breaks it and I can't see why.

Any input would be appreciated.  Thanks in advance!

 

As we have no idea what are buffers and what may be arrays ( and the size of arrays and variables) I can only guess that you are getting an array out or range critical error.

Do you not see anything in the experts log?

 

Hi Keith,

The only place I know to look for errors is the journal tab in the Metatrader terminal and I'm not seeing any issues there.  Is there a better place to look for those types of errors?

I'm attaching the full file if you'd like to take a look at it.

Thanks again!

 
jt27 #:

Hi Keith,

The only place I know to look for errors is the journal tab in the Metatrader terminal and I'm not seeing any issues there.  Is there a better place to look for those types of errors?

As I said, the Experts log.

The Experts tab is right next to the journal tab.

 
      trendTF1[i] = iCustom(NULL, 0, "Super smoothed average trend 1.2", TimeFrame1, TF1_Period, 
                            0, TF1_MA_Method, false, false, true, true, false, false, false, clrLimeGreen, clrOrangeRed, clrDimGray, 3, true, 
                    2, i); // Mode 2 indicates direction 

You are mixing apples and oranges.