Error with script

 

Hey guys. I'm doing a simple mql5 script that does the following:
Given 2 parameters: 1) start time 2) end time, take screenshots of every candle between start time and end time (inclusive). Save the photos as a png to a specified folder (name of the folder is parameter 3). The name of the file should be the time of the candle. Dimensions of the screenshot equal to the size of the window.

//+------------------------------------------------------------------+
//|                                       CandlestickScreenshots.mq5 |
//|                                                  Emmanuel Duarte |
//|                                                                                |
//+------------------------------------------------------------------+
#property copyright "Emmanuel Duarte"
#property link      "manuduarte.2000@hotmail.com"
#property version   "1.00"
#property script_show_inputs

input datetime StartTime  = D'2023.04.27 00:00'; // Start Time
input datetime EndTime    = D'2023.04.27 23:59'; // End Time
input string   FolderName = "Screenshots2";      // Folder Name

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   datetime curTime = StartTime;
   string screenshot_path;

   // Create the folder if it doesn't exist
   if (!FolderExists(FolderName))
   {
      if (FolderCreate(FolderName))
      {
         Print("Folder '", FolderName, "' created");
      }
      else
      {
         Print("Failed to create folder '", FolderName, "': Error ", GetLastError());
         return;
      }
   }

   // Iterate through the specified time range
   while (curTime <= EndTime)
   {
      // Set the chart to the current time
      SetChartToTime(curTime);

      // Create the screenshot path
      screenshot_path = FolderName + "\\" + TimeToString(curTime, TIME_SECONDS) + ".png";

      // Save the screenshot with dynamic dimensions
      SaveScreenshot(screenshot_path);

      // Move to the next candle (assuming a 1-minute timeframe)
      curTime = curTime + 60;
   }
}

//+------------------------------------------------------------------+
//| Set the chart to the specified time                              |
//+------------------------------------------------------------------+
void SetChartToTime(datetime time)
{
   int shift = iBarShift(_Symbol, _Period, time);
   ChartSetInteger(0, CHART_FIRST_VISIBLE_BAR, shift);
}

//+------------------------------------------------------------------+
//| Save the screenshot                                              |
//+------------------------------------------------------------------+
bool SaveScreenshot(string file_name)
{
   // Get the chart window size
   int width = (int)ChartGetInteger(0, CHART_WIDTH_IN_PIXELS);
   int height = (int)ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS);

   if(!ChartScreenShot(0, file_name, width, height, ALIGN_LEFT))
   {
      Print("Failed to save screenshot: ", file_name);
      return false;
   }
   else
   {
      Print("Screenshot saved: ", file_name);
      return true;
   }
}

//+------------------------------------------------------------------+
//| Check if the folder exists                                       |
//+------------------------------------------------------------------+
bool FolderExists(string folder)
{
   ResetLastError();
   int handle = FileOpen(folder, FILE_READ | FILE_TXT);
   if (handle == INVALID_HANDLE && GetLastError() == 5004)
      return false;

   if (handle != INVALID_HANDLE)
      FileClose(handle);

   return true;
}
//+------------------------------------------------------------------+ 

I always get the error "Failed to save screenshot". I think the issue might be related to line:

screenshot_path = FolderName + "\\" + TimeToString(curTime, TIME_SECONDS) + ".png";

But I'm not sure.

 

Please do a search before posting. Already discussed.

You don't have to include the full path in your filename.

 
Alain Verleyen #:

Please do a search before posting. Already discussed.

You don't have to include the full path in your filename.

I changed the line to:

screenshot_path = TimeToString(curTime, TIME_SECONDS) + ".png";

But still get the same error:

2023.04.27 20:33:09.317 CandlestickScreenshots (EURUSD,H1) Failed to save screenshot: 23:45:00.png

2023.04.27 20:33:09.327 CandlestickScreenshots (EURUSD,H1) Failed to save screenshot: 23:46:00.png
(...)
 
Manu Duarte #:

I changed the line to:

But still get the same error:

2023.04.27 20:33:09.317 CandlestickScreenshots (EURUSD,H1) Failed to save screenshot: 23:45:00.png

2023.04.27 20:33:09.327 CandlestickScreenshots (EURUSD,H1) Failed to save screenshot: 23:46:00.png
(...)
Wrong file name.
 
Alain Verleyen #:
Wrong file name.

So i've fixed everything and it is taking the screen shots. 


I have just a doubt, it is taking the screenshots with the first candle on the left being the input StartTime, but I wan't it so the first candle on the right on the screenshot is the input StartTime. I've tried ALIGN_RIGHT it doesn't work (just takes multiple screenshots of the most recent chart).


I've searched online but can't find anything.

 
Manu Duarte #:

So i've fixed everything and it is taking the screen shots. 


I have just a doubt, it is taking the screenshots with the first candle on the left being the input StartTime, but I wan't it so the first candle on the right on the screenshot is the input StartTime. I've tried ALIGN_RIGHT it doesn't work (just takes multiple screenshots of the most recent chart).


I've searched online but can't find anything.

I don't understand why you can't find the answer if you are searching ? How are you searching ?

I know the answer is on the forum, several times. It's also in the documentation by the way.

 
Manu Duarte: The name of the file should be the time of the candle.
screenshot_path = FolderName + "\\" + TimeToString(curTime, TIME_SECONDS) + ".png";

You can't have colons in filenames.

Reason: