Skip to content

Runtime error

4108 ERR_CHART_CANNOT_CREATE_TIMER

A timer that could not be created (MQL5), an invalid ticket (MQL4)

What it means

In MQL5, 4108 is a chart-group error: the program could not create its timer (EventSetTimer() returned false). Many people who search for 4108 are porting MQL4 code, where 4108 is ERR_INVALID_TICKET ("Invalid ticket"): the order ticket passed to OrderSelect(), OrderClose() or OrderModify() did not exist. In MQL5 that case shows up as 4753 or 4754.

Common causes

  • MQL5: EventSetTimer() failing; check its return value and GetLastError() (the reference does not list specific causes; one timer per program is allowed).
  • MQL4 code: a ticket kept in a variable after the order was closed by its stop loss or take profit.
  • MQL4 to MQL5 ports: keeping the deal ticket and using it as a position ticket. Orders, deals and positions have separate tickets in MQL5.

A real scenario

An MQL4 EA stored the ticket returned by OrderSend() and closed the trade with it later; when the stop loss had already closed it, MQL4 reported 4108. In the MQL5 port the same logic used trade.ResultDeal() as the position ticket, which fails on many accounts. Keeping the position identifier from the deal and finding the position by it works on netting and hedging accounts.

How to fix it

  1. MQL5 timer: check the return of EventSetTimer() in OnInit(), fail the start if the EA depends on OnTimer(), and call EventKillTimer() in OnDeinit().
  2. MQL4 meaning: before acting on a stored ticket, check that the position still exists; it may have been closed by the server.
  3. In MQL5, store the position identifier (DEAL_POSITION_ID of the opening deal) and select the position by it.

Fixed MQL5 code

MQL5
#include <Trade/Trade.mqh>
CTrade trade;


// MQL5 meaning of 4108: the timer could not be created.
int OnInit()
{
   ResetLastError();
   if(!EventSetTimer(60))
   {
      PrintFormat("EventSetTimer failed, error %d", GetLastError());
      return INIT_FAILED;                 // an EA that depends on OnTimer() should not run without it
   }
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
{
   EventKillTimer();
}

// MQL4 meaning (invalid ticket): keep the position identifier, not the deal ticket.
long OpenBuyAndGetPositionId(const double lots)
{
   if(!trade.Buy(lots, _Symbol) || trade.ResultRetcode() != TRADE_RETCODE_DONE)
      return -1;
   ulong deal = trade.ResultDeal();
   if(deal == 0 || !HistoryDealSelect(deal))
      return -1;                          // not in the history yet: look it up again on the next tick
   return HistoryDealGetInteger(deal, DEAL_POSITION_ID);
}

bool SelectPositionById(const long positionId)
{
   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      ulong ticket = PositionGetTicket(i);   // also selects the position
      if(ticket > 0 && PositionGetInteger(POSITION_IDENTIFIER) == positionId)
         return true;
   }
   return false;                             // closed already (SL, TP, stop-out or by hand)
}

A focused example for MetaTrader 5: adapt names, magic number and limits to your EA, and test it on a demo account first.

EA Doctor rules that catch it

  • HighMQL4 functions in an MQL5 fileThe file uses MQL4's order functions and variables (OP_BUY, OrderSelect by position, Bid, Ask, Close[]), which MQL5 does not have.

Sources

Get your EA fixed

Still failing with 4108? Send us the EA and the journal lines: we find the cause in the code and fix it. Or check it yourself first with the EA Doctor.