count total closed orders in history with magicnumber

 

Hello,

currently im stuck at my EA code. Almost everything works fine, but i also want it to display how many trades are already closed.

Right now is use the following piece of code. But the "Closed orders: " always shows 0, while i have enough closed trades with the same MagicNumber. What am i doing wrong?

int total = OrdersTotal();
   for (int cnt = 0 ; cnt < total ; cnt++)
   {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_SELL&& OrderMagicNumber()==MagicNumber) 
      {
         ObjectSetText("order","SELL order active", 13, "Arial Bold", Red);
         Profit=OrderProfit();
         if (Profit>0) ObjectSetText("pl","Order P/L= " + (DoubleToStr(Profit,2)), 20, "Arial Bold", Green);
         if (Profit<0) ObjectSetText("pl","Order P/L= " + (DoubleToStr(Profit,2)), 20, "Arial Bold", Red);
      }
      if(OrderType()==OP_BUY&& OrderMagicNumber()==MagicNumber) 
      {
         ObjectSetText("order","BUY order active", 13, "Arial Bold", Green);
         Profit=OrderProfit();
         if (Profit>0) ObjectSetText("pl","Order P/L= " + (DoubleToStr(Profit,2)), 20, "Arial Bold", Green);
         if (Profit<0) ObjectSetText("pl","Order P/L= " + (DoubleToStr(Profit,2)), 20, "Arial Bold", Red);
      }
   OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber) closed++;
   ObjectSetText("history","Closed orders: " + closed, 13, "Arial Bold", Silver);
   }
 

Hi,

You need to use OrdersHistoryTotal() in a separate loop to count the closed orders.

 

OK, thanks for pointing to it. i changed to this:

   // retrieving info from trade history
  int hstTotal = OrdersHistoryTotal(); 
  for (int i = 0 ; i < hstTotal ; i++)
  {
      //---- check selection result
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
      {
        Print("Access to history failed with error (",GetLastError(),")");
        break;
      }
   OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber) closed++;
   ObjectSetText("history","Closed orders: " + closed, 13, "Arial Bold", Silver);
   }

but then it just keeps on counting by every tick :) so i suppose i should change the close++ command to something else, but to what?

i also tried with the following code, but also keeps counting:

   for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
   {
      OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
            
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
      {
         if(OrderType()==OP_BUY || OP_SELL) closed++; 
         ObjectSetText("history","Closed orders: " + closed, 13, "Arial Bold", Silver);
      }
   }
 
static int closedOrders=0;

if(OrderHistoryTotal()!=closedOrders){

closedOrders=OrdersHistoryTotal();

//do your counting...

}
//z
 
zzuegg:
//z




thanks! i didnt understand correctly at first, but this one seems to work. appreciate it!

 

My full EA code is posted below. somehow it opened a buy order yesterday, but it got closed by stoploss today. right now, on eurusd the bar from yesterday (tuesday) opened above ema50 and closed below ema50, so it should take a short now, but it does not. could anyone see what part of my code i'm doing wrong?

Files:
Reason: