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

 
Nikolai Semko #:

Then that's enough

you still need the month and year to calculate the day

Put the unit in the array.

last_result_d   = last_result_yd-dm[last_result_m]+1 -((last_result_yd>59)?isleap:0);
 
fxsaber #:

Put the unit into the array.

Oh, right. Thank you.
 

Forum on trading, automated trading systems and testing trading strategies

Peculiarities of mql5 language, subtleties and techniques of work

fxsaber, 2024.04.16 12:46 pm

Day of the month.

Finally, let's try to apply the obtained results. Not forgetting about leap features.

#define  DAY (24 * 3600)

// Работает до конца XXI-века.
int GetDayMonth2100( const datetime time )
{
  int time2 = (int)(time / DAY);
  const int Year = ((time2 << 2) + 2) / (365 * 4 + 1); // to 2100

  const bool Flag = ((Year & 3) == 2);
  time2 -= Year * 365 + ((Year + 2) >> 2);    
  
  const int Month = (time2 < 59) ? (time2 + Flag) / 31 + 1
                                 : ((time2 >= 90) ? (time2 * 500 + 1532 * 11) / (1532 * 10) : 3);
                                                  
  // Количество дней от начала года до начала месяца: январь - 0, февраль - 31, март - 59, ...
  static const int Months[] = {0, 0-1, 31-1, 59-1, 90-1, 120-1, 151-1, 181-1, 212-1 ,243-1, 273-1, 304-1, 334-1};
  
  return(time2 - Months[Month] + (Flag && (Month < 3)));
}

It seems to be faster.

#define  DAY (24 * 3600)

// Работает до конца XXI-века.
int GetDayMonth2100_2( const datetime time )
{
  int time2 = (int)(time / DAY);
  const int Year = ((time2 << 2) + 2) / (365 * 4 + 1); // to 2100

  const bool Flag = ((Year & 3) == 2);
  
  time2 -= Year * 365 + ((Year + 2) >> 2);    
//  time2 -= ((Year * 1461 + 1) >> 2) + Flag;    
  
  const int Month = (time2 + Flag) < 31 ? 1 : (time2 * 5 + 166) / 153;
                                                  
  // Количество дней от начала года до начала месяца: январь - 0, февраль - 31, март - 59, ...
  static const int Months[] = {0, 0-1, 31-1, 59-1, 90-1, 120-1, 151-1, 181-1, 212-1 ,243-1, 273-1, 304-1, 334-1};
  
  const int Res = time2 - Months[Month] + (Flag && (Month < 3));
  
  return(Res);
}
 
In a piggy bank.
void TimeToStruct2100_fxsaber( const datetime dt, MqlDateTime &dt_struct )
{
  const uint time = (uint)dt;
  
  dt_struct.sec = (int)(time % 60);
  dt_struct.min = (int)(time / 60) % 60;
  dt_struct.hour = (int)(time / 3600) % 24;
  
  dt_struct.day_of_year = (int)(time / (24 * 3600));
  dt_struct.day_of_week = (dt_struct.day_of_year + THURSDAY) % 7;
  
  const int Year = ((dt_struct.day_of_year << 2) + 2) / (365 * 4 + 1); // to 2100
  const bool Flag = ((Year & 3) == 2);
  
  dt_struct.day_of_year -= Year * 365 + ((Year + 2) >> 2);
//  dt_struct.day_of_year -= ((Year * 1461 + 1) >> 2) + Flag;
  
  dt_struct.year = Year + 1970;
  
  dt_struct.mon = (dt_struct.day_of_year + Flag) < 31 ? 1 : (dt_struct.day_of_year * 5 + 166) / 153;
  
  static const int Months[] = {0, 0-1, 31-1, 59-1, 90-1, 120-1, 151-1, 181-1, 212-1,243-1, 273-1, 304-1, 334-1};
  
  dt_struct.day = dt_struct.day_of_year - Months[dt_struct.mon] + (Flag && (dt_struct.mon < 3));
  
  dt_struct.day_of_year += Flag;
  
  return;
}

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

 
fxsaber #:
In the piggy bank.

Highlighted the change. It's faster on the old CPU.

+
But saving the day through statics is still necessary. In real (not random, as in the test) application the performance gain will be much higher.
 
Nikolai Semko #:
But saving the day through statics is still necessary. In real (not random, as in the test) application the performance gain will be many times higher.

Caching has nothing to do with the algorithm itself. In reality, TimeToStruct is rarely used, because NextTime approach is practiced in Expert Advisors.

Usually caching is used for interactive products - definition of time data under the mouse cursor. I.e. the niche is not for a standard MQ-Tester, but for GUI, where the speed of TimeToStruct is a matter of twenty.

Библиотеки: Control_Trade_Sessions
Библиотеки: Control_Trade_Sessions
  • 2024.01.31
  • Automated-Trading
  • www.mql5.com
Статьи и техническая библиотека по автоматическому трейдингу: Библиотеки: Control_Trade_Sessions
 
fxsaber # :

Caching has nothing to do with the algorithm itself. In reality, TimeToStruct is rarely used, because The NextTime approach is practiced in advisors.

Typically cached for interactive products - determining time data under the mouse cursor. Those. The niche is not for a regular MQ Tester, but for a GUI, where the speed of TimeToStruct is second nature.

Your Custom function is faster than TimeToStruct() 3-4x times, the usefulness is in extraction of separate calendar components (year, month, etc...).
 
This is also found in MT4, it depends on the specific server
 
fxsaber #:

In reality, TimeToStruct is rarely used

I agree in general, but just the other day there was a task when TimeToStruct was used about 1000 times per second. A small thing, of course, but a nice little thing.
I can assume that there will be other similar tasks. I made my own variant with caching.
 
Nikolai Semko #:
I agree in general, but just the other day there was a task when TimeToStruct was used about 1000 times per second. A small thing, of course, but a nice little thing.
I can assume that there will be other similar tasks. I made my own variant with caching.
1000 times per second ? I can't imagine how it's possible ?