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.

Order Placement<br />

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

{<br />

double CloseLots = OrderLots();<br />

double ClosePrice = Bid;<br />

}<br />

bool Closed = OrderClose(CloseTicket,CloseLots,ClosePrice,UseSlippage,Red);<br />

The CloseTicket variable is the ticket number of the order we wish to close. The OrderSelect()<br />

function selects the order, and allows us to retrieve the order information. We use<br />

OrderCloseTime() to check the order closing time to see if the order has already been closed. If<br />

OrderCloseTime() returns 0, then we know the order has not been closed yet.<br />

We also need to check the order type, since the order type determines the closing price for the order.<br />

The OrderType() function returns an integer indicating the order type. If it's a buy market order,<br />

indicated <strong>by</strong> OP_BUY, we'll continue closing the order.<br />

Next, we retrieve the order lot size using OrderLots(), and store that value in CloseLots. We<br />

assign the current Bid price to ClosePrice. Then we call the OrderClose() function to close out<br />

our order.<br />

We specify our Slippage setting with UseSlippage, and indicate a Red arrow to be printed on the<br />

chart. A boolean return value is stored in the variable Closed. If the order has been closed<br />

successfully, the value of Closed will be true, otherwise false.<br />

To close a sell market order, all you need to do is change the order type to OP_SELL and assign the<br />

current Ask price to ClosePrice:<br />

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

{<br />

double CloseLots = OrderLots();<br />

double ClosePrice = Ask;<br />

}<br />

bool Closed = OrderClose(CloseTicket,CloseLots,ClosePrice,UseSlippage,Red);<br />

OrderDelete()<br />

There is a separate function for closing pending orders. OrderDelete() has two arguments, the<br />

ticket number and the arrow color. No closing price, lot size or slippage is required. Here is the code<br />

to close a pending buy stop order:<br />

35

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

Saved successfully!

Ooh no, something went wrong!