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

 
Fast528:

Don't tell me, I found a chip, the MCs still have not corrected the loan of the name of one of the main functions, killed a lot of time reading and searching for it

https://www.mql5.com/ru/docs/basis/function/functionoverload

Документация по MQL5: Основы языка / Функции / Перегрузка функций
Документация по MQL5: Основы языка / Функции / Перегрузка функций
  • www.mql5.com
Обычно в названии функции стремятся отобразить ее основное назначение. Читабельные программы, как правило, содержат разнообразные и грамотно подобранные идентификаторы. Иногда различные функции используются для одних и тех же целей. Например, рассмотрим функцию, которая вычисляет среднее значение массива чисел двойной точности, и такую же...
 
Реализация мультивалютного режима в MetaTrader 5
Реализация мультивалютного режима в MetaTrader 5
  • 2011.01.10
  • Konstantin Gruzdev
  • www.mql5.com
В настоящее время мультивалютных торговых систем, индикаторов и экспертов разработано огромное количество. Тем не менее, до сих пор создатели этого "огромного количества" сталкивались со специфическими для мультивалютных систем трудностями. С выпуском в свет терминала MetaTrader 5 и языка программирования MQL5 появилась возможность  реализации...
 
// Возвращает тип переменной. Exact == true - учитывает const-спецификатор
template <typename T>
string GetType( T&, const bool Exact = false )
{
  static const int Offset = StringFind(__FUNCTION__, "<") + 1;

  return(Exact ? StringSubstr(__FUNCTION__, Offset, StringLen(__FUNCTION__) - Offset - 1) : typename(T));
}


Application

#define  PRINT(A) Print(#A + " = " + (string)(A))

void OnStart()
{
  const int Var1 = 0;
  double Var2;
    
  PRINT(GetType(Var1));
  PRINT(GetType(Var2));  
  
  PRINT(GetType(Var1, true));
  PRINT(GetType(Var2, true));
}


Result

GetType(Var1) = int
GetType(Var2) = double

GetType(Var1,true) = const int
GetType(Var2,true) = double
 
fxsaber:

Application

Result

If it is possible to use MQL to describe a function, the number of arguments of which is not defined, e.g.

Print(arg1,arg.......) or StringConcatenate(string_var,void argument1,void argument2, .......)


 
Igor Makanu:

If it is possible to use MQL to describe a function with an undefined number of arguments, such as

Print(arg1,arg.......) or StringConcatenate(string_var,void argument1,void argument2, .......)

It's possible to come up with something based on macros, but it's crutchy.

Templates + passing arrays/structures allow to realize many ideas.

 
fxsaber:

It is possible to come up with something based on macros, but it is crutchy.

Templates + passing arrays/structures allow you to implement many ideas.

Thanks, that's what I thought, no need in principle, but watching your codes, I thought, what if you can do that?

 

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5 language, tips and tricks

fxsaber, 2017.02.27 18:40

// Альтернатива OrderCalcMargin
bool MyOrderCalcMargin( const ENUM_ORDER_TYPE action, const string symbol, const double volume, const double price, double &margin )

Here is a cross-platform prof. implementation.

Instant estimation of profit, margin level, drawdown on MetaTrader charts
Instant estimation of profit, margin level, drawdown on MetaTrader charts
  • 2018.07.16
  • www.mql5.com
I'm sure all of you use the crosshair tool on MetaTrader's charts. It's very useful to measure distance in bars or points between 2 spots. Yet I was always wondering, why it does not provide an option to show profit value, margin level, drawdown or gain percentage in addition to points. To remedy this problem I've developed a MQL program, which...
 
Why const is cool!
void OnTradeTransaction ( const MqlTradeTransaction &Trans, const MqlTradeRequest &Request, const MqlTradeResult &Result )
{
  if (Trans.type = TRADE_TRANSACTION_REQUEST) // 'type' - constant cannot be modified


Imagine how long it would take to find an error in a large code if instead of "==" you wrote "=". Here, on the other hand, the compiler itself tells you everything at once, thanks to const.

 

fxsaber:

Imagine how long it would take to find an error in a large code if you wrote "==" instead of "=". Here, thanks to const, the compiler itself tells you everything right away.

In such cases, you can start the comparison with a constant, which is even better.

void OnTradeTransaction ( const MqlTradeTransaction &Trans, const MqlTradeRequest &Request, const MqlTradeResult &Result )
{
  if (TRADE_TRANSACTION_REQUEST = Trans.type)
 
TheXpert:

In such cases, you can start the comparison with a constant - even better.

Yes, I just created an example for such a case

if (Trans.type = Variable)