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 />

Chapter 9<br />

Custom Indicators and Scripts<br />

No book on MQL would be complete without covering custom indicators and scripts. The built-in<br />

indicators in MetaTrader are rather limited, but fortunately MQL allows programmers to create their<br />

own indicators. If you're looking for a popular indicator that is not included in MT4, chances are<br />

someone has already created one.<br />

This chapter will be a basic overview of custom indicator creation. Most indicators use complex<br />

mathematical formulas, and as such are the domain of more experienced programmers. However, an<br />

indicator need not be complex. We'll create a custom indicator in this chapter that uses only a few<br />

lines of code.<br />

Buffers<br />

Buffers are arrays that store indicator values and calculations. A custom indicator can have up to 8<br />

buffers. Buffers use indexes, just like arrays do, and range from 0 to 7. When you call a custom<br />

indicator in an expert advisor using the iCustom() function, the next-to-last parameter in the<br />

function is the indicator buffer.<br />

To find the appropriate buffer for an indicator line, you usually check the source code, if available. If<br />

the source code is clearly formatted with descriptive variable names, you should be able to identify<br />

the appropriate buffer quite easily. We will address the proper naming of indicator buffers in this<br />

chapter.<br />

Creating A Custom Indicator<br />

Let's build a custom indicator using two built-in MetaTrader indicators to calculate our lines. We're<br />

going to build a modified Bollinger Bands indicator. The Bollinger Bands consist of 3 lines – a center<br />

line which is a simple moving average, along with an upper and lower line whose value is determined<br />

<strong>by</strong> the standard deviation.<br />

We can create our own Bollinger Bands indicator using the Moving Average and the Standard<br />

Deviation indicators. We'd like to create an indicator that uses an exponential moving average to<br />

calculate the lines, as opposed to a simple moving average.<br />

146

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

Saved successfully!

Ooh no, something went wrong!