Custom Indicator - Start Function Called Numerous Times - How do I limit the indicator to run only when a new bar comes in?

 

I have a simple indicator whereby I check for a condition, and then send an email when the condition is hit in the "start" section of the indicator. It works fine, but when I add the indicator to my chart, it seems to get called every 1/3 second or so.

My goal is to call the routine only when a new bar comes in.

How do I limit the indicator to run only when a new bar comes in?

 
jsanders:

I have a simple indicator whereby I check for a condition, and then send an email when the condition is hit in the "start" section of the indicator. It works fine, but when I add the indicator to my chart, it seems to get called every 1/3 second or so.

My goal is to call the routine only when a new bar comes in.

How do I limit the indicator to run only when a new bar comes in?

Welcome to mql4.com forum,

An indicator run on each new tick. If you need to run some code on a new bar only, you have to check that you get a newbar each time your indicator's code is running.

Something like :

//--- check for new bar
   static datetime lastBarTime=Time[0];
   if(Time[0]==lastBarTime) return;

//--- new bar processing
   lastBarTime=Time[0];
   ...


 
angevoyageur:
static datetime lastBarTime=Time[0]; // Will not compile
But any variation,
static datetime currBarTime=0; datetime prevBarTime = currBarTime; currBarTime=Time[0];
bool isNewBar = currBarTime != prevBarTime;
if(isNewBar){ ...
is fine, except they also trigger on initial load, not just new bar.
 

This is what I've been using. It works fine for me.

datetime CurrentTime; // global variable

   if (CurrentTime != Time[0])
   {
      CurrentTime = Time[0];
      Print("Prints once per bar");
   }
 
WHRoeder:
static datetime lastBarTime=Time[0]; // Will not compile

Of course my code will compile, I never post code that doesn't compile.

 
angevoyageur: Of course my code will compile, I never post code that doesn't compile.
https://www.mql5.com/en/forum/150885
 
static datetime lastBarTime=Time[0];

Lol :)

This thing really compiles with current MQL4 compiler. I just inserted this into script, indicator and EA, inside and outside of functions and it compiles.

I did not tested if it works, though.

Reason: