Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Libraries | CDatetimeMsc

When I recently started to intensively test the functionality of CopyTicksRange(), I encountered a few unfavourable problems when handling millisecond times:

  1. I actually wanted to use CDateTime, since it is derived from MqlTimeStruct and you can edit the different time units very easily using the included increment and decrement methods.
  2. At the same time, I needed a way to smoothly translate millisecond times (seconds since 1 January 1970 times 1000 in ulong format) back and forth.

Here I encountered the difficulty that the number of milliseconds, for example, is a very vague number that does not tell you very much about the exact date. However, if you use CDateTime or TimeStruct, all the time units are easy to change, but the format tears them apart and the result is also very unclear. This CDateTimeMsc class was created to solve this problem.

#include <Tools\DateTime.mqh>

struct CDateTimeMsc: public CDateTime
  {
public:
   int               msc;               // additional variable for msc storage
   datetime          check_datetime;    // for convenient observation a little overhead is required.

   //--- CDateTimeMsc methods

   ulong             MscTime(void) {return(ulong(double(CDateTime::DateTime()) * 1000) + msc);}
   void              MscTime(ulong a_msc_time);  // input (datetime*1000)
   void              Msc(int value);
   //bool IsNumInt(const int num);
   void              MscDec(int delta = 1);
   void              MscInc(int delta = 1);
   void              SecTime(int a_int_time);
   ulong             SecTime(void);
   void              UpdateDateTime();   // updates the observation variable check_datetime
   void              DateTime(datetime a_datetime) {CDateTime::DateTime(a_datetime); UpdateDateTime();}
   datetime          DateTime(void) {return(CDateTime::DateTime());}

  };

The new methods were inserted under the inheritance of the old structure.

Now the class, which is actually a structure (see Structures/Classes), can also work with milliseconds, provided that such exact times are available.

Furthermore, the increment and decrement methods have been extended to the msc property.

One could now argue whether the additional variable "check_datetime" is really necessary, as it creates a certain overhead (for some people this is very important). However, I assumed that usually not many CDateTime objects are used, but only a few in the important places where time units such as hours, minutes, seconds or days need to be changed. I also had a few doubts about the UpDateTime() method, which is called unnecessarily with every increment/decrement.

All in all, however, the handling factor was the main focus.


The test
You no longer have to query the time with an additional datetime variable if you want to know the exact date, but can simply place the variable "check_datetime" under supervision: (TestEA see above)


obs2

The millisecond time was automatically translated into the datetime format.


48156

Best MetaTrader Indicators + Profitable Expert Advisors