I am sharing because it worked and I kept doing it badly.
-
Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
Messages Editor
Forum rules and recommendations - General - MQL5 programming forum (2023) -
if(PositionSelectByTicket(PositionGetTicket(i)))
GetTicket selects that position, why are you selecting it again?
The function returns the ticket of a position with the specified index in the list of open positions and automatically selects the position to work with using functions PositionGetDouble, PositionGetInteger, PositionGetString.
-
Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
Messages Editor
Forum rules and recommendations - General - MQL5 programming forum (2023) - GetTicket selects that position, why are you selecting it again?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
This is a simple code that worked.
I am just sharing with y ou
The problem with counting is that there are functions that will not count in a for loop.
this actually worked for me . I use magic number for filtering and it worked. Hope you enjoy it.
first declare your magicnumber
let´s say
init magicnumber=123;
I will call Ctrade:
CTrade trade;
then OnInit section, I will assign the magic number:
int OnInit()
{
trade.SetExpertMagicNumber(magicNumber);
// rest of your code
}
then this fuction that I struggled with multiple versions:
int CountOrdersMagic(int magicnumber)
{
int count = 0;
for(int i = 0; i < PositionsTotal(); i++)
{
if(PositionSelectByTicket(PositionGetTicket(i)))
{
if(PositionGetInteger(POSITION_MAGIC) == magicnumber)
{
count++;
}
}
}
return (count);
}
//+------------------------------------------------------------------+
Enjoy it