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 />
Let's take a moment to talk about the Mode parameter. Some indicators draw multiple lines on the<br />
chart. The stochastic has two. We will need to call the iStochastic() function for both the %K and<br />
%D lines, as shown below:<br />
double KLine = iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MAMethod,Price,0,0);<br />
double DLine = iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MAMethod,Price,1,0);<br />
Note that the Mode parameter is 0 for the %K line, and 1 for the %D line. The MQL Reference topic,<br />
Standard Constants – Indicator lines lists the valid integer constants for the various indicators that<br />
use the Mode parameter.<br />
You can generate trade signals based on the relationship between the indicator lines and certain<br />
indicator levels, such as the overbought and oversold levels of 70 and 30, respectively. You can also<br />
evaluate trade signals based on the indicator lines' relationship to each other. For example, you may<br />
want to open a buy order only when the %K line is above the %D line. Here are some example<br />
conditions:<br />
if(KLine < 70) // Buy if stochastic is not overbought<br />
if(KLine > DLine) // Buy if %K is greater than %D<br />
The built-in indicator functions are in the MQL Reference under Technical indicators. If you'd like<br />
more information on an indicator's usage or method of calculation, consult the technical analysis<br />
section of the MQL website at http://ta.mql4.com/.<br />
Custom Indicators<br />
Hundreds of custom indicators for MetaTrader are available online. If you decide to use a custom<br />
indicator in your expert advisor, a little legwork will have to be done. It is best if you have the .mq4<br />
source code file when using a custom indicator. While it is possible to use a custom indicator without<br />
it, having the source code will make it easier to figure out the buffer indexes for the Mode parameter.<br />
MQL has a built-in function for handling custom indicators – iCustom(). Here is the syntax:<br />
double iCustom(string Symbol, int Timeframe, string IndicatorName, Indicator Parameters,<br />
int Mode, int Shift);<br />
You're already familiar with Symbol, Timeframe, Mode and Shift from earlier in the chapter. Let's<br />
start with IndicatorName. This is the name of the indicator file, exactly as it appears in the Custom<br />
Indicators list in the Navigator window. For example, "Slope Direction Line", or "super_signal".<br />
98