metatrader4 - Why is an information in my printf() function incorrect in MQL4? -
my print formatting giving me random answers, despite coding being correct. i'm trying display candle bar lengths of previous 2 candle bars prior trade entry. here coding i've used.
pcl1
& pcl2
relevant field entries. divided _point
give integer formatting.
pcl2
= previous candle stick length, shift 2
pcl1
= previous candle stick length, shift 1
in example have focused on shift2 candlestick
short_bull_2_close = iclose( symbol(), 0, 2 ); short_bull_2_open = iopen( symbol(), 0, 2 ); candlebody_2 = ( short_bull_2_close - short_bull_2_open ); // gives candlebody length in pips.
and printf()
coding:
printf( "pcl1 [%d] pcl2 [%d]", candlebody_1 / point, candlebody_2 / point ); // ___________________________________________________________ sell//
1) mql4 strong typed language ( declaration precedes use of variable )
+ mql4 has historical feature of number "normalisation"
an assumption
(cit.)
"they divided_point
give integer" does not hold.
once number has been declared double
in source code as:
double upfractal = 0.0, dnfractal = 0.0;
the mql4-compiler handles these numbers forever in ieee-754 floating point number representation , calls particular sub-version of operations on arithmetics operation such number entring into. reason simple, double
has different handling ( new-mql4.56789
) float
, more int
et al. numbers, represented in ieee-754, inexact by-definition, remain exact ( if happen direct power-of-2 , still in it's "virgin"-state ( have not yet been spoiled arithmetic operation, introduce un-avoidable im-precision binary-representatio ) ).
why im-precision?
why un-avoidable?
non-virgin numbers, binary-representation may yield infinitely-long description of value. on contrary, storage of value in mql4 restricted { 1 | 4 | 8 }
-bytes of memory-space , infinite-representation un-avoidably cut @ distance, introducing principal im-precision , expressed by:
dbl_epsilon
~ 2.2204460492503131e-016
flt_epsilon
~ 1.192092896e-07
beyond 2 numbers, represented in ieee-754 binary-format, compiled code "equal", if not principally equal, due ieee-754 format inherent im-precision.
for indeed historical reasons, double
typed values ought have been normalizedouble()
converted, if 1 wants send these metatrader server-side processing and/or compare values on fair ground.
2) mql4 operations {double | int }
- typed variables
if 1 had declared variable double
, remains double
forever.
if 1 had declared variable int
, int/3
remain int
, ignoring fractional products of division operation ( causes headaches not carefull mql4 coders ).
the same applied initial assumption above yields surprise, double/double -> double
in cases, division satisfactorily considered ( ) integer. never assume type conversion happen values of operands.
for purpose, there 2 sorts of syntax-supports:
- an explicit conversion function
int( ... )
ordouble( ... )
- an inline typecast directive
( (int) candlebody_1 / _point )
epilogue: ( ... must read custom indicator designers, cpu-performance kills )
said above, calling
(int) normalizedouble(...)
double-nonsense ( beyond belt + suspenders paradigm ), first, historically built-in functionnormalizedouble
still returnsdouble
, providing zero-benefit spending cpu crunch numbers once still yieldsdouble
, next division of numberdouble
value of_point
expensive , inefficient, compared fact,_point
relatedint
value ofdigits
formula of1 / 10^digits
-- fastest , cleanest way conversion, without spending avoidable cpu-cycles, multiplication ...
yes,
from computer-side of fast & efficient processing, the best isint( make_apricedomain_value_an_int_num_of_points * candlebody_1 )
,
re-using constantint make_apricedomain_value_an_int_num_of_points = mathpow( 10, digits );
Comments
Post a Comment