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?
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]; ...
static datetime lastBarTime=Time[0]; // Will not compile
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"); }
static datetime lastBarTime=Time[0]; // Will not compile
Of course my code will compile, I never post code that doesn't compile.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?