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

Pending Order Close Function<br />

Here's a function to close a single pending order. This will work on all pending order types, buy and<br />

sell.<br />

bool ClosePendingOrder(string argSymbol, int argCloseTicket, double argSlippage)<br />

{<br />

OrderSelect(argCloseTicket,SELECT_BY_TICKET);<br />

if(OrderCloseTime() == 0)<br />

{<br />

while(IsTradeContextBusy()) Sleep(10);<br />

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

if(Deleted == false)<br />

{<br />

int ErrorCode = GetLastError();<br />

string ErrDesc = ErrorDescription(ErrorCode);<br />

string ErrAlert = StringConcatenate("Close Pending Order - Error: ",<br />

ErrorCode,": ",ErrDesc);<br />

Alert(ErrAlert);<br />

string ErrLog = StringConcatenate("Ticket: ",argCloseTicket,<br />

" Bid: ",MarketInfo(argSymbol,MODE_BID),<br />

" Ask: ",MarketInfo(argSymbol,MODE_ASK));<br />

Print(ErrLog);<br />

}<br />

}<br />

return(Deleted);<br />

}<br />

Stop Loss & Take Profit Calculation Functions<br />

We're going to create a few short functions for calculating stop loss and take profit as discussed on<br />

pages 25-30. We will pass our external variables indicating the stop loss or take profit in pips to our<br />

function, as well as the order opening price. The return value of our function will be the actual stop<br />

loss or take profit price.<br />

Here's the function to calculate a buy stop loss in pips:<br />

double CalcBuyStopLoss(string argSymbol, int argStopLoss, double argOpenPrice)<br />

{<br />

if(argStopLoss == 0) return(0);<br />

double BuyStopLoss = argOpenPrice - (argStopLoss * PipPoint(argSymbol));<br />

return(BuyStopLoss);<br />

}<br />

70

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

Saved successfully!

Ooh no, something went wrong!