critical runtime error 565 in OnInit function

 

hi guys,

in this last few days, (i'm not quite sure when the exact date is), after updating mt5 i stuck with this "critical runtime error 565 in Oninit function" during optimization. the problem is just pop up suddenly out of nowhere, on the EA project i am currently working, and it doesn't shown such error on the optimization i did on the previous day.

i still couldn't understand where this error coming from. i even try to create some simple EA and custom indicator in hope of finding where i did wrong.

and it still shown such error.

this EA only read the change of color of the MA line from custom indicator,  only 2 item was in the optimization calculation (working time frame, and period).

the custom indicator as follow (MA line using 2 pole butterworth filter)

//+------------------------------------------------------------------+
//|                                         the custom indicator.mq5 |
//+------------------------------------------------------------------+
#property copyright "adicahyanto"
#property link      "//---------------//"
#property description "2MA Angle Indicator"
//
//
//
//
//

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1

#property indicator_label1  "MA Lines"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrDarkGray,clrAquamarine,clrDarkViolet
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
//
//
//
//
//
//  
//
input double               inpFltAdiv       = 16;             // MA Line Prd
//
//
//
//

double   mln[],mlc[];                        //MA Lines

double   acf1,acf2,acf3;
int      totalBars;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//

int OnInit()
{
   
   SetIndexBuffer(0, mln,  INDICATOR_DATA);
   SetIndexBuffer(1, mlc, INDICATOR_COLOR_INDEX);

   
   
    IndicatorSetString(INDICATOR_SHORTNAME,"Y_SVA (Period :"+(string)inpFltAdiv+")");    
    
      t2pbtf(inpFltAdiv,acf1,acf2,acf3);
     
   return(0);
}

//+------------------------------------------------------------------+

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

//
//

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])                
{                
   //
   //
   //
   //
   //

      totalBars = rates_total;
      for (int i=(int)MathMax(prev_calculated-2,2); i<rates_total; i++)
      {
           //MA Lines
           mln[i]    = (i>3) ? acf1*(close[i]+2*close[i-1]+close[i-2])+acf2*mln[i-1]+acf3*mln[i-2] : close[i];
           mlc[i]    = (i>0) ?(mln[i]>mln[i-1]) ? 2 :(mln[i]<mln[i-1]) ? 1 : mlc[i-1]: 0;
           
      }  
      

        
   return(rates_total);
}

//

//+------------------------------------------------------------------+ 
//two pole butterworth filter
void  t2pbtf(double tperiod, double   &tR1,  double   &tR2,  double   &tR3){

   //double deg2Rad = 0.017453292519943295769;//1.0 / (45.0 / MathArctan(1.0));
   //double pi = 3.14159265358979323846264338327950288;//MathArctan(1.0)*4.0;
   double a1 = MathExp(-4.442882938158366247 / MathMax(tperiod,4));//MathExp(-MathSqrt(2.0) * pi / MathMax(iperiod,4));
   double b1 = 2 * a1 * MathCos(4.442882938158366247 / MathMax(tperiod,4));//2 * a1 * MathCos(deg2Rad * MathSqrt(2.0) * 180 / MathMax(iperiod,4));
   
   tR2 = b1;
   tR3 = -a1 * a1;
   tR1 = (1.0 - b1 + a1 * a1) / 4.0;
}
//+------------------------------------------------------------------+ 

   

and then the expert to read  the change of line color

//+------------------------------------------------------------------+
//|                                          IndicatorBufferRead.mq5 |
//|                                                      adicahyanto |
//|                                           adi.cahyanto@gmail.com |
//+------------------------------------------------------------------+
#property copyright "adicahyanto"
#property link      "//---------------//"
#property version   "1.00"
//
//
//
//include 
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\SymbolInfo.mqh>  
//#include <CTradeStatistics.mqh>
//#include <CHistoryPositionInfo.mqh>

//create instance of CTrade
CPositionInfo        m_position;                   // trade position object
CTrade               m_trade;                      // trading object
CSymbolInfo          m_symbol;                     // symbol info object
//CTradeStatistics     m_stat;                       // trade statistic
//CHistoryPositionInfo m_hist;                       // history info


input ENUM_TIMEFRAMES      inpWorkPeriod    = PERIOD_CURRENT;          // EA Working Timeframe 

input double               inpFltAdiv       = 16;             // MA Line Prd

//
int      handle_Ind1;

         MqlRates mRate[];
double   LastATRvalue;

