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.
Working with Functions<br />
int OpenBuyOrder(string argSymbol, double argLotSize, double argSlippage,<br />
double argMagicNumber, string argComment = "Buy Order")<br />
{<br />
while(IsTradeContextBusy()) Sleep(10);<br />
// Place Buy Order<br />
int Ticket = OrderSend(argSymbol,OP_BUY,argLotSize,MarketInfo(argSymbol,MODE_ASK),<br />
argSlippage,0,0,argComment,argMagicNumber,0,Green);<br />
// Error Handling<br />
if(Ticket == -1)<br />
{<br />
int ErrorCode = GetLastError();<br />
string ErrDesc = ErrorDescription(ErrorCode);<br />
string ErrAlert = StringConcatenate("Open Buy Order – Error ",<br />
ErrorCode,": ",ErrDesc);<br />
Alert(ErrAlert);<br />
}<br />
string ErrLog = StringConcatenate("Bid: ",MarketInfo(argSymbol,MODE_BID),<br />
" Ask: ",MarketInfo(argSymbol,MODE_ASK)," Lots: ",argLotSize);<br />
Print(ErrLog);<br />
}<br />
return(Ticket);<br />
In the OrderSend() function, note that we've used the MarketInfo() function with the MODE_ASK<br />
parameter, in place of the predefined Ask variable. This will retrieve the current Ask price for the<br />
currency symbol indicated <strong>by</strong> argSymbol.<br />
If the trade was not placed successfully, the error handling routine will be run. Otherwise the order<br />
ticket will be returned to the calling function, or -1 if the order was not placed. The complete order<br />
placement function for sell market orders is in Appendix D.<br />
Pending Order Placement<br />
To place pending orders, we'll need to pass parameters for the pending order price as well as the<br />
order expiration time. The argPendingPrice and argExpiration arguments will be added to the<br />
function.<br />
We'll assume that the pending order price, as well as the stop loss and take profit, have been<br />
calculated and verified prior to calling this function. The pending order placement functions will place<br />
the stop loss and take profit with the pending order, so no separate order modification function is<br />
required.<br />
67