How to Execute an Action only Once per Bar with MQL4
When you learn in regards to the tick and MQL4 you discovered that the tick is a set off for a portion of code. You additionally discovered that usually there are extra ticks taking place in a candlestick, whether or not this can be a quick or lengthy timeframe. In many instances it’s your decision to execute some code only as soon as in a bar or candlestick. In this text you will note how one can code so {that a} portion of your program only runs as soon as for every bar.
Why to run some code only as soon as per bar
Without any management technique all of the code within the OnTick() operate of an knowledgeable advisor, or OnCalculate() operate of an indicator could be executed on each tick obtained. Implementing some management technique will enable you to restrict the execution of a portion of code only as soon as per every bar. But you could ask your self, why ought to I restrict the execution of an motion only as soon as per bar? We can see some examples:
- Submit only one order per bar
- Perform a trailing cease only as soon as per bar
- Save sources recalculating some previous values only as soon as per bar
- Send a notification only as soon as per bar
The causes could also be totally different however the consequence is identical, you need to run some code only as soon as per bar. We can see within the subsequent part how to do that with MQL4.
How to run some code only when a brand new bar begins
If you need to execute some code simply after a bar is closed, or a brand new is beginning, the code could be very easy. You can be utilizing the variable Time[0] which comprises the beginning time of the present bar (or candle). You then will want to save the worth of Time[0] in one other cariable whenever you execute some code. Comparing Time[0] with the variable you’ll now in the event you already executed the code within the present candle. The code may seem like the this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | //+——————————————————————+ //| Demo-1.mq4 |
//| Luca Spinello | //| https://mql4tradingautomation.com | //+——————————————————————+ #property copyright “Luca Spinello” #property link "https://mql4tradingautomation.com"
#property version "1.00" #property strict datetime LastActiontime; //+——————————————————————+ //| Expert initialization function | //+——————————————————————+ int OnInit() { //— //— return(INIT_SUCCEEDED); } //+——————————————————————+ //| Expert deinitialization function | //+——————————————————————+ void OnDeinit(const int reason) { //— } //+——————————————————————+ //| Expert tick function | //+——————————————————————+ void OnTick() { //— //Comparing LastActionTime with the current starting time for the candle if(LastActiontime!=Time[0]){ //Code to execute once in the bar Print(“This code is executed only once in the bar started “,Time[0]); LastActiontime=Time[0]; } } //+——————————————————————+ |
The consequence for the above is
How to run some code only as soon as per bar
If your purpose is to run some code only as soon as per bar however not on the opening of the bar then you will have to add a situation to the if. Basically you’ll have two or extra situations to confirm that
- Your situation is verified
- The code hasn’t been executed already within the bar
We can use the code above with a small distinction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | //+——————————————————————+ //| Demo-1.mq4 |
//| Luca Spinello | //| https://mql4tradingautomation.com | //+——————————————————————+ #property copyright “Luca Spinello” #property link "https://mql4tradingautomation.com"
#property version "1.00" #property strict datetime LastActiontime; //+——————————————————————+ //| Expert initialization function | //+——————————————————————+ int OnInit() { //— //— return(INIT_SUCCEEDED); } //+——————————————————————+ //| Expert deinitialization function | //+——————————————————————+ void OnDeinit(const int reason) { //— } //+——————————————————————+ //| 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 in the bar Print(“This code is executed only once in the bar started “,Time[0]); LastActiontime=Time[0]; } } //+——————————————————————+ |
Conclusion
Whatever is the rationale, it’s your decision to run a few of the MQL4 code in an Expert Advisor or Indicator only as soon as in a bar (or candle). This is kind of straightforward to obtain in MetaTrader4 only utilizing a few traces of MQL4 code. I bear in mind once I first began to study MQL coding, though I had some information of coding many idea had been nonetheless unclear. To pace up your studying course of I might suggest to observe a fast MQL course that provides you with the basics you want to create practical Trading Robot. You can take a look at the next two course, Udemy typically has gross sales on to make the programs much more inexpensive, and in the event you subscribe to my e-newsletter (prime and backside of the web page) I’ll let when this occurs.
MQL4 Programming for Traders: Build Robust Trading Robots!
Black Algo Trading: Build Your Trading Robot
Should you’ve gotten any doubt or suggestions please depart a remark beneath. Please additionally bear in mind to Follow and Like Us on Social Media.
If you need to save hours of analysis and coding and also you need to see some skilled code verify this out! You can use it to construct your individual EA!
How to Execute an Action only Once per Bar with MQL4