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

Updating the <strong>Expert</strong> <strong>Advisor</strong><br />

Let's modify the start() function of our moving average cross expert advisor to reflect the new<br />

functions we have created. First, we will check to see if there are any buy orders open before we<br />

open more. Instead of closing a single sell order, we will simply use the function to close all sell<br />

orders. This method does not require us to use an order ticket.<br />

// Buy Order<br />

if(FastMA > SlowMA && BuyTicket == 0 && BuyMarketCount(Symbol(),MagicNumber) == 0)<br />

{<br />

if(SellMarketCount(Symbol(),MagicNumber) > 0)<br />

{<br />

CloseAllSellOrders(Symbol(),MagicNumber,Slippage);<br />

}<br />

SellTicket = 0;<br />

}<br />

BuyTicket = OpenBuyOrder(Symbol(),LotSize,UseSlippage,MagicNumber);<br />

We used the function BuyMarketCount() that we defined on page 84 to return the number of buy<br />

orders currently open. We will keep the BuyTicket check in, so that only alternating buy/sell orders<br />

are opened.<br />

The function CloseAllSellOrders() closes any sell orders that are open. We check<br />

SellMarketCount() first to see if there are any sell orders to close. This function does not require<br />

an order ticket, unlike the CloseSellOrder() function in chapter 4. It is recommended you use this<br />

method for closing out opposite orders in your EA, as it is more robust.<br />

The rest of the buy order placement code is the same as before. The corresponding sell order<br />

placement code is below:<br />

// Sell Order<br />

if(FastMA < SlowMA && SellTicket == 0 && SellMarketCount(Symbol(),MagicNumber) == 0)<br />

{<br />

if(BuyMarketCount(Symbol(),MagicNumber) > 0)<br />

{<br />

CloseAllBuyOrders(Symbol(),MagicNumber,Slippage);<br />

}<br />

BuyTicket = 0;<br />

}<br />

SellTicket = OpenSellOrder(Symbol(),LotSize,UseSlippage,MagicNumber);<br />

92

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

Saved successfully!

Ooh no, something went wrong!