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