MQL4 - OrdersHistoryTotal() - one question

 

I have to know, how many closed orders was opened in current day.

If there was more than 9 orders in current day, then Expert Advisor should not open next order.

I would like max 10 orders daily.

How to use OrdersHistoryTotal() function in this case ?

 

I would tackle it something like this

static int startcount;
   
   if (New_Day)
      {
      startcount=OrdersHistoryTotal();
      }
   int currentcount=OrdersHistoryTotal();
   
   if (currentcount-startcount>9)
      {
      //STOP TRADING
      } 

I'm presuming you have a definition for your current day.


I suppose you could have 8 closed orders in orders history and two currently open orders so to stop at 10 orders total for the day you could do

   if (currentcount+OrdersTotal()-startcount>9)
      {
      //STOP TRADING
      } 


Hope that helps

V

 
Viffer:
static int startcount;
   
   if (New_Day)
      {
      startcount=OrdersHistoryTotal();
      }
   int currentcount=OrdersHistoryTotal();
   
   if (currentcount-startcount>9)
      {
      //STOP TRADING
      } 

OrdersHistoryTotal()'s name is a bit misleading, cause it also includes balance and credit statements. So this method might be potentially unreliable... A better solution would be to loop on orders in history and count (filtering out balance and credit statements).


FYI, although not officially documented, these are the OrderType() of balance/credit statements when they are selected:

  1. 'Balance' statement: OrderType()==6
  2. 'Credit' statement: OrderType()==7
 

To add to Gordon's statement that "OrdersHistoryTotal()'s name is a bit misleading" is the fact that the only order history available to the EA with the OrdersHistoryTotal() expression is the trades that have been loaded via the "Account History" tab.

The account could be 10yrs old with 5000 trades but if the terminal window is set to display only the last month's worth of trades then that is the EA will get when executing OrdersHistoryTotal(). The name is misleading indeed.

 

Aiding my own learning... is this better...

   static int startcount;
   int currentcount;
   
   if (New_Day)
      {
      startcount=OrdersHistoryTotal();
      }
   
   if (startcount<OrdersHistoryTotal())
      {
      for(int i = startcount ; i < OrdersHistoryTotal(); i++)
         {
         OrderSelect(n, SELECT_BY_POS, MODE_HISTORY);
         if(OrderSymbol()!=Symbol()||OrderType()>5 || OrderMagicNumber()!=magic)
            {
            continue;
            }  
         currentcount++;
         }
   
   if (currentcount>9)
      {
      //STOP TRADING
      } 


A couple of questions on this though... As you can see, I've added selection by magic in case that's useful and I've filtered by Ordertype. Does order history also hold deleted pending orders? if so, I guess it's fairly straight forward now to filter those out too.. I am presumming that once a statement is added in the history, it isn't removed... ie orderhistorytotal would stand still. Is this true?


But of more concern is as Phillip says it only counts what's loaded in tha account tab.. Does the account tab show a rolling list? ie the next day it would show only the last month or would it show last month +1day. If it's the former, then I'm not sure how well the code above would work at all. You would have to synchronise New_Day with the day according to account tab.

As ever, comments appreciated

V

EDIT- Fixed an error on the last if statement. Shouldn't have compared currentcount to startcount

 
but I would like to know, how many orders was traded on Wednesday & how many on Thursday in currnt week using builded in MT4 function.
 
puncher:
but I would like to know, how many orders was traded on Wednesday & how many on Thursday in currnt week using builded in MT4 function.

then you must calculate the timestemp from the days and look wich orderopentime() timestemp the orders have, but dont ask me for code, i am very busy soory, just wanted to let you know a solution ;-)


here a exampel how you can find wich day is:


switch(TimeDayOfWeek(TimeCurrent()))
{
case 0 : day="Sunday"; break;
case 1 : day="Monday"; break;
case 2 : day="Tuesday"; break;
case 3 : day="Wednesday"; break;
case 4 : day="Thursday"; break;
case 5 : day="Friday"; break;
case 6 : day="Sunday"; break;

}


if you look to this:


TimeDayOfWeek(Orderopentime())


then you can find out on wich day the order was open.

Reason: