Error 4022 mql5

 
Hey guys, so i coded an indicator and everything is ok but when i switch timeframes sometimes the uninitialize reason which i print to observe is 3 which is normal meaning REASON_CHARTCHANGE but other times it’s reason 1 meaning REASON_REMOVE but the indicator doesn’t remove. I tried to catch the error and it shows 4022. Someone have any idea on why when switching timeframe sometimes it gives REASON_REMOVE with error 4022 instead of REASON_CHARTCHANGE as it should? And a solution as i need it to give the correct reason as i use the reason code for other stuff in the code.
Thanks
 
Constant Code Description
ERR_PROGRAM_STOPPED 4022 Test forcibly stopped from the outside. For example, optimization interrupted, visual testing window closed or testing agent stopped

Stop trying to change the chart in the tester. PICNIC.

 

Hi, 

Error 4022 often relates to problems with resources used by the indicator, such as graphical objects or buffers. Make sure you’re properly releasing all resources in your code.

Ensure your OnDeinit function correctly handles all deinitialization reasons. It’s possible that your logic is inadvertently causing the indicator to be removed. Here is a sample structure for your OnDeinit function to capture more information:

void OnDeinit(const int reason)

{

    Print("Deinitializing. Reason: ", reason);

    switch(reason)

    {

        case REASON_CHARTCHANGE:

            // Specific code for chart change

            break;

        case REASON_REMOVE:

            // Specific code for indicator removal

            break;

        case REASON_RECOMPILE:

            // Specific code for recompilation

            break;

        // Add other cases if necessary

    }

    // Release resources

    // ...

}

Add additional logs to your system to better understand execution flow

 
William Roeder #:
Constant Code Description
ERR_PROGRAM_STOPPED 4022 Test forcibly stopped from the outside. For example, optimization interrupted, visual testing window closed or testing agent stopped

Stop trying to change the chart in the tester. PICNIC.

The thing is i am not in the tester. I am on live chart
 
axabba #:

Hi, 

Error 4022 often relates to problems with resources used by the indicator, such as graphical objects or buffers. Make sure you’re properly releasing all resources in your code.

Ensure your OnDeinit function correctly handles all deinitialization reasons. It’s possible that your logic is inadvertently causing the indicator to be removed. Here is a sample structure for your OnDeinit function to capture more information:

void OnDeinit(const int reason)

{

    Print("Deinitializing. Reason: ", reason);

    switch(reason)

    {

        case REASON_CHARTCHANGE:

            // Specific code for chart change

            break;

        case REASON_REMOVE:

            // Specific code for indicator removal

            break;

        case REASON_RECOMPILE:

            // Specific code for recompilation

            break;

        // Add other cases if necessary

    }

    // Release resources

    // ...

}


Add additional logs to your system to better understand execution flow

This is exactly what i did. I added the logs and that’s how i was able to capture the error 4022 and the fact it gives REASON_REMOVE when it should give REASON_CHARTCHANGE. 

In my deinit i have this

If(reason!=REASON_CHARTCHANGE){….}

But since reason sometimes doesn’t give the correct value it causes the issue
 
Bryan Djoufack Nguessong #:
This is exactly what i did. I added the logs and that’s how i was able to capture the error 4022 and the fact it gives REASON_REMOVE when it should give REASON_CHARTCHANGE. 

In my deinit i have this

If(reason!=REASON_CHARTCHANGE){….}

But since reason sometimes doesn’t give the correct value it causes the issue
Code ?
 
Alain Verleyen #:
Code ?
void OnDeinit(const int reason){
   
   EventKillTimer();
   
   int err = GetLastError();
   Print(reason," deinit",err);
   
   if(reason!= REASON_CHARTCHANGE){
      
      Print("deleted",reason," ",err);ObjectsDeleteAll(0, prefix);//ChartRedraw(0);
    }
  
   ChartRedraw(0);
   
}  

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[])
  {
   return (rates_total);
  }
  
void OnTimer()
{
      if(IsStopped()) {Print("stopped");return;}
      
      
        
         
         
         
         //lOAD DATA
         
         if(afficherHull){
            
            int c = CopyBuffer(hull, 0, 0, 3000, hma);
            if(c <= 0){return ;}
         
         }
         
         if(plusbashautcci){
            int end = iBarShift(_Symbol, PERIOD_CURRENT, iTime(_Symbol, PERIOD_D1, lookback+1));
           //double cci[];
           //ArraySetAsSeries(cci , true);
           int c = CopyBuffer(handlecci, 0, 0, end+1, cci);
            if(c != end+1){Print("4 ",c," ",end+1);return ;}
         }
         
         if(fibonacci){
         
            ArraySetAsSeries(h1, true);
         
            if(CopyTime(_Symbol, PERIOD_H1, 0, 48, h1) != 48){return ;}
            
         }
         
         if(croisementh1){
            
            int bshift = iBarShift(_Symbol, PERIOD_H1, iTime(_Symbol, PERIOD_D1, lookback));
           
              int c = CopyBuffer(h_ema5_h1, 0, 0, bshift, ema5_h1) ;
              int c1 = CopyBuffer(h_ema21_h1, 0, 0, bshift, ema21_h1);
              
              if(c != bshift) {return ;}
              if(c1 != bshift) {Print("6");return ;}
            
         }
         
         if(croisementm15){
            
            int bshift = iBarShift(_Symbol, PERIOD_M15, iTime(_Symbol, PERIOD_D1, lookback));
              int c = CopyBuffer(h_ema5_m15, 0, 0, bshift, ema5_m15) ;
              int c1 = CopyBuffer(h_ema21_m15, 0, 0, bshift, ema21_m15);
              
              if(c != bshift) {Print("8");return ;}
              if(c1 != bshift) {Print("9");return ;}
           
         }
         
         if(croisementm5){
             int c = CopyBuffer(h_ema5_m5, 0, 0, 500, ema5_m5) ;
              int c1 = CopyBuffer(h_ema21_m5, 0, 0, 500, ema21_m5);
              
              //Print(c," ",c1," ");
           
           if(c != 500) {return ; Print("10");}
           if(c1 != 500) {return ;Print("11");}
         }
         
         MqlRates rates[];
         if(pointpivot){
             
           ArraySetAsSeries(rates, true);
           if(CopyRates(_Symbol, PERIOD_D1, 0, 2, rates) != 2){
               Print("7");return ;
           }
         }

        //............
}

something like this

 
void OnDeinit(const int reason){
   
   EventKillTimer();
   
   int err = GetLastError();
  1. Capture the error, before calling more functions.
  2. Where is the log output?
 
William Roeder #:
  1. Capture the error, before calling more functions.
  2. Where is the log output?

Hey, i did as you asked. but still happens. Here's the result on pic when i tried swithing timeframe. 1 deinit4022 means reason was 1 and error was 4022. 

But on other times i will change timeframe and it will give 3 deinit0 so the correct values.



It gives the reason 1 (REASON_REMOVE) but indicator stay on chart when normally for reason 1 (REASON_REMOVE), the indicator should've been removed from the chart. For the logic of my program this cause an issue because the objects that i didn't want deleted when switching timeframes gets deleted because reason doesn't give the correct values sometimes when it is checked in OnDeinit.