NormalizeDouble(Bid, Digits) - page 2

To add comments, please log in or register
William Roeder
20339
William Roeder  
Petr Nosek: What is wrong with this code:
Assumes that ND rounds or truncates. Write self-documenting code and specify exactly which you mean.
          MT4:NormalizeDouble - General - MQL5 programming forum
          How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum
nicholi shen
2400
nicholi shen  
whroeder1:
Assumes that ND rounds or truncates. Write self-documenting code and specify exactly which you mean.
          MT4:NormalizeDouble - General - MQL5 programming forum
          How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum

This is wrong. 

double MathNearest(  double v, double to){ return to * MathRound(v / to); }

it should be

double MathNearest(  double v, double to){ return to * NormalizeDouble(v / to, 0); }
William Roeder
20339
William Roeder  
nicholishen: This is wrong. it should be
You assume ND(x,0) rounds to the nearest integer. MathRound always does to the nearest int by definition. ND use is wrong.
Petr Nosek
3132
Petr Nosek  
whroeder1:
Assumes that ND rounds or truncates. Write self-documenting code and specify exactly which you mean.

Is there anyone else who did not understand this simple example?
For whroeder1:
In this example I have a variable "profitUSD" (self-documenting). Its value is e.g. profit in USD and I want to find out profit in EUR but just on two decimal places. I use ND to set the variable "profitEUR" (self-documenting).
And now I want you to explain what is wrong with using ND.

nicholi shen
2400
nicholi shen  
whroeder1:
You assume ND(x,0) rounds to the nearest integer. MathRound always does to the nearest int by definition. ND use is wrong.

What is this nonsense? Neither of those returns an int!


Let's prove this quantitatively with a simple script. 

void OnStart()
{
   double test_number = 1.700605;
   double step = 0.00001;
   string result_str = "The expected result is 1.70061, and the actual result is";
   printf(
      "method: \"MathRound()\", %s %s",
      result_str,
      DoubleToString(step * round(test_number / step),5)
   );
   
   printf(
      "method: \"NormalizeDouble()\", %s %s",
      result_str,
      DoubleToString(step * NormalizeDouble(test_number / step, 0),5)
   ); 
}

SPOILER ALERT!!!

SPOILERS

nicholi shen
2400
nicholi shen  
I just wanted to comment that there's no shame in admitting when one is wrong. That's what science is all about, adapting to new evidence as it is brought to light. I, for one, will gladly admit to being proved wrong with new evidence - because I'd rather be correct and humble than arrogant and wrong. That being said, if there's anyone in particular whro's in denial, I say, embrace the new evidence! Perhaps a simple change in language is the difference between being correct and being totally wrong... perhaps one should change one's phrasing to something like, "its use is always usually wrong".
Petr Nosek
3132
Petr Nosek  
nicholishen:
I just wanted to comment that there's no shame in admitting when one is wrong. That's what science is all about, adapting to new evidence as it is brought to light. I, for one, will gladly admit to being proved wrong with new evidence - because I'd rather be correct and humble than arrogant and wrong. That being said, if there's anyone in particular whro's in denial, I say, embrace the new evidence! Perhaps a simple change in language is the difference between being correct and being totally wrong... perhaps one should change one's phrasing to something like, "its use is always usually wrong".
I totally agree with this.
William Roeder
20339
William Roeder  
printf("%7.2f %7.2f %7.5f",test_number / step, round(test_number / step),step * round(test_number / step));// 170060.50 170060.00 1.70060
Congratulations, you found a bug in MathRound.
nicholi shen
2400
nicholi shen  
whroeder1:
Congratulations, you found a bug in MathRound.

Ok... next step... since NormalizeDouble works correctly - what is your final conclusion?

Alain Verleyen
38992
Alain Verleyen  
Petr Nosek:
I totally agree with this.

Me too. That's a five years old discussion with WHRoeder, he never understood it, I never understood why he didn't.

Forum on trading, automated trading systems and testing trading strategies

Round Number

Alain Verleyen, 2013.08.29 16:00

Of course it's the simplest solution. It's not because WHRoeder don't use NormalizeDouble that you can't use it.

To add comments, please log in or register