Simple EA closing all trades at specific time

 

Hi people!

I am still a newbie (35 days) of MQL5, so please be patient.

I assembled a simple EA with the Wizard -> Expert Advisor (generate), using intraday time filter (ITF), trailing and fixed trailing volume. However, I want all trades be closed as 16:00, and decided to change the code:

void OnTick()
  {
      MqlDateTime now;
      datetime current_time=TimeCurrent();
      TimeToStruct(current_time,now);
      if (PositionsTotal() > 0)
      {
         if (now.hour > 15)
            {
               CloseAllPositions();
            };
      };
      ExtExpert.OnTick();
  }


Calling a function:

void CloseAllPositions()
{
   CTrade c_trade;
   for(int i=PositionsTotal()-1;i>=0;i--)
   {
      int ticket=PositionGetTicket(i);
      c_trade.PositionClose(i);
   }
}


It does not seem to be working... Any ideas?

Files:
Timer.mq5  8 kb
 
Arthur Albano:

Hi people!

I am still a newbie (35 days) of MQL5, so please be patient.

I assembled a simple EA with the Wizard -> Expert Advisor (generate), using intraday time filter (ITF), trailing and fixed trailing volume. However, I want all trades be closed as 16:00, and decided to change the code:


Calling a function:


It does not seem to be working... Any ideas?

Nice and clean question asking. :like:
c_trade.PositionClose(i)

That function can be called by passing a symbol name (a string), or a ticket number. you are passing the index of the position.

 
Code2219 or probably 2319:
Nice and clean question asking. :like:

That function can be called by passing a symbol name (a string), or a ticket number. you are passing the index of the position.

 
c_trade.PositionClose(ticket);


void CloseAllPositions()
{
   CTrade c_trade;
   for(int i=PositionsTotal()-1;i>=0;i--)
   {
      int ticket=PositionGetTicket(i);
      c_trade.PositionClose(ticket);
   }
}
 
I want all trades be closed as 16:00

I would use OnTimer() to determine when 16:00 occurred. With OnTick, you must wait until a tick comes in.

Also, this may be of use:

https://www.mql5.com/en/docs/standardlibrary/expertclasses/expertbaseclasses/cexpert/cexpertcloseall

 
Anthony Garot:

I would use OnTimer() to determine when 16:00 occurred. With OnTick, you must wait until a tick comes in.

Also, this may be of use:

https://www.mql5.com/en/docs/standardlibrary/expertclasses/expertbaseclasses/cexpert/cexpertcloseall

Great! But I still prefer OnTick() so I can simulate better on Strategy Tester, albeit it makes the code sluggish. I will try as you said.

 
Anthony Garot:

I would use OnTimer() to determine when 16:00 occurred. With OnTick, you must wait until a tick comes in.

Also, this may be of use:

https://www.mql5.com/en/docs/standardlibrary/expertclasses/expertbaseclasses/cexpert/cexpertcloseall

OnTimer()
{
   MqlDateTime TradeServerTime;
   TimeTradeServer(TradeServerTime);
   (int(TradeServerTime.hour)+int(TradeServerTime.min)/60) > 16.15 && (OrdersTotal() || PositionsTotal) ? expert.CloseAll()
}

Like this?

 
Arthur Albano:

Hi people!

I am still a newbie (35 days) of MQL5, so please be patient.

I assembled a simple EA with the Wizard -> Expert Advisor (generate), using intraday time filter (ITF), trailing and fixed trailing volume. However, I want all trades be closed as 16:00, and decided to change the code:


Calling a function:


It does not seem to be working... Any ideas?

Warning: I now use MqlDateTime TradeServerTime; instead of
TimeCurrent();

Because it will use local time, instead of trade server time

TimeTradeServer();
 
Arthur Albano:
Warning: I now use MqlDateTime TradeServerTime; instead of

Because it will use local time, instead of trade server time

it's not local time.
these are 3 different times. (though can be same) TimeLocal() , TimeCurrent() , TimeTradeServer()

Reason: