PositionsTotal()

 

How should I use PositionsTotal() to return the number of operations at the symbol (chart) applied and not the total? (MQL5 code)

For several reasons I need you to be as well. I don't want the total, as I do not wish to take into account manual trades in other symbols.

Thank you in advance.‌

int total=PositionsTotal();
   int b=0,s=0,n=0;
   for(int i=total-1; i>=0; i--)
 
Miguel Angel Vico Alba:

How should I use PositionsTotal() to return the number of operations at the symbol (chart) applied and not the total? (MQL5 code)

For several reasons I need you to be as well. I don't want the total, as I do not wish to take into account manual trades in other symbols.

Thank you in advance.‌

int total=PositionsTotal();
   int b=0,s=0,n=0;
   for(int i=total-1; i>=0; i--)
I think PositionSelect("the symbol you need") will help you...
 
adkort:
I think PositionSelect("the symbol you need") will help you...


Does not work, because what true/false is returned, not the number of open positions in that time and symbol.

Thanks anyway. ;)

 

Why people who are selling products on the Market want to get free help on the forum ?

 
Alain Verleyen:

Why people who are selling products on the Market want to get free help on the forum ?


You have never had a doubt? That lucky...

By the way...issue resolved.

PosCounter=0;

      for(int i=PositionsTotal()-1; i>=0; i--)
        {
         string CounterSymbol=PositionGetSymbol(i);

         if(Symbol()==CounterSymbol && m_position.Magic()==MagicNumber)
           {
            PosCounter+=1;


 
Miguel Angel Vico Alba:


You have never had a doubt? That lucky...

By the way...issue resolved.

PosCounter=0;

      for(int i=PositionsTotal()-1; i>=0; i--)
        {
         string CounterSymbol=PositionGetSymbol(i);

         if(Symbol()==CounterSymbol && m_position.Magic()==MagicNumber)
           {
            PosCounter+=1;


I was sure you will be able to find it.
 
Miguel Angel Vico Alba #:


Does not work, because what true/false is returned, not the number of open positions in that time and symbol.

Thanks anyway. ;)

PositionTotal is a void function, dont accept parameter.

 
Miguel Angel Vico Alba #:


You have never had a doubt? That lucky...

By the way...issue resolved.

PosCounter=0;

      for(int i=PositionsTotal()-1; i>=0; i--)
        {
         string CounterSymbol=PositionGetSymbol(i);

         if(Symbol()==CounterSymbol && m_position.Magic()==MagicNumber)
           {
            PosCounter+=1;


Quite easy Miguel, I use TotalPos++.

   int LongPos=0,ShortPos=0,TotalPos=0;
   for(int i=0;i<PositionsTotal();i++){
      ulong iTicket=PositionGetTicket(i);
      if(PositionGetString(POSITION_SYMBOL)==_Symbol
      &&PositionGetInteger(POSITION_MAGIC)==MagicNumber){
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){LongPos++;}
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){ShortPos++;}
         TotalPos++;
         }
      }
 
Roberto Pontes #:

PositionTotal is a void function, dont accept parameter.

Thank you! But as I answered 5 years ago...issue solved! Hahaha

 
Miguel Angel Vico Alba #:

Thank you! But as I answered 5 years ago...issue solved! Hahaha

Interesting twist - me thinks.

I have been trying to track down why my 'deletes' don't seem to be working properly.  Then using loads of test statements I think I have an answer.

int      Positions_Total   = PositionsTotal();
   if ( Direction_of_Trade == "DN" )
   {  for ( int i = 0; i< Positions_Total; i++) 


I now think the above gets over the problem ... because....

Positions_Total is now fixed at the value of PositionsTotal().

If I delete records in the loop PositionsTotal() reduces and I does not reach the 'end value' and consequently does not delete all expected records.

Hope - a) this is correct & b) helps someone

 
Peter Williams #:

Interesting twist - me thinks.

I have been trying to track down why my 'deletes' don't seem to be working properly.  Then using loads of test statements I think I have an answer.


I now think the above gets over the problem ... because....

Positions_Total is now fixed at the value of PositionsTotal().

If I delete records in the loop PositionsTotal() reduces and I does not reach the 'end value' and consequently does not delete all expected records.

Hope - a) this is correct & b) helps someone

Thats not correct....

Here is why: (Has been explained multiple times)

When you have a list and you delete the record, you are looking at right now, then the next record will take the place in the list which you are looking at.

Since you are done with this line in your list, you will go to the next line, but the record from that line had been moved to the one you are now coming from... so you will miss that entry.


Clear?

EDIT: 

The solution is to start at the end of the list, so your "for()-loop" must count backwards...