How to check open orders and see if the balance of that order is positive or negative.

 

Hi, I have a formula that calculates the ATR of a certain period and if ATR is too high i would like to check if my open orders are in profit or not. If they are in profit I will do nothing if they are negative I want to close my open order for that pair ( I trade forex). I already have the ATR and close orders function.


Thanks

 
Lucas Santana:

Hi, I have a formula that calculates the ATR of a certain period and if ATR is too high i would like to check if my open orders are in profit or not. If they are in profit I will do nothing if they are negative I want to close my open order for that pair ( I trade forex). I already have the ATR and close orders function.


Thanks

Lucas,


here is a function I use, you can tweak it as per your needs.


double TotalBuyProfit()
  {
   int total=0;
   double orderProfit =0;
   double orderGross = 0;
   
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderMagicNumber()==magic)
            if(OrderType()==OP_BUY)
            orderProfit = orderProfit +OrderProfit();
            total++;
        }
      else Print(__FUNCTION__,"Failed to select order ",i," ",GetLastError());
     }
   return (orderProfit);
  }

 
Lucas Santana:

How to check open orders

There are no open orders in MT5. Orders become positions.

Larisa Mathur: here is a function I use, you can tweak it as per your needs.

Why are you posting MT4 code in the Root / MT5 General section

 
William Roeder:

There are no open orders in MT5. Orders become positions.

Why are you posting MT4 code in the Root / MT5 General section

Sorry, I didnnt notice

 
Larisa Mathur:

Lucas,


here is a function I use, you can tweak it as per your needs.


double TotalBuyProfit()
  {
   int total=0;
   double orderProfit =0;
   double orderGross = 0;
   
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderMagicNumber()==magic)
            if(OrderType()==OP_BUY)
            orderProfit = orderProfit +OrderProfit();
            total++;
        }
      else Print(__FUNCTION__,"Failed to select order ",i," ",GetLastError());
     }
   return (orderProfit);
  }

Thanks a lot, I will try