only when new bar arrives

samjesse

Active Trader
Aug 30, 2011
118
0
27
Hi

I am trying to quite OnCalculate after the indicator is plotted and not to execute the code block unless a new bar is received.

the following seams not to cut it.
Any ideas ples. thx

MQL5:
int OnCalculate(const int rates_total,...
  {
//---
   if(!isNewBar()) 
      return(0); 
//do rest of block.

MQL5:
bool isNewBar()
{
 
   MqlRates rates[];
   static datetime dt_current;
   static datetime dt_prev;
 
   int copied=CopyRates(NULL,0,0,1,rates);
   if(copied<=0) Alert("Error copying price data inside isNewBar",GetLastError());
   //else Alert("Copied ",ArraySize(rates)," bars");
   if(!dt_current) 
   {
      dt_current = rates[0].time;
      dt_prev = rates[0].time;
      return 1;
   }
   else if(dt_current == dt_prev)
   {
      return 0;  
   }
   else
      return 1;
}
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
18,530
1,355
144
Odesa
www.earnforex.com
There's a much easier way of doing it:

Global variable:
MQL5:
int LastBars = 0;

Somewhere at the top of OnCalculate() function:
MQL5:
	if (LastBars == rates_total) return(rates_total);
	else LastBars = rates_total);
 
Last edited: