simple "Bar Size" function return with error. please help

 
//+------------------------------------------------------------------+
//|                                                          tst.mq4 |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

void OnTick()
  {
   barCalculations();
   
  }
//+------------------------------------------------------------------+
void barCalculations()
  {   
   double barSize=MathAbs(NormalizeDouble(Close[1]-Open[1],Digits));
   Print(barSize);
  }

the value returning with errors in some cases (see picture)

please help and explain why if possible.

Files:
1.jpg  250 kb
 

I recommend you read William Roeder's comments on not using NormalizeDouble (and when you would actually want to normalize a double).


If you need a user-friendly way of outputting the double value to a GUI or user, use DoubleToString.

void barCalculations()
  {   
   double barSize=MathAbs(Close[1]-Open[1]);
   Print(DoubleToString(barSize, Digits));
  }

I did a side-by-side comparison of the way you did it before and the proper way to output a double to a user/gui (using DoubleToString). Here are two of the results I got. The left was with your method, the right was with DoubleToString.


MT4:NormalizeDouble
MT4:NormalizeDouble
  • 2017.01.04
  • www.mql5.com
a simple question please: in order to remove the digits after the ...
 

thank you wery much for your reply. i will read the info.

 

NP and hope it could help 👍

Other things I recommend everyone read from William is when to use TickSize over Point and how to properly compare doubles.

The part about comparing doubles actually applies to every programming language. Here's one video (Java) that explains it in layman's term.

Note that the Epsilon for price will always be Point while the Epsilon for any other type of double is whatever accuracy you define.


Point - Predefined Variables - MQL4 Reference
Point - Predefined Variables - MQL4 Reference
  • docs.mql4.com
Point - Predefined Variables - MQL4 Reference
 

 👍 👍 👍

 
It's not Barsize it's Print.
Reason: