Close an Order x bars after order opened?

 

Hello,


I was wondering if anyone knew of an easy way to automatically close an order 3(which I can change as an extern variable) bars after the order was opened.

I will have a stoploss in place as backup but the main idea is to open an order from a indicator and then close that order x bars after the trade was opened. Any help

would be greatly appreciated.


Thanks

 
convert411:

Hello,


I was wondering if anyone knew of an easy way to automatically close an order 3(which I can change as an extern variable) bars after the order was opened.

I will have a stoploss in place as backup but the main idea is to open an order from a indicator and then close that order x bars after the trade was opened. Any help

would be greatly appreciated.


Thanks

convert411,

When you place the order with OrderSend, save the time, for example: datetime OrderTime = Time[0];

Then you can check if X bars passed like that:

if(TimeCurrent()>(OrderTime+XBars*TimeFrame*60)) { close order }

Set TimeFrame=1 for 1 minute chart, TimeFrame=5 for 5 minute chart and so on..

XBars will be your extern int variable.

I have a couple of other methods in mind but that should work.


Good luck! 

 

If you save the time of a previous order, it may not work very well when the order is closed due to stop-loss, take-profit, or you decided to close it yourself, or there is a restart of the terminal in between.


If you use time-based calculations, they will not handle weekend gaps. The iBarshift function however, will take care of weekend gaps and give you only bars that actually exist.

 
blogzr3:

If you save the time of a previous order, it may not work very well when the order is closed due to stop-loss, take-profit, or you decided to close it yourself

Not quite true.. but you're right that it won't work properly only when terminal is restarted and when an order is opened just before the market closes for the weekend. iBarShift is a better option.

 
robofx.org:

Not quite true.. but you're right that it won't work properly only when terminal is restarted and when an order is opened just before the market closes for the weekend. iBarShift is a better option.

If you save the value of order-time, and the order gets closed by stop-loss, you now have an order-time variable but no order. So what is the order-time variable supposed to represent?

 
blogzr3:

If you save the value of order-time, and the order gets closed by stop-loss, you now have an order-time variable but no order. So what is the order-time variable supposed to represent?

If the order gets closed by stop loss there will be no order to select and no need do close it automatically.

 
robofx.org:

If the order gets closed by stop loss there will be no order to select and no need do close it automatically.

If you are restricted to one and only one order at a time, sure. If you find out the time from an OrderSelect then you wouldn't have this restriction and the other issues as well.


But of course, what you do with your EA is entirely up to you. :)

Reason: