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.
Order Placement<br />
bottom of the file) and assign the return values to our global variables. We'll use these when<br />
referencing point or slippage values in the rest of our expert advisor.<br />
Next is the start() function, our main program execution. We've left out deinit(), since we have<br />
no use for it here. The iMA() function calculates the moving average The FastMA variable holds our<br />
10 period moving average, which is set using the FastMAPeriod variable. The SlowMA variable is our<br />
20 period moving average, set using SlowMAPeriod. Everything else is set to default (a no shift,<br />
simple moving average calculated on the close price).<br />
We use the if operator to define our order opening conditions. If the current 10 period moving<br />
average (the FastMA) is greater than the 20 period moving average (the SlowMA), and if BuyTicket<br />
is equal to 0, we will open a buy order.<br />
Before we open the buy order, we will close the current sell order, if it exists. We use OrderSelect()<br />
to retrieve the current SellTicket. If the order close time is 0 (indicating that the order has not yet<br />
been closed), and the SellTicket is greater than 0 (indicating that the SellTicket is likely valid),<br />
we will go ahead and close the sell order. We retrieve the lot size of the sell order and the current<br />
Ask price, which will be the closing price for the sell order. Then, we close the sell order using<br />
OrderClose().<br />
Next, we assign the current Ask price to the OpenPrice variable – this will be the opening price of<br />
our buy order. We calculate our stop loss and take profit relative to the opening price, checking first<br />
to make sure that we have specified a StopLoss or TakeProfit value in the settings. Then, we place<br />
the order using the OrderSend() function, and store the order ticket in BuyTicket. Lastly, we clear<br />
the value of SellTicket, allowing the placement of another sell order when the order condition<br />
becomes valid.<br />
The sell order block follows the same logic as the buy order block. We close the buy order first, and<br />
use the Bid as the OpenPrice and the buy order ClosePrice. The stop loss and take profit<br />
calculations are reversed.<br />
The start() function ends with a return operator. Our custom PipPoint() and GetSlippage()<br />
functions are defined at the end after the start() function. We will include these functions in every<br />
example in this book.<br />
Using Pending Orders<br />
Let's modify our EA to use pending orders. We'll use stop orders in this example. When the fast<br />
moving average is greater than the slow moving average, we will place a buy stop order 10 pips<br />
39