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

Here's the code to place a pending buy stop order:<br />

int OpenBuyStopOrder(string argSymbol, double argLotSize, double argPendingPrice,<br />

double argStopLoss, double argTakeProfit, double argSlippage, double argMagicNumber,<br />

datetime argExpiration = 0, string argComment = "Buy Stop Order")<br />

{<br />

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

// Place Buy Stop Order<br />

int Ticket = OrderSend(argSymbol,OP_BUYSTOP,argLotSize,argPendingPrice,<br />

argSlippage,argStopLoss,argTakeProfit,argComment,argMagicNumber,<br />

argExpiration,Green);<br />

// Error Handling<br />

if(Ticket == -1)<br />

{<br />

int ErrorCode = GetLastError();<br />

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

string ErrAlert = StringConcatenate("Open Buy Stop Order - Error ",ErrorCode,<br />

": ",ErrDesc);<br />

Alert(ErrAlert);<br />

}<br />

string ErrLog = StringConcatenate("Ask: ",MarketInfo(argSymbol,MODE_ASK),<br />

" Lots: ",argLotSize," Price: ",argPendingPrice," Stop: ",argStopLoss,<br />

" Profit: ",argTakeProfit," Expiration: ",TimeToStr(argExpiration));<br />

Print(ErrLog);<br />

}<br />

return(Ticket);<br />

Note that we've specified a default value of 0 for argExpiration. If you are not using a pending<br />

order expiration time, and you wish to use the default order comment, you can simply omit the<br />

arguments for argExpiration and argComment when calling the function. The following example will<br />

place a buy stop order with no expiration time and the default order comment, "Buy Stop Order":<br />

int Ticket = OpenBuyStopOrder(Symbol(),LotSize,PendingPrice,StopLoss,TakeProfit,<br />

UseSlippage,MagicNumber);<br />

We've added the pending price to the log in our error handling function, as well as the expiration<br />

time, if one is specified. The TimeToStr() function converts a datetime variable to a readable string<br />

format.<br />

The functions to open sell stop, buy limit and sell limit orders are identical to this one. The only<br />

difference is that the order type parameter for the OrderSend() function is changed accordingly. You<br />

can view all of the pending order placement functions in Appendix D.<br />

68

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

Saved successfully!

Ooh no, something went wrong!