1 (edited by geektrader 2018-09-24 09:54:05)

Topic: Possible logical error in MQ4 code for session management

Hi Mr. Popov,

I´ve noticed that EA Studio backtests (in the App) still close positions if the exit indicator has given an exit signal, even if it´s outside of the session. For example, my sessions always start at 00:05 and EA Studio (app) hence never opens any position at 00:00 - which is correct. However, it still closes positions at 00:00 in the backtest. I am OK with that, but the exported MQ4´s act different, they do not exit outside of the session because the session check comes before the close check in the MQ4 code, see:

void OnBar()
  {
   UpdatePosition();

   if(posType!=OP_FLAT && IsForceSessionClose())
     {
      ClosePosition();
      return;
     }

   if(IsOutOfSession())
      return;

   if(posType!=OP_FLAT)
     {
      ManageClose();
      UpdatePosition();
     }

   if(posType!=OP_FLAT && isTrailingStop)
     {
      double trailingStop=GetTrailingStop();
      ManageTrailingStop(trailingStop);
      UpdatePosition();
     }

   if(posType==OP_FLAT)
     {
      ManageOpen();
      UpdatePosition();
     }
  }

To make it match the backtest behavior of the EA Studio backtests, it would have to be:

void OnBar()
  {
   UpdatePosition();

   if(posType!=OP_FLAT && IsForceSessionClose())
     {
      ClosePosition();
      return;
     }

   if(posType!=OP_FLAT)
     {
      ManageClose();
      UpdatePosition();
     }

   if(posType!=OP_FLAT && isTrailingStop)
     {
      double trailingStop=GetTrailingStop();
      ManageTrailingStop(trailingStop);
      UpdatePosition();
     }


   if(IsOutOfSession())
      return;

   if(posType==OP_FLAT)
     {
      ManageOpen();
      UpdatePosition();
     }
  }

If the session check is done like this in the MQ4 code, it will replicate the behavior of EA Studio backtests correctly.


Thank you,

Geek

Re: Possible logical error in MQ4 code for session management

Interesting observation!

Probably it is better to fix the EA Studio behaviour.

I'm planning an update next week. I'll try to fix that also.

Thank you for the report!

Re: Possible logical error in MQ4 code for session management

Possibly that would make the most sense to fix it in EA Studio, although that will "screw up" some of my existing strategies....

Re: Possible logical error in MQ4 code for session management

GeekTrader, I cannot reproduce the issue.

Please make a screenshot of the "Trading session" page and attach a simple expert for further examination.

Re: Possible logical error in MQ4 code for session management

Hi :-) I will do that this evening most likely and post it here.

6 (edited by geektrader 2018-10-01 18:37:23)

Re: Possible logical error in MQ4 code for session management

Here is the example. The strategy (on H1) used is just a dummy I´ve thrown together (unprofitable), you can recreate it with any strategy that trades something really.

Settings show that the session is from 01:00 to 23:58:

https://preview.ibb.co/iztHuK/picture01.jpg

While it never enters trades at 00:00 (which is correct), it closes trades at 00:00 several times:

https://preview.ibb.co/b7mRoe/picture02.jpg

As already noticed, in the exported MQ4 code of such strategies, it never closes trades at 00:00 because the session check is implemented correctly there.

Let me know if you need more information. Thanks smile

Re: Possible logical error in MQ4 code for session management

Here is another example comparing the EA Studio backtest (Session is again 01:00 - 23:58) to the exported MQ4, as you can see, EA Studio closes at 00:00 (inside the "forbidden time"), MT4 closes (correctly) at 05:00:

https://preview.ibb.co/e9q8Je/picture04.jpg

Re: Possible logical error in MQ4 code for session management

Hi Popov,

has this been fixed already in one of the latest updates? Just wondering.

Thanks.

Re: Possible logical error in MQ4 code for session management

Just checked and the bug is still there :-(

Re: Possible logical error in MQ4 code for session management

Popov, can you do something about it please? It really is unreliable because the backtests in EAS / MT4 differ because of that issue and EAS finds strategies that won´t work live because it ignores the session times for the exits. Thanks so much :-)

Re: Possible logical error in MQ4 code for session management

Just to make this complete: this bug has been fixed today by Popov. Thank you :-)