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

 

In order to reduce the number of optimization overshoots, I use two methods to create a non-linear scale.

To enumerate the duration in hours with sufficient accuracy:

enum eHours { _0, _4, _6, _8, _12, _16, _24, _32, _48, _64, _96, _128 };

input eHours TrendHours = _8;

int
eHours2Hours(eHours e) {
	switch (e) {
	case _0:	return 0;
	case _4:	return 4;
	case _6:	return 6;
	case _8:	return 8;
	case _12:	return 12;
	case _16:	return 16;
	case _24:	return 24;
	case _32:	return 32;
	case _48:	return 48;
	case _64:	return 64;
	case _96:	return 96;
	case _128:	return 128;
	default:	return -1;
	}
}

To enumerate, for example, the SAR step with an accuracy of 2 digits (~1%):

// Приводит параметр оптимизации к нелинейному виду
// 001-099 >> 0.0001-0.0099
// 101-199 >> 0.001-0.099
// 201-299 >> 0.10-0.99
// Внимание: коды 000, 100, 200 возвращают 0.0
double
NonlinPar(int code) {
        int order = code / 100;
        int mod = code - order * 100;
        return mod * MathPow(10, order) / 10000;
}

Optimising from 0.0001 to 0.99 would require almost 10K steps. Using codes 001-299 in the optimisation would require less than 300 steps.

 
A way to run into division by zero even with a check.
void OnStart()
{  
  const double Profit = 100;
  const double Lots = 1;

  double TickValue[];  
  ArrayResize(TickValue, 1);
  
  const double Points = TickValue[0] ? Profit / (Lots * TickValue[0] * _Point) : 0; // zero divide    
}


The error is clear in fact. But while writing code like this, it is not always obvious that such a check is not enough to avoid division by zero.

 
An excuse not to use the ternary operator.
 
TheXpert:
An excuse not to use the ternary operator.

For if similar, of course.

 

Just don't expect that if there is no value, it is necessarily 0 and therefore false if it is not a bool-type variable. Even an explicit conversion to bool type will not save it.

void OnStart()
{  
  const double Profit = 100;
  const double Lots = 1;

  double TickValue[];  
  ArrayResize(TickValue, 1);
  
  const double Points = TickValue[0] == 0? Profit / (Lots * TickValue[0] * _Point) : 0;   
}

It works this way without errors.

 

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5, tips and tricks

Alexey Viktorov, 2019.10.28 10:22

You just don't expect that if there is no value, it is necessarily 0 and therefore false if it is not a bool type variable. Even explicit conversion to bool type will not save.

void OnStart()
{  
  const double Profit = 100;
  const double Lots = 1;

  double TickValue[];  
  ArrayResize(TickValue, 1);
  
  const double Points = TickValue[0] == 0? Profit / (Lots * TickValue[0] * _Point) : 0;   
}

It works this way without errors.

There is an error in the highlighted place.
 
fxsaber:
There is an error in the highlighted place.

I can't see the highlight. What is the error?

 
fxsaber:
There is an error in the highlighted place.

You wrote "==", it should be "!=".

It's underlined "very pale yellow" :)

 
Artyom Trishkin:

You wrote "==", it should be "!="

It's underlined "very pale yellow" :)

Well, not everyone knows I'm like that monkey, "I'm weak in the eyes when I'm old."

I guess so, but for some reason it worked without division by 0. And in general, my message was that we shouldn't check any variable for 0 as a boolean for false.

 
Alexey Viktorov:

I guess so, but for some reason it worked without division by 0.

Because TickValue is not zero.