Features of the mql5 language, subtleties and tricks - page 272

 

https://www.mql5.com/en/forum/363210#comment_20862779

Doesn't this work anymore? I can't get an empty field in place of the variable name after Left Alt and typing 0160. Maybe there is an alternative?

How to show a blank Variable In Inputs Setting?
How to show a blank Variable In Inputs Setting?
  • 2021.02.20
  • lengendmt4
  • www.mql5.com
Looks simple, but how to code this blank Variable? Thanks...
 

I have a question about ArraySort.

Функции ArraySort и ArrayBSearch принимают в качестве параметра массив любой размерности, при этом сортировка и поиск происходят только по первому (нулевому) измерению.

Is the zero dimension the first (zero) column? What is a dimension of an array in general?

 
Andrei Iakovlev ArraySort.

Is the zero dimension the first (zero) column? What is a dimension of an array in general?

One-dimensional array: array[ ];

Two-dimensional array: array[ ][ ];

Three-dimensional array array[ ][ ][ ][];

Thefirst, second, third dimensions are marked in colour.

I haven't heard about the zero dimension :)

S.F. Why did you have to delete it?

 
Artyom Trishkin #:

Haven't heard of zero-dimensionalisation :)

Itis written about zero measurement on the ArraySortpage.

Artyom Trishkin #:

Thefirst, second, third dimensions are marked with colour.

So the first dimension is the first line? What is a dimension?

Artyom Trishkin #:
. And why did you delete it?
I thought that the topic is not the right one.
 
Andrei Iakovlev #:
I thought it was the wrong topic.

Wrong topic indeed. I wasn't paying attention.

 
Hi, thank you EXPER.mqh is amazing. How can I activate "Algo trading" for EA opened with EXPERT::Run
 
fxsaber #:
In a piggy bank.

Highlighted the change. It is faster on the old CPU.

I made few changes to your code. Now, it looks cleaner and also executes faster.

void TimeToStruct2100( const datetime time, MqlDateTime &dt_struct )
{
  const uint t = (uint)time;
  const int n = (int)(t / (24 * 3600));
  const int tn = (n << 2) + 2;

  dt_struct.sec = (int)(t % 60);
  dt_struct.min = (int)(t / 60) % 60;
  dt_struct.hour = (int)(t / 3600) % 24;

  dt_struct.day_of_week = (n + THURSDAY) % 7;
  dt_struct.day_of_year = (tn % 1461) / 4;;

  dt_struct.year = (tn / 1461) + 1970; // to 2100
  const bool Flag = ((dt_struct.year & 3) == 0);

  static const int Months[13] = {0, -1, 30, 58, 89, 119, 150, 180, 211,242, 272, 303, 333};

  dt_struct.mon = (dt_struct.day_of_year < 31) ? 1 : ((dt_struct.day_of_year - Flag) * 5 + 166) / 153;
  dt_struct.day = dt_struct.day_of_year - Months[dt_struct.mon] - (Flag && dt_struct.mon >= 3);
//dt_struct.day = dt_struct.day_of_year - Months[dt_struct.mon] - (Flag && dt_struct.day_of_year > 59);

  return;
}
 
amrali #:

I have made a few changes to your code. Now it looks cleaner and runs faster.

That's very good. I can't measure it yet.

 

Also, for real life usage (not in synthetic benchmarks) the once-a-day caching can be beneficial to skip re-calculations of the same year, month and day.

The cache is done once per day. Each new call to the function within the same day will just update "hh:mm:ss" fields.

Other fields "yyyy/mm/dd" will be returned as is, from the saved cache.

Of course, all fields have to be re-calculated when passing a 'time' value from a different date.

