MQL4 - automated forex trading   /  

Forum

Only process each bar once

Back to topics list To post a new topic, please log in or register

avatar
2
CppNovice 2008.01.15 18:05 
How would one go about guaranteeing that each bar is only processed once?

I have a routine that performs some counting on each bar of occurrences of a setup. I need to make sure that each bar is only processed once so that each "occurrence" of this setup is only counted once. I can only need to process each bar after the bar itself is closed ... not tick by tick.

Any ideas would be greatly appreciated.
article

Method of Determining Errors in Code by Commenting

The article describes a method of searching the errors in the MQL4 code that is based on commenting. This method is found to be a useful one in case of problems occuring during the compilation caused by the errors in a reasonably large code.


avatar
877
BarrowBoy 2008.01.15 18:27 

Use this - OTTOMH :)

if (Volume[0]>1) return(0); // quit AFTER first tick of new, allowing you to evaluate High[1] etc ONCE below this line


avatar
32
Automated 2008.01.15 18:54 

You could execute your code at the very first tick of a new bar ( i.e. right after the previous bar has closed ).

Here's a function that will return TRUE if a new bar has just formed:

// This function returns TRUE at the very first tick a bar i.e. after the previous bar has just closed
bool NewBar()
{
   if(PreviousBarTime<Time[0])
   {
      PreviousBarTime = Time[0];
      return(true);
   }
 
   return(false);    // in case if - else statement is not executed
}

you need to declare datetime PreviousBarTime at the beginning of your EA...

then in your code you could just use

if ( NewBar() )
{
...... code you need to be executed after a bar has closed here ....
}

thank you

automatedfx@gmail.com

Back to topics list  

To add comments, please log in or register