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

 
Vladislav Andruschenko:

I have now encountered a problem when the history has more than 1000 deals and when you call the history processing function, for example, to calculate the profit of the history. + add the profit of the current trades - the information on the chart starts to slow down and the terminal hangs itself. I.e. quotes come with a delay.

I can only guess without the code.

 

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5, tips and tricks

Alexey Navoykov, 2017.07.21 17:04

This article describes the case of the trading strategy with an example of the MetaTrader 5. However, you can get out of it by different means: templates and macros - what should I do without them?)

Here is what I have made.All source classes should be declared as templates that define the parent class.

class CBase { };  // базовый класс

// Макросы, задающие список наследования:

#define  INHERIT1(T)  T<CBase>

#define  INHERIT2(T1, T2)  T2<INHERIT1(T1)>

#define  INHERIT3(T1, T2, T3)  T3<INHERIT2(T1,T2)>

#define  INHERIT4(T1, T2, T3, T4)  T4<INHERIT3(T1,T2,T3)>


// Различные пользовательские классы:

template<typename TParent>
class A : public TParent { public: void a() { Print("A"); } };

template<typename TParent>
class B : public TParent { public: void b() { Print("B"); } };

template<typename TParent>
class C : public TParent { public: void c() { Print("C"); } };


class X : public INHERIT3(A, B, C)  {  };   // Объявляем класс, наследуемый от A, B, C


template<typename T>
void SomeFunc(B<T>& obj)  { obj.b(); }   // Проверочная функция, принимающая класс B


void OnInit()
{
  X x;
  x.a();
  x.b();
  x.c();
  
  SomeFunc(x);
}

Of course there are some subtleties, related to the fact that classes are inherited sequentially (in the order we've set), rather than in parallel (as in true multiple inheritance). In particular, they will have different priorities when an overload occurs. Besides, if the same template class participates in an inheritance chain several times, they will be completely different classes that are not related to each other in any way. So you have to be careful here. But there are no problems with interfaces, you can inherit without restrictions.

But what about without declaration in the form of templates?

For example, we have two ready-made self-sufficient libraries:

class CLib1 : public CClass1 { };and
class CLib2 : public CClass2 { };

We need to make such inheritance in the program class:

CLib1--> CLib2 --> CProgram so that both libraries are available in the CProgram class. And CLib1 library will be available in CLib2 accordingly.

And you won't be able to change the code of both libraries.

Is it possible?

With multiple inheritance it would probably be like this
class CProgram : public CLib1,CLib2 { };

 
Artyom Trishkin:

And if without the announcement in the form of templates?

For example, we have two ready-made self-sufficient libraries:

class CLib1 : public CClass1 { };and
class CLib2 : public CClass2 { };

We need to make such inheritance in the program class:

CLib1--> CLib2 --> CProgram so that both libraries are available in the CProgram class. And CLib1 library will be available in CLib2 accordingly.

And you won't be able to change the code of both libraries.

Is it possible?

With multiple inheritance it would probably be like this
class CProgram : public CLib1,CLib2 { };


You won't loose anything if you do it this way

class CProgram
{
  CLib1 lib1;
  CLib2 lib2;
};
 
Koldun Zloy:

You won't lose anything if you do it this way:

Yes. That's what I did. I just wanted to avoid unnecessary objects.
 

Probably the best way to get the data from asynchronous Copy functions (CopyRates, CopyTicks, etc.) is through EventChartCustom.

It is especially important for indicators.

 

It is possible to know that there was a change of trade server, not just account -AccountInfoString (ACCOUNT_SERVER) in OnDeinit (EA, not indicator) will return the new trade server.

 

Memo

Action/type of programChange of TF or symbolAccount change
IndicatorRunning OnDeinit and OnInit, global class object changes (complete reboot).Nothing happens except that prev_calculated is reset.
EARunning OnDeinit and OnInit, global class object does not change.Running OnDeinit and OnInit, global class object changes (full reload).
 
File compression and universal execution time measurement

Forum on trading, automated trading systems and trading strategies testing

My dissatisfaction with strategy tester. to MQL developers

fxsaber, 2017.12.04 09:11

#define  BENCH(A)                                                              \
{                                                                             \
  const ulong StartTime = GetMicrosecondCount();                              \
  A;                                                                          \
  Print("Time[" + #A + "] = " + (string)(GetMicrosecondCount() - StartTime)); \
} 

void OnStart()
{
  uchar Data[];
  uchar Key[1];
  uchar Result[];
  
  FileLoad("thousands_rubies_galaxy.bmp", Data);  
  BENCH(Print(CryptEncode(CRYPT_ARCH_ZIP, Data, Key, Result)))
  
  ArrayFree(Data);
  
  FileLoad("space_wind.wav", Data);  
  BENCH(Print(CryptEncode(CRYPT_ARCH_ZIP, Data, Key, Result)))
}

Result

826534
Time[Print(CryptEncode(CRYPT_ARCH_ZIP,Data,Key,Result))] = 53334
306648
Time[Print(CryptEncode(CRYPT_ARCH_ZIP,Data,Key,Result))] = 29029
 

There is a whole class of indicators, which superimpose price charts of other symbols on the price chart. They are done in the same way - through indicator buffers.

But in MT5 there is a wonderful OBJ_CHART, which allows you to realize this task much more beautifully without indicator buffers.

For example, you can put any indicator and immediately see how it looks on another symbol.

Slave scheme - several charts are displayed as the background of the main chart.

 
fxsaber:

For example, you can cast any indicator and immediately see how it looks on another symbol.

Slave scheme - several charts are displayed as the background of the main chart.

Is there a picture of what that would look like? It's not quite clear, I haven't usedOBJ_CHART yet.
Reason: