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

 

how to fill an array with one line?

struct clr_struct{
   color uncheck;
};
clr_struct clr[];

//clr[0].uncheck=0x999999;
//clr[1].uncheck=0x999999;

clr[].uncheck={0x999999,0x999999};//как правильно?
 
Pavel Kolchin fill an array with one line?
clr_struct clr[] = {{0x999999}, {0x999999}};
 

One more lifehack for access rights: If you have an irresistible desire to give access to private fields/functions of one class to another class, you can do the following using standard MQL tools:

We need to: make access to A::f1() from B

//--------- как было
class   A
  {
   int               m;
   int               f1() {return m;}                       // нужно дать к нему доступ классу B
public:
                     A(int a): m(a) {}
  };

class B {};

Let's rewrite it like this:

class A;
class B
  {
private:
   class AB
     {
   public:
      virtual int    CallAf1(A&) const  = 0;
                     AB() {ABptr = &this;}
     } static *ABptr;

   // внутри B используем  этот метод для получения доступа к A::f1()
   static int        CallAf1(A & obj)
     {
      return ABptr.CallAf1(obj);
     }

public:
   class AB2: AB {};

   // вспомогательный метод - только для проверки
   static void       _f1_test(A & obj)
     {
      Print("test ", CallAf1(obj));
     }
  };
B::AB *B::ABptr = NULL;

class   A
  {
   int               m;
   int               f1() {return m;}
public:
                     A(int a): m(a) {}

   //--  добавлено
private:
   class AB3: B::AB2
     {
      int            CallAf1(A &obj) const {return obj.f1();}
     }   static  const     ABlink;
  };
const A::AB3 A::ABlink;

To call (A)a.f1() inside B, call CallAf1(a). If f1() has parameters, we add them to CallAf1().

Test:

void OnStart()
  {
    A f(2);
    B::_f1_test(f);
  }

You can make CallAf1() protected, but it will be a big hole in access rights - anywhere in the code you can create a descendant of class B and in it a public method to call CallAf1() - i.e. everyone will have access to A::f1().


P.S. The construct is very cumbersome (if you want, you can cram it into macros), but it has one advantage over friend C++: it gives access not to all class members, but only to selected ones.

 
mktr8591 #:

One more lifehack for access rights: If you have an irresistible desire to grant access to private fields/functions of one class to another class, you can do this using standard MQL tools

It took me a long time to get into it... good move with virtualisation, thanks!
 
fxsaber #:
Took a long time to get into it... good move with virtualisation, thanks!
The only thing I didn't write at the beginning is that you can't create any more objects of class B::AB2 (and its descendants), otherwise everything will fall apart :-)
 
Running this Expert Advisor in the Tester in the all symbols mode
input int inTmp = 0; // https://www.mql5.com/ru/forum/321656/page90#comment_44727856

bool Res = true;

// Проверяет, нормализована ли цена по алгоритму NormalizeDouble.
bool IsNDPrice( const double &Price )
{
  return(Price == NormalizeDouble(Price, 5));
}

// Проверяет, нормализована ли цена, как при парсинге или ручном вводе.
bool IsNDPrice2( const double &Price )
{
  return(Price == (double)DoubleToString(Price, 5));
}

void OnTick()
{
  MqlTick Tick;
  
  if (SymbolInfoTick(_Symbol, Tick))
//    Res &= IsNDPrice(Tick.bid) && IsNDPrice(Tick.ask); // OnTester == 1.
    Res &= IsNDPrice2(Tick.bid) && IsNDPrice2(Tick.ask); // OnTester == 0.
}

double OnTester()
{
  return(Res);
}
shows that all prices are normalised using the NormalizeDouble algorithm. And that if you work with prices obtained not from the Terminal, but by parsing or manual input, they require normalisation for correct operation. There is no need to be mistaken.
 

REASON_ACCOUNT (even if the account is not changed, but just re-login is done) the Expert Advisor is completely unloaded and a new copy is loaded.

For this reason ExpertRemove in OnDeinit does not affect the new copy, because it touches the unloaded one.

void OnInit() { Print(__FUNCSIG__); }

void OnDeinit( const int ) { Print(__FUNCSIG__); }


Result after relogin.

2023.02.07 11:33:12.717 Test5-3 (EURUSD,M1)     void OnDeinit(const int)
2023.02.07 11:33:13.926 Test5-3 (EURUSD,M1)     void OnInit()


After Deinit, the new EA copy waits for more than a second (up to two seconds) to start. What are the reasons for such a long wait and is it possible to speed it up?

 

SymbolInfoTick will return the latest tick on each of these three calls. That is, the so-called collection of ticks without skipping through indicators is questionable, to put it mildly.


Proof of the impossibility of collecting ticks by an indicator (without skips).

