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.
Order Placement<br />
• MagicNumber – An optional integer value that will identify the order as being placed <strong>by</strong> a<br />
specific expert advisor. It is highly recommended that you use this.<br />
• Expiration – An optional expiration time for pending orders. Not all brokers accept trade<br />
expiration times – for these brokers, an error will result if an expiration time is specified.<br />
• Arrow – An optional color for the arrow that will be drawn on the chart, indicating the<br />
opening price and time. If no color is specified, the arrow will not be drawn.<br />
The OrderSend() function returns the ticket number of the order that was just placed. If no order<br />
was placed, due to an error condition, the return value will be -1.<br />
We can save the order ticket to a global or static variable for later use. If the order was not placed<br />
due to an error condition, we can analyze the error and take appropriate action based on the<br />
returned error code.<br />
Placing A Market Order<br />
Here's an example of a buy market order. We'll assume that the variables LotSize, Slippage,<br />
BuyStopLoss, BuyTakeProfit and MagicNumber have already been calculated or assigned and are<br />
valid.<br />
OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,BuyStopLoss,BuyTakeProfit,<br />
"Buy Order",MagicNumber,0,Green);<br />
The Symbol() function returns the current chart symbol. We will be placing orders on the current<br />
chart pair 99% of the time. OP_BUY indicates that this is a buy market order. Ask is a predefined<br />
variable in MQL that stores the most recent Ask quote. (Remember that buy orders open at the Ask<br />
price!)<br />
The Slippage is set using an external variable. The slippage parameter is an integer, indicating the<br />
number of points to allow for price slippage. If your broker uses 4 digit quotes (2 for Yen pairs), 1<br />
point would be equal to 1 pip. If your broker offers 3 and 5 digit quotes however, then 1 point would<br />
be 0.1 pips. In this case, you'd need to add an additional zero to the end of your Slippage setting.<br />
We've added the generic comment "Buy Order" to this order. Since there is no expiration for market<br />
orders, the Expiration parameter is 0. Finally, we specify the color constant Green to draw a green<br />
arrow on the chart.<br />
Here is an example of a sell market order, using the same parameters as above:<br />
23