Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1589

 
Miguel Angel Vico Alba #:

At what moment do you try to make a snapshot? Arbitrarily, at the moment of opening a new bar, by indicator signal or something else?

Show your piece of code...

 
Alexey Viktorov # :

At what moment do you try to make a snapshot? Arbitrarily, at the moment of opening a new bar, by indicator signal or something else?

Show your piece of code...

All the methods you mention are valid. It depends on how much you want to do it. There's no mystery to it.

Quick example based on the opening of a new bar:

input int W = 1280 ;        // screenshot width
input int H = 720 ;         // screenshot height
input int TimerSecs = 1 ;   // timer interval in seconds

bool need_shot = false ;    // flag to trigger screenshot
string fname;               // screenshot file name
datetime last_bar_time = 0 ;
//--- format datetime to safe filename (no ":" or spaces)
string SafeStamp( datetime t)
  {
   string s = TimeToString (t, TIME_DATE | TIME_MINUTES | TIME_SECONDS ); // "YYYY.MM.DD HH:MM:SS"
   StringReplace (s, ":" , "-" );   // replace ":" with "-"
   StringReplace (s, " " , "_" );   // replace space with "_"
// Optional: replace dots if you prefer
// StringReplace(s, ".", "-");
   return s;
  }
//--- initialization
int OnInit ()
  {
   EventSetTimer (TimerSecs); // start timer
   return INIT_SUCCEEDED ;
  }
//--- deinitialization
void OnDeinit (const int reason)
  {
   EventKillTimer (); // stop timer
  }
//--- detect new bar on tick
void OnTick ()
  {
   datetime bar_time = iTime (_Symbol , _Period , 0);
   if (bar_time != last_bar_time) // new bar detected
     {
      last_bar_time = bar_time;
      need_shot = true ;
      fname = "bar_" + SafeStamp(bar_time) + ".png" ; // safe filename
     }
  }
//--- take screenshot on timer event
void OnTimer ()
  {
   if (!need_shot)
       return ; // nothing to do
// force render before screenshot
   ChartRedraw (0);
   ChartRedraw (0);
   ResetLastError ();
   bool ok = ChartScreenShot (0 , fname, W, H);
   int err = GetLastError ();
   if (ok)
       Print ("Screenshot saved: " , fname);
   else
       PrintFormat ("Screenshot failed: %s, err=%d" , fname, err);
   need_shot = false ; // reset flag
  }


Files:
 
Miguel Angel Vico Alba Strategy Tester? There are no restrictions in the documentation, but I am getting blank images. Has anyone encountered this or found a solution?
 
Alexey Viktorov #:

It may seem obvious but I think it is worth mentioning. Are you running the test in visual mode? Without visual mode there is no chart rendering, which would explain the blank images.

 
Miguel Angel Vico Alba #:

All the methods you mention are valid. It depends on how much you want to do it. There's no mystery to it.

Quick example based on the opening of a new bar:


Thanks for the code and image, but your screenshot (bar_2025.08.15_11-00-00.png) is from live trading, not visual backtest, as my question asked: "Does the ChartScreenShot() function work reliably in MetaTrader 5 Strategy Tester's visual mode?". I tested your code with visual mode enabled, and it still produces blank images in backtest. Using OnTimer() and ChartRedraw() is nothing new—I've already tried it.  Your vague advice about "delays, retries, or triggering from a timer" doesn't fix the issue. If you have a working code example for visual backtest mode, or if you're capable of providing one, show it.

 
Nauris Zukas #:

Thanks for the code and image, but your screenshot (bar_2025.08.15_11-00-00.png) is from live trading, not visual backtest, as my question asked: "Does the ChartScreenShot() function work reliably in MetaTrader 5 Strategy Tester's visual mode?". I tested your code with visual mode enabled, and it still produces blank images in backtest. Using OnTimer() and ChartRedraw() is nothing new—I've already tried it.  Your vague advice about "delays, retries, or triggering from a timer" doesn't fix the issue. If you have a working code example for visual backtest mode, or if you're capable of providing one, show it.

First, my apologies if my previous replies came across as vague or unhelpful. I should clarify that I have no personal need for ChartScreenShot() in MT5's Strategy Tester and have never used it in my own work. Everything I posted here was created from scratch with the sole intention of trying to help you and others explore possible solutions.

After reviewing the forum history in depth, I think it is important to be completely transparent. As far as I can see, ChartScreenShot() has never worked reliably in MT5's Strategy Tester, even in visual mode. There is no confirmed case of it working consistently. On the contrary, there are years of reports describing blank or zero-byte files.

The cause lies in how MT5 is built. In MT4, the tester runs in a single thread and renders the chart in the same context as the terminal window, so when you take a screenshot there is an actual chart image in memory to capture. In MT5, the tester is multi-threaded and distributed across agent processes, with chart rendering happening asynchronously. In visual mode, the chart you see is drawn by the terminal's interface, but the EA code in the tester runs in an agent process that has no direct access to that rendered image. The function returns "true" because it believes it succeeded, but there is no completed chart in that context to capture.

Because of this separation, the problem cannot be solved with timing tricks, retries, or code tweaks. The limitation is a direct result of MT5's architecture and the way it handles backtesting.

I hope this explains why you and others see blank images and why there are no genuine working examples for visual backtests. It is not a question of skill or creativity; the feature simply is not supported in MT5's tester environment.

Forum on trading, automated trading systems and testing trading strategies

Questions from Beginners MQL5 MT5 MetaTrader 5

Miguel Angel Vico Alba, 2025.08.14 16:05

Yes, it can feel a bit crazy compared to MT4, but there is a reason for it, the rendering is asynchronous.


 
Nauris Zukas #:

Thanks for the code and image, but your screenshot (bar_2025.08.15_11-00-00.png) is taken in real trading, not in visual backtest, as I asked "Does ChartScreenShot() function work reliably in MetaTrader 5 Strategy Tester visual mode?". I tested your code with visual mode enabled and it still produces blank images in the backtest. There is nothing new in using OnTimer() and ChartRedraw() - I have tried this before. Your vague advice about "delay, retry or run on timer" doesn't solve the problem. If you have a working code example for visual backtesting mode, or if you are able to provide one, please show it.

What's the point of taking screenshots in the tester if you can take them afterwards?

 
Aleksey Vyazmikin #:

What's the point of taking screenshots in the tester if you can take them afterwards?

At one time I was going crazy too... But it was on MT4. After running the tester, I look at open orders, indicator readings and wonder... "How could an order be opened here?". It is tiresome to stare at the monitor, so I started to take pictures at the moment of order opening to see the indicator readings... But now the language has become much more convenient. And with the introduction of the debugger, all problems have disappeared.

 
Alexey Viktorov #:

At one time I also went crazy... But it was on MT4. After running the tester, I look at open orders, indicator readings and wonder... "How could an order open here?". It is tiresome to stare at the monitor, so I started to take pictures at the moment of order opening to see the indicator readings... But now the language has become much more convenient. And with the introduction of the debugger, all problems have disappeared.

I could think of one option - a person makes a neuron with picture recognition, then yes - you need screenshots in the process, without history on the right side and with scaling at the moment of screenshot.

If the brakes are due to asynchronous processing, then maybe it is necessary to suspend the logic calculations so that the rendering could come up? For example, make resource-intensive calculations in a loop and make a screenshot in it?

 

After metatester release aprox. 5120 GUI limit maximum number of created agents to half of CPU threads as in the attached picture.

Why is that? I am not even connecting to cloud? 

Files: