ChartTimePriceToXY() Getting error 4107 in EA when changing timeframes. Loading, loaded recompile works ok.

 

Gurus any ideas?

If EA is loaded or getting recompiled, no error. If changing tf while EA is loaded, error comes.

ChartTimePriceToXY() failed Error 4107


//+------------------------------------------------------------------+
int x=0, y=0;

int OnInit()
  {
   ResetLastError(); 
   if(!ChartTimePriceToXY(0, 0, TimeCurrent(), SymbolInfoDouble(_Symbol, SYMBOL_BID),x ,y)) 
     { 
      Print("ChartTimePriceToXY() failedfailed. Error ", GetLastError()); 
      return(false); 
     } 
   Print(x, " : ", y);
   return(INIT_SUCCEEDED);
  }



void OnDeinit(const int reason)
  {
//---
   
  }
 

Error 4107 is ERR_CHART_NOT_FOUND.

When you change timeframe, MT briefly rebuilds the chart and for a short moment the chart context/data is not ready.

During that transition ChartTimePriceToXY() can fail, so you see it only when switching TF while the EA is already attached.

Fix: don’t call it just once in OnInit() . Add a small retry loop with Sleep() and only keep retrying for 4107. Any other error should be treated as a real failure.

//+------------------------------------------------------------------+ int x=0, y=0; int OnInit() { int attempts = 0; while(attempts < 10) { Sleep(100); // wait for chart to finish TF rebuild ResetLastError(); if(ChartTimePriceToXY(0, 0, TimeCurrent(), SymbolInfoDouble(_Symbol, SYMBOL_BID), x, y)) { Print(x, " : ", y); return(INIT_SUCCEEDED); } int err = GetLastError(); if(err != 4107) // not "chart not found" -> real error { Print("ChartTimePriceToXY() failed. Error ", err); return(INIT_FAILED); } attempts++; } Print("ChartTimePriceToXY() failed after multiple attempts. Error 4107"); return(INIT_FAILED); } void OnDeinit(const int reason) {}

This gives the chart time to initialize after the TF switch, preventing the transient 4107 from killing your init.

 
Andreas Smigadis #:

Error 4107 is ERR_CHART_NOT_FOUND.

When you change timeframe, MT briefly rebuilds the chart and for a short moment the chart context/data is not ready.

During that transition ChartTimePriceToXY() can fail, so you see it only when switching TF while the EA is already attached.

Fix: don’t call it just once in OnInit() . Add a small retry loop with Sleep() and only keep retrying for 4107. Any other error should be treated as a real failure.

//+------------------------------------------------------------------+ int x=0, y=0; int OnInit() { int attempts = 0; while(attempts < 10) { Sleep(100); // wait for chart to finish TF rebuild ResetLastError(); if(ChartTimePriceToXY(0, 0, TimeCurrent(), SymbolInfoDouble(_Symbol, SYMBOL_BID), x, y)) { Print(x, " : ", y); return(INIT_SUCCEEDED); } int err = GetLastError(); if(err != 4107) // not "chart not found" -> real error { Print("ChartTimePriceToXY() failed. Error ", err); return(INIT_FAILED); } attempts++; } Print("ChartTimePriceToXY() failed after multiple attempts. Error 4107"); return(INIT_FAILED); } void OnDeinit(const int reason) {}

This gives the chart time to initialize after the TF switch, preventing the transient 4107 from killing your init.

you are right. thanks for the input. 

also found below function very useful.

SeriesInfoInteger(Symbol(),Period(),SERIES_SYNCHRONIZED));