# Position_Volume = 0

Hello I'm working into a new grid and I use PositionVolume to calculate a proper take profit.

Main loop is backcounter (i--), previously I've coded a forward counter (i++) to get values from the first opened position:

```      //+------------------------------------------ Internal loop ---!
double SellOrders=0,SellProfit=0;
double FirstLongPV=0,FirstShortPV=0;
for(int i=0;i<PositionsTotal();i++){
if(PositionGetString(POSITION_SYMBOL)==_Symbol){
FirstLongPV=PositionGetDouble(POSITION_VOLUME); // ERROR: volume=0.0.
}
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){
SellOrders++;SellLots+=PositionGetDouble(POSITION_VOLUME);
FirstShortPV=PositionGetDouble(POSITION_VOLUME); // ERROR: volume=0.0.
SellProfit+=PositionGetDouble(POSITION_PROFIT);
}
}
}```

The main loop where they are the TP lines starts like this:

```      //+-------------------------------------- OrderManagement loop ---!
for(int i=PositionsTotal()-1;i>=0;i--){
ulong iTicket=PositionGetTicket(i);
if(PositionGetString(POSITION_SYMBOL)==_Symbol){
double LastPV=PositionGetDouble(POSITION_VOLUME);
double GridLots=MathRound((LastPV*Martingale)/minLot)*minLot;
if(ATR_Divider>0){
if(TP_Method==0){
Print("FirstLongPV: ",FirstLongPV); // Volume is 0!!
CloseLongFIFO();
break;
}```

Is there someone who can help on this matter?

Yes.

You need to do a PositionSelect() before you read positions values.

Regards

Dominik Egert:
Yes.

You need to do a PositionSelect() before you read positions values.

Regards

Could you explain better through a line of code? I'm not done with PositionSelect().

Posted wrong code, it's like this:

```      double BuyLots=0,SellLots=0;
double SellOrders=0,SellProfit=0;
double FirstLongPV=0,FirstShortPV=0;
for(int i=0;i<PositionsTotal();i++){
ulong iTicket=PositionGetTicket(i);
if(PositionGetString(POSITION_SYMBOL)==SName){
FirstLongPV=PositionGetDouble(POSITION_VOLUME); // ERROR: da cero.
}
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){
SellOrders++;SellLots+=PositionGetDouble(POSITION_VOLUME);
FirstShortPV=PositionGetDouble(POSITION_VOLUME); // ERROR: da cero.
SellProfit+=PositionGetDouble(POSITION_PROFIT);
}
}
}```
BuyProfit and SellProfit are correct, as they are working through another averaging method.

David Diez:

Could you explain better through a line of code? I'm not done with PositionSelect().

Posted wrong code, it's like this:

BuyProfit and SellProfit are correct, as they are working through another averaging method.

Try something like that.

```//+------------------------------------------ Internal loop ---!
double SellOrders=0,SellProfit=0;
double FirstLongPV=0,FirstShortPV=0;
//---
for(int i=PositionsTotal()-1; i>=0; i--) //for first order need to countdown
{
if(PositionGetTicket(i))
{
if(PositionGetString(POSITION_SYMBOL)==_Symbol)
{
{
FirstLongPV=PositionGetDouble(POSITION_VOLUME); // ERROR: volume=0.0.
}
if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
SellOrders++;
SellLots+=PositionGetDouble(POSITION_VOLUME);
FirstShortPV=PositionGetDouble(POSITION_VOLUME); // ERROR: volume=0.0.
SellProfit+=PositionGetDouble(POSITION_PROFIT);
}
}
}
}```

Nikolaos Pantzos:

Try something like that.

Tried, but not solved.

Changing the loop count direction into both loops gives also a bad performance in the only averaging method that works.

I tried to print the values, and they're correct until I get into the TP method lines:

```                  if(TP_Method==0){ // No FirstLongPV below this line.
Print("FirstLongPV: ",FirstLongPV);
if(Condition){ // Close profit by first order lot.
CloseLongFIFO();
break;
}
}
else if(TP_Method==1){
if(BuyProfit>=LastPV*(ATR[1]/ATR_Divider)/SPoint){ // Close profit by averaging (working).
CloseLongFIFO();
break;
}
}
else if(TP_Method==2){ // No BuyLots below this line.
if(Condition){ // Close profit at netting point.
CloseLongFIFO();
break;
}
}```

Still stucked.

Very important!!!
Before you can rely on PositionGet*() you MUST do a PositionSelect ().

RTFM... :-)

Dominik Egert:

Very important!!!
Before you can rely on PositionGet*() you MUST do a PositionSelect ().

RTFM... :-)

It's done this way:

```      for(int i=0;i<PositionsTotal();i++){
ulong iTicket=PositionGetTicket(i);
if(PositionGetString(POSITION_SYMBOL)==_Symbol){```

Tried PositionSelect() in several ways but unuseful.

Before the PositionGetTicket() insert this:

PositionSelectByTicket(PositionGetTicket(i));

Or after the line:
PositionSelectByTicket(iTicket);

Looks to me like you still did not RTFM.

An example of traversing all positions with and without trading classes:

```#include <Trade\PositionInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
***
//+------------------------------------------------------------------+
//| Calculate all positions                                          |
//+------------------------------------------------------------------+
void CalculateAllPositions()
{
int total_with=0;
for(int i=PositionsTotal()-1; i>=0; i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
total_with++;
//---
int total_without=0;
for(int j=PositionsTotal()-1; j>=0; j--) // returns the number of current positions
{
ulong ticket=PositionGetTicket(j);
if(ticket>0);
{
if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC)==InpMagic)
{
total_without++;
}
}
}
//---
print(total_with,", ",total_without);
}```

Ok, as it seems I have not RTFM.
Doc says PositionGetTicket() selects the position for further working with it.

I am sorry for the mistake and pointing towards you.

I've been using the PositionSelectByTicket in any case and had never issues like you.

Therefore I expected such a mistake here. Haven't done the research correctly.

My apologies.

Reason: