Sunday, July 16, 2017

MT4 - Detecting a New Bar

Often when I am coding a new EA (Expert Advisor) I only want to execute the code to look for a new trade setup on the start of a new bar.  The code below provides this function.

Function



bool IsNewBar()
{
   bool result = false;
   static datetime lastBarTime = NULL;
  
   if (lastBarTime == NULL)
   {
      lastBarTime = iTime(NULL, 0, 0);

     
      // always return false when the EA first starts
      // so as not to falsely trigger buys etc. 
      result = false;
   }
   else if (iTime(NULL, 0, 0) != lastBarTime)
   {
      lastBarTime = iTime(NULL, 0, 0);
      result = true;
   }
   return result;
}


Usage


if ( IsNewBar() ) {
   // Insert code to only run when a new bar has formed.
}





3 comments:

  1. You have done good work by publishing this article here. I found this article too informative, and also it is beneficial to enhance our knowledge. Grateful to you for sharing an article like thisforex brokers comparison

    ReplyDelete
  2. It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait to read. I am impressed with your work and skill. Thanks. Forex Mt4 Download

    ReplyDelete
  3. I really appreciate your work which you have shared here about the Finance. The article you have shared here is very informative and the points you have mentioned are very helpful. Thank you so much.Ethereum Price Eur site.

    ReplyDelete

MT4 - Detecting a New Bar

Often when I am coding a new EA (Expert Advisor) I only want to execute the code to look for a new trade setup on the start of a new bar.  T...