You need to write loop using PositionsTotal() and then examine the position type of each position, depending whether it is buy or sell you can add them up.
Some references below:
https://www.mql5.com/en/docs/trading/positionstotal
https://www.mql5.com/en/docs/trading/positionselect
https://www.mql5.com/en/docs/trading/positiongetinteger
https://www.mql5.com/en/docs/constants/tradingconstants/positionproperties#enum_position_type

- www.mql5.com
You need to write loop using PositionsTotal() and then examine the position type of each position, depending whether it is buy or sell you can add them up.
Some references below:
....Sir, can you please... write working example code..
because still its hard for me.. like
1. how to detect its a buy or sell positions???
2. how to detect its a same symbol or other symbol??
if you don't mind can you please write an code which will produce on chart like below
total positions = 7
buy = 5
sell = 2

- www.mql5.com
int buy_count = 0; int sell_count = 0; void OnTick() { Comment("Buy Count : ", buy_count , "\n", "Sell Count : ", sell_count , "\n" ); for(int i=PositionsTotal()-1 ; i <= 0 ; i-- ) { if( PositionSelect(_Symbol) == POSITION_TYPE_BUY ) { buy_count++; } if( PositionSelect(_Symbol) == POSITION_TYPE_SELL) { sell_count++; } } // for loop end } // on tick endthe above code is not working... there is lots of mistake please help me
the above code is not working... there is lots of mistake please help me
Here is a working version - see the comments to understand the errors in your version
//+------------------------------------------------------------------+ //| 429908-CountBUYSELLpositions.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" // https://www.mql5.com/en/forum/429908 //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ int buy_count = 0; int sell_count = 0; void OnTick() { Comment("\n\n\n\n\n"+ "Buy Count : ", buy_count, "\n", "Sell Count : ", sell_count, "\n"); buy_count = 0; sell_count = 0; //#1 initialize counts for(int i = PositionsTotal() - 1 ; i >= 0 ; i--) //#2 not i <= 0 { PositionGetTicket(i); //#3 Missing PositionGetTicket(i); if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) //#4 not PositionSelect(_Symbol) { buy_count++; } if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) //#4 not PositionSelect(_Symbol) { sell_count++; } } // for loop end }
Thank You... sir (+1 Respect From Me To You)
still i have some questions sir Can You Please clear my doubts??
Q.1 > why did you again initialize buy_count =0; sell_count=0; inside the ontick funtions??? how its effecting ??
Q.2 > when i comment out PositionGetTicket(i); it don't work, how its contributing here?? is it someting line ArraySetAsSeries()??
Please Suggest Me Some books ?? and good beginers/intermediat mql5 articles and books which i should definitely read
Q.1 > why did you again initialize buy_count =0; sell_count=0; inside the ontick funtions??? how its effecting ??
Q.2 > when i comment out PositionGetTicket(i); it don't work, how its contributing here?? is it someting line ArraySetAsSeries()??
A.1 > SCOPE OF VARIABLES. Jump to GLOBAL VARIABLES.
A.2 > highlight positiongetticket or part of it, then press F1 on keyboard to open documentation.
Thank You... sir (+1 Respect From Me To You)
still i have some questions sir Can You Please clear my doubts??
Q.1 > why did you again initialize buy_count =0; sell_count=0; inside the ontick funtions??? how its effecting ??
Q.2 > when i comment out PositionGetTicket(i); it don't work, how its contributing here?? is it someting line ArraySetAsSeries()??
Please Suggest Me Some books ?? and good beginers/intermediat mql5 articles and books which i should definitely read
A1. Comment them out and see the result - you will get ever increasing incorrect numbers with each new tick
A2. Read the documentation - this function must be used to select a ticket's data so it can be read correctly
PositionGetTicket(i); doubt is clear now, it's selecting the i 'th no. of open positions and getting ticket no. of that i and auto selecting and keeping it for next operating to done with it..
but.. i have still some doubt relating to variables like how its working ??
like first it initialized in globally right..
int buy_count = 0; int sell_count = 0;
then why i reinitialized it inside the ontick function??
void OnTick() { buy_count = 0; sell_count = 0; //
and when the for loop is running ... its adding to local variable or global variable???
for(int i = PositionsTotal() - 1 ; i >= 0 ; i--) //#2 not i <= 0 { PositionGetTicket(i); //#3 Missing PositionGetTicket(i); if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) //#4 not PositionSelect(_Symbol) { buy_count++; } if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) //#4 not PositionSelect(_Symbol) { sell_count++; }
and it comment() is taking buy_count from local variable or global variable?
Comment("\n\n\n\n\n", "Buy Count : ", buy_count, "\n", "Sell Count : ", sell_count, "\n");
can you please explain the whole process step by step ????
All references inside OnTick() are to the global variables, so you have to initialize them on each tick, otherwise you will get ever increasing numbers.
There are no local variables defined inside OnTick().
If that is not clear, comment out the line and you will see the numbers increase with every tick
buy_count = 0; sell_count = 0; //
Please Suggest Me Some books ?? and good beginers/intermediat mql5 articles and books which i should definitely read
Search the forum for tutorials in MQL5.
As a basis you could learn some C++ - a good place would be https://www.w3schools.com/c/index.php

- www.w3schools.com

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey i'm new here.. can you please help me to count the no. of positions do i have open, and in which how many there are buy positions and sell ,
i need mql5 code.
like..
totol open positions = 5
buy = 3
sell = 2
thank you.