MQL5 TUTORIAL BASICS – 29 HOW TO CALCULATE THE NUMBER OF SELL POSITIONS

video
play-sharp-fill
  1. Introduction to Counting Sell Positions in MQL5 (00:00 – 00:25)
    • Introduction to the tutorial on how to count the number of sell positions using MQL5.
  2. Opening MetaEditor and Creating a New Expert Advisor File (00:25 – 00:37)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “Simple Sell Position Count”.
  3. Setting Up the Code Structure and Including Trade.mqh (00:37 – 00:55)
    • Removing unnecessary code and including the ‘Trade.mqh’ file for trading functions.
  4. Creating a Custom Function to Open Test Positions (00:55 – 01:24)
    • Defining a custom function to open test positions for demonstration purposes.
  5. Implementing the Sell Position Counting Logic (01:24 – 03:17)
    • Coding the logic to count sell positions using a for loop and checking each position’s type and symbol.
  6. Compiling and Testing the Expert Advisor in MetaTrader (03:17 – 04:18)
    • Compiling the code and instructions on testing the Expert Advisor in MetaTrader using the Strategy Tester.
  7. Verifying the Sell Position Count in MetaTrader (04:18 – 05:11)
    • Running the Expert Advisor in MetaTrader and verifying the count of sell positions matches the actual number on the platform.

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. 

That’s about it.
I want to show you the function to open the test positions, I’m using a random generator to calculate a random number and this random number is used to buy and sell a random number of test positions that we can count. You wouldn’t do that on your real money account so when you’re done please click on the compile button here or press F7, that should work without any errors, if you have some errors or if you don’t understand what all the code does maybe you want to watch the other videos in this basic video series or maybe even the premium course is interesting for you but if you don’t have any errors here please click on the little button here or press F4 to go back to Metatrader. 

And in Metatrader we want to click on view, strategy tester or press control and r, here we pick the new file simple position count dot ex5, please enable the visualization mode here and start a test.

Here we are, the expert advisor says that we have nine sell positions, so let’s sort by type, these are buy positions, here are the sell positions and this is one, two, three, four, five, six, seven, eight, nine positions, the next one is a buy position so the EA is working.
If you have any questions or suggestions that should become a video like this one just write me an email but for now you know how to create an expert advisor to calculate the current number of sell positions and you have coded it yourself with a few lines of mql5 code.