MQL5 Tutorial – How to simply count positions with MQL5


In this video we want to create a little Expert Advisor that will tell us how many positions we have opened for a currency pair.
An Expert Advisor like this is useful if you want to handle more than one position for each currency pair as I do, this is called a Multi-currency Bot.
As you can see I have opened one, two, three, four, five, six positions for the Euro U.S. dollar.
So if I click on Euro U.S. dollar and drag it on the chart, it will say positions for this currency pair six.
Now how can we do that?
First of all please click on this button here or hit the F4 key to bring up that Meta Quote language editor also called Meta Editor.
Now we need to click on new and select Expert Advisor from template, click on continue, I will give it the name Simple Position Count.
Click on continue, and finish. You can delete everything above the OnTick function.
We also will remove the two comment lines here and the first thing below is a variable but it will be a local variable to count the positions for this currency pair.
Now we are going to count all the open positions, that is done by using the function PositionsTotal, subtract one and count down to zero until we have no open positions.
We will need to open a bracket for loop, and a closing bracket, and between these two brackets we will get the symbol of the open position by using the function PositionGetSymbol.
And if the symbol position is equal to the symbol on the chart, we want to add the value one to the current positions for this currency pair.
And after we have counted all positions, we want to output the current positions for this currency pair on the chart.
This is the text, this is the value and we are using the Comment function.
So this is it. Let’s see if it compiles.
Okay that worked without any errors or warnings.
So let’s hit the F4 key, open a chart window and drag the EA simple position count on your chart. And it says we have two open positions for this currency pair.
This is one and this is the other one. So let’s switch to Euro U.S. dollar, and now it says we have six open positions.
Let’s check the Australian against the Canadian dollar and it says we have one open position, and this is this one.
In this little video you learned how to create a simple MQL 5 Expert Advisor to count the positions for a currency pair and output the number on the chart.

Download “MQL5 Simple Position Count Expert Advisor from MQL5 Tutorial” SimplePositionsCount.txt – Downloaded 26 times – 583 B

Learn how to create your own MQL5 trading robot for $1
Sign Up Now