How to Execute an Action only Once per Bar with MQL4

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

 

metatrader4-mql4-codeonlyonceperbar-1b

 

Why to 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 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 reasons may be different 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 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 the variable you will now if you already executed the code in the current candle.

The code could look like the this:

The result for the above is

metatrader4-mql4-codeonlyonceperbar-1

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 a condition to the if. Basically you will have two or more conditions to verify that

  1. Your condition is verified
  2. The code hasn’t been executed already in the bar

We can use the code above with a small difference

Conclusion

Whatever is the reason, 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 MetaTrader4 only using a couple of lines of MQL4 code.

I remember when I first started to learn MQL coding, although I had some knowledge of coding many concept were still unclear. To speed up your learning process I would recommend to follow a quick MQL course that will give you the fundamentals you need to create functional Trading Robot.

You can check out the following two course, Udemy often has sales on to make the courses even more affordable, and if you subscribe to my newsletter (top and bottom of the page) I will let you know when this happens.

MQL4 Programming for Traders: Build Robust Trading Robots!

Black Algo Trading: Build Your Trading Robot

Should you have any doubt or feedback please leave a comment below.

Please also remember to Follow and Like Us on Social Media.

If you want to save hours of research and coding and you want to see some professional code check this out! You can use it to build your own EA!

We use cookies to ensure that we give you the best experience on our website.