MQL5 TUTORIAL BASICS – 28 HOW TO CALCULATE THE NUMBER OF OPEN BUY POSITIONS

video
play-sharp-fill

 

  1. Introduction to Counting Buy Positions in MQL5 (00:00 – 00:11)
    • Introduction to the tutorial on how to count the number of buy positions using MQL5, based on a question from a premium course member.
  2. Opening MetaEditor and Creating a New Expert Advisor File (00:11 – 00:47)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “SimpleBuyPositionCount”.
  3. Setting Up the Code Structure and Including Trade.mqh (00:47 – 01:14)
    • Removing unnecessary code and including the ‘Trade.mqh’ file for trading functions.
  4. Creating a Custom Function to Open Test Positions (01:14 – 01:37)
    • Defining a custom function to open test positions for demonstration purposes.
  5. Implementing the Buy Position Counting Logic (01:37 – 03:59)
    • Coding the logic to count buy positions using a for loop and checking each position’s type and symbol.
  6. Compiling and Testing the Expert Advisor in MetaTrader (03:59 – 05:02)
    • Compiling the code and instructions on testing the Expert Advisor in MetaTrader using the Strategy Tester.
  7. Verifying the Buy Position Count in MetaTrader (05:02 – 06:03)
    • Running the Expert Advisor in MetaTrader and verifying the count of buy positions matches the actual number on the platform.

 

In this video, we are going to find out how many buy positions we have. This is a question that came from one of my members of the premium course.
We have several buy and sell positions here, let’s order them by type and we have 1, 2, 3, 4, 5, 6, 7, 8 buy positions, that’s what the text is saying here so let’s find out how to calculate that with MQL5. 

To do that please click on the little button here or press F4 in your Metatrader now you should see the Metaeditor window and here you want to click on: “File/ New File/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleBuyPositionCount”, click on “Continue”, “Continue” and “Finish”.

Now you can delete everything between the “OnTick” function and the “OnInit” function, we also remove the comment lines here, that’s everything that’s grey.

We start by importing a “Trade.mqh” file – that’s a library – it includes several trading functions and it enables us to create an instance of the class “CTrade” that will be called: “trade”, inside of the “OnInit” function I will call a custom function that is called: open test positions (OpenTestPositions).
You wouldn’t do that on a real account but I need to open some test positions to have something to count.
Inside of the “OnTick” function, we are going to do one thing and that is to use the “Comment” statement to output the number of positions that we will get from calling a custom function called buy positions and we are going to code that now.
Actually, let’s make that a little bit more user-friendly and create the custom function called count buy positions (CountBuyPositions), this one will return a whole number so it’s an integer value.
We will start by creating a local variable called number of buy positions (NumberOfBuyPositions), the value is 0 (zero) right now because we want to calculate that number of buy positions and to do that we use a “for” loop, inside of the “for” loop we will use the function “PositionsTotal” and as long as our counter value is above 0 (zero) we will go through all the open positions and here is what we are going to do; first we want to create a string for the currency pair and assign the value for “PositionGetSymbol” of the current counter value that will identify the currency pair, afterwards we want to get the position type that is done by using “PositionGetInteger” and the parameter we use is “POSITION_TYPE” – all in capital letters – now we check if the current symbol on the chart equals the currency pair of our position and one more thing we want to know is if the position direction equals position type buy (POSITION_TYPE_BUY) – also in capital letters.
And if that is the case we want to take the current value for number of buy positions and increase it by one. We need to end the “for” loop here and now we want to return the number of buy positions to the main function that is done by using the “return” statement and we will return the value for the calculated number of buy positions (NumberOfBuyPositions) here.
One more bracket is missing to close the whole function, that’s about it.

I will show you my function to open test positions (OpenTestPositions), don’t use it on a real account, I use a random generator to calculate a random number so we will have up to 10 positions and afterwards I will open buy and sell trades just for testing purposes, don’t do that on a real account.

So let’s compile the code by clicking on the little button here or you can also press F7.
I have an error here, let’s make that a “void” function because I don’t need to check the return value, let’s recompile the code and this time we didn’t get any errors so now we can click on a little button here or press F4 to go back to Metatrader. 

In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, we pick the “SimpleBuyPositionCount.ex5” file, please mark the visualization option here and start a test.

Here we are our! Our Expert Advisor tells us that we have 8 buy positions, so let’s count: 1, 2, 3, 4, 5, 6, 7, 8, that is correct.
I will stop the test here, start a new one and this time it says we have 5 buy positions, 1, 2, 3, 4, 5, that’s correct.
So now you know how to calculate the number of buy positions, if you have a question for a video like this one just send me an email but for now, you know how to calculate the number of open buy positions and you have coded it yourself with a few lines of MQL5 code.