How To Determine Total Deals In An Open Position

 

I am trying to figure out the total open deals in an open position.  I am using the following code but it seems to return the value of PositionsTotal() instead of the total deals within the position.  Can someone help. 

Once again it seems I would have to call on both Rosh and Wackena to help me resolve this coding issue.

 


//+------------------------------------------------------------------+
//|   return Total Deals in current open position                    |
//+------------------------------------------------------------------+
int TotalDeals()
  {
   uint pos_total=0;
   uint total=0;
   long pos_id=0;
   ulong HTicket=0;
   int count=0; // I added initial default value
   pos_total=PositionsTotal();
   if(pos_total>0)
     { // continue if current open position

      if(PositionSelect(Symbol())) // continue if open position is for chart symbol
        {
         pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER);
         HistorySelectByPosition(pos_id);
         total=HistoryDealsTotal();
         HTicket=HistoryDealGetTicket(total-1); // get ticket number for last deal in position
        
         if(HTicket>0)
           
               if(PositionGetInteger(POSITION_MAGIC)==MagicNumber)
                  {
                     count++;
                  }

         return(count);
        }

     }
   return(0);
  }
     

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Position Properties - Documentation on MQL5
 

Try this.

//+------------------------------------------------------------------+
//|   return Total Deals in current open position                    |
//+------------------------------------------------------------------+
int TotalDeals()
  {
   uint pos_total=0;
   uint total=0;
   long pos_id=0;
   pos_total=PositionsTotal();
   if(pos_total>0)
     { // continue if current open position

      if(PositionSelect(Symbol())) // continue if open position is for chart symbol
        {
         pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER);
         HistorySelectByPosition(pos_id);
         total=HistoryDealsTotal();
        }
     }
   return(total);
  }



 
wackena:

Try this.



Thanks for the quick response.  It is returninng the cumulative total in history.  What I want is the number of deals within the current open position.   That means  it has to reset to zero when the position is liquidated.  The count restarts when a new position is established.

I hope I am clear. 

 

Thanks for your help 

Documentation on MQL5: Trade Functions / HistoryDealsTotal
  • www.mql5.com
Trade Functions / HistoryDealsTotal - Documentation on MQL5
 

Lets loop through all potential open Positions and then select chart symbol to find position deals.

//+------------------------------------------------------------------+
//|   return Total Deals in current open position                    |
//+------------------------------------------------------------------+
int PositionDeals()
{
   uint pos_total=0;
   uint total=0;
   ulong pos_id=0;
   
   pos_total=PositionsTotal();
   if(pos_total > 0) {
   for(uint i=0;i<pos_total;i++)
      {
         if(PositionSelect(Symbol())) 
            {
               pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER);
               HistorySelectByPosition(pos_id);
               total=HistoryDealsTotal();
   
               return(total); 
            }
      }
   }   
   return(0);
}




 
wackena:

Lets loop through all potential open Positions and then select chart symbol to find position deals.




Thanks buddy.
 
wackena:

Lets loop through all potential open Positions and then select chart symbol to find position deals.


 

 


There is no need for loop
//+------------------------------------------------------------------+
//|  die Anzahl der Deals der offenen Position des Symbol            |
//+------------------------------------------------------------------+
int PositionDeals()
{
 
   uint total=0;
   ulong pos_id=0;
  
         if(PositionSelect(Symbol())) 
            {
               pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER);
               HistorySelectByPosition(pos_id);
               total=HistoryDealsTotal();
           
            return(total); 
            }
   
   return(0);
}
Reason: