How to close trades after a certain amount of time has passed

 

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

 

Bump... Trying to get some attention here

 

void CloseOrder(int minutes)

Maji:
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

Hi 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);

}

}

}

 
Maji:
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?

 
codersguru:
Do you mean you want to close a limited order (BUYSTOP,SELLSTOP,BUYLIMIT and SELLLIMIT) after x minutes they have placed?

Yes

Maji

 

Pending orders with expiration!

Maji:
Yes Maji

Maji,

I'm using this code to open a pending order and set its expiration time (in seconds).

extern double Expiration = 7200; // The time (in seconds) the order have to expire!

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

 
Maji:
Thank you Coder's guru. You really are a BIGGGG HELPPPP

You're welcome Maji!

 

close the current position when the price hits a moving average

Hello,

Does any expert know how to close the current position when the price hits a moving average?

 

Close Position(s) when Price hits MA

zhu28ming,

I am looking for the exact same function - please let me know if you find anything on this and I will do the same.

Thanks, Monty

Reason: