How can I close all positions that are open after 24 hours?

 

Hi folks!


I was wondering if anyone can help with this simple problem. I set my EAs to run on the 1 hr timeframe, but I want all positions to close after 24 hours.. I've attached my code below and am still having a hard time transitioning from VBA.. Any help would be greatly appreciated. =)


Thanks!



//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if((iTime > OrderOpenTime()+1440)) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}
if(OrderType()==OP_SELL)
{
if((iTime > OrderOpenTime()+1440)) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
}
}
//----

 
heyarn:


Hi folks!


I was wondering if anyone can help with this simple problem. I set my EAs to run on the 1 hr timeframe, but I want all positions to close after 24 hours.. I've attached my code below and am still having a hard time transitioning from VBA.. Any help would be greatly appreciated. =)


Thanks!



//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if((iTime > OrderOpenTime()+1440)) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}
if(OrderType()==OP_SELL)
{
if((iTime > OrderOpenTime()+1440)) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
}
}
//----


heyarn, try the code bellow:


int timeout=24; // hours
for (int cnt=0; cnt<OrdersTotal(); cnt++) {
   OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if ((TimeCurrent()-OrderOpenTime())>=(60*60*timeout)) {
      if (OrderType()==OP_BUY) {
         OrderClose(OrderTicket(),OrderLots(),Bid,0,Red);
      }
      if (OrderType()==OP_SELL) {
         OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);
      }
   }
}


best regards,


figurelli@fxcoder.com

 

Hi,

If you want to close Orders,this is BAD loop.

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

GOOD loop is here.

for (int i = OrdersTotal() - 1; i >= 0; i--)

 
figurelli:


heyarn, try the code bellow:



best regards,


figurelli@fxcoder.com


Hi Figurelli,


Thanks so much for your help, I really appreciate it! However, it doesn't work in testing. I think it's because the last known server time is modeled in testing.. So I modified it using iTime instead.. It didn't work.. Any suggestions? =)


int timeout=4; // hours
for (int cnt=0; cnt<OrdersTotal(); cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if ((iTime("GBPUSD",PERIOD_H1,0)-OrderOpenTime())>=(60*60*timeout)) {

if (OrderType()==OP_BUY) {
OrderClose(OrderTicket(),OrderLots(),Bid,0,Red);
}
if (OrderType()==OP_SELL) {
OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);
}
}
}
//----
}

 

What is iTime? why 1440? you should drop your time limit to seconds not just minutes.


For example :

if(OrderOpenTime()<=TimeCurrent()-24*60*60) OrderClose(...);


Hope this helps

 
devilian1899:

What is iTime? why 1440? you should drop your time limit to seconds not just minutes.


For example :


Hope this helps



Thanks Devilla, It's working now!

 
figurelli:


Hi,

How can I modify this code in order to be able to modify ( not close) the order to Breakeven point after xxx hours ? Thanks.


int timeout=24; // hours


for (int cnt=0; cnt<OrdersTotal(); cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if ((TimeCurrent()-OrderOpenTime())>=(60*60*timeout) && OrderProfit() <= 0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
return;

}
}