# Why PositionsTotal() is always returning zero even when there is open trades

I have the code below but PostionsTotal() is always returning zero even when there is open trades.

I am trying to return the total number of currently open trades.

Any help highly appreciated. Thanks.

{
int totalPositions=0;
int total   =  PositionsTotal();
int oType;

if(total<=0)
{
return(0);
}
else
{
for(int i=total-1;i>=0;i--)
{
if (!PositionSelectByTicket(i)) continue;
oType =  (int)PositionGetInteger(POSITION_TYPE);

{
totalPositions++;
}
}
}
return(totalPositions);
}

Always 0 because you are comparing different things :

this can’t be true. A position type is not the same as an order type.

But PositionsTotal() is always zero.

That’s is before the error you pointed out

At least that should return a value?

How do you know it always returns zero ?

If there is a position, as soon as the terminal is notified about it PositionTotal() must return a value >0

Turns out the culprit was the variable type.

Change

int total   =  PositionsTotal();

to

uint total   =  PositionsTotal();

The corrected version of the code is this

{
int totalPositions=0;
uint total = PositionsTotal();
int oType;

if(total<=0)
{
return(0);
}
else
{
for(uint i=0;i<total;i++)
{
if (!PositionSelect(_Symbol)) continue;
oType =  (int)PositionGetInteger(POSITION_TYPE);

{
totalPositions++;
}
}
}
return(totalPositions);

}

BTW, oType is just a variable name.

I placed a “watch” on the variable during debug.

the fix was to change:

int total   =  PositionsTotal();

to

uint total   =  PositionsTotal();