Download MetaTrader 5

Order Count Issue

To add comments, please log in or register
Fabio Geraci
370
Fabio Geraci  

 The following function works fine if I only get signalBuy or signalSell, if I have position opened in both buy and sell the (the Ea will allow multiple buy and one sell, and viceversa) the order count and order lot go wrong as shown below the code:

 

void GetOrders(int anActionCode1, int anActionCode2, int &aNumberLots, double &aLots, double &aProfit, double &aWeightedPips, int &aNumBuy, int &aNumSell)
{
   aProfit = 0;
   aWeightedPips = 0;
   aLots = 0;
   aNumberLots = 0;   
   aNumBuy = 0;     
   aNumSell = 0;  
   double aLotsBuy = 0;
   double aLotsSell = 0;
   
   for (int i = 0 ; i < OrdersTotal(); i++)
   {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         continue;
         
      if (Symbol() != OrderSymbol() || MagicNumber != OrderMagicNumber() || (OrderType() != anActionCode1 && OrderType() != anActionCode2))
         continue;

      aProfit += OrderProfit()+OrderSwap() + OrderCommission();
      aNumberLots++;
      double pipsBuy = 0;
      double pipsSell = 0;
      
      if (OrderType() == OP_BUY)
      {
         aLotsBuy += OrderLots(); 
         pipsBuy = Bid - OrderOpenPrice();
         aNumBuy++;
      }
      else if (OrderType() == OP_SELL)
      {
         aLotsSell += OrderLots();
         pipsSell = OrderOpenPrice() - Ask;
         aNumSell++;
      }
      
      aWeightedPips += ((pipsBuy * aLotsBuy) + (pipsSell * aLotsSell));

   }
   Print("Profit = " + DoubleToStr(aProfit,2) + 
            //", aLots = " + DoubleToStr(aLots,2) + 
            ", pipsSell = " + DoubleToStr(pipsSell,6) +
            ", pipsBuy = " + DoubleToStr(pipsBuy,6) +
            ", LotsBuy = " + DoubleToStr(aLotsBuy,2) + 
            ", LotsSell = " + DoubleToStr(aLotsSell,2) + 
            ", aWeightedPips = " + DoubleToStr(aWeightedPips,7) +
            ", aNumberLots = " + IntegerToString(aNumberLots) +
            ", aNumBuy = " + IntegerToString(aNumBuy) +
            ", aNumSell = " + IntegerToString(aNumSell));
   if (aNumberLots > 0)
   {  
      aLots = MathAbs(aLotsBuy - aLotsSell);

      aWeightedPips /= aLots;
      aWeightedPips = PriceToPip(aWeightedPips);
   }
   Print(", aLots = " + DoubleToStr(aLots,2));
   
}
EURUSD,M1: Profit = -41.95, LotsBuy = 0.58, LotsSell = 0.08, aWeightedPips = -0.0006448, aNumberLots = 9, aNumBuy = 1, aNumSell = 8
EURUSD,M1: Profit = -41.95, LotsBuy = 0.58, LotsSell = 0.08, aWeightedPips = -0.0006448, aNumberLots = 9, aNumBuy = 1, aNumSell = 8
EURUSD,M1: Profit = -7.81, LotsBuy = 0.00, LotsSell = 0.08, aWeightedPips = -0.0003062, aNumberLots = 8, aNumBuy = 0, aNumSell = 8
EURUSD,M1: Profit = -7.81, LotsBuy = 0.00, LotsSell = 0.08, aWeightedPips = -0.0003062, aNumberLots = 8, aNumBuy = 0, aNumSell = 8
EURUSD,M1: Profit = -7.81, LotsBuy = 0.00, LotsSell = 0.08, aWeightedPips = -0.0003062, aNumberLots = 8, aNumBuy = 0, aNumSell = 8
EURUSD,M1: Profit = -41.45, LotsBuy = 0.58, LotsSell = 0.08, aWeightedPips = -0.0006426, aNumberLots = 9, aNumBuy = 1, aNumSell = 8
EURUSD,M1: Profit = -41.45, LotsBuy = 0.58, LotsSell = 0.08, aWeightedPips = -0.0006426, aNumberLots = 9, aNumBuy = 1, aNumSell = 8
EURUSD,M1: Profit = -7.81, LotsBuy = 0.00, LotsSell = 0.08, aWeightedPips = -0.0003062, aNumberLots = 8, aNumBuy = 0, aNumSell = 8
EURUSD,M1: Profit = -41.45, LotsBuy = 0.58, LotsSell = 0.08, aWeightedPips = -0.0006426, aNumberLots = 9, aNumBuy = 1, aNumSell = 8
EURUSD,M1: Profit = -7.73, LotsBuy = 0.00, LotsSell = 0.08, aWeightedPips = -0.0003026, aNumberLots = 8, aNumBuy = 0, aNumSell = 8
EURUSD,M1: Profit = -7.73, LotsBuy = 0.00, LotsSell = 0.08, aWeightedPips = -0.0003026, aNumberLots = 8, aNumBuy = 0, aNumSell = 8
EURUSD,M1: Profit = -7.73, LotsBuy = 0.00, LotsSell = 0.08, aWeightedPips = -0.0003026, aNumberLots = 8, aNumBuy = 0, aNumSell = 8
EURUSD,M1: Profit = -41.95, LotsBuy = 0.58, LotsSell = 0.08, aWeightedPips = -0.0006448, aNumberLots = 9, aNumBuy = 1, aNumSell = 8
EURUSD,M1: Profit = -41.95, LotsBuy = 0.58, LotsSell = 0.08, aWeightedPips = -0.0006448, aNumberLots = 9, aNumBuy = 1, aNumSell = 8

Jose Francisco Casado Fernandez
4658
Jose Francisco Casado Fernandez  

When you call GetOrders function, first parameter passed (int anActionCode1)  must be OP_BUY, AND second parameter (int anActionCode2) must be OP_SELL. Otherwise you would have to remove yellow highlighted part of the code:

if (Symbol() != OrderSymbol() || MagicNumber != OrderMagicNumber() || (OrderType() != anActionCode1 && OrderType() != anActionCode2))
         continue;

Regards.

Fabio Geraci
370
Fabio Geraci  
Jose Francisco Casado Fernandez:

When you call GetOrders function, first parameter passed (int anActionCode1)  must be OP_BUY, AND second parameter (int anActionCode2) must be OP_SELL. Otherwise you would have to remove yellow highlighted part of the code:

Regards.

Thank you I will try that later on
To add comments, please log in or register