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.
EXPERT ADVISOR PROGRAMMING<br />
}<br />
if(BuyStopLoss > 0 || BuyTakeProfit > 0)<br />
{<br />
bool TicketMod = OrderModify(BuyTicket,OrderOpenPrice(),BuyStopLoss,<br />
BuyTakeProfit,0);<br />
}<br />
The OrderSend() function is identical to our earlier example, except that we use a value of 0 for the<br />
stop loss and take profit parameters. A value of zero means that there is no stop loss or take profit<br />
being placed with the order. The BuyTicket variable stores the ticket number of the order.<br />
We use an if statement to check that the BuyTicket number is valid – i.e. greater than zero. If so,<br />
we call the OrderSelect() function using our BuyTicket number. We retrieve the opening price for<br />
the order using OrderOpenPrice(), and assign that to the OpenPrice variable.<br />
Next, we calculate the stop loss and take profit, relative to the opening price of the order we just<br />
placed. We check first to see if the StopLoss and TakeProfit external variables are greater than<br />
zero. If so, we calculate the new stop loss and/or take profit price.<br />
Finally, we call the OrderModify() function to add our stop loss and take profit to the order. We<br />
check first to make sure that the BuyStopLoss or BuyTakeProfit variables are something other than<br />
zero. If we attempt to modify the order with unchanged values, we'll get an error code 1 from the<br />
OrderModify() function.<br />
The first parameter for OrderModify() is our BuyTicket number. We could also use OrderTicket()<br />
as well. The second parameter is the new order price. Since we are not modifying the order price, we<br />
use the OrderOpenPrice() function, to indicate that the order price is unchanged.<br />
Remember that we can only modify order prices for pending orders. If we are modifying a market<br />
order, we can pass any value for the Price parameter, since you cannot change the order price of a<br />
market order. But we cannot assume that we will always be modifying market orders, so we will<br />
always use OrderOpenPrice().<br />
The BuyStopLoss and BuyTakeProfit variables pass the changed stop loss and take profit values to<br />
the OrderModify() function. If you plan on using order expiration times for your pending orders,<br />
you can use OrderExpiration() as the unchanged Expiration parameter. Otherwise, just use 0.<br />
Although this method adds a few extra steps, we recommended that you use this method of placing<br />
stop losses and take profits for market orders in your expert advisors to ensure that they are<br />
compatible with all brokers. This method also has the advantage of allowing us to place accurate stop<br />
loss and take profit prices without the effects of slippage.<br />
44