NormalizeDouble not working

 

Hi everyone I'm not sure why NormalizeDouble is not working here. Can anyone suggest a fix to this?

***

Files:
Alert.jpg  21 kb
 
RedBeanSoup:

Hi everyone I'm not sure why NormalizeDouble is not working here. Can anyone suggest a fix to this?

***

Please insert the code correctly: when editing a message, press the button    Codeand paste your code into the pop-up window
 
RedBeanSoup:

Hi everyone I'm not sure why NormalizeDouble is not working here. Can anyone suggest a fix to this?

***

It is working, some numbers just can't be represented perfectly.

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

The documentation is clear when you try to print these numbers:

https://www.mql5.com/en/docs/convert/normalizedouble

"Please note that when output to Journal using the Print() function, a normalized number may contain a greater number of decimal places than you expect"
Documentation on MQL5: Conversion Functions / NormalizeDouble
Documentation on MQL5: Conversion Functions / NormalizeDouble
  • www.mql5.com
NormalizeDouble - Conversion Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

You used NormalizeDouble, It's use is usually wrong, as it is in your case.

  1. Floating point has a infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia, the free encyclopedia

    See also The == operand. - MQL4 programming forum (2013)

  2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

  3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
              On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)

    And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

  4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
              Trailing Bar Entry EA - MQL4 programming forum (2013)
              Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

  5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong.
              Do it right. (2013)

  6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
              MT4:NormalizeDouble - MQL5 programming forum (2017)
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

  7. Prices you get from the terminal are already correct (normalized).

  8. PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum (2014)

 

The code that is inserted correctly:

int OnInit()
  {
   Alert("");
   Alert("Starting Strategy Mid Line");
   double tradeBuyValue[420];
   ArrayInitialize(tradeBuyValue,0);
   for(int i = 0; i<420; i++)
     {
      tradeBuyValue[i] = 90 + NormalizeDouble(i*0.1, 2);
     }
  }

***
   Alert(tradeBuyValue[1]);
   Alert(tradeBuyValue[2]);
   Alert(tradeBuyValue[3]);
   Alert(tradeBuyValue[4]);
   Alert(tradeBuyValue[5]);
***
 
Rounding is better done as the final step in calculations involving floating point numbers, in order to strip the roundoff errors. 

The addition operation itself produces a roundoff error. 

I would code it like this:

      tradeBuyValue[i] = NormalizeDouble(90 + i*0.1, 2);

Additionally, MQL5 Print() and Alert() may produce strings that may be confusing to some people without deep understanding of how floating-point format works at the binary level.

You would better use this custom toString() function to print floating point numbers with extreme accuracy.

So, your code would be like this:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   Alert("");
   Alert("Starting Strategy Mid Line");
   double tradeBuyValue[420];
   ArrayInitialize(tradeBuyValue,0);
   for(int i = 0; i<420; i++)
     {
      tradeBuyValue[i] = NormalizeDouble(90 + i*0.1, 2);
     }

   for(int i = 0; i<420; i++)
     {
      Alert(toString(tradeBuyValue[i]));
     }

  }

//+------------------------------------------------------------------+
//| Converting numeric value into the shortest string representation |
//| that round-trips into the same numeric value. The result may hold|
//| up to the first 17 significant digits, discarding trailing zeros.|
//| The round-trip ("%.17g") format specifier ensures that a numeric |
//| value converted to a string is always parsed back into the same  |
//| numeric value, StringToDouble(toString(f)) == f.                 |
//+------------------------------------------------------------------+
string toString(double value)
  {
   string str = "";
//--- First format with 15 spaces of precision. If round-trip fails,
//--- format with 17 spaces of precision.
   if(value == StringToDouble(str = StringFormat("%.15g", value)))
      return str;

   return StringFormat("%.17g", value);
  }


https://www.mql5.com/en/code/20822

Math Utils
Math Utils
  • www.mql5.com
Handy functions for comparison, rounding, formatting and debugging of doubles (prices, lots and money).
 

The following works:


include <stdlib.mqh>


DoubleToStrMorePrecision(Wert,2)

Reason: