How to close an open order after X hours?

 

How do I close opened order after "X" hours? I can use expiration=X to close pending orders, but how can I close active positions after X hours that they have been opened. I have multiple simultaneous orders and open positions that open based on stop prices placed at various levels and they are triggered off at various times.

Hope some of the gurus can help me.

Maji

 
Maji:
How do I close opened order after "X" hours? I can use expiration=X to close pending orders, but how can I close active positions after X hours that they have been opened. I have multiple simultaneous orders and open positions that open based on stop prices placed at various levels and they are triggered off at various times.

Hope some of the gurus can help me.

Maji

You could insert this function into your code.

extern int MaxHours=3;

int YOUR_MAGIC_NUMBER_VARIABLE_HERE=298374;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

void AfterHours(){

for(int i=0;i<OrdersTotal();i++){

if(OrderSelect(i,SELECT_BY_POS)){

if(OrderSymbol()==Symbol() && OrderMagicNumber()== YOUR_MAGIC_NUMBER_VARIABLE_HERE){

if(CurTime() - OrderOpenTime() > MaxHours * (60 * 60) ){

if(OrderType()==OP_BUY){

OrderClose(OrderTicket(),OrderLots(),Bid,2,Red);

}

if(OrderType()==OP_SELL){

OrderClose(OrderTicket(),OrderLots(),Ask,2,Red);

}

}

}

}

}

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

AfterHours();

//----

return(0);

}
 

Thank you very much

 

What to add to this code...?

Hello, everyone!

I have three questions:

1. Can the code posted above be used all by itself as an EA, by simply putting the code into a plain text file and then renaming the text file with the .mq4 extension?

2. If I want to use this code/EA to close trades that have been placed by another different EA, does the Magic Number in this code have to match the Magic Number used in the other EA that placed the trades?

3. If I want this EA to, for example, close all trades after 24 hours after they have been opened, but to ignore Saturday and Sunday, what lines of code would I need to add? In other words, if I set MaxHours to 24 and if a trade was opened on Friday at a certain time, and I want this code/EA to close that trade on Monday at the same certain time, thus ignoring Saturday and Sunday, what lines of code would I need to add to this existing code posted above?

Thank You to anyone who can answer these questions!

 
4X1:
Hello, everyone!

I have three questions:

1. Can the code posted above be used all by itself as an EA, by simply putting the code into a plain text file and then renaming the text file with the .mq4 extension?

2. If I want to use this code/EA to close trades that have been placed by another different EA, does the Magic Number in this code have to match the Magic Number used in the other EA that placed the trades?

3. If I want this EA to, for example, close all trades after 24 hours after they have been opened, but to ignore Saturday and Sunday, what lines of code would I need to add? In other words, if I set MaxHours to 24 and if a trade was opened on Friday at a certain time, and I want this code/EA to close that trade on Monday at the same certain time, thus ignoring Saturday and Sunday, what lines of code would I need to add to this existing code posted above?

Thank You to anyone who can answer these questions!

1. Yes

2. You have to set your own MN.

3. It's possible, but why. Usually traders close all orders on Friday. If they use weekly or monthly charts, they don't count hours.

 

i opened a position on eur/usd and set the close position "max hours=1",

but then after an hour the position was not closed...

why ?

 

his code is ok....but he missed one word MODE_TRADES

copy the line


void AfterHours(){

for(int i=0;i<OrdersTotal();i++){

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){

if(OrderSymbol()==Symbol() && OrderMagicNumber()== YOUR_MAGIC_NUMBER_VARIABLE_HERE){

if(CurTime() - OrderOpenTime() > MaxHours * (60 * 60) ){

if(OrderType()==OP_BUY){

OrderClose(OrderTicket(),OrderLots(),Bid,2,Red);

}

if(OrderType()==OP_SELL){

OrderClose(OrderTicket(),OrderLots(),Ask,2,Red);

}

}

}

}

}

}
 
Amritendu Maji:

How do I close opened order after "X" hours? I can use expiration=X to close pending orders, but how can I close active positions after X hours that they have been opened. I have multiple simultaneous orders and open positions that open based on stop prices placed at various levels and they are triggered off at various times.

Hope some of the gurus can help me.

Maji

You must use a Magic Number.

If each order has its own expiration date, you should be constructing  like an Array of opened orders and then use OrderClose() or PositionClose() individually (you need to know if you are working with a Netting or a Hedging account).

Obviously this array is a weak "system" in a case of a Terminal crash, so I recommend you to save this on disk or onto the Global Terminal Variables.

;)

Documentation on MQL5: Global Variables of the Terminal
Documentation on MQL5: Global Variables of the Terminal
  • www.mql5.com
Global variables are kept in the client terminal for 4 weeks since the last access, then they will be deleted automatically. An access to a global variable is not only setting of a new value, but reading of the global variable value, as well.
 
Hefajatur Rahman:

his code is ok....but he missed one word MODE_TRADES

copy the line


From my understanding, MODE_TRADES is default - order selected from trading pool(opened and pending orders),
Reason: