Adding a time delay

 

I want my ea to wait for three bars after the close of the last trade before opening another one. How do I do this? I can't seem to get access to the close time of the last closed trade. anybody?

 

if you have the order's ticket number you can find the close time by selecting the order with OrderSelect (using SELECT_BY_TICKET and MODE_HISTORY to check closed orders) and then OrderCloseTime().

Stick this in a datetime variable, add 3 hours to it (3*60*60) and then each start, make it check if TimeCurrent() is >= to that variable.

 
alladir: if you have the order's ticket number you can find the close time by selecting the order with OrderSelect (using SELECT_BY_TICKET and MODE_HISTORY to
If you select by ticket, mode is irrelevant.
johnpea: I want my ea to wait for three bars after the close
Adding 3 hours to it is not the same as three bars (even on a H1 chart). Test for iBarShift of OrderCloseTime to be >= 3
 
Thanks guys - but I'm a bit new to this - you're way ahead of me. What would the code look like? I'm not sure how to get the ticket number - all I know is that it would be the most recent trade on my account.
 

Well, friendly advice... you must be way better at coding before you risk any money with your EA. I've been studying for nearly half a year and (as you can see from WHRoeder's reply) still not confident to risk real money yet. Best thing is search for CodersGuru's beginners' tutorial, its' really simple and useful and got me started. The next thing is read as much as you can stomach of this: https://book.mql4.com//. Also remember you can press F1 when you select any command in MetaEditor and it shows you exactly how to use it.

Basically, you will open an order with OrderOpen, which returns an integer variable which is the ticket number, so do it like this:

int ticket = OrderOpen(.....blah blah..);

After that, most order commands need you to select the order first so do:

OrderSelect(ticket);

and after that you can use OrderCloseTime which returns a datetime variable, and then add three hours to it. MT4 time counts the seconds, so add 3*60*60:

datetime sleeptime = OrderClose() + 10800;

Like WHR said, it's not that same thing as three hour bars, since the order might have closed in the middle of a bar. iBarShift finds the number of the bar in which OrderClose() is. Maybe that's what you want.

 
alladir:

Well, friendly advice... you must be way better at coding before you risk any money with your EA. I've been studying for nearly half a year and (as you can see from WHRoeder's reply) still not confident to risk real money yet. Best thing is search for CodersGuru's beginners' tutorial, its' really simple and useful and got me started. The next thing is read as much as you can stomach of this: https://book.mql4.com//. Also remember you can press F1 when you select any command in MetaEditor and it shows you exactly how to use it.

Basically, you will open an order with OrderOpen, which returns an integer variable which is the ticket number, so do it like this:

After that, most order commands need you to select the order first so do:

and after that you can use OrderCloseTime which returns a datetime variable, and then add three hours to it. MT4 time counts the seconds, so add 3*60*60:

Like WHR said, it's not that same thing as three hour bars, since the order might have closed in the middle of a bar. iBarShift finds the number of the bar in which OrderClose() is. Maybe that's what you want.


Ok, thanks - I've got it - don't worry, I'm not going to risk any money yet - I'm still very much in 'demo mode' and will be for some time to come...

Thanks for your help

Reason: