Start function every tick? - page 2

 
szgy74:

it is enough to calculate the things you want once per bar. that is why I mentioned new bar checking. there are several ways to check, below is an example.

this returns true when a new bar is formed (it happens after when the the next (#1) bar is completed).

Call your function twice during the same tick . . see what happens.
 

This works also:

//+------------------------------------------------------------------+
//| isNewBar()                                                       |
//+------------------------------------------------------------------+
bool isNewBar() 
{
   bool blnReturn = false;
   static int iLastBar = 0;
   if ( Bars != iLastBar ) 
   {
      iLastBar = Bars;     
      blnReturn = true;
   }
   
   return( blnReturn );
}

Is the MACD_sample.mq4 example distributed with MetaTrader then wrong?

There is no checking of a new bar.

The calculation of MACD is not shifted by 1.

When I include isNewBar in the example, backtesting results are different, the same if I shift by 1.

That's not good as example.

 
ilhany:

This works also:

It won't when Bars = Max bars on chart because the value of Bars will no longer increment. Always use time.

The MACD sample will perform these calculations for each tick . . .

   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
   MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
Reason: