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

OrdersTotal() is a function that returns the number of currently opened orders. Why are we<br />

subtracting 1 from the value of OrdersTotal()? Let's explain how the order pool works:<br />

The order pool contains all orders that are currently open in our terminal, including manually placed<br />

orders as well as orders placed <strong>by</strong> expert advisors. The order indexes are numbered starting from<br />

zero. If there is one order open, its index is 0. When a second order is opened, its index is 1. If a<br />

third order is opened, its index will be 2, and so on. Index 0 is the oldest order, and index 2 is the<br />

newest.<br />

OrdersTotal() will return the number of currently opened orders. In the above example, we have<br />

three orders open. But because our order index starts at 0, we want our counter variable to only<br />

count to 2. The value of Counter must correspond with our order index numbers, so that is why we<br />

must subtract 1 from OrdersTotal().<br />

When an order in the open order pool is closed, any newer orders in the pool will have their order<br />

indexes decremented. For example if the order with index 0 is closed, then the order with index 1<br />

becomes index 0, and order index 2 becomes index 1. This is important when we close orders, and<br />

we'll cover this in more detail soon.<br />

Back to our order loop: The OrderSelect() statement uses our Counter variable as the order<br />

position index. As explained above, we will increment our way through the order pool from the oldest<br />

order to the newest. The SELECT_BY_POS parameter indicates that we are selecting the order <strong>by</strong> its<br />

position in the order pool, as opposed to its ticket number.<br />

For the first iteration of this loop, Counter will be equal to 0 and we will select the oldest order from<br />

the order pool using OrderSelect(). We can then examine the order information using functions<br />

such as OrderTicket() or OrderStopLoss(), and modify or close the order as necessary.<br />

Order Counting<br />

It is often very useful to find out how many orders our EA has open, and of what type. We will create<br />

several order counting functions to count the current number of open orders, based on the order<br />

type. The following function will count the total number of open orders:<br />

int TotalOrderCount(string argSymbol, int argMagicNumber)<br />

{<br />

int OrderCount;<br />

for(Counter = 0; Counter

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

Saved successfully!

Ooh no, something went wrong!