NormalizePrice() any suggestions on how to do this better - page 4

 

Dominik, this function has a more predictable output. You could try it

//+------------------------------------------------------------------+
//| Round a calculated price to the nearest tick price.              |
//+------------------------------------------------------------------+
double NormalizePrice(double pPrice, string pSymbol = NULL)
  {
   pSymbol = pSymbol == NULL ? _Symbol : pSymbol;

   double ticksize = SymbolInfoDouble(pSymbol, SYMBOL_TRADE_TICK_SIZE);

   if(ticksize == 0)
     {
      Print(__FUNCTION__, ": error, cannot retrieve ticksize for " + pSymbol);
      return (0);
     }

   // this calculation suffer from roundoff errors
   //return MathRound(pPrice / ticksize) * ticksize;

   double ticks = MathRound(StripError(pPrice / ticksize));
   return StripError(ticks * ticksize);
  }

//+------------------------------------------------------------------+
//| Eliminate the floating-point binary round-off error.             |
//+------------------------------------------------------------------+
double StripError(double num)
  {
//--- Round to 15 significant digits
   return StringToDouble(StringFormat("%.15g", num));
  }

 
Thank you for the effort.

So basically you are saying, printf has it implemented, right?

So I will lookup printf implementation.
 
Dominik Christian Egert #:
Thank you for the effort.

So basically you are saying, printf has it implemented, right?

So I will lookup printf implementation.
I think you need to orient yourself by the binary floating point format first. Good luck. 
Reason: