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

 
fxsaber #:

I never imagined that I would use a machine-generated piece of code in the source code. Especially in a place of archival importance for performance.


Below is the generated code.

A much more concise script was written for generation. This may be useful to quickly test hypotheses and avoid human error.

Be careful, the same ChatGPT makes a lot of mistakes, both in syntax and logic, so you have to double-check everything.

But it is a really good giver, it helps you to express ideas in code.

 
fxsaber #:

Below is the generated code.

A much more concise script was written for generation. This may be useful to quickly test hypotheses and avoid human error.

There is not enough background information: did the new code get faster than the old code or not?

If not, why change the old understandable code for the new incomprehensible one?

If yes, why couldn't the MQL compiler generate the code optimal in terms of performance at once, while it has much more possibilities for this than chat

 
Aleksey Vyazmikin #:

Be careful, the same ChatGPT makes a lot of mistakes, both in syntax and logic, so you have to double-check everything.

But it is a really good pitchman, it helps to express ideas in code.

Generating script.

// Генерация switch-кода.
string GetString( const int Num )
{
  static const string Condition[] = {"(Tick.bid >= this.bid.Max)", "(Tick.ask <= this.ask.Min)",
                                     "(Tick.bid <= this.bid.Min)", "(Tick.ask >= this.ask.Max)"};
  string Str = NULL;

  for (int i = 0; i < ArraySize(Condition); i++)
    if ((bool)(Num & (1 << i)))
      Str += ((Str == NULL) ? NULL : " || ") + Condition[i];

  return(Str);
}

void OnStart()
{
  for (int i = 0; i < 16; i++)
    Print("case " + (string)i + ":\n  return(" + GetString(i) + ");");
}
 
A100 #:

Not enough background information: did the new code get faster than the old code or not?

The new code is faster.

If yes, then why the MQL compiler could not generate the code optimal in terms of performance at once, while it has much more possibilities for this than chat

This is algorithmic optimisation, not compiler optimisation.

 
fxsaber #:

The new one is faster.

This is algorithmic optimisation, not compiler optimisation.

How much faster? By 1.5% or 1.5 times? And as if it's faster? Or based on correct measurements?

So you didn't give it a code, but an algorithm?

 
A100 #:

How much faster? By 1.5 per cent or 1.5 times? And by feel or by specific measurements?

So you didn't give it a code, but an algorithm?

That's what the Virtual source code uses. You have to make four checks on each tick.

return((Tick.bid >= this.bid.Max) || (Tick.ask <= this.ask.Min) || (Tick.bid <= this.bid.Min) || (Tick.ask >= this.ask.Max));


But in some situations (switch 0-15) you can do with fewer checks: 0-4. It depends on the TS.


For example, if the TS opens/closes positions only with market orders and does not use SL/TP, then you do not need to make a single check.

But if all types of orders are used simultaneously, you need to make all four checks: there will be no acceleration.


That's why you should take a specific TS and look at the results of measurements for it. Different TS have different acceleration results.


It was possible to classify cases (number of cases) into more variants. I couldn't do it, frankly speaking.

 

In MQL5, there is a StringReserve function, using which we can theoretically reduce the number of memory reallocations for our string: we allocated a large enough buffer at once and then work in it.

But, as practice shows, any subsequent assignment of a value to this string changes the size of its buffer (i.e., apparently, memory reallocation occurs).

As a result, instead of

string str;
str.Reserve(8192);
str="test";

it makes sense to use

string str;
str.Reserve(8192);
StringSetLength(str,0);  // или str.SetLen(0); - в документации есть, но у меня в 4073 не поддерживается
str+="test";
 
JRandomTrader #:

it makes sense to use

Exactly. This mechanism works great on string completion. For example, when generating large HTML reports.

 

How can I find out if a symbol has data, so that I don't leave it in the [Market Watch] window if it doesn't?

I use such a check in a loop:

ulong first_server_date = (ulong)SeriesInfoInteger(symbol, PERIOD_M1, SERIES_SERVER_FIRSTDATE);

if(first_server_date == NULL) {
  SymbolSelect(symbol, false);
  continue;
}

But after that I cannot manually remove symbols from the [Market Watch] window either one by one or all at once, while the Expert Advisor is on the chart:


 
Anatoli Kazharski #:

How can I find out if a symbol has data, so that I don't leave it in the [Market Watch] window if it doesn't?

SymbolInfoTick.