Expert Advisor Programming by Andrew R. Young
Expert Advisor Programming by Andrew R. Young
Expert Advisor Programming by Andrew R. Young
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
EXPERT ADVISOR PROGRAMMING<br />
Here's another example using moving averages. We'll create a condition to open an order only when<br />
the FastMA and the SlowMA have crossed within the last bar:<br />
double FastMA = iMA(NULL,0,FastMAPeriod,0,0,0,0);<br />
double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);<br />
double LastFastMA = iMA(NULL,0,FastMAPeriod,0,0,0,1);<br />
double LastSlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,1);<br />
if(FastMA > SlowMA && LastFastMA = LastSlowMA<br />
&& SellMarketCount(Symbol(),MagicNumber) == 0)<br />
{<br />
// Open sell order<br />
}<br />
In this example, we're comparing the relationship of two indicators to each other. LastFastMA and<br />
LastSlowMA return the moving average values for the previous bar. If LastFastMA is less than (or<br />
equal to) LastSlowMA, and FastMA is currently greater than SlowMA, then we know that the fast<br />
moving average line has crossed above the slow moving average line within the last bar.<br />
This provides a reliable trading signal, since we can limit our order placement to right after the cross<br />
occurs. You can change the Shift value for the LastFastMA and LastSlowMA functions if you want<br />
to increase the number of bars to look back when finding an indicator cross.<br />
We've added the LastFastMA and LastSlowMA comparison to our buy and sell order conditions in<br />
our expert advisor. We can now remove the BuyTicket and SellTicket check, since this method is<br />
more reliable that checking a stored order ticket number. We also don't have to worry about orders<br />
being placed well after the cross has occurred. See the expert advisor code in Appendix C to view all<br />
of the changes.<br />
110