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.

Order Conditions and Indicators<br />

In this example, MA is the variable that holds the current bar's indicator value, while LastMA holds the<br />

previous bar's indicator value. Note that the Shift parameter is 0 for the current bar, and 1 for the<br />

previous bar.<br />

Here is the code to determine whether a moving average line is moving up or down:<br />

if(MA > LastMA)<br />

{<br />

// MA is going up<br />

}<br />

else if(MA < LastMA)<br />

{<br />

// MA is going down<br />

}<br />

If the indicator value of the current bar (MA) is greater than the value of the previous bar (LastMA),<br />

we can conclude that the indicator is moving up. The reverse is true when the current bar's indicator<br />

value is less than the previous bar's indicator value.<br />

By comparing the indicator value of a previous bar to the current one, we can determine whether the<br />

indicator has recently crossed above or below a certain value, such the overbought/oversold levels of<br />

an oscillator, or another indicator line.<br />

For example, let's say your trading system gives a trade signal when the stochastic passes above 30<br />

or below 70. Here is the code to check for that:<br />

double Stoch = iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MAMethod,Price,0,0);<br />

double LastStoch = iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MAMethod,Price,0,1);<br />

if(Stoch > 30 && LastStoch < 30)<br />

{<br />

// Open buy order<br />

}<br />

if(Stoch < 70 && LastStoch > 70)<br />

{<br />

// Open sell order<br />

}<br />

Stoch is the indicator value of the current bar, while LastStoch is the indicator value of the previous<br />

bar. If Stoch is greater than 30 and LastStoch is less than 30, we can conclude that the indicator<br />

crossed above the oversold level within the last bar. By reversing the comparison operators, we can<br />

check for a recent cross below a constant value, such as the overbought level of 70.<br />

109

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

Saved successfully!

Ooh no, something went wrong!