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

// Delete order<br />

else if(OrderCloseTime() == 0 && SellTicket > 0 && OrderType() == OP_SELLSTOP)<br />

{<br />

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

if(Deleted == true) SellTicket = 0;<br />

// Error handling<br />

if(Deleted == false)<br />

{<br />

ErrorCode = GetLastError();<br />

ErrDesc = ErrorDescription(ErrorCode);<br />

ErrAlert = StringConcatenate("Delete Sell Stop Order - Error ",ErrorCode,<br />

": ",ErrDesc);<br />

Alert(ErrAlert);<br />

}<br />

}<br />

ErrLog = StringConcatenate("Ask: ",Ask," Ticket: ",SellTicket);<br />

Print(ErrLog);<br />

We've added the code to delete pending orders using OrderDelete() after the OrderClose()<br />

function. The order type of the previous sell order determines which function is used to close the<br />

order.<br />

The main difference between the following code and the market order code is that we do not have an<br />

order modification block. It is not necessary to place the stop loss and take profit separately for<br />

pending orders. Therefore we will calculate the stop loss and take profit before placing the order with<br />

OrderSend().<br />

// Calculate stop level<br />

double StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;<br />

RefreshRates();<br />

double UpperStopLevel = Ask + StopLevel;<br />

double MinStop = 5 * UsePoint;<br />

// Calculate pending price<br />

double PendingPrice = High[0] + (PendingPips * UsePoint);<br />

if(PendingPrice < UpperStopLevel) PendingPrice = UpperStopLevel + MinStop;<br />

// Calculate stop loss and take profit<br />

if(StopLoss > 0) double BuyStopLoss = PendingPrice - (StopLoss * UsePoint);<br />

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

// Verify stop loss and take profit<br />

UpperStopLevel = PendingPrice + StopLevel;<br />

double LowerStopLevel = PendingPrice – StopLevel;<br />

62

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

Saved successfully!

Ooh no, something went wrong!