Double to String in MQL5

To add comments, please log in or register
quantumninja
64
quantumninja  

Hi All,

i am trying to convert double to string in MQL5, the NormalizeDouble is such a failure as it get numbers such as 0.07000000000000001 and 0.9399999999999999


i have created the below function, but it can only filter our cases as 0.9399999999999999, but fails with 0.07000000000000001 

double RNormalizeDouble(double val, int places){
   
   int index = StringFind(val, ".");
   
   string substring = StringSubstr(val, index, places+1);
   if(StringGetCharacter(substring, StringLen(substring)-1) == "0"){
      substring = StringSubstr(substring, 0, StringLen(substring)-2);
   }
   Print("Debug ",substring);
   string WholeNum = StringSubstr(val, 0, index-1);
   string res = WholeNum + substring;
   return StringToDouble(res);
}

Sample of the output

PN 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.9399999999999999

LM 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .93

FE 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.93

II 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.9399999999999999

JP 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .93

EN 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.93

MN 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.9399999999999999

RE 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .93

PD 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.93

EM 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.79

QL 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .79

LG 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.79

PG 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.63

JO 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .63

JP 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.63

NF 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.64

RG 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .64

EG 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.64

DO 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.34

EO 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .34

DD 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.34

DD 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.35

RM 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .35

GM 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.35

KR 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.06

HJ 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .06

NJ 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.06

FL 0 00:52:30.450 Core 1 2017.01.02 06:24:41   A -0.07000000000000001

QN 0 00:52:30.450 Core 1 2017.01.02 06:24:41   Debug .07

EG 0 00:52:30.450 Core 1 2017.01.02 06:24:41   B -0.07000000000000001

William Roeder
19991
William Roeder  
quantumninja: convert double to string in MQL5, the NormalizeDouble
Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
quantumninja
64
quantumninja  

Hi Whroeder1,

I saw this comment you type in another post, and i tried it already but didn't work, regardless look at the function again, i Do NOT use NormalizeDouble, clearly i am looking for an alternative in this MQL"5" not 4

anyway, thanks for passing by

nicholi shen
2391
nicholi shen  
whroeder1:
Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong


You really should stop spreading misinformation around here. What's wrong is your use of MathRound instead of NormalizeDouble in your rounding function since NormalizeDouble properly implements logic to prevent drift and MathRound does not (as pointed out by amrali in this thread). 


Correct method:

double price = 1.2345678;
price = step * NormalizeDouble(price / step,0);


OP, you can use the CDouble lib make your life easy. :)

quantumninja
64
quantumninja  
nicholishen:


You really should stop spreading misinformation around here. What's wrong is your use of MathRound instead of NormalizeDouble in your rounding function since NormalizeDouble properly implements logic to prevent drift and MathRound does not (as pointed out by amrali in this thread). 


Correct method:


OP, you can use the CDouble lib make your life easy. :)

Hi Nicholishen,

I have tried the recommended function, but didn't work at all, even failed to pass any of the tests as below

double RNormalizeDouble(double num, double precision){      
     double c = 0.5 * DBL_EPSILON * num;
//      double p = MathPow(10, precision);  //slow
        double p = 1; while (precision--> 0) p *= 10;
        if (num < 0)
                p *= -1;
        return MathRound((num + c) * p) / p;
        
}

2018.03.25 10:24:03.126 Core 1 2017.01.02 08:37:40   A -0.8100000000000001

2018.03.25 10:24:03.126 Core 1 2017.01.02 08:37:40   B -0.8100000000000001

2018.03.25 10:24:03.126 Core 1 2017.01.02 08:37:40   A -0.82

2018.03.25 10:24:03.126 Core 1 2017.01.02 08:37:40   B -0.82

2018.03.25 10:24:03.126 Core 1 2017.01.02 08:37:40   A -0.6899999999999999

2018.03.25 10:24:03.126 Core 1 2017.01.02 08:37:40   B -0.6899999999999999


Alain Verleyen
38085
Alain Verleyen  

Your topic is "double to string", what's wrong with DoubleToString() function ?

You should read and learn what is a double in mql.

Double-precision floating-point format - Wikipedia
Double-precision floating-point format - Wikipedia
  • en.wikipedia.org
Floating point is used to represent fractional values, or when a wider range is needed than is provided by fixed point (of the same bit width), even if at the cost of precision. Double precision may be chosen when the range and/or precision of single precision would be insufficient. In the IEEE 754-2008 standard, the 64-bit base-2 format is...
quantumninja
64
quantumninja  
Alain Verleyen:

Your topic is "double to string", what's wrong with DoubleToString() function ?

You should read and learn what is a double in mql.

Hi Alain,

I wrote the title this way as i convert it to String First, thanks for the note but i know what double and float are

anyway, my problem is actually with the precision of double as stated earlier

quantumninja
64
quantumninja  
Alain Verleyen:

Your topic is "double to string", what's wrong with DoubleToString() function ?

You should read and learn what is a double in mql.

Dear Alain,

Could you advise if such values could affect the calculations ?

they might seem like infinitesimal yet it occurred to me that some trades fail because of wrong values like this due to unexpected precision, is that thought correct ?

Alain Verleyen
38085
Alain Verleyen  
quantumninja:

Dear Alain,

Could you advise if such values could affect the calculations ?

they might seem like infinitesimal yet it occurred to me that some trades fail because of wrong values like this due to unexpected precision, is that thought correct ?

Forum on trading, automated trading systems and testing trading strategies

Double to String in MQL5

quantumninja, 2018.03.25 19:12

Hi Alain,

I wrote the title this way as i convert it to String First, thanks for the note but i know what double and float are

anyway, my problem is actually with the precision of double as stated earlier

Your question and all this topic shows that you don't understand what a double is. Some values can't be represented exactly as double, for example 0.1 can't.

There is no "wrong" values. nicholishen shows you the code to normalize a price.

quantumninja
64
quantumninja  
Alain Verleyen:

Your question and all this topic shows that you don't understand what a double is. Some values can't be represented exactly as double, for example 0.1 can't.

There is no "wrong" values. nicholishen shows you the code to normalize a price.

Okay

To add comments, please log in or register