rounding problem

 

   double roundUp(double price){
   double ticksize = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE),_Digits);
   price = NormalizeDouble(price,_Digits);
   
   double rest = price - NormalizeDouble(price/ticksize,0)*ticksize;
   rest = NormalizeDouble(rest,_Digits);
   if(rest != 0){
      for(double i = _Point;i <= ticksize;i = i + _Point){
         price = NormalizeDouble(price + _Point,_Digits);
         rest = price - NormalizeDouble(price/ticksize,0)*ticksize;
         rest = NormalizeDouble(rest,_Digits);
         if(rest == 0) break;
      }
   }
   return price;
}

double roundDn(double price){
   double ticksize = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE),_Digits);
   price = NormalizeDouble(price,_Digits);
   
   double rest = price - (NormalizeDouble(price/ticksize,0)*ticksize);
   rest = NormalizeDouble(rest,_Digits);
   if(rest != 0){
      for(double i = _Point;i <= ticksize;i = i + _Point){
         price = NormalizeDouble(price - _Point,_Digits);
         rest = price - NormalizeDouble(price/ticksize,0)*ticksize;
         rest = NormalizeDouble(rest,_Digits);
         if(rest == 0) break;
      }
   }
   return price;
               }
 

Forum on trading, automated trading systems and testing trading strategies

rounding problem

Yango, 2022.04.24 09:46

hello, these 3 lines are set and the prizes are displayed. unfortunately he gives the price of the white line unrounded and i don't know why. I can't get any further with NormalizeDouble..




 
Sergey Golubev #:


Thank you for your prompt reply. i will read this
 

for the most part he rounds it correctly, but sometimes he still has problems and gives me the price with too many decimal places.. in the backtest


it works

it does not work

double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE ); 

double price_red_middle_position   = 0;

price_red_middle_position   = NormalizeDouble(MathRound(ObjectGetDouble(ChartID(),objects_lines,OBJPROP_PRICE)/tickSize)*tickSize,_Digits);

 

You can can use these functions for accurate rounding: 

double RoundToStepUp(double num, double step);

double RoundToStepDown(double num, double step);

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).
 
It works, I managed it with the great functions :) Thank you very much
 
Yango #: s he still has problems and gives me the price with too many decimal places..

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

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

If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
          question about decima of marketinfo() - MQL4 programming forum (2016)

Reason: