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 Management<br />
}<br />
if(OrderMagicNumber() == argMagicNumber && OrderSymbol() == argSymbol)<br />
{<br />
OrderCount++;<br />
}<br />
}<br />
return(OrderCount);<br />
We've named our order counting function TotalOrderCount(). It will return an integer value<br />
indicating how many orders are currently opened on the specified chart symbol matching the magic<br />
number that we've passed as a function argument.<br />
We start <strong>by</strong> declaring the OrderCount variable. Since we have not indicated an initial value,<br />
OrderCount will be initialized as 0. You'll recognize the for operator and the OrderSelect()<br />
function from the previous section.<br />
Since the order pool contains all open orders, including those placed <strong>by</strong> other EAs, it is necessary for<br />
us to identify which orders were placed <strong>by</strong> our EA. We check the OrderSymbol() of the selected<br />
order first, and make sure that it matches the argSymbol argument. The we check the magic number<br />
on the order.<br />
If OrderMagicNumber() matches the argMagicNumber argument, we can be fairly sure that this<br />
order was placed <strong>by</strong> this EA. As long as the user is not running two EA's on the same currency<br />
symbol with the same magic number, we can be certain that this order was placed <strong>by</strong> this EA. When<br />
running multiple expert advisors on the same instrument, take care to ensure that you're using a<br />
unique magic number on each EA.<br />
If the order matches both our magic number and our chart symbol, the value of OrderCount will be<br />
incremented <strong>by</strong> one. After we have looped through all of the orders in the order pool, we return the<br />
value of OrderCount to the calling function.<br />
Here's an example of how we would use this in code:<br />
if(TotalOrderCount(Symbol(),MagicNumber) > 0 && CloseOrders == true)<br />
{<br />
// Close all orders<br />
}<br />
If there are orders opened <strong>by</strong> this EA, and the value of CloseOrders is true (we'll assume this was<br />
set somewhere else in the program), then the code inside the braces will run, which will close all<br />
open orders.<br />
83