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.

Advanced Order Placement<br />

When modifying orders, we must be sure that the values we are passing to the function are valid. For<br />

example, the order must still be open – we cannot modify a closed order. When modifying pending<br />

orders with the Price parameter, the order must not have already been filled – i.e. hit its order price.<br />

The modified order price also must not be too close to the current Bid or Ask price. We should also<br />

check to make sure that the stop loss and take profit are valid. We can do this using the price<br />

verification routines that we will cover later in this chapter.<br />

If we are not modifying a particular parameter, we must pass the original value to the<br />

OrderModify() function. For example, if we are modifying only the stop loss for a pending order,<br />

then we must retrieve the current order price and take profit using OrderSelect() , and pass those<br />

values to the OrderModify() function.<br />

If you attempt to modify an order without specifying any changed values, you'll get an error 1: "no<br />

result". You should verify why your code is passing unchanged values to the function, but otherwise<br />

this error is harmless and can be safely ignored.<br />

Adding Stop Loss and Take Profit to an Existing Order<br />

First, we need to verify that the order has been placed correctly. We do this <strong>by</strong> examining the return<br />

value of the OrderSend() function, which is the ticket number of the order that was just placed. If<br />

the order was not placed due to an error condition, the ticket number will be equal to -1.<br />

Next, we use the OrderSelect() function to retrieve the information for the order that was just<br />

placed. We will use the OrderOpenPrice(), OrderTakeProfit(), OrderStopLoss() and optionally<br />

the OrderExpiration() functions when passing unchanged values to the OrderModify() function.<br />

Finally, we'll use OrderModify() to add the stop loss and take profit to the order.<br />

Here's an example where we set the stop loss and take profit for a buy order using the<br />

OrderModify() function. We've moved the stop loss and take profit calculation after the<br />

OrderSend() function, so that it is calculated before we modify the order:<br />

int BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,0,0,<br />

"Buy Order",MagicNumber,0,Green);<br />

if(BuyTicket > 0)<br />

{<br />

OrderSelect(BuyTicket,SELECT_BY_TICKET);<br />

double OpenPrice = OrderOpenPrice();<br />

if(StopLoss > 0) double BuyStopLoss = OpenPrice – (StopLoss * UsePoint);<br />

if(TakeProfit > 0) double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);<br />

43

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

Saved successfully!

Ooh no, something went wrong!