Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 561

 
Ihor Herasko:

Simple:

gives an error

 
Aleksandr Lishchenko:

error message

Where is the error? What kind of error?

 

Hello. I have a task to check the input trading volume parameter specified by the user for correctness, namely the specified lot should be a multiple of the lot change step. To do that, I find a real remainder of division and compare it to a zero or a value very close to zero (for example, 0.000000001)

void OnStart()
{
   double volume = 0.03;
   Alert("Результат деления ", volume/MarketInfo(Symbol(), MODE_LOTSTEP));
   Alert("Шаг изменения лота ", MarketInfo(Symbol(), MODE_LOTSTEP));
   Alert("Вешественный остаток ", fmod(volume, MarketInfo(Symbol(), MODE_LOTSTEP)));
}

Result:

/*
   Вещественный остаток 0,009999999999999998
   Шаг изменения лота 0,01
   Результат деления 3,0
*/

Also attached a screenshot.

I think the remainder should be zero or a very small number, but the result is almost 0.01 for some reason? Then how can I check the input parameter if, for example, the user sets 0.001 by mistake?

Files:
4npm6qh.png  8 kb
 
Aleksandr Teleguz:

Hello. I have a task to check the input trading volume parameter specified by the user for correctness, namely, the specified lot should be a multiple of the lot change step. To do that, find a real remainder of division and compare it to a zero or a value very close to zero (for example, 0.000000001)

Result:

Also attached a screenshot.

I think the remainder should be zero or a very small number, but the result is almost 0.01 for some reason? Then how can I check the input parameter if, for example, the user sets 0.001 by mistake?

It's a bit wrong. The first thing to do is to calculate the closest correct value (based on the set value), and then check the difference between the values obtained. If it is equal to zero (or not more than DBL_EPSILON), then everything is OK:

void OnStart()
{
   double volume = 0.03;

   double fVolumeMin = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN);   
   double fVolumeMax = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX);   
   double fVolumeStep = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_STEP); 

   double fNearestCorrectVolume = VolumeCast(volume, fVolumeMin, fVolumeMax, fVolumeStep);
   if (fabs(fNearestCorrectVolume - volume) > DBL_EPSILON)
      Alert("Ошибка");
   else
      Alert("Все ОК");
}

The VolumeCast function can be taken from here.

 
Ihor Herasko:

Where does it go wrong? What kind of error does it give out?

1 error(s), 0 warning(s) 2 1
'if' - expressions are not allowed on a global

 

Afternoon!!!

Guys, can you tell me the code on how to link an EA to one account (for the latest MT4 update).

Thank you very much.

 
Aleksandr Lishchenko:

Good afternoon!

Guys, can you tell me the code to bind an EA to one account (for the latest MT4 update).

Thank you very much.

You have already been given the code.

You have made the error yourself - it is written in the error description that you have entered the code in the area of global variables and not inside OnTick() or OnInit():

'if' - expressions are not allowed on a global scope

And with such knowledge you are still trying to protect your code from someone else?
 
Artyom Trishkin:

You have already been told the code.

You made the error yourself - it is written to you in the error description that you have entered code in the area of global variables, not inside OnTick() or OnInit():

'if' - expressions are not allowed on a global scope

And with that knowledge you're still trying to protect your code from someone else?

I'm just learning!

Do not judge! ;)

 
Aleksandr Lishchenko:

1 error(s), 0 warning(s) 2 1
'if' - expressions are not allowed on a global

Show me how you applied the code. Did you just copy what I wrote?

 
Ihor Herasko:

Show me how you applied the code. Did you just copy what I wrote?

I did :)))