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.

Advanced Order Placement<br />

Here's how we check the trading execution thread using IsTradeContextBusy():<br />

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

int Ticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,0,0,<br />

"Buy Order",MagicNumber,0,Green);<br />

We use a while loop to evaluate IsTradeContextBusy(). If the function returns true, indicating that<br />

the trade execution thread is occupied, the expert advisor will Sleep for 10 milliseconds. The while<br />

loop will continue to execute as long as IsTradeContextBusy() returns true. Once the trade thread<br />

is freed up, trading will commence.<br />

If the expert advisor attempts to trade while the trade execution thread is occupied, an error 147 :<br />

"trade context busy" will result. Although this method is fairly reliable at avoiding the "trade context<br />

busy" error, it is not foolproof, especially when multiple expert advisors are attempting to trade at the<br />

same time. Later in the book, we will explore ways to retry trade operations after certain error<br />

conditions.<br />

Refreshing Predefined Variables<br />

The values of predefined variables such as Bid and Ask are set when the expert advisor begins its<br />

execution. The amount of time required to execute our expert advisor code is very short, and can be<br />

measured in milliseconds. But when you figure in delays for trade server response, and the fact that<br />

prices can change very rapidly, it's very important that you always use the most current prices.<br />

The RefreshRates() function updates the contents of predefined variables with the latest prices<br />

from the server. It is recommended that you call this function every time you use the Bid or Ask<br />

variables, especially after a prior trade execution.<br />

Note that if you retrieve the price using the MarketInfo() function, it is not necessary to use<br />

RefreshRates(). We covered MarketInfo() on page 29. When we get to the chapter on creating<br />

functions, we will use MarketInfo() to retrieve prices instead of using predefined variables.<br />

However, you may still want to use Bid and Ask in your start() function to reference the current<br />

chart prices.<br />

Error Handling<br />

When placing, modifying or closing orders, errors can occur due to invalid trade parameters,<br />

requotes, or server issues. We've done our best to make sure that the trade parameters we use are<br />

valid and have been checked to prevent common, preventable errors. But when errors do occur, we<br />

need to alert the user of the error and log any relevant information for troubleshooting.<br />

53

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

Saved successfully!

Ooh no, something went wrong!