Trader’s Tech – Writing Your Own EA Part 64 – Detecting Events

Forex Robot

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 63 we started looked at the code for detecting trading events in MT4. In this article we’ll continue to study the code.

We’ve covered the variable declarations for our function. The next thing we need to do is see if there are any orders in the system that meet our criteria. We start with a for() statement to set up a loop that increments the order list. The integer variable i will represent the ordinal position of the orders in the list.

The for() statement uses code fragments separated by semi-colons as its parameters. It starts by performing the first line (i=0). It then tests to see if the second line (i<TotalOrders) is true. If so, it will execute the following code that is set off by the braces ({}). After executing the code, it will perform the third line (i++, this is shortcut code to say i = i + 1. It increments i by 1), test the second line again (i<TotalOrders), if it’s still true, the code will be executed again with the new value of i. It will continue this process until the second line tests false or until we break out of the loop in the code. Functionally this allows us to loop through a segment of code with a new value of i for each loop. With it we can execute the same calculations on all the elements of a file or an array (we’ll talk about arrays in a future installment) or, as we are doing in this case, test each order in MT4′s order list.

Since MQL4 counts arrays, file positions, order positions and just about everything else by starting with 0 (0 is the new 1 ;)), we must begin our loop with i = 0. Also, since TotalOrder is set to OrderTotal() which returns the actual number of orders in the system, the highest we can increment i to is TotalOrder – 1. So, as long as i is less than TotalOrder we will continue to iterate the loop.

Now that we have established our loop, we want to use it to test each order in the list. First thing is to select the order represented by i:

OrderSelect(i,SELECT_BY_POS);

 

MQL4′s built-in function OrderSelect() positions the order list pointer to the appropriate order so we can use the other functions to look at elements of that order. OrderSelect() has three parameters, the first is an integer that tells where to position the pointer.

The second parameter tells the function what the first parameter represents. The second parameter has two possible values; SELECT_BY_POS which means select by position. This is the one we use here since i represents the position of the order in the list. The other value is SELECT_BY_TICKET. In that case, the first parameter would be a valid ticket number in the system.

The third parameter tells us what order list we will be searching through. The default is MODE_TRADES which is the list of active orders in the system. The other option is MODE_HISTORY which allows us to look through the closed and canceled orders. Since it defaults to the active orders list, we can leave that position blank for the purposes of our trade detection, although we may want to elaborate on this code by returning information about an already closed order.

And that brings us to a convenient stopping point for today. Next time we’ll continue our look at the DetectTradeEvents() function. Thanks for your attention and please follow me on Twitter and LinkedIn.

Tim

 

here_for_you_001

1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube

Connect_With_Winners_Edge

Get trade set ups everyday!

The following two tabs change content below.

Latest posts by Tim Black (see all)

Follow our social media:
1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news