Quote:
|
Originally Posted by 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.
PHP 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);
}