Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1593

 
MrBrooklin execution time.

Almost a second (highlighted in yellow).

Hello, Vladimir.

If I understand correctly, you have highlighted in yellow not the execution time but the time for compiling this code. Compilation is performed once, after that you run the already compiled file on the chart or in the tester. That's why you may not worry much about this indicator. It does not reflect how slow or fast the Expert Advisor will work on a chart.

 
Yuriy Bykov #:
you didn't allocate runtime, you allocated time to compile this code.
Exactly!
 
Yuriy Bykov #:

Hello, Vladimir.

If I understand correctly, you have highlighted in yellow not the execution time but the compilation time of this code. Compilation is performed once, after that you run the already compiled file on the chart or in the tester. That's why you may not worry much about this indicator. It does not reflect how slow or fast the Expert Advisor will work on a chart.

Hello, Yuri, will the compilation time of this code be much different from the execution time? To be honest, I am not good at this issue. It is good that if it is so , as you wrote. ) I decided to check the compilation time of my code again right now and suddenly got a completely different result:

Test_3.mq5                      
code generated  
0 errors, 0 warnings, 442 msec elapsed, cpu='X64 Regular'

Now I don't understand at all why there is such a significant difference in compilation time compared to yesterday's data. ))

Regards, Vladimir.

 
MrBrooklin #:

Hello, Yuri, will the compilation time of this code be much different from the execution time? Frankly speaking, I am not good at this issue. It is good that if it is so , as you wrote. )
I decided to check the compilation time of my codeagain right nowand unexpectedly got a completely different result:

Compilation, roughly speaking, is the process of converting the code you have written in MQL5 into commands encoded with zeros and ones, which can be executed by the processor. It is performed by MetaEditor when you press the "Compile" button. Therefore, the duration of this process depends mainly on the number of lines in the written programme and the .mqh library files attached to it. But also the current load of your computer by other background processes at the moment of compilation affects the duration. For example, if you are listening to music in parallel (on the same computer), then playing it requires a part of computational resources, which can be taken away from the compilation process. But besides music, there are usually dozens of other processes launched by the operating system running in the background . At different times these processes create different load on the processor, so the duration of compilation can be different at different times.

When launching a programme, for example, an EA in the terminal, there is no recompilation. Therefore, the compilation time is not added to the execution time at each launch. This is the difference between compiled languages and interpreted languages.

As to whether there is a dependence between compilation time and runtime, imagine this example:

int OnInit() {
   double x = 0.0;
   while (x < 100) {
      x = x - 0.1;
   }
   return 0;
}

In terms of the number of operations that need to be encoded during compilation, they are very few: a few arithmetic operations, conditional operations and jump operations. That's why compilation of such a code will take very little time. And when you start executing such a code, the while loop will be infinite because the value of x only decreases and will never become more than 100. That's why the execution time can be as long as you like.

It is clear that this is an intentionally exaggerated example. However, in real programmes, it often happens that a rather compact source code requires a considerable amount of time to execute.

 
Yuri wrote everything correctly, there is no connection between these times. But the desire to make the code easier is great. Since the fate of the EA is long-term optimisation, we need to achieve maximum speed. Eliminate all the loops, prepare data in such a way that it can be used on the next tick without loops. Or almost. That's it.
 
MrBrooklin execution time.

Almost a second (highlighted in yellow).

I have a question for real programmers - please suggest some simpler and faster version.

Regards, Vladimir.

Hi Vladimir. Note 1:

   datetime date_time = TimeCurrent(); // get the last known time of the server
   MqlDateTime mql_datatime = {}; // declare and initialise the mql_datatime variable for simplified access to the MqlDateTime structure
   TimeToStruct(date_time, mql_datatime); // convert from datetime type value to a variable of MqlDateTime structure type

It would be better to write it like this

   MqlDateTime mql_datatime = {}; // declare and initialise the mql_datatime variable for simplified access to the MqlDateTime structure
   datetime date_time = TimeCurrent(mql_datatime ); // get the last known server time // into a variable of MqlDateTime structure type

Remark 2:

Maybe it's easier to count the number of daily bars rather than taking into account weekend times?

int  Bars( 
   string           symbol_name,     // character name 
   ENUM_TIMEFRAMES  timeframe,       // period 
   datetime         start_time,      // from what date 
   datetime         stop_time        // by what date 
   );

Like this:

bool TimeClosePos()
 {
  MqlDateTime mql_datatime = {},
              mql_pos_open; // declare and initialise the mql_datatime variable for simplified access to the MqlDateTime structure
  datetime date_time = TimeCurrent(mql_datatime); // get the last known time of the server
//---
  for(int i = PositionsTotal()-1; i >= 0; i--) // go through all the positions
   {
    ulong pos_ticket = PositionGetTicket(i);  // get a position ticket
    datetime time_pos_open = 0;
    TimeToStruct((time_pos_open=(datetime)PositionGetInteger(POSITION_TIME)),mql_pos_open); // get the time of position opening
    if(Bars(_Symbol,PERIOD_D1,time_pos_open,date_time)>10 && mql_datatime.hour>mql_pos_open.hour && mql_datatime.min>mql_pos_open.min)
      return(true); // it's time to close the position
   }
  return(false);      // the time to close the position has not yet come
 }
 
Yuriy Bykov #:

Compilation, roughly speaking, is the process of converting the code you have written in MQL5 into commands encoded with zeros and ones, which can be executed by the processor............

It is clear that this is an intentionally exaggerated example. However, in real programmes it often happens that a rather compact source code requires considerable time for execution.

Yuri, thank you for your detailed explanation. I will know now. Live and learn!!! ))

Regards, Vladimir.

 
Aleksei Stepanenko #:
Eliminate all cycles, prepare data in such a way that it can be used on the next tick without cycles. Or almost. That's it.

It would be great, Alexey, but I have not managed to do without cycles yet. ) Thanks for the hint!

Regards, Vladimir.

 
Alexey Viktorov #:

Hi Vladimir. Comment 1:

it would be better to write it like this

Remark 2:

Maybe it's easier to count the number of daily bars rather than counting weekend times?

Roughly like this:

Hi Alexei, thanks for the tips. I will definitely test your option. )

Regards, Vladimir.

 

Good evening and good mood, everyone!

I've long been used to the word handle, but how to briefly describe its true meaning? Search engines all kinds of digging and found in my opinion the best explanation. Although I may be mistaken!

Taken from Wikipedia and specially condensed to a concept acceptable to me:

Handle (descriptor - not the best name either!) is a pointer to the source of further information retrieval.

Dear programmers!!! How would you briefly characterise the word handle?

Regards, Vladimir.