The report file can not be open for writing

I wonder what might be causing the error message "The report file can not be open for writing".

It comes immediately after a backtest runs. The list of trades appears but the report is not available.

I have renamed the file for this formula in the Amibroker / Reports directory, and renamed the formula - no help.

Other formulae run with no such message and produce the report as expected, so I can only assume it is something in the code.

When it appeared the first time I was working on a routine that wrote to a text file. I couldn't see anything wrong there (the file was closing) but I have tried removing that and it had no effect.

image

Such a message is displayed when program tries to open report files (such as stats.html, trades.html, formula.html, charts.html, montecarlo.html, settings.html, symbols.html) for writing but Windows rejects opening the file.

This may occur if:

  • there is no free space on disk
  • the subfolder inside Reports folder can not be created (i.e. you changed current working directory or changed report folder location to non-existing, or folder is read-only)
  • you have open many files that you failed to close and you reached the limit of open files
  • some other process locked files exclusively (antivirus)

I would:

  1. Restart Windows
  2. Fix the formula not to do fopen() without fclose().

I’ve think I’ve explored all aspects of this, but the problem continues.

there is no free space on disk - there is abour 50G remaining - should be plenty

No file locations have been deliberately changed. In many cases I can’t determine rules for location or check location.

you changed current working directory - Can’t find a reference to current working directory in documentation, but assume it is the startup directory in the shortcut properties, which is unchanged - points to the amibroker directory.

or changed report folder location to non-existing - again, can’t find reference to reports folder location, but assume it defaults to within amibroker directory. When I remove existing report directory a new one is created at this location, though it remains empty.

or folder is read-only - Nothing has been changed explicitly here, and all files in amibroker and below have been set to non read only. User has administrator access (ie full control in properties)

you have open many files that you failed to close and you reached the limit of open files - fopens were accompanied by fclose from the outset, but for debugging all file access within the formula has been disabled.

Even if there was a problem with open files during a run, I would imagine that there would be no files open on the first backtest after a startup, yet the problem occurs then.

some other process locked files exclusively (antivirus - If this were the case then I would not be able to remove the reports directory, and AB would not be able to create a new reports directory.

As for

Restart Windows
Fix the formula not to do fopen() without fclose().

Have done so, with no result.

I return to the fact that the problem must lie in the code, since reports can be opened when backtesting other code.

Again, there is no longer any text file access in the code - not its not that.

The code does a fair bit in the custom backtester, including looping through trades and adding custom metrics. Could the problem lie there ?

I also noted that at the time the problem started there were thousands of files in the report folder - seems like several for every backtest ever run. Should this be cleaned out automatically or something ?

Normally I’d go to standard debugging procedures, but this code is long and colvoluted, with about 900 lines and pushing my knowledge of some aspects of amibroker, so any pointers would be appreciated.

There is always a reason and the reason is always on user side. Are you using SetFormulaName in your code? If so make sure you are NOT using name that is forbidden. Forbidden characters include backslash, asterisk, quotations marks, etc.
You can only use names that are VALID for file names.

Thank you Tomasz. Your warning about Setformulaname solved the problem.

As you say, there is always a reason.

I don’t know that it helps to look for blame, but I agree that it is probably something that the user has done in most cases.

I guess that’s where this forum will eventually help in the absence of more comprehensive documentation.

Thank you for it.