Show profit of all positions actually opened for all symbol

 
printf("Numbers of opened positions : %G", PositionsTotal());
for (int x=0;x<=PositionsTotal();x++) { //numbers of position = index 
printf("Index of the position %G",x);
printf("Symbol of the selected position : %G",PositionGetSymbol(x));
printf("Profit on the selected position : %G", PositionGetDouble(POSITION_PROFIT));
}

Can't figure why this doesn't work, indeed, it shows :


2014.03.27 13:53:14     Core 1  2014.01.21 13:45:00   Numbers of opened positions : 1
2014.03.27 13:53:14     Core 1  2014.01.21 13:45:00   Index of the position 1
2014.03.27 13:53:14     Core 1  2014.01.21 13:45:00   Symbol of the selected position : 0 <---------


any help ? 

 
blouf:

Can't figure why this doesn't work, indeed, it shows :


any help ? 

Because symbol is a string, check also your indexation :

printf("Numbers of opened positions : %G", PositionsTotal());
for (int x=0;x<PositionsTotal();x++) { //numbers of position = index 
printf("Index of the position %G",x);
printf("Symbol of the selected position : %s",PositionGetSymbol(x));
printf("Profit on the selected position : %G", PositionGetDouble(POSITION_PROFIT));
}
 
angevoyageur:

Because symbol is a string, check also your indexation :

for (int x=0;x<=PositionsTotal()-x;x++)

This works. Once all profit of current positions found, is there a way to compare each to the others to find the one with the bigger loss ? 

EDIT : Arrays ?

 
blouf:

This works. Once all profit of current positions found, is there a way to compare each to the others to find the one with the bigger loss ? 

EDIT : Arrays ?

That doesn't work.

If you have PositionsTotal() positions, they are indexed from 0 to PositionsTotal()-1, so :

for (int x=0;x<PositionsTotal();x++)
 
angevoyageur:

If you have PositionsTotal() positions, they are indexed from 0 to PositionsTotal()-1, so :

Indeed. What about using array to compare profits of current opened position ?
 
blouf:
Indeed. What about using array to compare profits of current opened position ?
Why not.
 
angevoyageur:
Why not.
for (int x=0;x<=PositionsTotal()-1;x++) { //numbers of position = index 
printf("Index of the position %G",x);
printf("Symbol of the selected position : %s",PositionGetSymbol(x));
MaxCurrentLoss[x] = PositionGetDouble(POSITION_PROFIT); <--- ERROR :  array out of range
printf("Profit on the selected position : %G", MaxCurrentLoss[x]);
}
}
Because i'm trying to figure how. unfortunately .. :/
 
blouf:
Because i'm trying to figure how. unfortunately .. :/

Hi blouf, try this quite simple change in your code, without arrays, since you just want to know the bigger loss value:

printf("Numbers of opened positions : %G", PositionsTotal());
double bigger_loss=0;
int bigger_loss_index=-1;
for (int x=0;x<PositionsTotal();x++) { //numbers of position = index 
   printf("Index of the position %G",x);
   printf("Symbol of the selected position : %s",PositionGetSymbol(x));
   printf("Profit on the selected position : %G", PositionGetDouble(POSITION_PROFIT));
   if (PositionGetDouble(POSITION_PROFIT)<bigger_loss) {
      bigger_loss=PositionGetDouble(POSITION_PROFIT);
      bigger_loss_index=x;
   }
}
if (bigger_loss_index>=0)
   printf("Bigger loss : %G (Index of the position : %G)",bigger_loss,bigger_loss_index);
else
   printf("No loss, yet! :-)");
 
figurelli:

Hi blouf, try this quite simple change in your code, without arrays, since you just want to know the bigger loss value:

yep. thank you it works like a charm.
 
blouf:
yep. thank you it works like a charm.
A piece of cake, you are welcome.
 
figurelli:

Hi blouf, try this quite simple change in your code, without arrays, since you just want to know the bigger loss value:

Nope sorry it doesn't since in MT5 there's 1 position by symbol, it doesn't compare the loss on each symbol

EDIT : alright, when // debugs' printf forgot that PositionGetSymbol() ALSO select the position