In Part 65 we discussed some of the details of searching your MT4 platform’s list of orders for a specific order. We also discussed the formation of a logic statement. In this article we’ll discuss how to detect changes in the order list.
After we’ve identified a trade by symbol and magic number, we set OrderFound to true. That will let us know if we jumped out of the loop because we found something or it it just “fell through” after going through all the orders.
Next we want to test if the enter time and order type of the found order are the same as those passed to the function. If one or the other is different, we can assume that we’ve got a new order here and not one that we’ve previously identified. Remember that with a logical OR statement, if either side of the argument is true, the whole argument is true. So we want to know if either the OpenOrderTime() is not equal to EntryTime or the OrderType() is not equal to the Type.
If we do find that we have a brand new order, we start by setting our new integer variable OType to the new order type so we can test it without changing the value passed to the function. We could probably assign the new type to the Type integer parameter we passed to the function, but I don’t like to do that. It may not make a difference in some functions (in this case we will be testing the Type parameter later on), but this is one of the standards to which I adhere.
We now test OType to see what value should be assigned to our variable Status. If we discovered that this was indeed an order we had previously identified, Status would remain 0 as it was initialized.
After testing for and handling a new order situation, we can jump out of the for() loop. There are a couple ways that could be done. We could assign TotalOrders to i (i = TotalOrders)- which would cause the for() loop to fall through and end. That would work, but, in my opinion, that’s ugly. Remember, we’re creating art. 😉 I’ve run into trouble assigning values to counters inside a loop in the past. This probably wouldn’t cause a problem, but to me it’s inelegant. We always want an elegant solution. In this case, I like to use the break statement. Break will cause the code to jump out of the innermost loop.
Now that we’re out of the loop, we need to test to see if there was actually an order found – and whether we were looking for an order at all. Looking back at prior articles, I realized that I didn’t mention that we should pass a Type of -1 when there was no order already in play. We use -1 so it doesn’t conflict with the constants that MQL4 uses with the OrderType() function.
This test uses a logical AND statement. Remember, each statement on either side of the AND must be true for the whole statement to be true. Our first test (Type != -1), if true, let’s us know that we were expecting an order of some type. Then we test to see if an order was NOT found (remember, the exclamation point (!) negates a value. So !OrderFound is true if OrderFound is false. Wrap your head around that one.) If true, it means we did not find an order in the list. So what we are saying is if we were expecting to find an order and didn’t find one, we want to set our Status to 10, which tells the calling program that the previously open order was closed.
And that’s it. We return the Status to the calling program and we’re done.
Well, since I’ve been numbering these articles, that sort of implies that maybe there’s an end somewhere. Actually, I have no end in sight! 😉 I expect to continue adding to your knowledge of MQL4 until you all become expert MQL4 programmers. Please let me know what you’re thinking and what you would like me to write about.
Thanks for your attention and please follow me on Twitter and LinkedIn.
Latest posts by admin (see all)
- Forex Tax Basics- Treatment of Forex Transactions - July 17, 2017
- Forex Trading Master Train to Be Great - July 17, 2017
- Before A Forex Strategy Matters, Build a Foundation - July 16, 2017
Winner’s Edge Trading, as seen on: