Features of the mql4 language, subtleties and techniques - page 29

 
Vladimir Karputov:

Is the time the same in theData window?

Yes.

 
Andrey Khatimlianskii:

Yes.

I see. So it really is the bar time. I just had a suspicion about the"Precise timeline" parameter.

Настройки платформы - Начало работы - Справка по MetaTrader 5
Настройки платформы - Начало работы - Справка по MetaTrader 5
  • www.metatrader5.com
Торговая платформа обладает множеством настроек, что позволяет организовать работу в ней так, как это удобно именно вам. Выполните команду " Настройки" меню "Сервис" или нажмите "Ctrl+O". Графики — общая настройка отображения ценовых графиков, а также настройка параметров управления объектами: выделение объектов после их создания, немедленная...
 

Hello!
What is the correct way to use "double" in the "for" loop? There seems to be no prohibition on using "double" in the help. I understand that I have to normalize " i ", but it doesn't work for me.

double expr1=1.0;
double expr2=2.8;
double step=0.2;
double i;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int count=0;
   for(i=expr1; i<=expr2; i+=step)
     {
      NormalizeDouble(i,1);
      count++;
      Print(" i: ",DoubleToStr(i,8)," step: ",DoubleToStr(step,8));
     }
   int MaxPass=(int)NormalizeDouble(((expr2-expr1)/step),0)+1;
   Print(" MaxPass: ",MaxPass," count: ",count," i: ",i);
  }
//+------------------------------------------------------------------+

If "expr2=2.6" is still ok for 9 iterations, but when "expr2=2.8" is already showing 9 iterations wrong (the correct value is 10).

 
Nauris Zukas:

Hello!
What is the correct way to use "double" in the "for" loop? There seems to be no prohibition on using "double" in the help. I understand that I have to normalize " i ", but it doesn't work for me.

If "expr2=2.6" is still fine for 9 iterations, but when "expr2=2.8" is already showing incorrect 9 iterations (the correct value is 10).

I probably need to normalize the values.

 
Alexey Viktorov:

Probably need to normalise the values.

If you meant this way, it didn't help either:

double expr1=1.0;
double expr2=2.8;
double step=0.2;
double i;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int count=0;
   for(i=NormalizeDouble(expr1,1); i<=NormalizeDouble(expr2,1); i+=NormalizeDouble(step,1))
     {
      NormalizeDouble(i,1);
      count++;
      Print(" i: ",DoubleToStr(i,8)," step: ",DoubleToStr(step,8));
     }
   int MaxPass=(int)NormalizeDouble(((expr2-expr1)/step),0)+1;
   Print(" MaxPass: ",MaxPass," count: ",count," i: ",i);
  }
 
Nauris Zukas:

If you meant this way, it didn't help either:

No. At the very least you have to optimise it

double expr1=1.0;
double expr2=2.8;
double step=0.2;
double i;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int count=0;
   for(i=NormalizeDouble(expr1,1); i<=NormalizeDouble(expr2,1); NormalizeDouble(i+=step,1))
     {
      NormalizeDouble(i,1);
      count++;
      Print(" i: ",DoubleToStr(i,8)," step: ",DoubleToStr(step,8));
     }
   int MaxPass=(int)NormalizeDouble(((expr2-expr1)/step),0)+1;
   Print(" MaxPass: ",MaxPass," count: ",count," i: ",i);

because that's what gives you the wrong value. And the others may not have to.

 
Nauris Zukas:

Hello!
What is the correct way to use "double" in the "for" loop? There seems to be no prohibition on using "double" in the help. I understand that I have to normalize " i ", but it doesn't work for me.

If "expr2=2.6", then all is normal 9 iterations, but when "expr2=2.8" shows not correct 9 iterations (correctly 10).

Here you can find, read Nikolay's post, very detailed and very qualitative description of doublehttps://www.mql5.com/ru/forum/1111/page2623#comment_14473837

If you cannot assimilate the material from the first time, try to replace Print() with printf() in your code with output of double at least to 15-th digithttps://www.mql5.com/ru/forum/1111/page2623#comment_14474138.

Maybe then you will see why this is so

 
Alexey Viktorov:

No. At the very least you have to optimise this

because that's what gives you the wrong value. And the others may not have to.

Thank you, but your example didn't give us the right result either. In printf you can see that no normalization happens:

0 10:29:31.595 Test_Double_For EURCHF,H4: i = 1.0000000000000000000000000000000000000000000000000000000000
0 10:29:31.595 Test_Double_For EURCHF,H4: i = 1.199999999999999999995559107901499373838305473327636718750000
0 10:29:31.595 Test_Double_For EURCHF,H4: i = 1.399999999999991111182158029987476766109466552734375000000000
0 10:29:31.595 Test_Double_For EURCHF,H4: i = 1.5999999999999999999866773237044981212151491164199829101562500000000
0 10:29:31595 Test_Double_For EURCHF,H4: i = 1.799999999999999999982236431605997495353221893310546875000000
0 10:29:31.595 Test_Double_For EURCHF,H4: i = 1.9999999999999997779553950749686919152736638618359375000000
0 10:29:31.595 Test_Double_For EURCHF,H4: i = 2.1999999999999997335464740899624302983283996582031250000000
0 10:29:31.595 Test_Double_For EURCHF,H4: i = 2.3999999999999999999999119182158029987476766109466552734375000000000
0 10:29:31595 Test_Double_For EURCHF,H4: i = 2.6000000000000088817841970012523233890533447265625000000000
0 10:29:31595 Test_Double_For EURCHF,H4: MaxPass: 10 count: 9
0 10:29:31.595 Test_Double_For EURCHF,H4: Last i = 2.8000000000000002664535259100375697016716003417968750000000

 
Igor Makanu:

here it is, read Nikolay's post, he described the work with double in great detail and with great qualityhttps://www.mql5.com/ru/forum/1111/page2623#comment_14473837

If you cannot digest the material from the first time, try to replace Print() with printf() with output of double at least to 15-th digithttps://www.mql5.com/ru/forum/1111/page2623#comment_14474138.

Maybe then you will see why this is so.

Thanks, I understand why it happens, that's why I tried to normalize it. It did not work for me. Alexey Viktorov option didn't fix it either, values remain non-normalized. So the question remains, can "double" be used in a loop and always get the correct number of iterations?

 
Nauris Zukas:

So the question remains, "double" can be used in the loop

can

Nauris Zukas:

and get always the correct number of iterations?

the value of double is always correct, but it does not correspond to the positional decimal system you would like to see or get

Nauris Zukas:

Thanks, I understand why this happens, that's why I tried to normalise. It did not work for me. Alexey Viktorov option did not fix it either, the values remain non-normalized

Any manipulation with the double conversion will always have limitations, I doubt that this problem can be solved in the way as in your examples

Make a reliable solution: use int in the loop and the formula to calculate the result value of double and do not use addition of double as a result when iterating in the loop - once again look at my example in the dialogue with Nikolaev