In Part 64 we discussed the for() statement and the MQL4’s OrderSelect() function. This time we’ll look further into the DetectTradeEvent() function.
We’ve got the order list pointer pointed at the order designated by our counter i. Now we can use MQL4’s order functions on this order. For the purpose of this EA, we’re going to use the following:
OrderSymbol() – Returns the pair symbol of the currently selected order.
OrderMagicNumber() – Returns the magic number assigned to the currently selected order.
OrderType() – Returns the order type associated with the currently selected order. The OrderType() function returns an integer value that corresponds to the following built-in MQL4 constants:
OP_BUY – this is an active long trade
OP_SELL – this is an active short trade
OP_BUYLIMIT – this is a pending buy where the market is above the order entry price
OP_SELLLIMIT – this is a pending sell where the market is below the order entry price
OP_BUYSTOP – this is a pending buy where the market is below the order entry price
OP_SELLSTOP – this is a pending sell where the market is above the order entry price
First we want to be sure the OrderSymbol() matches the PairName sent to the function. We will also test for either a MagicNumber equal to –1 or a MagicNumber that matches OrderMagicNumber(). Let me take a minute here to talk about logic statements and how they work. The double-ampersand symbol (&&) is a logical AND and the double-pipe symbol (||) is a logical OR When you have AND in a logic statement, both equalities on either side of the AND must be true for the whole statement to be true. When you have OR in a logic statement, you are saying that either one or the other statement on either side of the AND (or both) must be true for the whole statement to be true.
Now I’m going to take you back to grade school mathematics for a moment. Remember what happens in a math formula when you surround a portion of the formula with parentheses? That means you evaluate that portion of the formula separately and substitute the result of that evaluation for the portion in parentheses and then proceed to evaluate the overall formula. For example, 2 + (5 x 10) yields a totally different result from (2 + 5) x 10. In the first, you multiply 5 by 10 and substitute 50 for the portion of the formula in the parentheses for a total result of 52. In the second you add 2 and 5 and substitute 7 for the portion of the formula in the parentheses for a total result of 70.
Logical formulae work the same way. In our function, we group (MagicNumber == -1 || MagicNumber == OrderMagicNumber()) together so they should be evaluated separately from the rest of the formula. The function determines first if the MagicNumber equals -1 (the value we send the function if the MagicNumber is not important.) Then the function compares the MagicNumber to OrderMagicNumber(). If either of those (or both) are true, this portion of the formula is true. We drop that result into the overall formula and test to see if the OrderSymbol() is equal to the PairName parameter. If both are true, then the whole statement is true and the code in the braces after the if() statement are executed.
That wraps us up for this week. I hope everyone has a great weekend. We’ll continue this discussion next week. Thanks for your attention and please follow me on Twitter and LinkedIn.
Winner’s Edge Trading, as seen on: