From doubles to integers

 

Hi;

I want to perform a number of calculations that use double variables (Open[], Close[], Ma Value, iHighest, iLowest, etc.)

My problem is that no matter which conversion function I use (NormalizeDouble, MathAbs, MathRound, (int), etc.) I can not get 

whole numbers (integers) on a continuous basis.  Ever so often there will be a value that is a slue of digits instead of

a whole number and when that happens it throws my entire formula out of sinc.


I am using _Digits to comply with decimal positions.  Any suggestions?


Thanks

 
  1. iHighest/iLowest does not return doubles.
  2. int o = int(Open[0] / _Point + 0.5);
  3. Power(10,_digits) equals 1/_Point
 
Lode Loyens: I want to perform a number of calculations that use double variables (Open[], Close[], Ma Value, iHighest, iLowest, etc.). My problem is that no matter which conversion function I use (NormalizeDouble, MathAbs, MathRound, (int), etc.) I can not get whole numbers (integers) on a continuous basis.  Ever so often there will be a value that is a slue of digits instead of a whole number and when that happens it throws my entire formula out of sinc. I am using _Digits to comply with decimal positions.  Any suggestions?
Show your code attempt. Your explanation is insufficient.
 

As requested: partial code:

      // define MACD MAIN and MACD SIGNAL settings
      double MACDMain=iMACD(NULL,0,Fast_Ema_Period,Slow_Ema_Period,Sig_Sma_period,PRICE_CLOSE,MODE_MAIN,0);
      double MACDSignal=iMACD(NULL,0,Fast_Ema_Period,Slow_Ema_Period,Sig_Sma_period,PRICE_CLOSE,MODE_SIGNAL,0);
      
      // get FAST and SLOW MA settings and values based on current and previous candles
      double FastMA = iMA(NULL,0,FAST_MA_Period,FAST_MA_Shift,FAST_MA_Method,FAST_MA_Price,0);  // 0 = current candle
      double SlowMA = iMA(NULL,0,SLOW_MA_Period,SLOW_MA_Shift,SLOW_MA_Method,SLOW_MA_Price,0);     
      
      double prevFastMA = iMA(NULL,0,FAST_MA_Period,FAST_MA_Shift,FAST_MA_Method,FAST_MA_Price,1);  // 1 = previous candle
      double prevSlowMA = iMA(NULL,0,SLOW_MA_Period,SLOW_MA_Shift,SLOW_MA_Method,SLOW_MA_Price,1);            
      
      // get current candle open and close price
      double priceOpen  = iOpen (NULL,0,0);
      double priceClose = iClose (NULL,0,0);  
      long priceVolume  = iVolume (NULL,0,0);         
      
      // get current candle high and low values
      candleHigh = High[0];
      candleLow  = Low[0];        
      
      // adjust values to proper digit size
      MACDMain   = NormalizeDouble(MACDMain,_Digits);
      MACDSignal = NormalizeDouble(MACDSignal,_Digits);
      FastMA     = NormalizeDouble(FastMA,_Digits);
      SlowMA     = NormalizeDouble(SlowMA,_Digits);      
      prevFastMA = NormalizeDouble(prevFastMA,_Digits);
      prevSlowMA = NormalizeDouble(prevSlowMA,_Digits);    
      priceOpen  = NormalizeDouble(priceOpen,_Digits);
      priceClose = NormalizeDouble(priceClose,_Digits);    


Additional code:  Display of output on chart

     ObjectCreate("Symboli", OBJ_LABEL,0,0,0);
     ObjectSetText("Symboli","MA Gap: "+(string)minMaGap+" ("+(string)maGapValue+") "+(string)maxMaGap+" - "+maGapFlag, 9, "Arial",Gray);     
     ObjectSet("Symboli", OBJPROP_CORNER, WhichCorner);
     ObjectSet("Symboli", OBJPROP_XDISTANCE, 310);  //left to right
     ObjectSet("Symboli", OBJPROP_YDISTANCE, 20);  //top to bottom      

     ObjectCreate("Symbol1w", OBJ_LABEL,0,0,0);
     ObjectSetText("Symbol1w","Candle Gap: "+(string)mincandleGap+" ("+(string)newcandleGap+") "+(string)maxcandleGap+" pips - N", 9, "Arial",Gray);  
     ObjectSet("Symbol1w", OBJPROP_CORNER, WhichCorner);
     ObjectSet("Symbol1w", OBJPROP_XDISTANCE, 310);  //left to right
     ObjectSet("Symbol1w", OBJPROP_YDISTANCE, 5);  //top to bottom  

 

Forgot to add code for output values:

maGapValue   = (FastMA - SlowMA); 
newcandleGap = (FastMA - priceOpen); 


Actual output:

Results


Originally I used NormalizeDouble for the output including (string) but the result is the same.  The same for MathAbs, MathRound, MathFloor & MathCeil.


Thanks.

 
Lode Loyens #: Originally I used NormalizeDouble for the output including (string) but the result is the same.  The same for MathAbs, MathRound, MathFloor & MathCeil.

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)

 

William;

You did it (again).  The int( .... / _Point) formula did the trick.

Thank you sir.