In this video, we are going to create an expert advisor that is able to count the current number of open buy positions. This is a question that I have gotten from a customer. Right here the expert advisor says that we have six buy positions, so let’s click on results, we have buy and sell trades here, let’s order them by type and we have one, two, three, four, five, six buy positions, so let’s find out how to create a simple expert advisor that can count our positions.
To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new, expert advisor from template, continue, I will call this file simple buy position counter, click on continue, continue and finish, now you can delete everything between the on tick function and the on init function, we also want to remove everything above the on init function and all the grey comment lines here.
Inside of the on init function, I’m going to call a function called open test positions, you wouldn’t do that on real account but I need to open some positions first before I can count them.
Inside of the on tick function, I would like to use the comment statement to create an output: number of buy positions and the number of open buy positions will be calculated in a function that we are going to create right now.
The new function should return an integer value, the name is count buy positions and we start by creating a local variable, called number of buy positions, right now the value is zero and we are going to use a for loop. Inside of the for loop we use orders total, this will deliver the number of all the orders, we use a counter value to go through all the orders until there is no order left and for each order we want to get the currency pair for that particular order that is done by using the function order symbol, we use order select to select an open trade, the one for the current counter value, we want to use select by position and we use mode trades, if you mark that and press F1 you will learn that this is for opened and pending orders while mode history would be used when we want to calculate something about the closed and cancelled orders, now we use the function order symbol to find out the currency pair for the order and after we have done that we can check if the current symbol on the chart equals the currency pair of the order and we also want to know if the order type equals op buy, let’s look that up.
Op buy is a buy operation, op sell would be a sell operation, the other four here are for pending orders but we just wanted to count buy positions so op buy is the one we need and if all those conditions are true we now want to increase the number of buy positions by one, here we close the for loop and finally we use the return statement to return the number of five positions to our main module.
That’s about it.
I will show you my open test positions function, I’m using a random generator to generate a random number of positions, I use order send to open buy and sell trades, so that we have something to count, you wouldn’t do that on a real account so when you’re done please click on the compare button here or press F7 on your keyboard, we have no errors here so now we can click the little button here or press F4 to go back to Met trader.
In Metatrader please click on view, strategy tester or press ctrl and r, here we pick the new file, simple buy positions counter dot ex4, please enable the visual mode here and start a test.
Here we are!
We have one buy and one sell position and the number of buy positions is one, let’s look into the results tab and this is correct, now let’s stop the test and restart it, this time the expert advisor says that we have two buy positions, this is the first and this is the second position so our little expert adviser is working, if you have any similar questions that could become a video like this one just send me an email but for now you know how to create a simple expert advisor that is able to count open buy positions and you have coded it yourself with a few lines of mql4 code.