A function to count historical trades :

Code:
int counthistbar(int ordertype, datetime time){
   int ordercount=0;
   for(int j=OrdersHistoryTotal()-1;j>=0;j--){
      if(!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY))continue;
      if(OrderType()==ordertype)
         if(OrderMagicNumber()==Magic)
            if(OrderSymbol()==Symbol())
               if(OrderOpenTime()>=time)ordercount++;
   }
   return(ordercount);
}