# How To Determine Total Deals In An Open Position

Write articles and earn money. We pay 200 USD for them!
376
2010.09.11 21:38

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.

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
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
648
2010.09.11 21:51

Try this.

```//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
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);
}```

376
2010.09.11 23:59
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.

Documentation on MQL5: Trade Functions / HistoryDealsTotal
• www.mql5.com
Trade Functions / HistoryDealsTotal - Documentation on MQL5
648
2010.09.12 04:00

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

```//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
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);
}```

376
2010.09.12 14:20
wackena:

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

Thanks buddy.
6949
Alexander Piechotta 2010.10.11 21:05
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);
}
```