Anyone come acrross these type of errors???

 

I have EA in which there is IF statement and based on the IF expression certain bool variabl;es are set.


Here is the piece of code:


Print("Just before IF ","(",poss_sell_sig," && (",low_4_20_prev,"<=",low_2_15_prev,") && (",low_4_20,">",low_2_15,"))");

if(poss_sell_sig && (low_4_20_prev<=low_2_15_prev) && (low_4_20>low_2_15)){

sell_trade=true;

buy_trade=false;

buy_close=true;

Print("It is here");

}

if(debug){

Print("AFter SELL POSS_b-BUY_t-POSS_s-SELL_t==BUY_c-SELL_c ===",poss_buy_sig,"-",buy_trade,"-",poss_sell_sig,"-",sell_trade,

"--",buy_close,"-",sell_close);

Print("=====================================================================");

}


The correspoding print output from Journal is as follows:

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: =====================================================================

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: H_2_15_prev == H_4_20_prev ==> 1.3635--1.3665

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: H_2_15 == H_4_20 ==> 1.3628--1.3664

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: L_2_15_prev == L_4_20_prev ==> 1.3621--1.3621

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: L_2_15 == L_4_20 ==> 1.3612--1.3619

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: Just before IF (1 && (1.3621<=1.3621) && (1.3619>1.3612))

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: AFter SELL POSS_b-BUY_t-POSS_s-SELL_t==BUY_c-SELL_c ===0-1-1-0--1-0

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: =====================================================================

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: After BUY POSS_b-BUY_t-POSS_s-SELL_t==BUY_c-SELL_c ===0-1-1-0--1-0

14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: =====================================================================


As you can see the print output of the corresponding IF statement --- if(poss_sell_sig && (low_4_20_prev<=low_2_15_prev) && (low_4_20>low_2_15)){


Just before IF is excuted is --- 14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: Just before IF (1 && (1.3621<=1.3621) && (1.3619>1.3612))


The above IF statemnet is excuted as TRUE therefore the following boolean statement should be excuted and corresponding valuse should be printed out in next Print statemetn:


sell_trade=true;

buy_trade=false;

buy_close=true;



BUT from the Print statement under debug flag it can be seen that above variables are not SET properly.

The Print statement output is as follows:


14:29:06 2008.10.15 02:00 myGrail_10K_to_1_6M_debug EURUSD,H1: AFter SELL POSS_b-BUY_t-POSS_s-SELL_t==BUY_c-SELL_c ===0-1-1-0--1-0

Here is the Print statement for above:

Print("AFter SELL POSS_b-BUY_t-POSS_s-SELL_t==BUY_c-SELL_c ===",poss_buy_sig,"-",buy_trade,"-",poss_sell_sig,"-",sell_trade,

"--",buy_close,"-",sell_close);


---In the above print output:

sell_trade must be 1 but it is '0'

buy_trade should be '0' but it is '1'

buy_close is ok.


Looks like there is problem in evaluating IF statement although based on values in expression it should have evqaluated true but it is not happening.


Can someone tell me if they experience such thing in any EA programs they have run. If so can someone kindly point out the probable cause of such anamalous behaviour of MT4 platform?

Your feedback will be greatly appreciated.

Thanks

NetFX.



 

"...Just before IF (1 && (1.3621<=1.3621) && (1.3619>1.3612))"

1.3621 may or may not be less than or equal to 1.3621

Print more decimal places or normalize the double values before the compare

 
phy:

"...Just before IF (1 && (1.3621<=1.3621) && (1.3619>1.3612))"

1.3621 may or may not be less than or equal to 1.3621

Print more decimal places or normalize the double values before the compare

Thanks Phy for your suggestion.

Does that mean Print statemnet truncate double number by 4 digits after decimal by default?

Is that the reason why it is printing both number as same even if they may not be same if more than 4 digit after decimal are considered?

If so, why such limitation?

 
netFX wrote >>

Does that mean Print statemnet truncate double number by 4 digits after decimal by default?

Yes

Is that the reason why it is printing both number as same even if they may not be same if more than 4 digit after decimal are considered?

Yes - check out https://docs.mql4.com/common/Print

If so, why such limitation?

Lost in the mists of time...

-BB-

 

Not all decimal numbers can be exactly represented in binary.

Two different calculations that "should" give the same result may not.

It is an old known problem.

http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html

There is a starting point for your journey into "why we avoid comparing doubles for equality"

.

For example, the decimal number 0.1 is not representable in binary floating-point of any finite precision:

0.1 decimal = floating point binary 1 .1001100110011001100110011001100110011001100110011001100110011001100 .... forever

Reason: