Download MetaTrader 5

Count Daily Opened Orders

To add comments, please log in or register


I want to only open 2 orders every day. But i don't know how to count only the current day's orders.

to be more clear, I should count today's opened orders before opening a new position. if they were 2 then stop opening a new order for today.

Please help me in this piece of code.



You will need to use a counter and loop through all the orders and test OrderOpenTime(). You may also choose to filter by OrderSymbol() and/or OrderMagicNumber().

There are plenty of code snippets about how to count orders so you should be able to add the OrderOpenTime() test in fairly easily.

OrderOpenTime - Trade Functions - MQL4 Reference
OrderOpenTime - Trade Functions - MQL4 Reference
OrderOpenTime - Trade Functions - MQL4 Reference
  1. Just remember the count.
    #define SECONDS uint
    #define HR2400 86400
    SECONDS    time(datetime when=0){
          return SECONDS(when == 0 ? TimeCurrent() : when) % HR2400;               }
    datetime   date(datetime when=0){
          return datetime( (when == 0 ? TimeCurrent() : when) - time(when) );      }
    static int nOrdersToday = 0;  static datetime today = 0;
    datetype t = today;  today = date();   bool isNewDay = t != today;
    if(isNewDay)   nOrdersToday = 0;
    if(nOrdersToday < 2){
       int ticket = OrderSend( ... );
       if(ticket < 0) Alert( ... );
       else           ++nOrdersToday;
    // else max trades today
  2. EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.
    If you want to be able to recover you will have to reset nOrdersToday in OnInit, by doing a OrderSelect loop through open orders like honest_knave said and through history in case the order has already closed. See external static variable - MQL4 forum

To add comments, please log in or register