MQL4 On New Bar

From this guide, you can learn how to check if the price is first on a new bar with MQL4.

When you are programming an indicator or an expert advisor, in many cases, you want to execute some task at the opening of a new bar.

For many beginners, this can seem like a difficult operation, but you will see that it isn't. In this guide, you will see with the MQL4 on new bar code that it is a very simple operation.

Check New Bar in MQL4

In trading automation, often, you want to run some tasks or checks when one bar closes and a new bar begins. Some example of operations to run at the start of a new candle are:

  • Send a notification regarding the status of the last closed bar.
  • Assess an indicator to check if you should open an order.
  • Assess an indicator to check if you should close an order.
  • Update the take-profit or stop-loss levels.

These are very frequent tasks that usually should be performed once per candle when a new bar starts.

Coding such types of behavior is not difficult and only requires a few lines of code. You can see the actual code example below.

// Creating a variable to store the time when a new candle start.
// Populating the variable with the current server time.
datetime NewCandleTime = TimeCurrent();

// Creating a boolean function that returns:
// True, if this is a new candle;
// False if this candle isn't new.
// Remember that the function returns true every time you initialize the script.
// For example, when you load the indicator or EA to the chart or when you switch timeframes.
bool IsNewCandle()
{
   // If the time of the candle when the function ran last
   // is the same as the time this candle started,
   // return false, because it is not a new candle.
   if (NewCandleTime == iTime(Symbol(), 0, 0)) return false;
   
   // Otherwise, it is a new candle and we need to return true.
   else
   {
      // If it is a new candle, then we store the new value.
      NewCandleTime = iTime(Symbol(), 0, 0);
      return true;
   }
}

void OnTick()
{
   if (IsNewCandle())
   {
      // Tasks to execute at the start of the new candle...
      // ...
   }
}

You can see how if we remove all the code commentaries from the snippet above, it all boils down just a few lines of code really.

datetime NewCandleTime = TimeCurrent();
bool IsNewCandle()
{
   if (NewCandleTime == iTime(Symbol(), 0, 0)) return false;
   else
   {
      NewCandleTime = iTime(Symbol(), 0, 0);
      return true;
   }
}

void OnTick()
{
   if(IsNewCandle())
   {

   }
}

Generally, to check whether you are on a new bar via MQL4, the program logic is the following:

  1. Create a variable and store the current server time in it.
  2. Create a function that checks the time and returns true if it is a new candle and false if not.
  3. Compare the variable's contents with the start time of the current candle.
  4. If the time is the same, then it is the same candle; if it is different, then we are in a new candle.
  5. If it is a new candle, return true and update the time variable.
  6. In the OnCalculate() or OnTick() functions, use if (IsNewCandle()) { } to execute the task you need to execute in the new candle.

It is worth remembering that IsNewCandle() would return true every time the function is initialized. For example, the first time it runs or when you change the symbol or timeframe of the chart.

Conclusion on MQL4 On New Bar

As you can see in the code above, checking if the candle is new is not hard at all. With only a few changes you can adapt the code to your needs. Coding an MQL4 "On New Candle" routine is simple and can resolve several headaches.

If you want to save hours of research and coding and if you want to see some professional code, you can have a look at our MT4 Expert Advisor Template. You can even use it to build your own EA!


If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.

© 2005–2021

EarnForex.com

Design — Mart Studio

Forex trading bears intrinsic risks of loss. You must understand that Forex trading, while potentially profitable, can make you lose your money. Never trade with the money that you cannot afford to lose! Trading with leverage can wipe your account even faster.

CFDs are leveraged products and as such loses may be more than the initial invested capital. Trading in CFDs carry a high level of risk thus may not be appropriate for all investors.