// Аналог Sleep для индикатора.
void Sleep2( const uint Pause )
{
  const uint StartTime = GetTickCount();
  
  while (!IsStopped() && (GetTickCount() - StartTime) < Pause)
    ;
}

#define  TOSTRING(A) #A + " = " + (string)(A) + " "

int OnCalculate( const int rates_total,
                 const int,
                 const datetime &time[],
                 const double &open[],
                 const double &high[],
                 const double &low[],
                 const double &close[],
                 const long &tick_volume[],
                 const long &volume[],
                 const int &spread[] )
{  
  static const int period = PeriodSeconds();
  
  Sleep2(1000); // Расчеты.
  
  MqlTick Tick[1];
  
  if (SymbolInfoTick(_Symbol, Tick[0]) &&
      ((Tick[0].bid != close[rates_total - 1]) || // Свежий бар не соответсвует последнему тику.
       Tick[0].time / period > time[rates_total - 1] / period))
  {
    ArrayPrint(Tick);
    Print(TOSTRING(time[rates_total - 1]) + TOSTRING(close[rates_total - 1]));
  }

  return(rates_total);
}


Result.

                 [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags] [volume_real]
[0] 2023.02.07 16:28:39 1.54951 1.54959 0.0000        0 1675787319322      98       0.00000
time[rates_total-1] = 2023.02.07 16:28:00 close[rates_total-1] = 1.5495 

                 [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags] [volume_real]
[0] 2023.02.07 16:28:40 1.54948 1.54959 0.0000        0 1675787320719      98       0.00000
time[rates_total-1] = 2023.02.07 16:28:00 close[rates_total-1] = 1.5495 

                 [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags] [volume_real]
[0] 2023.02.07 16:28:41 1.54952 1.54960 0.0000        0 1675787321823     100       0.00000
time[rates_total-1] = 2023.02.07 16:28:00 close[rates_total-1] = 1.54954 

                 [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags] [volume_real]
[0] 2023.02.07 16:28:42 1.54954 1.54961 0.0000        0 1675787322223     102       0.00000
time[rates_total-1] = 2023.02.07 16:28:00 close[rates_total-1] = 1.54951 00000000002 

                 [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags] [volume_real]
[0] 2023.02.07 16:28:43 1.54955 1.54964 0.0000        0 1675787323721     100       0.00000
time[rates_total-1] = 2023.02.07 16:28:00 close[rates_total-1] = 1.54948 

                 [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags] [volume_real]
[0] 2023.02.07 16:28:44 1.54954 1.54962 0.0000        0 1675787324323     100       0.00000
time[rates_total-1] = 2023.02.07 16:28:00 close[rates_total-1] = 1.54952 

                 [time]   [bid]   [ask] [last] [volume]    [time_msc] [flags] [volume_real]
[0] 2023.02.07 16:28:45 1.54956 1.54962 0.0000        0 1675787325421     102       0.00000
time[rates_total-1] = 2023.02.07 16:28:00 close[rates_total-1] = 1.54952
 
One of the possible MQL5-language constructs.

Forum on Trading, Automated Trading Systems and Testing Trading Strategies

Errors, bugs, questions

fxsaber, 2023.02.14 13:11

input string inStr = "B2";

class A {};

class B1 : public A {};
class B2 : public A {};
// .....
class B100 : public A {};

void OnStart()
{
  A* a = New2(inStr); // создает объект, который прописан в inStr.
}

// Решение.
template <typename T>
A* New( const string &ClassName ) { return((typename(T) == ClassName) ? new T : NULL); }

typedef A* (*TNew)( const string& );
static const TNew FuncNew[] = {New<B1>, New<B2>, /*....,*/ New<B100>};

A* New2( string ClassName )
{  
  A* Res = NULL;
  
  ClassName = "class " + ClassName;
  
  for (int i = ArraySize(FuncNew) - 1; !Res && (i >= 0); i--)
    Res = FuncNew[i](ClassName);  
    
  return(Res);
}

Unfortunately, MQL4 does not pull this kind of work yet.

 

begDayBar= iBarShift(_Symbol,_Period,begDayTime,false) according to the documentation the function returns -1 or the nearest bar offset depending on the exact parameter.

but today for some reason if begDayTime=2023.01.26 00:00:00 it returns -1 although there are such bars and they are not the last ones.

2023.02.15 15:19:23.254 !indDAY_WSOWROhLine (EURRUB_TOM,M15) begDayTime=2023.01.26 00:00:00 endDayTime=2023.01.27 00:00:00 begDayBar=-2 endDayBar=-1 indATR=0 Q5days=0

it may return -1 or it may return the correct bar.

Документация по MQL5: Доступ к таймсериям и индикаторам / iBarShift
Документация по MQL5: Доступ к таймсериям и индикаторам / iBarShift
  • www.mql5.com
iBarShift - Доступ к таймсериям и индикаторам - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5