NormalizeDouble not "working" - page 2

 
Mohamad Zulhairi Baba:
Well-done.
 
Mohamad Zulhairi Baba:

MarketInfo() is for mql4 which is the mql5 alternative??


---

Nvm: found it here SYMBOL_TRADE_TICK_SIZE

 
AWer1001: MarketInfo() is for mql4 which is the mql5 alternative?

This is valid for both MQL4 and MQL5:

double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
 
Mohamad Zulhairi Baba:

Why are you still insisting on using NormalizeDouble()? It is a kludge! It is useless! If you do things correctly, you don't need it!

 
Probably because the documentation examples it like everywhere ?
 
Marco vd Heijden: Probably because the documentation examples it like everywhere ?

Yes, that is unfortunately that MetaQuotes sets the wrong example for the coders!

 

Just as a side note.

Initially, when I started with MQL, I would manipulate prices as "doubles". Nowadays, especially in the more complex EA's, I manipulate prices as "ints". At the very first opportunity, I convert a price into ticks:

int priceTicks = (int) round( price / tickSize );

From then on, all my calculations and manipulations are done with "ints". Not only is it more memory compact and much faster, but comparisons are much easier to handle.  Doing a "priceA == priceB" for "doubles" is quite problematic, but not for "ints" because it gives exact matches. Not to mention, that in this way prices, stop sizes, etc. are ALWAYS aligned.

Then, just before I have to place or modify an order, I then convert it back:

priceTicks * tickSize
EDIT: I do the same for volume/lots by using the broker's Lot-Step.
 
I concur with Fernando.
Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
See also
          MT4:NormalizeDouble - General - MQL5 programming forum
           How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum
 
Fernando Carreiro:

Just as a side note.

Initially, when I started with MQL, I would manipulate prices as "doubles". Nowadays, especially in the more complex EA's, I manipulate prices as "ints". At the very first opportunity, I convert a price into ticks:

From then on, all my calculations and manipulations are done with "ints". Not only is it more memory compact and much faster, but comparisons are much easier to handle.  Doing a "priceA == priceB" for "doubles" is quite problematic, but not for "ints" because it gives exact matches. Not to mention, that in this way prices, stop sizes, etc. are ALWAYS aligned.

Then, just before I have to place or modify an order, I then convert it back:

EDIT: I do the same for volume/lots by using the broker's Lot-Step.

Good idea. :thumbsup:

 
It's not only about price, though.. there are various other indicating fractions, besides the price, that one wants to follow.. and its a pain in the ***, cause formatting them via string manipulation is costly in terms of calculations


string myRound( double in ){

   string result[];  
   
   StringSplit( in, 46, result );
   
   return result[ 0 ] +"."+ round( (double) result[ 1 ] / MathPow( 10, StringLen(result[1])-2) );
}