bool  buySign,sellSign;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//--- create handle of the indicator 2maa2
   handle_Ind1 = iCustom(m_symbol.Name(),inpWorkPeriod,"Y_SVA_02",
                  inpFltAdiv
                        );
//--- if the handle is not created 
   if(handle_Ind1 == INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code 
      PrintFormat("Failed to create handle of the 2maa2 indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early 
      return(INIT_FAILED);
     }


   ChartIndicatorAdd(ChartID(),0,handle_Ind1);
   //twopbtf(inpSigSmth,mcf1,mcf2,mcf3);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
  
//+------------------------------------------------------------------+
//---
  
//
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
// calculate on new bar -----------------------------------------------------------
      if(!NewBar()) return;
      
      
//get 2MAA data

double   mln[],mlc[];                        //MA Lines

          
      if(           //Band Value for min max 
                       CopyBuffer(handle_Ind1,0,0,3,mln)<0
                    || CopyBuffer(handle_Ind1,1,0,3,mlc)<0
                    
         )
        {
         //--- if the copying fails, tell the error code 
         PrintFormat("Failed to copy data from 2maa2 indicator, error code %d",GetLastError());
        }

//----------------------------------------------------------------------------------------------------------
      buySign  =  mlc[1] == 2 && mlc[0] == 1;
      sellSign =  mlc[1] == 1 && mlc[0] == 2;
            
//----------------------------------------------------------------------------------------------------------
// execute order

      if(sellSign)
        {
            for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions
                {
                  if(m_position.SelectByIndex(i)) // Choose a position
                      {
                        if(m_position.Symbol()==_Symbol)
                            {
                              if(m_position.PositionType()==POSITION_TYPE_BUY) // If the position is BUY
                                  {
                                    m_trade.PositionClose(m_position.Ticket());
 
                                  }                                    
                            }
                      }
                }
                
            CopyRates(_Symbol,inpWorkPeriod,0,6,mRate);
            LastATRvalue      =  (MathMax(mRate[4].high,mRate[3].close) - MathMin(mRate[4].low,mRate[3].close))*0.4 +
                                 (MathMax(mRate[3].high,mRate[2].close) - MathMin(mRate[3].low,mRate[2].close))*0.3 +
                                 (MathMax(mRate[2].high,mRate[1].close) - MathMin(mRate[2].low,mRate[1].close))*0.2 +
                                 (MathMax(mRate[1].high,mRate[0].close) - MathMin(mRate[1].low,mRate[0].close))*0.1;
            
            double   sellPrice   =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
            double   sellSL      =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID) + 1.25*LastATRvalue,_Digits);
            
            m_trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,0.02,sellPrice,sellSL,0,NULL);
        }

      if(buySign)
        {
           for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions
                {
                  if(m_position.SelectByIndex(i)) // Choose a position
                      {
                        if(m_position.Symbol()==_Symbol)
                            {
                              if(m_position.PositionType()==POSITION_TYPE_SELL) // If the position is SELL
                                  {
                                    m_trade.PositionClose(m_position.Ticket());
                                    
                                  }                                    
                            }
                      }
                }
                
            CopyRates(_Symbol,inpWorkPeriod,0,6,mRate);
            LastATRvalue      =  (MathMax(mRate[4].high,mRate[3].close) - MathMin(mRate[4].low,mRate[3].close))*0.4 +
                                 (MathMax(mRate[3].high,mRate[2].close) - MathMin(mRate[3].low,mRate[2].close))*0.3 +
                                 (MathMax(mRate[2].high,mRate[1].close) - MathMin(mRate[2].low,mRate[1].close))*0.2 +
                                 (MathMax(mRate[1].high,mRate[0].close) - MathMin(mRate[1].low,mRate[0].close))*0.1;
            
            double   buyPrice    =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
            double   buySL       =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK) - 1.25*LastATRvalue,_Digits);
            
            m_trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.02,buyPrice,buySL,0,NULL);
        }
              
//----------------------------------------------------------------------------------------------------------        
        
      Comment("\nSignal Val \t: ",NormalizeDouble(mln[1],4)," : ", NormalizeDouble(mln[0],4),
              "\nBuy Sign \t : ", buySign,
              "\nSell Sign \t : ", sellSign
               );        
           
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
      return(false);
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
//---
   return(true);
  } 
  
//+------------------------------------------------------------------+
   bool NewBar(){
      static datetime prevTime      = 0;
      datetime        currentTime   = iTime(_Symbol,inpWorkPeriod,0);
      if(currentTime != prevTime){
         prevTime = currentTime;
         return(true);
      }
      return(false);
      
   }

//+------------------------------------------------------------------+

screenshot of the setting and journal of the test i've run are attached.

MT5 version: 5.00 build 3500 (11 Nov 2022)


could you maybe show me where did i do wrong.?

and thanks.

Files:
1.png  185 kb
2.png  179 kb
3.png  280 kb
 

Error 565 seems to be undocumented, and only one other forum thread mentions it (but without solution): open error [2] & error code 565 on EA optimization

What build of MetaTrader 5 are you using now after the update and what build did you have before?

open error [2] & error code 565 on EA optimization
open error [2] & error code 565 on EA optimization
  • 2020.12.19
  • www.mql5.com
Hello all I'm having a problem when running my optimization on MT5 V 5...
 
Yes, it's just something number.
Sorry, i normally just don't taking a note on what metatrader build number i currently at.
 
adicahyanto #:I'm currently using metatrader 5 build 3500. But i just don't remember the build number before the update.

Do you not keep backups? You should! Its a good practice to keep backups.

Thanks to Andrey Khatimlianskii, you can download previous builds here — https://drive.google.com/drive/folders/1YSUVehcElTpLxCLPMsdlDfFu4Y61yrcc
 
Thanks Fernando, I'll keep that in mind to keep track of my mt5 build. I'll try to downgrade my mt5 installation with previous version.
I'll post an update if that solve this problem.

Thanks
 

Sorry, I accidentally deleted this post - copied below

adicahyanto #:

I'm currently using metatrader 5 build 3500.
But i just don't remember the build number before the update.

I Also have some suspicion with my windows 10, as the update also happened at the same day with the metatrader.

I've read at the german forum section regarding to the possibility of windows problem related to this 565 error.


 
adicahyanto #: Thanks Fernando, I'll keep that in mind to keep track of my mt5 build. I'll try to downgrade my mt5 installation with previous version. I'll post an update if that solve this problem. Thanks
You are welcome!
 

an update to this problem so far: problem seems to be mitigated.

1. downgrading MT5 build version did no help.

2. the problem seem to be from the windows just as mentioned on discussion here: https://www.mql5.com/de/forum/367097

    mentioned on windows system error code documentation are as follow:   

         ERROR_TOO_MANY_THREADS

565 (0x235)

Indicates a process has too many threads to perform the requested action. For example, assignment of a primary token may only be performed when a process has zero or one threads.

source : https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--500-999-

3. what i've done so far, i downgraded my windows 10 to version : 21H2 build 19044, (previously i'm on 22H2)

    installed with MT5 build 3504, running optimization on EA & Indicator test code above shown no error.

    so what i suspected to be the cause of this problem would be the last quality update of the windows 10 which unfortunately happened at the same day as the MT5 update roll out


i hope this just solve the problem for now, still not trying to test with the full EA, not the dummy one.

btw, thanks everyone :)

critical runtime Error 565
critical runtime Error 565
  • 2021.04.12
  • www.mql5.com
Hallo zusammen, ich wollte meinen Multiymbol EA backtesten und erhielt schon mehrfach diesen runtime Error 565...
 

I had similar problems. I found the cause in the log of the remote agents.

In my case my EA uses a custom indicator which was in DEBUG mode, while the EA was in normal mode. After recompiling my custom indicator, the remote agents run my EA properly.

So in short, check that everything your program refers to is compiled properly.

I hope this applies to your error 565 as well.

 

I had a same problem with the MetaTrader strategy tester, and I want to share my solution with you. It showed a strange error message like 'critical runtime error 565 in Init function (error code 565, module Experts).' After some investigation, I found out that the issue was with the timeframes I used. I also found that the error can caused by variety of things and since EA in optimization mode the errors got suppressed but if you run that iteration that cause an error in display mode you will see an error message.

In my case was timeframes. You see, in the settings, I had chosen M20 as my timeframe. But during one of my optimization tests, I used M30 as a higher timeframe for a trend filter. This caused the error! M30 cannot work with M20 bar open price modeling. Oops!

To fix this, I made sure that the timeframes in my optimization tests are compatible with the one I selected in the settings. Timeframes like M20, 1H, 2H, and so on work fine because they can be divided by M20. But using M30 with M20 bar open price modeling gives errors. The alternative is to use all ticks or 1 min OHLC modelling, that works.

So, if you encounter the 'critical runtime error 565,' double-check your timeframes and make sure they match throughout your strategy testing. I hope this helps.

Remember: When researching the solution if found that updating and/or cleaning your system may solve the problem, if not downgrade your Metatrader installation to the previous versions that works.

Reason: