Download MetaTrader 5
To add comments, please log in or register
They are already making profit from their signals. Publish your signal and join them!
jsanders
39
jsanders 2014.04.16 16:04 

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?

Alain Verleyen
Moderator
28561
Alain Verleyen 2014.04.19 22:26  
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];
   ...


whroeder1
13610
whroeder1 2014.04.20 00:48  
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.
MisterDog
380
MisterDog 2014.04.20 01:57  

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");
   }
Alain Verleyen
Moderator
28561
Alain Verleyen 2014.04.20 21:12  
WHRoeder:
static datetime lastBarTime=Time[0]; // Will not compile

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

whroeder1
13610
whroeder1 2014.05.15 11:20  
angevoyageur: Of course my code will compile, I never post code that doesn't compile.
https://www.mql5.com/en/forum/150885
Drazen Penic
2405
Drazen Penic 2014.05.15 12:26  
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.

/
To add comments, please log in or register