Net Position value function

 

Hi all,

I am a new code learner and after finding almost everything I needed on MQL5 forums and help files, I cannot get out of a simple calculation.

I open different volume buy/sell positions with the same Magic Number, and I want to calculate my net opened positions. My code is as follows:

Please anyone help me with what is wrong with this? THANK YOU.

double MyNetPosition(int Magic)
  {
   double netpos=0;
   double buylots=0;
   double selllots=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==_Symbol && OrderMagicNumber()==Magic && OrderType()==OP_BUY) buylots = OrderLots();
      if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==_Symbol && OrderMagicNumber()==Magic && OrderType()==OP_SELL) selllots = OrderLots();
      }
  if (buylots>selllots) netpos = buylots-selllots;
  if (selllots>buylots) netpos = selllots-buylots;
      
  return netpos;
  }
 
PainKillerRO:

Hi all,

I am a new code learner and after finding almost everything I needed on MQL5 forums and help files, I cannot get out of a simple calculation.

I open different volume buy/sell positions with the same Magic Number, and I want to calculate my net opened positions. My code is as follows:

Please anyone help me with what is wrong with this? THANK YOU.

Here is how I calculate the position average price

double PosicionPrecioMedio()
{
 	double SumPrice = 0;
        double Sumlots = 0;
        
        for( int i = 0; i < OrdersTotal(); i++ )

                if( OrderSelect( i, SELECT_BY_POS ) && OrderSymbol() == _Symbol && OrderType() < 2 ) // 
                {
              		SumPrice += OrderOpenPrice() * OrderLots();  // suma pondera de precios y lotes
              		Sumlots  += OrderLots();                     // suma de lotes        
                }
                
        if( Sumlots == 0 ) return(0);
        
        return ( NormalizeDouble( SumPrice / Sumlots, Digits) );
}
 
PainKillerRO:

Hi all,

I am a new code learner and after finding almost everything I needed on MQL5 forums and help files, I cannot get out of a simple calculation.

I open different volume buy/sell positions with the same Magic Number, and I want to calculate my net opened positions. My code is as follows:

Please anyone help me with what is wrong with this? THANK YOU.


#define ALL_SYMBOLS  "ALL_SYMBOLS"
#define ALL_MAGICS   INT_MAX

void OnStart() {
   double p = net_position();
   Print(p);
}

double net_position(int magic=ALL_MAGICS) {
   double position = 0.0;
   for (int i=OrdersTotal()-1; i>=0; --i)
      if (position_select(i, _Symbol, magic)
         position += (OrderType() == OP_BUY ? OrderLots() : -OrderLots());
   return position;
}

bool position_select(const int index, string symbol=ALL_SYMBOLS, const int magic=ALL_MAGICS) {
   if (symbol == NULL)
      symbol = _Symbol;
   return (
      OrderSelect(index, SELECT_BY_POS)
      && OrderType() < 2
      && (symbol == ALL_SYMBOLS || OrderSymbol() == symbol)
      && (magic == ALL_MAGICS || magic == OrderMagicNumber())
   );
}
 
nicholi shen:

At a first sight (of a noob (that being ME)) seems a working solution, but I read it as an indicator.

I wil try to figure out how to insert insert this code in my EA and I will let you know.

Thanks for the help, really appreciate it!

 
Fernando Morales:

Here is how I calculate the position average price

Appreciate it Fernando, but your code calculates an average price.

What I need is to calculate the difference between opened BUY and SELL lots for hedging positions on the same symbol and magic number.

To be more specific, let's say I have this situation:

- 3 opened BUY trades for a total of 0.30 lots (whatever the price is)

- 4 opened SELL trades for a total of 0.45 lots

I want my function to return me the difference between the BUY and SELL orders. That would be -0.15 (meaning 0.15 net position in SELL lots).


nicoli shen here gave me a good tip and I will try it. I will let you know. Thanks for the help.