Closing an open order after 15 minutes

 

I have a quick question about closing an order after a set number of minutes/seconds.

My code works up until the point where I'm ready to close the order. If the order hasn't reached its TP or SL after 15 minutes, I want it to close no matter what.

There is only one order open at any given time and each ticket number is assigned to a variable such as "order_id_sell" or "order_id_buy".

How do I go about doing that?

Thanks for the help...

 
jmskates:

I have a quick question about closing an order after a set number of minutes/seconds.

My code works up until the point where I'm ready to close the order. If the order hasn't reached its TP or SL after 15 minutes, I want it to close no matter what.

There is only one order open at any given time and each ticket number is assigned to a variable such as "order_id_sell" or "order_id_buy".

How do I go about doing that?

Thanks for the help...

Hi jmskates,

You need to save the time when your order is opened


OrderSend(....);

datetime order_open_time = TimeCurrent();


And then check if 15 min passed like this:

if(TimeCurrent()>order_open_time+15*60) OrderClose(...);

 
robofx.org wrote >>

Hi jmskates,

You need to save the time when your order is opened

OrderSend(....);

datetime order_open_time = TimeCurrent();

And then check if 15 min passed like this:

if(TimeCurrent()>order_open_time+15*60) OrderClose(...);

I'll give it a try and let you know how it goes. Thanks for your help.

 
robofx.org wrote >>

Hi jmskates,

You need to save the time when your order is opened

OrderSend(....);

datetime order_open_time = TimeCurrent();

And then check if 15 min passed like this:

if(TimeCurrent()>order_open_time+15*60) OrderClose(...);

jmskates
wrote
>>

I'll give it a try and let you know how it goes. Thanks for your help.

Now I'm getting two different errors:

1. 2009.06.13 12:45:24 2009.06.09 20:07 EURUSD,M15: OrderClose error 4051

2. 2009.06.13 12:45:24 2009.06.09 20:07 EURUSD,M15: invalid ticket for OrderClose function

My code attempting to close the orders is below. Is there anything that stands out that would indicate why I am getting those two errors?

if((OrderSelect(order_id_buy, SELECT_BY_POS)==true) && (TimeCurrent()>order_buy_time+15*60) ) {
         OrderClose(order_id_buy,MM_LotSize,Ask,10,Red);
         
      }
      
      if((OrderSelect(order_id_sell, SELECT_BY_POS)==true) && (TimeCurrent()>order_buy_time+15*60)) {
         OrderClose(order_id_sell,MM_LotSize,Bid,10,Red);
      }

Thanks.

 
jmskates:

Now I'm getting two different errors:

1. 2009.06.13 12:45:24 2009.06.09 20:07 EURUSD,M15: OrderClose error 4051

2. 2009.06.13 12:45:24 2009.06.09 20:07 EURUSD,M15: invalid ticket for OrderClose function

My code attempting to close the orders is below. Is there anything that stands out that would indicate why I am getting those two errors?

Thanks.

Two things are wrong here:

1. You select the orders by position and then try to use this number in OrderClose() function. But OrderClose() needs order ticket!

2. If your opened trade is a buy trade you close it with Bid and if it is a sell trade you close it with Ask

Reason: