Why doesn't this work?

 

Hey guys, why doesnt this Print out the Symbols of open positions please?

void print_symbols()
  {
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      string symbol = PositionGetString(POSITION_SYMBOL);
      Print(symbol);   
     }
  } 
 

Please read the documentation: PositionGetString

The function returns the requested property of an open position, pre-selected using PositionGetSymbol or PositionSelect. The position property should be of the string type. There are 2 variants of the function.

The correct iteration in the loop by positions ((this is an example of counting positions)   

int total=0;
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
     {
      ulong ticket=PositionGetTicket(index);
      if(ticket>0);
        {
         if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC)==InpMagic)
           {
            total++
           }
        }
     }
Documentation on MQL5: Trade Functions / PositionGetString
Documentation on MQL5: Trade Functions / PositionGetString
  • www.mql5.com
PositionGetString - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov #:

Please read the documentation: PositionGetString

The function returns the requested property of an open position, pre-selected using PositionGetSymbol or PositionSelect. The position property should be of the string type. There are 2 variants of the function.

The correct iteration in the loop by positions ((this is an example of counting positions)   

Ah I see, so I need to make sure it has been "pre-selected using PositionGetSymbol or PositionSelect". It seems that PositionGetTicket() does the job also, so in my example this would be the correction:

void print_symbols()
  {
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
     {
      ulong ticket=PositionGetTicket(i);
      if(ticket > 0)
        {
         string symbol = PositionGetString(POSITION_SYMBOL);
         Print(symbol); 
        }
     }
  }   

Thankyou! 

Documentation on MQL5: Trade Functions / PositionGetSymbol
Documentation on MQL5: Trade Functions / PositionGetSymbol
  • www.mql5.com
PositionGetSymbol - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Benjamin David Hardman # :

Ah I see, so I need to make sure it has been "pre-selected using  PositionGetSymbol  or  PositionSelect ". It seems that PositionGetTicket() does the job also, so in my example this would be the correction:

Thankyou! 

Yes, now your example will work. And yes: ' PositionGetTicket ' also selects a position

 ... and automatically selects the position to work ...
Documentation on MQL5: Trade Functions / PositionGetTicket
Documentation on MQL5: Trade Functions / PositionGetTicket
  • www.mql5.com
PositionGetTicket - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5