# PositionsTotal function not working!

@William Roeder @vladimir250273@VLADISLAV BUTOV@Vladimir@Ahmet Metin Yilmaz@Stanislav Ivanov@Keith Watford@nicholish en @Marco Y Anja VlaskampHello  guys sorry for taking your time ,but I have this big problem  since I started  making EAs.......! Why didnt  Metatrader  Create a predefined  Function that could calculate PositionsTotal  based on EAs Magic Number!! This could save newbies like I when starting programming journey as there is no great use of having only one EA running to avoid interruption in the PositionsTotal function.

So I tried all my best, remember am newbie in MQL5 .... to create my own PositionTotal() function but the problem comes when I close the position it doesn't decrement ,though I was looking on a way to do that ! So iam asking for your help Senior MQl5 Programmers .... Thanks

```//Create an instance of CTrade

CPositionInfo m_position;

input double LotSize=0.2;
input ulong MagicNumber=2345678;

int PositionTotal;

void OnInit(){//

}//

void OnTick()
{

if(PositionTotal==0)
{//
LotSize,//how much
NULL,//current symbol
0,//stop loss
0,//take profit
NULL//comment
);

}//

PositionTotalFunction();

}//ontick

int PositionTotalFunction(){//

PositionTotal=0;

for(int h=0; h<PositionsTotal();h++){
ulong PositionsMagicNumber=PositionGetInteger(POSITION_MAGIC);
if(PositionsMagicNumber==MagicNumber){

PositionTotal++;

}
}

return(PositionTotal++);

}//PositionTotalFunction```

`return(PositionTotal++);`

Why the ++ ?

### Simple example One position

• 2020.07.05
• www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...

Keith Watford:

Why the ++ ?

Hello Keith...... If  I use return(PositionTotal) without the ++ is that in real time trading my positionTotal is always equal to zero as per my code above. But in the strategy tester it works fine without the ++ , I don't know why!!!! But my main goal is to make a positionTotal function based on magic numbers but the  problem is when I close a position it cannot detect if it's closed and I was looking on a way to do that, so your advice would be much appreciated thanks@Keith Watford

### Simple example One position

Hi thanks Vladimir Karputov for your code ,but the problems of return(true/false),  if position equals to my MagicNumber  is that ..... What if am making an EA based on averaging strategy   which have  lots of open positions at the same time!! How can I stop another  position being opened if condition s are still true?  Your code is quite useful for one position at a time  but what if there are more than one and all have the same MagicNumber?

Hi thanks Vladimir Karputov for your code ,but the problems of return(true/false),  if position equals to my MagicNumber  is that ..... What if am making an EA based on averaging strategy   which have  lots of open positions at the same time!! How can I stop another  position being opened if condition s are still true?  Your code is quite useful for one position at a time  but what if there are more than one and all have the same MagicNumber?

Sorry, I did not understand your question.

Sorry, I did not understand your question.

Hi , what I actually meant is that what if  Positions to be opened are two at the same TIME ? ...  and not one. How can it be possible in your code to limit only  two positions at the same time ?  I say this because my EA opens a second position when the first  position  is in loss by x amount of pips. I could easily limit this using PositionsTotal() function for MQl5 but that will make my EA not trade well while other positions are opened manually or with other EAs

Documentation on MQL5: Trade Functions / PositionsTotal
• www.mql5.com

@William Roeder @vladimir250273@VLADISLAV BUTOV@Vladimir@Ahmet Metin Yilmaz@Stanislav Ivanov@Keith Watford@nicholish en @Marco Y Anja VlaskampHello  guys sorry for taking your time ,but I have this big problem  since I started  making EAs.......! Why didnt  Metatrader  Create a predefined  Function that could calculate PositionsTotal  based on EAs Magic Number!! This could save newbies like I when starting programming journey as there is no great use of having only one EA running to avoid interruption in the PositionsTotal function.

So I tried all my best, remember am newbie in MQL5 .... to create my own PositionTotal() function but the problem comes when I close the position it doesn't decrement ,though I was looking on a way to do that ! So iam asking for your help Senior MQl5 Programmers .... Thanks

Hi there, try this:

```      int OrdersCount=0;
for(int i=PositionsTotal()-1;i>=0;i--){
ulong iTicket=PositionGetTicket(i);
if(PositionSelectByTicket(iTicket)&&
PositionGetString(POSITION_SYMBOL)==_Symbol){ // <-- Here your MN condition.
OrdersCount++;
}
}```