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

OrderSelect(CloseTicket,SELECT_BY_TICKET);<br />

if(OrderCloseTime() == 0 && OrderType() == OP_BUYSTOP)<br />

{<br />

bool Deleted = OrderDelete(CloseTicket,Red);<br />

}<br />

As we did with the OrderClose() function above, we need to check the order type to be sure it is a<br />

pending order. The pending order type constants are OP_BUYSTOP, OP_SELLSTOP, OP_BUYLIMIT and<br />

OP_SELLLIMIT. To close other types of pending orders, simply change the order type.<br />

If the order has been filled, then it is now a market order, and must be closed using OrderClose()<br />

instead.<br />

A Simple <strong>Expert</strong> <strong>Advisor</strong><br />

Let's see how the code we've discussed so far would work in an expert advisor. This is a simple<br />

moving average cross system. A buy order is opened when the 10 period moving average is greater<br />

than the 20 period moving average. When the 10 period moving average is less than the 20 period<br />

moving average, a sell order is opened.<br />

This EA will alternate between opening buy and sell orders. Orders will be closed when an order is<br />

opened in the opposite direction, or <strong>by</strong> stop loss or take profit. We will use the global variables<br />

BuyTicket and SellTicket to store the last order ticket. When a new order is opened, the last<br />

order ticket is cleared. This prevents multiple consecutive orders from opening.<br />

#property copyright "<strong>Andrew</strong> <strong>Young</strong>"<br />

// External variables<br />

extern double LotSize = 0.1;<br />

extern double StopLoss = 50;<br />

extern double TakeProfit = 100;<br />

extern int Slippage = 5;<br />

extern int MagicNumber = 123;<br />

extern int FastMAPeriod = 10;<br />

extern int SlowMAPeriod = 20;<br />

// Global variables<br />

int BuyTicket;<br />

int SellTicket;<br />

36

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

Saved successfully!

Ooh no, something went wrong!