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

Let's say we're using a trading system that places the stop loss 2 pips below the low of the current<br />

bar. We use the predefined price array Low[] to retrieve the low of a bar. Low[0] is the low of the<br />

current bar, Low[1] is the low of the previous bar, and so on.<br />

Once we've determined the low of the current bar, we multiply 2 <strong>by</strong> UsePoint to get a decimal value,<br />

and subtract that from our low:<br />

double BuyStopLoss = Low[0] – (2 * UsePoint);<br />

So if the low of the bar is 1.4760, the stop loss will be placed at 1.4758.<br />

But maybe you want to place your stop loss at the lowest low of the last x number of bars. There's a<br />

function built into MetaTrader just for that. iLowest() returns the shift value indicating the bar with<br />

the lowest value in a specified time range. We can use high, low, open or close values.<br />

Here's an example of how we would use iLowest() to find the lowest low of the last 10 bars:<br />

int CountBars = 10;<br />

int LowestShift = iLowest(NULL,0,MODE_LOW,CountBars,0);<br />

double BuyStopLoss = Low[LowestShift];<br />

The first parameter of iLowest() is the currency symbol – NULL means that we're using the current<br />

symbol. Many functions in MQL use the string constant NULL to refer to the current chart symbol. The<br />

second parameter is the chart period – 0 refers to the current chart frame.<br />

MODE_LOW is an integer constant that specifies the low price series array. In other words, we're<br />

looking for the lowest low of the last CountBars. If we wanted to find the lowest close, for example,<br />

we would use MODE_CLOSE. You can find all of the series array constants in the MQL Reference under<br />

Standard Constants – Series Arrays.<br />

CountBars is the number of bars we want to search, in this case 10. Finally, the last parameter is our<br />

starting location. 0 is the current bar. To start at a previous bar, count backward from the current bar<br />

– the previous bar is 1, the bar before that is 2, etc.<br />

The output of the iLowest() function is an integer indicating the backward shift of the bar with the<br />

lowest value in the price series. In the example above, if iLowest() returns a 6, that means that the<br />

lowest low is 6 bars back. We store that value in the variable LowestShift. To find the actual price,<br />

we simply retrieve the price value of Low[LowestShift], or in other words, Low[6].<br />

31

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

Saved successfully!

Ooh no, something went wrong!