You're reading...
EA development, MQL4

Do mql4 AND and OR operators have short circuit evaluation?

Some background on short circuit evaluation (skip this if you already know what it is):


Examine this if statement with a logical OR operator:

int A=1;
int B=2;
if(A=1 || B=6){
   do stuff;
}

If there is short circuit evaluation(“SCE”), as long as the leftmost condition fulfills, the entire if OR statement fulfills and passes control to the next line.

If there is no SCE, even though A may be true, the code will still check what B is.

Similarly for an if statement with a logical AND operator:

int A=1;
int B=2;
if(A!=1 && B=2){
   do stuff;
}

With SCE, A!=1 returns FALSE hence does not proceed to check what B is, resulting in a correct FALSE output for the whole if statement.

Without SCE, even though A!=1 returns FALSE, B is still checked even though we already know the if statement will output FALSE regardless of what the value of B is.


How can mql4’s AND and OR operators be checked for short circuit evaluation?

As a breakpoint can’t be placed within an if statement in C much less mql4 (I’m sure someone will be able to point out otherwise), a workaround is to make the 2nd operand an illegal operation and the 1st operand not lead up to it.


A simple EA to test the OR operator:

mql4 OR Short Circuit Evaluation Test.mq4

extern double numerator   = 1;
extern double denominator = 0;

int init(){
   if(1>0 || numerator/denominator)Print("numerator/denominator = ", numerator/denominator);
   return(0);
}

int start(){
   return(0);
}

Since the left operand, 1 > 0, returns TRUE, we can expect the whole if statement to return TRUE.

With SCE, the 2nd operand, numerator/denominator, is not performed.

Without SCE, the 2nd operand will execute regardless that the 1st operand already returned TRUE, and thereby giving a zero divide error for denominator with value of 0.

Notes:
1. Added the numerator and denominator as external variables to play around with, the statement could simple be written as if(1>0 || 1/0).
2. Initially wrote it as a script but the zero divide prevented any printing in the Journal tab, hence it’s an EA to be run in the Strategy Tester.

With denominator=2, we see:

OR_no zero divide


 

When run in the Strategy Tester, with denominator=0, we see:

OR_zero divide


 

Hence, mql4’s OR operator does not have SCE.


Similarly for the AND operator:

mql4 AND Short Circuit Evaluation Test.mq4

extern double numerator   = 1;
extern double denominator = 0;

int init(){
   if(denominator!=0 && numerator/denominator)Print("numerator/denominator = ", numerator/denominator);
   return(0);
}

int start(){
   return(0);
}

Since the left operand, denominator!=0, returns FALSE, we expect that the whole if statement will return FALSE.

With SCE, the 2nd operand, numerator/denominator, is not performed.

Without SCE, the 2nd operand will execute even though the 1st operand already returned FALSE, and thereby giving a zero divide error for denominator with value of 0.

With denominator=2, we see:

AND_no zero divide

 

 

When run in the Strategy Tester, with denominator=0, we see:

AND_zero divide

 

 

Ergo, mql4’s AND operator does not have SCE too.


Unlike ANSI-standard C/C++, mql4’s AND and OR operators do not perform SCE.

What’s the big hairy point?
1. SCE makes code run faster, particularly for multiple AND or OR operations. Two things that trading algorithms are particularly fond of.
2. While improvements will be in the sub-ms range per pass, backtesting should be sped up for large histories.

For mql4, you can work around this by cascading your if statements for AND operations, and duplicating results for OR operations.


Couple that with efficient function calling (more on that in a future post), I’ve shaved about 30% off my backtesting time, which is insanely time consuming.

Update 2013-04-03:

A good way to avoid || or && is to convert the multiple || or && statement into a single arithmetic to be assigned to a variable to be polled.

Advertisements

Discussion

Trackbacks/Pingbacks

  1. Pingback: Unified mql4 and mql5 MetaEditor | RaidenWorks - 2013-09-03

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives

Visitors

Flag Counter
%d bloggers like this: