Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 767

 

Of course, the comparison is based on the number of seconds that have elapsed since the year 70, but this number of seconds takes into account everything - date and time.

I will just give you an example, which is essentially a simplification of a problem I recently encountered.

The task: to calculate the time (datetime) of end of trading on Friday, taking into account the string variable "21:30".

Initially I had such a solution:

datetime EndTrading;

if(TimeDayOfWeek(TimeCurrent())==5)     // если сейчас пятница, например возьмем сегодняшний день, первый тик 2014.11.07 00:00
  EndTrading=StringToTime("21:30");     // подразумевается что тут должно быть время пятницы...

if(TimeDayOfWeek(TimeCurrent())==5)     // если настала пятница
  if(TimeCurrent()>EndTrading)          // и время удовлетворяет условию
    Print("Настала пятница и текущее время больше чем 21:30");

And now the gimmick: the print specified in the code will pop up on Friday, but not after 21:30, but at 00:00 on the first tick.

Because when my broker's server got Friday, on my local PC it was still Thursday, timeshift is different.

In the tester, everything is OK and the print will be released as it should be on Friday after 21:30. Isn't that confusing?

 
tuner:

Of course, the comparison is based on the number of seconds that have elapsed since the year 70, but this number of seconds takes into account everything - date and time.

I will just give you an example, which is essentially a simplification of a problem I recently encountered.

The task: to calculate the time (datetime) of end of trading on Friday, taking into account the string variable "21:30".

Initially I had such a solution:

And now the gimmick: the print specified in the code will pop up on Friday, but not after 21:30, but at 00:00 on the first tick.

Because when my broker's server got Friday, on my local PC it was still Thursday, timeshift is different.

In the tester, everything is OK and the print will be released as it should be on Friday after 21:30. Isn't that confusing?

Of course it's confusing. It's just confusion in your head. TimeToString("21:30") will give the current date from 21:30:00 to 21:29:59 the next day. I'll correct the comments in your code.

datetime EndTrading;

if(TimeDayOfWeek(TimeCurrent())==5)     // если сейчас пятница, от текущего времени. Наступит когда по времени сервера будет 00:00
  EndTrading=StringToTime("21:30");     // А тут в 00:00 устанавливается четверг 21:30 потому, что это время пятницы ещё далеко.

if(TimeDayOfWeek(TimeCurrent())==5)     // если настала пятница
  if(TimeCurrent()>EndTrading)          // А тут условие если(текущее время больше чем четверг 21:30)
   {
    Print("EndTrading = ", TimeToString(EndTrading)); // Посмотри что будет напечатано.
   }


And this code will work correctly

if(TimeDayOfWeek(TimeCurrent()) == 5 && TimeCurrent() >= StringToTime("21:30"))
Print("Настала пятница и текущее время больше чем 21:30");
 
ikatsko:
There are two MT4 terminals on the same laptop: one with 4 decimal places and one with 5 decimal places. In the same time the traffic on the first one is 105/0 kb, on the second one 3450/0 kb. First one is CPU intensive by 6%, second one by 39%. What is the problem? Is this even a norm?
Yes.
 

AlexeyVik

Changed the date on my computer to 2000.

ran this script:

input string time="21:30";

Comment("TimeCurrent() = ",TimeCurrent(),"\n",
"TimeLocal() = ",TimeLocal(),"\n",
"StringToTime(",time,") = ",StringToTime(time));

and got this:

hence StringToTime() takes the date from the local computer

 
tuner:

AlexeyVik

Changed the date on my computer to 2000.

ran this script:

and got this:

So StringToTime() takes the date from the local computer

So what? Let it take it from the moon, as long as you set "from and to" time passed XXX seconds and this integer number we compare with number of seconds passed from 01.01.1970 of the needed time.

You'd better check this line

if(TimeDayOfWeek(TimeCurrent()) == 5 && TimeCurrent() >= StringToTime("21:30"))

where you had the problem. Today is Friday.

 
AlexeyVik:

You'd better check that line

where you had the problem. Today is Friday.

I checked, if the local computer has a lower date than the broker's date, then the printers are there,

but if the local computer has a higher date than the broker's, no printers.

i.e. the time has passed, i.e. "11:30".

in the first case TimeCurrent() is compared to 2014.11.06 11:30, and in the second caseto 2014.11.0811:30

PS corrected the highlighted

if(TimeDayOfWeek(TimeCurrent())==5)      // если сейчас пятница, от текущего времени. Наступит когда по времени сервера будет 00:00
   EndTrading=StringToTime("21:30");     // А тут в 00:00 устанавливается четверг 21:30 потому, что это время пятницы ещё далеко.

The first line is clear, the comment on the second line is not.

Why is Friday far away when it has already come? After all, the second line is only executed if it is Friday.

 
tuner:

I have checked, if the local computer has a lower date than the broker's date, then the printers are there,

and if the date on the local computer is higher than the broker's date, then there are no printers.

i.e. the time has passed, i.e. "11:30".

in the first case TimeCurrent() is compared to 2014.11.06 11:30, and in the second caseto 2014.11.0811:30

PS corrected the highlighted


Well here I have local time greater than server time by 1 hour.

This script

   datetime time_begin;
    time_begin = StringToTime("21:30");
    Comment("Текущее локальное время ", TimeLocal(), " ", TimeToString(TimeLocal(), TIME_DATE|TIME_SECONDS), "\n"
          , "Текущее время сервера    ", TimeCurrent(), " ", TimeToString(TimeCurrent(), TIME_DATE|TIME_SECONDS), "\n"
          , "Сегодня 21:30 по TimeLocal будет через    ", time_begin-TimeLocal(), " секунд или ", 
           TimeToString(time_begin-TimeLocal(), TIME_MINUTES|TIME_SECONDS), "\n"
          , "Сегодня 21:30 по TimeCurrent будет через ", time_begin-TimeCurrent(), " секунд или ", 
           TimeToString(time_begin-TimeCurrent(), TIME_MINUTES|TIME_SECONDS), "\n"
          );


Outputs the following values.

First the number of seconds elapsed from 01.01.1970 to the specified time, and then the time in the form we are used to.


On this basis, please explain what confuses you about these values?


Yes. In my second comment I was thinking about something else.

 
TheXpe
TheXpert:
да.
Thank you. Brevity is the sister of talent!
 

Hello.

There is a task like this. (can't attach it to a tick)

If a pending order triggers {then...}.

In my EA, I use Terminal.mqh to calculate orders.

Terminal // Mas_Tip[0] open Buy

// if the number of Buy ordershas increased by 1

if (Mas_Tip[0]+1)

{

function

}

Everything works. But it triggers on every next tick.

How to attach this case to a tick? And then compare the value on the previous tick and on the current tick.

 
AlexeyVik:

Well, my local time is 1 hour longer than the server time.

So, can you explain what is confusing about these values?

There is nothing confusing about these values, everything is correct there, but that is not what we are talking about.

The problem with Friday in particular is not relevant if your local time is X hours longer than the broker's time.

But imagine if on your local computer there is not +1, but -1 hour difference with the broker.

And there is this simple condition:

if(TimeDayOfWeek(TimeCurrent()) == 5 && TimeCurrent() >= StringToTime("21:30"))
   Print("Настала пятница и время больше чем 21:30");

In this case:

1. the broker will have the first tick of Friday, for example today's tick, which has a time of 2014.11.07 00:00

2. Take the number of seconds elapsed from01.01.1970 00:00 till the tick in question, i.e.2014.11.07 00:00

3. we take the number of seconds elapsed from 01.01. 1970 00:00 till06.11.2014 21:30 (it is yesterday's date, because we take the time of the local PC, which at the moment of the tick is not Friday, but Thursday, which is the 6th day, or to be more precise, 2014.11.06 23:00, the difference of 1 hour)

Voila, the number of seconds in point 2 is greater than in point 3.

This means that the first Friday tick will execute the print specified in the code, although if theStringToTime() function takes the date from the broker's server, then everything would work as planned, the print would pop up on Friday only after the time goes over21:30