need some help to clarify when objects update

 

Hi forum, had it recently pointed out to me by one of our esteemed moderators (THANK YOU RAPTOR)

That I writing my code such that my indicators were updating every tick.

Have just found the time to start looking at this and hopefully re writing some code to ease load on CPU, but I am a bit confused

I looked at documentation for IndicatorCounted (code, cut and pasted below) "The function returns the amount of bars not changed after the indicator had been launched last"

I also looked at a suggestion from Raptor:

//-- Comment("") Only update the objects if they need updating
if( iTime(NULL, PERIOD_H1, 1) == Bar1Time) return(0);

Bar1Time = iTime(NULL,PERIOD_H1,1);

Would someone mind stating in really simple terms, the difference here, and specifically how IndicatorCounted works

The text above suggests to me that if Bar1Time is the same as the previous hours time, do nothing (indicator is not recalculated and drawn)

That is you wont have the indicator update or the following loop proceed until 1 hour and 1 tick from the previous update

But I am unable in get the documentation into simple terms that allows me to understand exactly how IndicatorCounted works in practice

thanks as always

int start()
    {
     int limit;
     int counted_bars=IndicatorCounted();
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
     if(counted_bars>0) counted_bars--;
     limit=Bars-counted_bars;
  //---- main loop
     for(int i=0; i<limit; i++)
       {
        //---- ma_shift set to 0 because SetIndexShift called abowe
        ExtBlueBuffer[i]=iMA(NULL,0,JawsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
        ExtRedBuffer[i]=iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
        ExtLimeBuffer[i]=iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
       }
  //---- done
     return(0);
    }
 

IndicatorCounted returns the number of bars that the indicator has already made its calculations

You use it so that your indicator is not calculating data on every bar, every tick.

Usually you will code it so that it only recalculates the last completed and the current bar, but it will still do this every tick. So you might check for a new bar so it only calculates when a new bar is opened.

 
  1. Always count down so you're not repainting
  2. Don't decrement Contradictory information on IndicatorCounted() - MQL4 forum
  3. Don't look back past the last candle.
    int start()
        {
         int counted_bars=IndicatorCounted(),
             lookback = MathMax(JawsPeriod, MathMax(TeethPeriod, LipsPeriod));
         if(counted_bars < lookback) counted_bars = lookback;
         for(int i=Bars - 1 - counted_bars; i>=0; i--)
           {
            //---- ma_shift set to 0 because SetIndexShift called abowe
            ExtBlueBuffer[i]=iMA(NULL,0,JawsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
            ExtRedBuffer[i]=iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
            ExtLimeBuffer[i]=iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
           }

 
WHRoeder:
  1. Always count down so you're not repainting
  2. Don't decrement Contradictory information on IndicatorCounted() - MQL4 forum
  3. Don't look back past the last candle.

thank you both, I will try and learn this properly