Help needed to make indicator start calculation only after certain bars

 

I am still using the old way of coding as below:

int LimitOfCandle = 500;                                                            //The number of candle from the left to start the function of the indicator

int Counted_bars=IndicatorCounted();                                        // Number of counted bars
int i=Bars-Counted_bars-1;                                                         // Index of the first uncounted
   while(i>=0)                                                                             // Loop for uncounted bars
     {
         while (IndicatorCounted()>=LimitOfCandle && i>=0)  // Check if limit is hit to start calculation and Loop of number of bars counted
        {
         Start calculation only when the two conditions in the while operator are met.
        }             
      i--;                          // Calculating index of the next bar
     }

I have only come this far in my effort to try to code my strategy and already doubtful of myself. Any confirmation if I am on the right track would be nice ;) Thanks in advance

 
Help needed to make indicator start calculation only after certain bars
  1. Don't do that. Just compute what needs to be done.
              How to do your lookbacks correctly.

  2. You should stop using the old event handlers and IndicatorCounted and start using the new ones.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

 
int Counted_bars;                       // Number of counted bars
//--------------------------------------------------------------------
   Counted_bars = prev_calculated;        // Number of counted bars
   int i=rates_total-Counted_bars-1;      // Index of the first uncounted
   int x = 500;                           // Min of candle before indicator's purpose kicks in 
   while(i>=0)                            // Loop for uncounted bars
     {
        while (Counted_bars>=x && i>=0)    // Loop of number of bars counted
         { 
            int y;                         //Index of candle to lookback
            for (y=i+2; High[i]-High[y]>=0; y++)
            {
               if (High[y]>High[y+1] && High[y]>High[y-1])
               DrawArrowUp("Up"+Bars,High[i]+10*Point,Lime);
               break;
            }
         }              
      i--;                          // Calculating index of the next bar
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void DrawArrowUp(string ArrowName,double LinePrice,color LineColor)
{
ObjectCreate(ArrowName, OBJ_ARROW_SELL, 0, Time[0], LinePrice); //draw an up arrow
ObjectSet(ArrowName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(ArrowName, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
ObjectSet(ArrowName, OBJPROP_COLOR,LineColor);
}

void DrawArrowDown(string ArrowName,double LinePrice,color LineColor)
{
ObjectCreate(ArrowName, OBJ_ARROW, 0, Time[0], LinePrice); //draw an up arrow
ObjectSet(ArrowName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(ArrowName, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
ObjectSet(ArrowName, OBJPROP_COLOR,LineColor);
}

Hi William,


Thanks for the feedback. Did a prev_calculated and rates_total and have seen a couple of example of how they are used for looping so that the indicator will start from bar on the extreme left to right(time series). My intention is to code an indicator that plot a arrow when price break above a swing point(with lower high to the left and right of the candle) with the option to change the limit of the amount of candle to lookback(not sure is its the same as the "How to do your lookback correctly" you mention in your feedback.



Unfortunately MQL4/5 book itself is not really a beginner friendly let alone someone without programming back ground :(