Specifying the start and the end date of the trading

1.6k views Asked by At

I just added the ability to specify the start and end date of the trading to my EA, however, I discovered a weird behaviour!

If I have specified the starting date for trade from
2016.05.01 00:00:00
and the end date to
2016.05.10 00:00:00
the EA trades from 2015.05.02 00:00 to 2016.05.12 14:02
but if I specified the date from
2016.06.01 00:00:00 to 2016.06.10 00:00:00
it trades from
2016.06.01 00:00 to 2016.06.10 14:53

Here is my code:

   extern datetime StartDate = __DATETIME__;
   extern datetime EndDate   = __DATETIME__;

   int isTrading = 0;

   if (  StartDate < EndDate ) {
         if (  TimeCurrent() > StartDate
            && TimeCurrent() < EndDate
               ) isTrading = 1;
   } else {
         if (  TimeCurrent() > StartDate
            || TimeCurrent() < EndDate
               ) isTrading = 1;
   }

I don't know what is wrong here, any help will be much appreciated.

Thank you in advance.

1

There are 1 answers

4
user3666197 On

The simplest part:

Given the StartDate == 2016.05.01 00:00, there indeed the Market is closed ( and the call to TimeCurrent() function would return just some old datetime value from the last tick from the last week of April ).

As the Market is closed yet, no OnTick()-triggering event ( message ) will arrive ( the less any trading will ever be able to happen ) until 2016.05.02 ( Monday ) starts and the first "new" QUOTE message indeed arrives from the Server-side, based on some further details.

Documentation warns:

datetime TimeCurrent();
Returns the last known server time, time of the last quote receipt for one of the symbols selected in the "Market Watch" window. In the OnTick() handler, this function returns the time of the received handled tick. In other cases ( for example, call in handlers OnInit(), OnDeinit(), OnTimer() and so on ) this is the time of the last quote receipt for any symbol available in the "Market Watch" window, the time shown in the title of this window. The time value is formed on a trade server and does not depend on the time settings on your computer.


The few harder points:

  • GMT-offset of the Broker Server-side setup ( not a problem in very this server-side / server-side datetime perspective, otherwise yes )

  • FX-instrument ( _Symbol ) trading conditions ( Precious metals and Indices typically start with additional time-offsets and do not follow 24*5 Market Trading Hours of the Majors ).

  • Broker-specific changes of trading hours / outages announced in advance.

  • MetaTrader Terminal 4 [ Strategy Tester ] may behave surprisingly on incomplete data ( Gaps in History Centre ) and the documentation warns just "indirectly" about this:

    During testing in the Strategy Tester, TimeCurrent() is simulated according to historical data.