Advertisements
$ £ ¥
¥ £ $

How to Execute an Action Only Once per Bar with MQL4

When you read about a tick and MQL4, you learned that the tick is a trigger for a portion of code. You also learned that, generally, many ticks occur within a candlestick, whether it is a short-term or long-term timeframe. In many cases, you may want to execute some code only once in a bar or candlestick. In this guide, you will see how you can code so that a portion of your program only runs once for each bar.

MetaTrader 4 Code Execution Only Once per Bar

Why Run Some Code Only Once per Bar?

Without any control strategy, all the code in the OnTick() function of an expert advisor or OnCalculate() function of an indicator would be executed on every tick received. Implementing some control strategy will allow you to limit the execution of a portion of your code only once per each bar.

But you may ask yourself, why should I limit the execution of an action only once per bar? We can see some examples:

  • Submit only one order per bar.
  • Perform a trailing stop only once per bar.
  • Save resources recalculating some past values only once per bar.
  • Send a notification only once per bar.

The actual reasons may vary but the result is the same — you want to run some code only once per bar. We can see in the next section how to do this with MQL4.

How to Run Some Code Only When a New Bar Starts?

If you want to execute some code just after a bar is closed or a new one is starting, the code is very simple. You will be using the variable Time[0], which contains the start time of the current bar (or candle). You then will need to save the value of Time[0] in another variable when you execute some code. Comparing Time[0] with that variable, you will know if you already executed the code during the current candle.

The code could look like the this:

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

datetime LastActionTime = 0;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // Comparing LastActionTime with the current starting time for the candle.
   if (LastActionTime != Time[0])
   {
      // Code to execute once per bar.
      Print("This code is executed only once in the bar started ", Time[0]);
      
      LastActionTime = Time[0];
   }
}
//+------------------------------------------------------------------+

The execution result for the above is:

Once Per Bar Test Code Execution Result

How to Run Some Code Only Once per Bar?

If your aim is to run some code only once per bar but not at the opening of the bar, then you will need to add another condition to the if statement. Basically, you will have two or more conditions to verify that:

  • Your condition is met.
  • The code hasn't been executed already in the bar.

We can use the code above with a small difference:

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

datetime LastActionTime = 0;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // Comparing LastActionTime with the current starting time for the candle.
   if ((LastActionTime != Time[0]) && (YOUR_CONDITION))
   {
      // Code to execute once per bar.
      Print("This code is executed only once in the bar started ", Time[0]);
      LastActionTime = Time[0];
   }
}
//+------------------------------------------------------------------+

Conclusion

Whatever the reason is, you may want to run some of the MQL4 code in an expert advisor or indicator only once in a bar (or candle). This is quite easy to achieve in MetaTrader 4 by using only a couple of lines of MQL4 code.