Overtrading is one of the quieter ways an Expert Advisor damages an account. A signal fires, the trade loses, the condition is still true, and the EA enters again. And again. A simple daily cap on new trades stops that loop.

The common mistake is keeping the count in a global variable. That works until the terminal restarts, the EA is reattached, or the VPS reboots. Then the counter is back at zero halfway through the day and the cap is gone.

The fix is to count from the account's deal history, which survives restarts.

Find the start of the trading day

Use server time, not your local clock, because the broker's day is what matters.

input long InpMagic = 20261130; input int InpMaxTradesPerDay = 3; datetime DayStart() { MqlDateTime t; TimeToStruct(TimeTradeServer(), t); t.hour = 0; t.min = 0; t.sec = 0; return StructToTime(t); } 

Count today's entry deals for this EA

Only deals with DEAL_ENTRY_IN are new positions. Exits, partial closes and stop outs are DEAL_ENTRY_OUT and should not count against the cap.

int TradesOpenedToday() { // If history can't be loaded, fail safe and treat the cap as reached if(!HistorySelect(DayStart(), TimeTradeServer() + 60)) return InpMaxTradesPerDay; int count = 0; int total = HistoryDealsTotal(); for(int i = 0; i < total; i++) { ulong ticket = HistoryDealGetTicket(i); if(ticket == 0) continue; if(HistoryDealGetInteger(ticket, DEAL_MAGIC) != InpMagic) continue; if(HistoryDealGetString(ticket, DEAL_SYMBOL) != _Symbol) continue; if(HistoryDealGetInteger(ticket, DEAL_ENTRY) == DEAL_ENTRY_IN) count++; } return count; } bool DailyCapReached() { return TradesOpenedToday() >= InpMaxTradesPerDay; } 

Use it as a gate before any entry

void OnTick() { if(DailyCapReached()) return; // entry logic goes here } 

Details that matter

  1. Pending orders that have not filled yet are not deals. If your EA places limit or stop orders, also count open pending orders with your magic number, or the cap can be exceeded when several fill at once.
  2. Calling HistorySelect on every tick is fine for a small day window, but if your EA trades a lot you can cache the count and refresh it in OnTradeTransaction when a new deal arrives.
  3. The 60 second padding on the end time covers small differences between TimeTradeServer and the deal timestamps.
  4. If you trade several symbols from one EA instance, drop the symbol filter and count by magic number only.

A daily cap is not a strategy. It is a seatbelt. It costs nothing on good days and it limits the damage on the days when the market does something your logic never expected.