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--)
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);
}
}
}
//----
}
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;
}
}
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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;
}
}
//----