In this video we want to find out how to count the number of open sell positions, right now our expert advisor is telling us that we have six open sell positions and when we sort by type we will see one, two, three, four, five, six positions, so let’s find out how to code that in mql4.
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 sell position counter, click on continue, continue and finish.
Now you can delete everything between the on tick function and the on init function here, we also delete everything above the on init function and we want to remove the comment lines.
Inside of the on init function I want to call a custom function called: open test positions, you wouldn’t do that on your real account but I need some positions that I can count later on and in the on tick function we use the comment statement to output the text: the number of sell positions and this little function here will deliver the current number of open sell positions.
We need to still create it now, so let’s get started.
I don’t need a floating type value here, the name of the current function will be count sell positions and we start by creating an integer variable called: number of sell positions, the first value will be zero because we need to calculate it later on.
Now we use a for loop and the function orders total that comes with mql4 and will give us the number of orders.
To go through all the open orders and for each of those orders we need to get the current currency pair that will be delivered by the function order symbol, first we use the order select function to select the order for the current number, we want to use select by position and mode underscore trades –all in capital letters.
Afterwards I would like to get the current currency pair of the order that is delivered by the function order symbol because that will be helpful now when we want to check if the current symbol on the chart equals the currency pair for the open order and if that is the case we want to check if the order type equals type op sell. Op underscore sell – all in capital letters – will give us the sell trades, we are not interested in pending orders in this example so op sell is what we need and if all those conditions are true we want to take the current number of sell positions and increase it by one.
Finally, we close the for loop here and use the return statement to return the number of sell positions to our main module.
I would like to show you the open test positions function that I used, this is the initialization process for a random generator, it will create a random number of test positions and we will use that number to open buy and sell trades here and as I said you wouldn’t do that on a real account! So when you’re done you can click on the compile button here or press F7.
If this is not the case or if you don’t know what all the code here does I would suggest to go through the other videos in this starter series or maybe even the premium course is interesting for you, in my case the compilation process was successful so now we can click on the little button here or press F4 to go back to Metatrader.
In Metatrader you want to click on view, strategy tester or press control and r, here we pick the new file simple sell position counter dot ex4, please enable the visual mode here and start a test.
Here is our expert advisor, it currently says that we have four open sell positions, so let’s see if that is true. Let’s change the order here and we have one, two, three, four sell positions so our little expert advisor is working.
If you want to suggest anything to become a video like this one just write me an email but for now you have learned how to create a simple expert advisor in mql4 to automatically calculate the current number of open sell positions and you have coded it yourself with a few lines of mql4 code.