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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

EXPERT ADVISOR PROGRAMMING<br />

We begin <strong>by</strong> including the file that has our functions in it, in this case IncludeExample.mqh. The<br />

variable declarations and the contents of the init() function are the same as before. At the<br />

beginning of the start() function, we use CalcLotSize() and VerifyLotSize() to to calculate<br />

and verify our lot size.<br />

In our buy and sell order blocks, we use CloseBuyOrder() and CloseSellOrder() to close the<br />

opposite order. Our new orders are opened using OpenBuyOrder() or OpenSellOrder(). Before<br />

calculating the stop loss and take profit, we check that the order was opened and that a StopLoss or<br />

TakeProfit has been specified.<br />

We retrieving the opening price of the order using OrderSelect() and OrderOpenPrice(). We then<br />

calculate our stop loss using CalcBuyStopLoss() or CalcSellStopLoss(), and our take profit using<br />

CalcBuyTakeProfit() or CalcSellTakeProfit().<br />

We check to see if the stop loss or take profit is greater than 0, and use the functions<br />

AdjustAboveStopLevel() and AdjustBelowStopLevel() to verify our stop loss and take profit<br />

prices. Finally, we pass those prices to the AddOrderProfit() function, which adds the stop loss and<br />

take profit to the order.<br />

The EA above does exactly the same thing as the code starting on page 51, but is much easier to<br />

read. By breaking the code into functions, we have de-cluttered our source code and made our EA<br />

easier to manage. We will add a few more features to this expert advisor before the end of the book.<br />

You can view the complete code in Appendix C.<br />

The initial work in creating these functions will take some time, but it will save you time in the long<br />

run, as you will more easily be able to prototype trading ideas and turn out working expert advisors<br />

in a short amount of time.<br />

78

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

Saved successfully!

Ooh no, something went wrong!