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

 
Alain Verleyen #:

Unfortunately, I can't reproduce this (although I've had it before too). As you realise, the only way to (possibly) get the bug fixed is to provide MetaQuotes with a way to reproduce it.

Once I have such a procedure, I can report the bug.

When I have some free time, I will try to reproduce the problem. One of the key clues is that the MT5 terminal waited a long time for historical data to synchronise before running the optimisation.
 

Useless but interesting examples of working with bits.

1. Getting the average of two integers. The usual way (x + y) >> 1 can lead to overflow when adding large x and y. Here is the solution (the speed is only slightly slower):

int Avg(int x, int y) {
        return ((x ^ y) >> 1) + (x & y);
}

2. Number flipping (b31...b0 --> b0...b31) is used sometimes in cryptography and radio engineering, usually done in a loop. Here's a solution almost twice as fast:

uint ReverseBits(uint v) {
        v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
        v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);
        v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
        v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);
        v = (v >> 16) | (v << 16);
        return v;
}
 

How diverse margin requirements can be.


I have no idea how to work with it through MQL5.

 
Probably invented an ancient bicycle. For parsing numbers, a cheap digit identifier.

Fórum de negociação, sistemas de negociação automatizados e testes de estratégias de negociação

Bibliotecas: JSON Library for LLMs

fxsaber, 2026.02.19 08:14

Interesting result.

bool IsDigit( const uchar Char )
{
  return((bool)(Char & (1 << 4)));
}


Result.

0: 0011 0000 - true
1: 0011 0001 - true
2: 0011 0010 - true
3: 0011 0011 - true
4: 0011 0100 - true
5: 0011 0101 - true
6: 0011 0110 - true
7: 0011 0111 - true
8: 0011 1000 - true
9: 0011 1001 - true
.: 0010 1110 - false
e: 0110 0101 - false
E: 0100 0101 - false


Therefore, double checking can be replaced with single checking.

// if (c == '.' || c > '9') // Pre-validated token: c>'9' catches e/E
if (!(c & (1 << 4))) 
 
fxsaber #:
Probably invented an ancient bicycle. For parsing numbers, a cheap digit identifier.
Are the characters ':', ';', '<', '=', '>', '?' and those with codes in the range 70..7F parsed normally? Or are they also digits?
 
Sergey Gridnev #:
Are ':', ';', '<', '=', '>', '?' and those with codes in the range 70..7F parsed normally? Or are they numbers too?
It is assumed that it is numbers that are parsed (identification that it is a number has already been done).
Print(DBL_MIN); // 2.2250738585072014e-308


But in some cases it can be used for identification as well.

Fórum de negociação, sistemas de negociação automatizados e testes de estratégias de negociação

Bibliotecas: JSON Library for LLMs

fxsaber, 2026.02.19 09:02

A more versatile option.
bool IsDigit( const uchar Char )
{
// return((bool)(Char & (1 << 4)));
  return((Char & 0xF0) == 0x30);
}

void OnStart()
{
  for (int Char = 0; Char <= UCHAR_MAX; Char++)
    PrintChar((uchar)Char);
}


Result.

... - false

.: 00101110 - false
/: 00101111 - false
0: 00110000 - true
1: 00110001 - true
2: 00110010 - true
3: 00110011 - true
4: 00110100 - true
5: 00110101 - true
6: 00110110 - true
7: 00110111 - true
8: 00111000 - true
9: 00111001 - true
:: 00111010 - true
;: 00111011 - true
<: 00111100 - true
=: 00111101 - true
>: 00111110 - true
?: 00111111 - true
@: 01000000 - false
A: 01000001 - false

... - false
 
Sergey Gridnev #:
Are ':', ';', '<', '=', '>', '?' and those with codes in the range 70..7F parsed normally? Or are they numbers too?
Only digits.

Fórum de negociação, sistemas de negociação automatizados e testes de estratégias de negociação

Bibliotecas: JSON Library for LLMs

