In this video we are going to find out how we can calculate the number of sell positions, the expert advisor currently counts seven positions here, that’s one, two, three, four, five, six, seven, so let’s find out how to do that.
To get started 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 file, expert advisor from template, I will call this file simple sell position count, click on continue, continue and finish.
Now you can delete everything between the on tick function and the on init function here.
Let’s also remove everything above the on init function and the grey comment lines. We are going to start with an include statement to include the file trade dot mqh, this file contains trading functions so now we can create an instance of the class c trade with the name trade.
Inside of the on init function, I’m going to call a custom function open test positions to open some test positions. Obviously, you wouldn’t do that on a real account but I need some positions so I have something to count.
Inside of the on tick function we want to use the comment statement to output the text: number of sell positions and we call another custom function called: count sell positions that we need to create now. This new function should return an integer value and the first thing we need to do is to create an integer variable called: number of sell positions that has a value of zero and now we use a for loop to go through all the open positions.
The number of positions is delivered by the function orders total? (positions total) and as long as we have more positions we will look at all the positions and check a few things.
The first thing is that we want to get the position symbol for the current position, we also want to get the position type that is done by using the function position get integer and the parameter here is position underscore type – all in capital letters – and we are looking for the position type sell but before we do that we want to know if the current symbol on the chart equals the symbol of the currency pair for the position and if that is also true we check if the position direction equals position type sell.
That is the case we have a sell position and that is when we increase the current number of sell positions by one and when we are done we want to close the for loop and finally we use the return statement to return the number of sell positions that we have calculated right now.