Limit trades in a certain time frame

 

I want to limit trades within a day to x number

is this the best way to do? by checking the history?

//+------------------------------------------------------------------+
//| Trade Count                                                      |
//+------------------------------------------------------------------+
int TradeCount(ENUM_TIMEFRAMES TimeFrame)
  {
//---

   int      Cnt;
   ulong    Ticket;
   long     EntryType;
   datetime DT[1];

   Cnt = 0;

   if(CopyTime(_Symbol, TimeFrame, 0, 1, DT) <= 0)
   {
      Cnt = -1;
   }
   else
   {
      HistorySelect(DT[0], TimeCurrent());

      for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
      {
         Ticket    = HistoryDealGetTicket(i);
         EntryType = HistoryDealGetInteger(Ticket, DEAL_ENTRY);

         if(EntryType == DEAL_ENTRY_IN)
         {
            if(_Symbol == HistoryDealGetString(Ticket, DEAL_SYMBOL))
            {
               Cnt++;
            }
         }
      }
   }

//---
   return(Cnt);
  }
 
Dua Yong Rew:

I want to limit trades within a day to x number

is this the best way to do? by checking the history?

It's the more reliable way. Any problem with this code ?
 
Alain Verleyen:
It's the more reliable way. Any problem with this code ?
no problem but its kinda slow. so searching for more opinions :)
 
Dua Yong Rew:
no problem but its kinda slow. so searching for more opinions :)

Don't execute such function on each tick, but only when needed.

Use a global variable for your counter, initialize it with your function, then update your variable each time you place a trade with your EA.

By the way, you are counting deals, it can be different from the number of trades depending of what you call a trade.

 
Alain Verleyen:

Don't execute such function on each tick, but only when needed.

Use a global variable for your counter, initialize it with your function, then update your variable each time you place a trade with your EA.

By the way, you are counting deals, it can be different from the number of trades depending of what you call a trade.

that's an interesting idea, so I just need to reset the counter at the start of the day.

How can I restart at start of the day? 

 
Dua Yong Rew:

that's an interesting idea, so I just need to reset the counter at the start of the day.

How can I restart at start of the day? 

By doing some efforts
 
Alain Verleyen:
By doing some efforts
Im thinking of newbar. any ideas?
Reason: