Measure phase 1 and phase 2 backtest durations with this code

Here's an example of code you can use to measure the duration of phase 1 and phase 2 backtests. I recently worked this out and thought it was worth sharing. The reported phase 1 duration is the sum of phase 1 executions for all symbols. It works during optimizations or normal backtests. View the log window for the trace output.

//////////////////////////////////////////////////////////////////////////
// Measure and log the duration of phase 1 and phase 2 backtests.
//////////////////////////////////////////////////////////////////////////
Action = Status("Action");

// Start the duration timer for first symbol in phase 1
if (Status("stocknum") == 0 && Action != actionPortfolio)
{
  phase1Start = GetPerformanceCounter();
  StaticVarSet("phase1Start", phase1Start);
}

// At the start of phase 2 we stop timing phase 1 and start timing phase 2
if (Action == actionPortfolio)
{
  phase1Start = StaticVarGet("phase1Start");
  phase1End = GetPerformanceCounter(True);
  phase1_ms = phase1End - phase1Start;
}

//////////////////////////////////////////////////////////////////////////
// Phase 1
//////////////////////////////////////////////////////////////////////////
x = Optimize("x", 1, 1, 100, 1);
Buy = 1; Sell = 0; Short = 0; Cover = 0;

//////////////////////////////////////////////////////////////////////////
// Phase 2
//////////////////////////////////////////////////////////////////////////
SetOption("UseCustomBacktestProc", true);
if (Action == actionPortfolio)
{
  bo = GetBacktesterObject();
  bo.Backtest();

  phase2_ms = GetPerformanceCounter();
  _Trace("phase1=" + NumToStr(phase1_ms, 1.0) + "ms" +
        " phase2=" + NumToStr(phase2_ms, 1.0) + "ms");
}
2 Likes

It is not needed at all. The information is natively included in the "Info" tab, in fact there is a whole lot more information there. Figures are explained in the Knowledge Base:

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.