As you know, you can have multiple trades (or positions in MT5) and/or pending orders at a time. So, if you want to operate with specific one, you have to point at it. Manually you can do this by clicking on it, you just know where to put your cursor and click on the mouse, but your program picks trades and orders in a slightly different way.
Imagine that you have 10 pending orders, what if you want your robot to delete 5 of them, the older ones that were created? Note that the robot can operate with a single order at a time. These are the logical steps to do it:
- 1. Check how many orders exists - If none, there is nothing else to do, but let's say we have 10 orders, so we continue to step 2.
- 2. Sort - We know we have 10 orders, and we know their parameters - open time, lot size, trading type and many more. We want to sort our orders by age.
- 3. Close 5 trades - we sorted out trades by age, so starting from the oldest one we close the first 5 that we have one after another.
Additionaly, while going from order to order we canfilter them, so if for example we don't want to close Sells, we have to skip any order that is Sell and close only the one that is Buy.
The next example shows how to work with trades in MetaTrader 4:
Check other examples to see how to deal with loops in fxDreema: /examples
