Closing orders after a certain time

 

I'm trying to code a function to close any trade after x time 

I wrote a code for mt4 and it worked perfectly but not working on mt5 because mt5 uses class ctrade , so can someone modify it,

Here's what i did with mql4;

void TimeBasedSL(string symbol, double TimeBasedSLPeriod,int mode=4) {
 bool result;
 double TimeBasedSL=0.0;
 string smode="unknown";
 if (mode==1) {smode="days"; TimeBasedSL = TimeBasedSLperiod * 86400;}//days
 if (mode==2) {smode="hours"; TimeBasedSL = TimeBasedSLperiod * 3600;}//hours
 if (mode==3) {smode="minutes"; TimeBasedSL = TimeBasedSLperiod * 60;}//minutes
 if (mode==4) {smode="bars"; TimeBasedSL = TimeBasedSLperiod * Period() * 60;}//bars
 
 for (int cnt=OrdersTotal()-1; cnt>=0; cnt--) {
  if (!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) continue;
   {
    if ((TimeCurrent() - OrderOpenTime()) >= TimeBasedSL) {
    
     while(IsTradeContextBusy()) Sleep(100);
     RefreshRates();
     
     if (OrderType()==OP_BUY) {
      result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),9999,clrRed);
           } //buy
     if (OrderType()==OP_SELL) {
      result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),9999,clrBlue);
           } //sell
    
      }
    }
  }
}
Documentation on MQL5: Standard Library / Trade Classes / CTrade
Documentation on MQL5: Standard Library / Trade Classes / CTrade
  • www.mql5.com
CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Abdullah Saleh Abdullah Alnuaimi:

Hey I'm new to mql coding and I'm trying to code a function to close any trade after x time 

I wrote a code for mt4 and it worked perfectly but I can't convert it to mt5 because mt5 uses class ctrade , so can someone modify it please,

Here's what i did with mql4;

Please create a freelance order. 

 
Abdullah Saleh Abdullah Alnuaimi:

Hey I'm new to mql coding and I'm trying to code a function to close any trade after x time 

I wrote a code for mt4 and it worked perfectly but I can't convert it to mt5 because mt5 uses class ctrade , so can someone modify it please,

Here's what i did with mql4;

Your approach is wrong! Search for e.g. trailing stop, open positions are continuously controlled ... Copy paste and amend one of these working examples.
 
I found the solution, thanks alot guys!
 
Abdullah Saleh Abdullah Alnuaimi #: I found the solution, thanks alot guys!

Don't do that. Someone searching might find this thread and still be clueless. What was the problem? What solved what?

How To Ask Questions The Smart Way. (2004)
     When You Ask.
          Follow up with a brief note on the solution.