Do I have to NormalizeDouble Everything ? - page 2

 
NormalizeDouble, It's use is usually wrong, as it is in your case.
  1. Floating point has 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

  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 metals. (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum) 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

  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 Metals. So do it right: Trailing Bar Entry EA - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum

  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.

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

  7. Prices you get from the terminal are already normalized.
 
Thank you William, your post helped me! : )
 

Hi Guys, great help from every input, But I'm new to programming and the Normalize double function does not gives consistent output even though used correctly and so I was looking for alternative to Normalize the values of indicator result like RSI, iBands, etc?

I know there are solutions mentioned above and in other forums but, those don't apply because I need this value to be further used for calculations in my code. So, I cannot use DoubletoString, and don't really know what I need to do as I just want to limit the digits to 4 decimals for any values.

For e.g: My code:

double RSI = iRSI(NULL,0,14,0,0);
Alert("RSI : " + RSI);
double NormRSI = NormalizeDouble(RSI,Digits);
Alert("Normalized RSI : " + NormRSI);

Output:

Normalized RSI : 49.04245
RSI : 49.04245028405346

Here, how do I get RSI to 5 digits without using NormalizeDouble?

Appreciate any help and Thanks for reading :)

Improperly formatted code edited by moderator.

Documentation on MQL5: Conversion Functions / DoubleToString
Documentation on MQL5: Conversion Functions / DoubleToString
  • www.mql5.com
DoubleToString - Conversion Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
@Loddy the Redeemed #: Hi Guys, great help from every input, But I'm new to programming and the Normalize double function does not gives consistent output even though used correctly and so I was looking for alternative to Normalize the values of indicator result like RSI, iBands, etc? I know there are solutions mentioned above and in other forums but, those don't apply because I need this value to be further used for calculations in my code. So, I cannot use DoubletoString, and don't really know what I need to do as I just want to limit the digits to 4 decimals for any values. Here, how do I get RSI to 5 digits without using NormalizeDouble?

Appreciate any help and Thanks for reading :)

EDIT: I misread you post. Please read my answer at post #19 further below ...

When outputting values as strings, use the DoubleToString function.

double RSI = iRSI(NULL,0,14,0,0);
Alert("RSI : " + RSI);
Alert("Normalized RSI : " + DoubleToString(RSI,Digits));

Also, please use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Loddy the Redeemed #:

Since the RSI always has 2 decimals, I used this:

double RSI = iRSI(NULL,0,14,0,0);
Alert("RSI: " + (string)RSI);
Alert("Normalized RSI: " + DoubleToString(RSI,2));

If you select "Digits," you will be taking the decimals from the symbol on the chart or the one you have selected in the tester. That's why in the normalized example you showed, I deduce that you used a 5-decimal forex pair.

 
@Miguel Angel Vico Alba #: Since the RSI always has 2 decimals, I used this: If you select "Digits," you will be taking the decimals from the symbol on the chart or the one you have selected in the tester. That's why in the normalized example you showed, I deduce that you used a 5-decimal forex pair.

It's only MetaQuotes rendition of the Indicator that limits the display to two digits, but internally it has more.

RSI is based on an exponential moving average which has been scaled to be between 0 and 100, but at no point is it restricted to only 2 digits in its equation.

Code Base

Wilder's Relative Strength Index

Fernando Carreiro, 2023.01.22 18:12

An implementation of the Relative Strength Index indicator by John Welles Wilder Jr. as described in his book—New Concepts in Technical Trading Systems [1978].
 
Fernando Carreiro #:

It would be better to say that it is the universal interpretation, since in ALL trading platforms it is ALWAYS 2 decimals (it is not exclusive to MetaQuotes).

Obviously I know how the RSI is constructed and that there can be tens of decimals, but I speak for practical purposes based on what we all know and the OP asked for (4 digits, i.e. 00.00 as any platform shows).

In any case the OP wanted to "normalize" to 2 decimals and I have already shown how ....NO using "Digits".

 
Miguel Angel Vico Alba #: It would be better to say that it is the universal interpretation, since in ALL trading platforms it is ALWAYS 2 decimals (it is not exclusive to MetaQuotes). Obviously I know how the RSI is constructed and that there can be tens of decimals, but I speak for practical purposes based on what we all know and the OP asked for (4 digits, i.e. 00.00 as any platform shows). In any case the OP wanted to "normalize" to 2 decimals and I have already shown how ....NO using "Digits".
  1. No it is not universal. For example Investopedia use 4 digits.
  2. The OP (the one both I and you replied to), asked for 4 and 5 digits, not 2.
Loddy the Redeemed #: ... I just want to limit the digits to 4 decimals ... how do I get RSI to 5 digits...

Relative Strength Index (RSI) Indicator Explained With Formula
Relative Strength Index (RSI) Indicator Explained With Formula
  • www.investopedia.com
The relative strength index (RSI) is a momentum indicator used in technical analysis. RSI measures the speed and magnitude of a security's recent price changes to evaluate overvalued or undervalued conditions in the price of that security. The RSI is displayed as an oscillator (a line graph) on a scale of zero to 100. The indicator was...
 
Loddy the Redeemed #: So, I cannot use DoubletoString, and don't really know what I need to do as I just want to limit the digits to 4 decimals for any values.

Now that I read your post again, I now see that you don't want to use DoubleToString.

However, you can not limit a floating point to only 4 digits, because floating point numbers are not decimal. They are binary.

Forum on trading, automated trading systems and testing trading strategies

MathRound fails for one particular number

Fernando Carreiro, 2018.01.01 22:08

He means that the value "0.69" cannot be exactly represented given the way a floating point number works (based on binary and not decimal representation) - hence, why the value gets represented as "0.68999999..." (see below).

You can never really "normalize" it and it is the main reason why both @whroeder1 and myself contest the use of the NormalizeDouble() function. It should in the very least be renamed to something like "RoundDigits()" because it is more of a "rounding" function and does not "normalize" in any way or fashion.


https://www.h-schmidt.net/FloatConverter/IEEE754.html

EDIT: Please note that the above images are for examples of representation in the 4-byte "float", and not the 8-byte "double" which offers more precision but still cannot represent the value "0.69" exactly due to the "binary" nature of the format.

EDIT2: For future readers that have difficulty understanding (or accepting) this concept, of decimal values not having an exact representation with a "float" or a "double", here is an article worth reading:

Why 0.1 Does Not Exist In Floating-Point

Many new programmers become aware of binary floating-point after seeing their programs give odd results: “Why does my program print 0.10000000000000001 when I enter 0.1?”; “Why does 0.3 + 0.6 = 0.89999999999999991?”; “Why does 6 * 0.1 not equal 0.6?” Questions like these are asked every day, on online forums like stackoverflow.com.

The answer is that most decimals have infinite representations in binary. Take 0.1 for example. It’s one of the simplest decimals you can think of, and yet it looks so complicated in binary:


Decimal 0.1 In Binary ( To 1369 Places

The bits go on forever; no matter how many of those bits you store in a computer, you will never end up with the binary equivalent of decimal 0.1.

... Read the rest of the article at: http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/

 
Fernando Carreiro #:
  1. No it is not universal. For example Investopedia use 4 digits.
  2. The OP (the one both I and you replied to), asked for 4 and 5 digits, not 2.

1.- I said trading platform. Not a spreadsheet based study.

2.- Correct, the OP asked for 4 decimals (I understood digits). It simply has to change the 2 to a 4. Valid whether you use NormalizeDouble or DoubleToString.


Reason: