How to use PositionGetDouble(POSITION_VOLUME) correctly?

 

My EA places multiple trades in a row, thus adding to an open position. Let's take an example:

First trade: open a sell position of 0.01 lot EURUSD

Second trade, some time later: open a sell position of 0.01 lot EURUSD

Third position, some time after the second trade: open a sell position of 0.02 lot EURUSD

Now, again some time later, I want to know the total size of the open EURUSD position.

The code that I'm using is:

PositionSelect(Symbol());
double size = PositionGetDouble(POSITION_VOLUME);


However, this results in size = 0.01 lot, where I expect a value of 0.04 lot.

What code should I use to get the total open EURUSD position size?

 
WindmillMQL:

My EA places multiple trades in a row, thus adding to an open position. Let's take an example:

First trade: open a sell position of 0.01 lot EURUSD

Second trade, some time later: open a sell position of 0.01 lot EURUSD

Third position, some time after the second trade: open a sell position of 0.02 lot EURUSD

Now, again some time later, I want to know the total size of the open EURUSD position.

The code that I'm using is:


However, this results in size = 0.01 lot, where I expect a value of 0.04 lot.

What code should I use to get the total open EURUSD position size?

Check out the help: the difference between netting and a hedge account.  You use a function that only works correctly on a netting account or on a hedge account (but only if there is only one position for the symbol).
 
Vladimir Karputov:
Check out the help: the difference between netting and a hedge account.  You use a function that only works correctly on a netting account or on a hedge account (but only if there is only one position for the symbol).
Thank you for your reply. I did not know that there are two sorts of accounts. I will do a search and hope to find a description of these two and how to handle this.
 
WindmillMQL:

My EA places multiple trades in a row, thus adding to an open position. Let's take an example:

First trade: open a sell position of 0.01 lot EURUSD

Second trade, some time later: open a sell position of 0.01 lot EURUSD

Third position, some time after the second trade: open a sell position of 0.02 lot EURUSD

Now, again some time later, I want to know the total size of the open EURUSD position.

The code that I'm using is:


However, this results in size = 0.01 lot, where I expect a value of 0.04 lot.

What code should I use to get the total open EURUSD position size?

Simply using loop.  Within the loop use if statement, filter with magicnumber and order symbol then return sum amount lot. 
 
Cosmas Moses:
Simply using loop.  Within the loop use if statement, filter with magicnumber and order symbol then return sum amount lot. 

Thank you for your suggestion. My account is a hedging account. I assumed that if I use PositionGetDouble(POSITION_VOLUME) I would automatically get the total, net, size of the position. That assumption seems to be incorrect.

You suggest to use a loop. What parameter should I use to run the loop? I should get a list of open positions in Symbol() first, and then loop over all of these?

I am new to programming an EA and am not yet familiar with the best practices. Help is appreciated.

 
WindmillMQL:

Thank you for your suggestion. My account is a hedging account. I assumed that if I use PositionGetDouble(POSITION_VOLUME) I would automatically get the total, net, size of the position. That assumption seems to be incorrect.

You suggest to use a loop. What parameter should I use to run the loop? I should get a list of open positions in Symbol() first, and then loop over all of these?

I am new to programming an EA and am not yet familiar with the best practices. Help is appreciated.

there is several way to solve this problem , mine might differ from other . 

Here my code ;

//this function is only check amount lot for EURUSD only 	
int magicNumberEURUSD=12345; // insert magicNumberEURUSD in ordersend parameter for EURUSD
double lotEURUSD() 
  {
   double lot=0;
   int i=OrdersHistoryTotal();
   while(i>=0)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         if(OrderMagicNumber()==magicNumberEURUSD)
            if(OrderSymbol()=="EURUSD") // if want to check other pair , change "EURUSD" to other pair in string format
              {
               lot+= OrderLots(); // return lot values 
              }
      i--;
     }
   return lot;
  }

 
WindmillMQL :
Thank you for your reply. I did not know that there are two sorts of accounts. I will do a search and hope to find a description of these two and how to handle this.

Example loop to traverse all positions in the code Example: Calculate Positions and Pending Orders:

Cycle by the current symbol (the current symbol is the symbol on which the EA works -> m_symbol ) and by the specified Magic number ( InpMagic )

//+------------------------------------------------------------------+
//| Calculate all positions Buy and Sell                             |
//+------------------------------------------------------------------+
void CalculateAllPositions(int &count_buys,int &count_sells)
  {
   count_buys=0;
   count_sells=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)
        {
         if(m_position.PositionType()==POSITION_TYPE_BUY)
            count_buys++;

         if(m_position.PositionType()==POSITION_TYPE_SELL)
            count_sells++;
        }
//---
   return;
  }
How to start with MQL5
How to start with MQL5
  • 2020.03.12
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
Cosmas Moses:

there is several way to solve this problem , mine might differ from other . 

Here my code ;

Your code is for MQL4 so not applicable here.
Not only that it sums the lot sizes for closed trades, not open positions as the OP asked for.

 
Keith Watford:

Your code is for MQL4 so not applicable here.
Not only that it sums the lot sizes for closed trades, not open positions as the OP asked for.

dear Keith Wafrod ,

thank you for  review my code  and by saying Your code is for MQL4 so not applicable here , seem iam not welcome to help other here . If that is ur attention just let me know  then . I hope you have a nice day . 

Yes , iam admit my code are checking total lot for closed order , if user want to check total lot open order simply change 

 OrdersHistoryTotal() to OrdersTotal() and change MODE_HISTORY to  MODE_TRADES

regard

Cosmas 

 
Cosmas Moses:

dear Keith Wafrod ,

thank you for  review my code  and by saying Your code is for MQL4 so not applicable here , seem iam not welcome to help other here . If that is ur attention just let me know  then . I hope you have a nice day . 

Yes , iam admit my code are checking total lot for closed order , if user want to check total lot open order simply change 

 OrdersHistoryTotal() to OrdersTotal() and change MODE_HISTORY to  MODE_TRADES

regard

Cosmas 

You are welcome to help others, but please only do so when your help is relevant to the question.

This topic is about MQL5 and calculating the total position.

 
Keith Watford:

You are welcome to help others, but please only do so when your help is relevant to the question.

This topic is about MQL5 and calculating the total position.

my apologies , i should check the url first , whereby this forum is attended for mql5 and not mql4. No wonder my code are not applicable here . 

regards

Cosmas .