25.10.2016 Views

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXPERT ADVISOR PROGRAMMING<br />

if((BooleanVar1 == true && Indicator1 > Indicator2) || BooleanVar1 == false)<br />

The statement (BooleanVar1 == true && Indicator1 > Indicator2) is evaluated first. If both<br />

of these conditions are true, the statement evaluates to true, and we are left with an OR operation:<br />

if(true || BooleanVar1 == false)<br />

This statement automatically evaluates to true, since one of the conditions is already true. But what if<br />

(BooleanVar1 == true && Indicator1 > Indicator2) evaluates to false?<br />

if(false || BooleanVar1 == false)<br />

If the condition BooleanVar1 == false evaluates to true, then the entire statement is true. (In<br />

other words, if BooleanVar1 is set to false, that condition evaluates to true.) Otherwise, the<br />

statement is false.<br />

It's possible to create complex boolean operations using AND, OR and parentheses to control the<br />

order of operations. Be sure to watch the locations of your parentheses, as one wrong parenthesis<br />

can cause the statement to evaluate differently, and a missing parenthesis could lead to some tedious<br />

debugging.<br />

Turning An Indicator On and Off<br />

You can use the AND/OR example in the previous section to turn an indicator on and off. Let's say<br />

your EA uses multiple indicators, and you'd like to be able to switch indicators on and off. Here's how<br />

we do it. First, let's declare an external boolean variable to use as the on/off switch. We'll use the<br />

stochastic indicator in this example:<br />

extern bool UseStochastic = true;<br />

We define two sets of conditions for our indicator – an "on" state and an "off" state. The on state<br />

consists of the on/off variable being set to true, along with the order opening condition. The off state<br />

simply consists of the on/off variable being set to false.<br />

if((UseStochastic == true && Kline > Dline) || UseStochastic == false)<br />

{<br />

// Buy order<br />

}<br />

106

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!