OnTesterDeinit() not getting called, why?

 

Hi

I think the OnTesterDeinit() function is not getting called. 
I have written this test file.

And in an actual chart window it calls OnInit and OnDeinit() as is expected.

But in the StrategyTester it calls OnInit and calls OnDeinit() as well instead of OnTesterDeinit()

Was the OnTesterdeinit() functionality removed somehow? Seems its not working anymore.

//+------------------------------------------------------------------+
//| TestOnTesterDeinit.mq5                                           |
//| Purpose: Test if OnTesterDeinit() gets called properly in MT5    |
//+------------------------------------------------------------------+
#property copyright "Test EA"
#property link      ""
#property version   "1.00"

// Input parameters
input int SimulatedTradeCount = 5;  // Number of simulated trades to log

// Global variables
int g_tickCount = 0;
int g_simulatedTrades = 0;
datetime g_lastTradeTime = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                    |
//+------------------------------------------------------------------+
int OnInit()
{
   Print("OnInit() called at ", TimeToString(TimeCurrent()));
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Print("OnDeinit() called with reason code: ", reason, " at ", TimeToString(TimeCurrent()));
}

//+------------------------------------------------------------------+
//| Expert tick function                                              |
//+------------------------------------------------------------------+
void OnTick()
{
   g_tickCount++;
   
   // Simulate trading logic every 50 ticks
   if(g_tickCount % 50 == 0 && g_simulatedTrades < SimulatedTradeCount)
   {
      g_simulatedTrades++;
      g_lastTradeTime = TimeCurrent();
      
      Print("Simulated trade #", g_simulatedTrades, " would be executed at ", TimeToString(g_lastTradeTime));
      
      // Fake trade decision logic
      double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      if(g_simulatedTrades % 2 == 0)
         Print("Would BUY at price: ", currentPrice);
      else
         Print("Would SELL at price: ", currentPrice);
   }
}

//+------------------------------------------------------------------+
//| Tester function                                                   |
//+------------------------------------------------------------------+
double OnTester()
{
   Print("OnTester() function was called!");
   return 0.0;
}

//+------------------------------------------------------------------+
//| Tester initialization function                                    |
//+------------------------------------------------------------------+
void OnTesterInit()
{
   Print("OnTesterInit() function was called!");
}

//+------------------------------------------------------------------+
//| Tester deinitialization function                                  |
//+------------------------------------------------------------------+
void OnTesterDeinit()
{
   // Use multiple output methods to ensure visibility
   string message = "VERIFICATION: OnTesterDeinit() function was called at " + TimeToString(TimeCurrent());
   
   // Method 1: Standard Print (to Experts tab)
   Print("========================================================");
   Print(message);
   Print("Total simulated trades: ", g_simulatedTrades);
   Print("Total ticks processed: ", g_tickCount);
   Print("========================================================");
   
   // Method 2: Print with __FUNCTION__ for better visibility in logs
   Print(__FUNCTION__, " -> ", message);
   
   // Method 3: Alert (popup message)
   // Note: Alerts may not work in Strategy Tester
   Alert(message);
   
   // Method 4: Comment (display on chart)
   Comment("\n\n", message, "\nTotal trades: ", g_simulatedTrades);
   
   // Method 5: Debug print with line numbers
   PrintFormat("%s (line %d): %s", __FILE__, __LINE__, message);
   
   // Method 6: Write to file (most reliable method)
   string filename = "OnTesterDeinit_verification.txt";
   int fileHandle = FileOpen(filename, FILE_WRITE|FILE_TXT);
   if(fileHandle != INVALID_HANDLE)
   {
      FileWrite(fileHandle, "OnTesterDeinit() was called successfully");
      FileWrite(fileHandle, "Test completed at: ", TimeToString(TimeCurrent()));
      FileWrite(fileHandle, "Total simulated trades: ", g_simulatedTrades);
      FileWrite(fileHandle, "Total ticks processed: ", g_tickCount);
      FileClose(fileHandle);
      
      // Confirm file was written
      Print("Verification file created: ", filename);
   }
   else
   {
      Print("Failed to create verification file. Error: ", GetLastError());
   }
   
   // Method 7: Write to terminal log file directly
   PrintFormat("OnTesterDeinit TEST VERIFICATION: This message should appear in terminal logs");
}
 
Andreas Alois Aigner:

Hi

I think the OnTesterDeinit() function is not getting called. 
I have written this test file.

And in an actual chart window it calls OnInit and OnDeinit() as is expected.

But in the StrategyTester it calls OnInit and calls OnDeinit() as well instead of OnTesterDeinit()

Was the OnTesterdeinit() functionality removed somehow? Seems its not working anymore.

i believe those are only used during optimisation.

 
Michael Charles Schefe #:

i believe those are only used during optimisation.

oh right. i just discovered this too. so one can only write to a log file in that case.