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

 
Alexey Viktorov #:

Well, I showed the full code of the function too. Again:

uchar WhatWeekDay(datetime t)
 {
  return uchar(t/(PeriodSeconds(PERIOD_D1))+4)%7;
 }

Although, I agree with you on a good point. Perhaps Nikolai wrote from memory and made an inaccuracy. But to say that it does not work is not quite right.

Semyon Semyonych!... Well, how so?... Well, it works, but not always... ))))))))

 
Mikola_2 #:

Nope, it's not working. (((

Yeah, sorry. I don't know how I messed up. Thank you.

Of course that's the right way to do it:

uchar WhatWeekDay(datetime t) {
   return uchar((t/(24*60*60)+4 )%7);
}
 
Alexey Viktorov #:

Well, I showed the full code of the function too. Again:

Although, I agree with you on a good point. Perhaps Nikolai wrote from memory and made an inaccuracy. But it is not quite right to say that it does not work.

void OnStart()
  {
   for(int i1=0; i1<100; i1++)
      Print(WhatWeekDay(i1*7*24*60*60));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
uchar WhatWeekDay(datetime t)
  {
   return uchar(t/(PeriodSeconds(PERIOD_D1))+4)%7;
  }

It doesn't always work, this is a test code. Result:

2024.02.10 17:51:47.584 2 (EURUSD,H1)   4
2024.02.10 17:51:47.584 2 (EURUSD,H1)   4
2024.02.10 17:51:47.584 2 (EURUSD,H1)   4
2024.02.10 17:51:47.584 2 (EURUSD,H1)   4
2024.02.10 17:51:47.584 2 (EURUSD,H1)   4
2024.02.10 17:51:47.584 2 (EURUSD,H1)   4

... 
2024.02.10 17:51:47.584 2 (EURUSD,H1)   4
2024.02.10 17:51:47.584 2 (EURUSD,H1)   4
2024.02.10 17:51:47.584 2 (EURUSD,H1)   4
2024.02.10 17:51:47.584 2 (EURUSD,H1)   4
2024.02.10 17:51:47.584 2 (EURUSD,H1)   0
2024.02.10 17:51:47.584 2 (EURUSD,H1)   0
2024.02.10 17:51:47.584 2 (EURUSD,H1)   0

... 
2024.02.10 17:51:47.584 2 (EURUSD,H1)   0
2024.02.10 17:51:47.584 2 (EURUSD,H1)   0
2024.02.10 17:51:47.584 2 (EURUSD,H1)   0
2024.02.10 17:51:47.584 2 (EURUSD,H1)   0
2024.02.10 17:51:47.584 2 (EURUSD,H1)   3
2024.02.10 17:51:47.584 2 (EURUSD,H1)   3
2024.02.10 17:51:47.584 2 (EURUSD,H1)   3
...

There should be fours everywhere

Again, the screen is correct :)
 
(uchar)((t/86400+4)%7)
 
amrali #
(uchar)((t/86400+4)%7)
😂
The 24*60*60 compiler converts 24*60*60 to 86400 even at the precompile stage.
So it doesn't affect performance.
The 24*60*60 record is just more readable from the point of view of the logic concept.
 

Forum on trading, automated trading systems and testing trading strategies

Features of the mql5 language, subtleties and methods of work

Aliaksandr Hryshyn , 2024.02.10 15:52

void  OnStart  () 
  { 
   for  ( int  i1= 0  ; i1< 100  ; i1++)
        Print  (WhatWeekDay(i1* 7  * 24  * 60  * 60  )); 
  } 
//+------------------------------------------------ - -------------------+ 
//| | 
//+------------------------------------------------ - -------------------+ 
uchar  WhatWeekDay( datetime  t) 
  { 
   return  uchar  (t/( PeriodSeconds  ( PERIOD_D1  )) + 4  )% 7  ; 
  } 

It is regarding the cast, not about 2 * 2 = 4 😉

uchar cast is modulo 256.

The above means (a % 256) % 7, which is different from (a % 7) % 256
 
amrali #:

It is regarding the cast, not about 2 * 2 = 4 😉

uchar cast is modulo 256.

The above means (a % 256) % 7, which is different from (a % 7) % 256
h ttps://www.mql5.com/ru/forum/170952/page243#comment_52244665
What's wrong here?
You must be looking at old posts and were too lazy to look ahead.
 
Nikolai Semko #:
What's wrong with this?

About that:

Print(uchar(257) % 7);   // 1
Print(257 % 7);          // 5
 
trader6_1 #:

About that:

Oh trouble

See posts above


 
Nikolai Semko # : https://www.mql5.com/ru/forum/170952/page243#comment_52244665

What's wrong here?
You are probably looking at old messages and are too lazy to look ahead.

Sorry, maybe because google auto translate