fxsaber, 2026.02.19 23:12

bool IsDigit( const uchar Char )
{
// return((Char >= '0') && (Char <= '9'));

// return((Char ^ (UCHAR_MAX - '1')) >= (UCHAR_MAX - 9));
// return((Char ^ '1') <= 9);

// return((Char ^ (UCHAR_MAX - '0')) >= (UCHAR_MAX - 9));
  return((Char ^ '0') <= 9);
}
 

Into work techniques

now ssd m2 are sold on 16gb intel optane(original), new 150 write without reduction, and read about 900, resource almost eternal for 16gb - 180tb

if someone has enough 16gb for a tester and a slot is available (or in PCI through an adapter) good solution

the price is just under 400r! search on ozone M10 16gb


someone under the system cache takes, the resource is almost eternal

 

You need to create a copy of a real symbol into a custom one. To do this, we need to transfer all the bar history to the custom one.


And we need to know if it is possible to do it or not. For this purpose I wrote such a function.

datetime GetMonth( const datetime time )
{
  MqlDateTime sTime;

  TimeToStruct(time, sTime);

  const MqlDateTime sTime2 = {sTime.year, sTime.mon, 1};

  return(StructToTime(sTime2));
}

// Returns true if there is access to the first bar of the entire available symbol history:
// Real Symbol: Bases\ServerName\history\SymbolName\*.hcc
// Custom Symbol: Bases\Custom\history\SymbolName\*.hcc
bool IsFullAccess( const string Symb = NULL )
{    
  const datetime FirstDateM1 = (datetime)SeriesInfoInteger(Symb, PERIOD_M1, SERIES_FIRSTDATE);
  const datetime MonthM1 = GetMonth(FirstDateM1);
  
  datetime FirstDateMN1 = (datetime)SeriesInfoInteger(Symb, PERIOD_MN1, SERIES_FIRSTDATE);

  MqlRates RatesMN1[1] = {{FirstDateMN1}};
  MqlRates RatesM1[1];
  
  return(((FirstDateMN1 <= MonthM1) || (CopyRates(Symb, PERIOD_MN1, MonthM1, 1, RatesMN1) > 0)) &&
         (RatesMN1[0].time == MonthM1) &&
         (RatesMN1[0].open || CopyRates(Symb, PERIOD_MN1, MonthM1, 1, RatesMN1) > 0) &&
         (CopyRates(Symb, PERIOD_M1, FirstDateM1, 1, RatesM1) > 0) &&
         (RatesMN1[0].open == RatesM1[0].open));
}

Is there a more elegant option to find out if all bars are available or not?

 
Subject: Feature Request: Introduction of OnChartSwitch Event Handler for Terminal-wide Monitoring

Description:
Currently, in MQL5, an Expert Advisor is strictly bound to the chart it is running on. There is no native, event-driven way to detect when a user switches focus (clicks) between different chart tabs within the terminal unless the EA is present on every single chart.

While a high-frequency OnTimer (e.g., 50ms) can poll CHART_BRING_TO_TOP, this is inefficient and resource-heavy when managing many charts.

Proposed Solution:
Introduce a new event handler: void OnChartSwitch(long focusedChartID, string symbol, ENUM_TIMEFRAME period).

This event should:

    Trigger whenever the user clicks on a different chart tab in the terminal.

    Be available to any EA running in the terminal, regardless of which chart the EA is physically attached to (or at least provide a global chart property accessible via OnChartEvent).

Use Case:

    Multi-symbol Dashboards: Allowing a single "Controller" EA to sync external tools (via DLL/Named Pipes) or GUI panels instantly when the trader navigates through their Market Watch or open charts.

    Dynamic Analysis: Syncing external Order Management Systems (OMS) with the currently viewed symbol without needing to attach an EA to dozens of open windows.

Benefit:
Significant reduction in CPU overhead compared to Timer-based polling and a much more responsive "Push" architecture for modern trading setups.