Bump... Trying to get some attention here
void CloseOrder(int minutes)
I believe Nich had posted a snippet to close a trade after X hours have passed since the trade was opened. However, this time I need help with the following:
How do I close opened order after "X" hours have elapsed since placing the order?
Hope one of the resident experts can lend me a helping hand.
MajiHi Maji,
Please try this function:
void CloseOrder(int minutes)
{
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if ((CurTime()-OrderOpenTime())>minutes*60)
{
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet);
}
}
}
Thanks
Thank you Coders guru for your code. However, it does not do what I need. The code you gave me closes trades X minutes after the trade is opened. However, I place buy and sell stop orders above the current price and I want to exit the trades X minutes from now... when the trades are placed. I use expiration to let the orders expire after some time.
Here is a snippet that I am trying to use. Is this correct? It works in backtesting, I think but would like your expert opinion.
thanks,
Maji
//Main body .. partial code
if(Time[0] >= exittime) CloseAll(MagicNumber);
if(CountTradesPlaced() < MaxTrades)
{
expiration=Time[0]+(LiveHour*60*60)+(LiveMin*60);
exittime=Time[0]+(MaxOpenTradeHours*60*60 + MaxOpenTradeMin*60);
....
...
}
// Close routine
void CloseAll(int MagicNum)
{
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNum)
{
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
}
}
}
Thank you Coders guru for your code. However, it does not do what I need. The code you gave me closes trades X minutes after the trade is opened. However, I place buy and sell stop orders above the current price and I want to exit the trades X minutes from now... when the trades are placed. I use expiration to let the orders expire after some time.
Here is a snippet that I am trying to use. Is this correct? It works in backtesting, I think but would like your expert opinion.
thanks,
Maji
//Main body .. partial code
if(Time[0] >= exittime) CloseAll(MagicNumber);
if(CountTradesPlaced() < MaxTrades)
{
expiration=Time[0]+(LiveHour*60*60)+(LiveMin*60);
exittime=Time[0]+(MaxOpenTradeHours*60*60 + MaxOpenTradeMin*60);
....
...
}
// Close routine
void CloseAll(int MagicNum)
{
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNum)
{
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
}
}
}
Do you mean you want to close a limited order (BUYSTOP,SELLSTOP,BUYLIMIT and SELLLIMIT) after x minutes they have placed?
Do you mean you want to close a limited order (BUYSTOP,SELLSTOP,BUYLIMIT and SELLLIMIT) after x minutes they have placed?
![](https://c.mql5.com/forextsd/smiles/confused_smile.png)
Yes
Maji
Pending orders with expiration!
Yes
![](https://c.mql5.com/forextsd/smiles/smile.png)
Maji,
I'm using this code to open a pending order and set its expiration time (in seconds).
extern int Level = 6; // How many pips above/under the current Bid/Ask price you want to open the pending order!
......
OpenPendingOrder(OP_BUYSTOP,Lots,Level,Slippage,StopLoss,TakeProfit,ExpertComment,MagicNumber,CurTime() + Expiration);
.......
int OpenPendingOrder(int pType=OP_BUYLIMIT,double pLots=1,double pLevel=5,int sp=0, double sl=0,double tp=0,string pComment="",int pMagic=123,datetime pExpiration=0,color pColor=Yellow)
{
int ticket=0;
int err=0;
int c = 0;
switch (pType)
{
case OP_BUYLIMIT:
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_BUYLIMIT,pLots,Ask-pLevel*Point,sp,(Ask-pLevel*Point)-sl*Point,(Ask-pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
case OP_BUYSTOP:
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_BUYSTOP,pLots,Ask+pLevel*Point,sp,(Ask+pLevel*Point)-sl*Point,(Ask+pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
case OP_SELLLIMIT:
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_SELLLIMIT,pLots,Bid+pLevel*Point,sp,(Bid+pLevel*Point)+sl*Point,(Bid+pLevel*Point)-tp*Point,pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
case OP_SELLSTOP:
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_SELLSTOP,pLots,Bid-pLevel*Point,sp,(Bid-pLevel*Point)+sl*Point,(Bid-pLevel*Point)-tp*Point,pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
}
return(ticket);
}
Thank you Coder's guru. You really are a BIGGGG HELPPPP
Thank you Coder's guru. You really are a BIGGGG HELPPPP
![](https://c.mql5.com/forextsd/smiles/teeth_smile.png)
![](https://c.mql5.com/forextsd/smiles/teeth_smile.png)
![](https://c.mql5.com/forextsd/smiles/regular_smile.png)
You're welcome Maji!
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I believe Nich had posted a snippet to close a trade after X hours have passed since the trade was opened. However, this time I need help with the following:
How do I close opened order after "X" hours have elapsed since placing the order?
Hope one of the resident experts can lend me a helping hand.
Maji