void TimeToStruct2100( const datetime time, MqlDateTime &dt_struct )
{
  static const int Months[13] = {0, -1, 30, 58, 89, 119, 150, 180, 211,242, 272, 303, 333};
  static MqlDateTime last_result = {};
  static int last_days = 0;

  const uint t = (uint)time;
  const int n = (int)(t / (24 * 3600));
  const int tn = (n << 2) + 2;

  if (last_days != n)
  {
    last_days = n;
    last_result.day_of_week = (n + THURSDAY) % 7;
    last_result.day_of_year = (tn % 1461) / 4;;

    last_result.year = (tn / 1461) + 1970; // to 2100
    const bool Flag = ((last_result.year & 3) == 0);

    last_result.mon = (last_result.day_of_year < 31) ? 1 : ((last_result.day_of_year - Flag) * 5 + 166) / 153;
    last_result.day = last_result.day_of_year - Months[last_result.mon] - (Flag && last_result.mon >= 3);
  //dt_struct.day = dt_struct.day_of_year - Months[dt_struct.mon] - (Flag && dt_struct.day_of_year > 59);
  }

  dt_struct = last_result;
  dt_struct.hour= (int)(t / 3600) % 24;
  dt_struct.min = (int)(t / 60) % 60;
  dt_struct.sec = (int)(t % 60);

  return;
}
 

For the fastest replacement to the built-in TimeToStruct() function,

I suggest using this function :

bool TimeToStructFast(datetime time, MqlDateTime& dt_struct)
  {
   static const int Months[13] = {0, -1, 30, 58, 89, 119, 150, 180, 211,242, 272, 303, 333};
   uint t       = (uint)time;
   int  n       = (int)(t / (24 * 3600));
   int  tn      = (n << 2) + 2;
   int  dow     = (n + 4) % 7;
   int  doy     = (tn % 1461) / 4;
   int  year    = (tn / 1461) + 1970; // to 2100
   int  isleap  = ((year & 3) == 0);
   int  leapadj = ((doy < (isleap + 59)) ? 0 : (2 - isleap));
   int  mon     = ((((doy + leapadj) * 12) + 373) / 367);
   int  day     = doy - Months[mon] - (isleap && doy > 59);
   int  HH      = (int)(t / 3600) % 24;
   int  MM      = (int)(t / 60) % 60;
   int  SS      = (int)(t % 60);

   dt_struct.year           = year;
   dt_struct.mon            = mon;
   dt_struct.day            = day;
   dt_struct.hour           = HH;
   dt_struct.min            = MM;
   dt_struct.sec            = SS;
   dt_struct.day_of_week    = dow;
   dt_struct.day_of_year    = doy;
   return (true);
  }
//+------------------------------------------------------------------+
bool TimeToStructFast(datetime time, MqlDateTime& dt_struct)  // with once-a-day-cache
  {
   static const int Months[13] = {0, -1, 30, 58, 89, 119, 150, 180, 211,242, 272, 303, 333};
   static int last_days = 0;
   static MqlDateTime last_result = {};

   const uint t = (uint)time;
   const int  n = (int)(t / (24 * 3600));
   if (last_days != n)
     {
      int  tn      = (n << 2) + 2;
      int  dow     = (n + 4) % 7;
      int  doy     = (tn % 1461) / 4;
      int  year    = (tn / 1461) + 1970; // to 2100
      int  isleap  = ((year & 3) == 0);
      int  leapadj = ((doy < (isleap + 59)) ? 0 : (2 - isleap));
      int  mon     = ((((doy + leapadj) * 12) + 373) / 367);
      int  day     = doy - Months[mon] - (isleap && doy > 59);
      last_days = n;
      last_result.year          = year;
      last_result.mon           = mon;
      last_result.day           = day;
      last_result.day_of_week   = dow;
      last_result.day_of_year   = doy;
     }

   dt_struct = last_result;
   dt_struct.hour= (int)(t / 3600) % 24;
   dt_struct.min = (int)(t / 60) % 60;
   dt_struct.sec = (int)(t % 60);
   return (true);
  }

The speed-up factor is approx. 4-5x (for the version without cache).