Occasional error "Failed to create empty document" using OLE

I am processing a lots of automatic backtests using Python and OLE.
Basically I am running APX containing AFL that has only include:

#pragma nocache
#include "M:\afl\stf.txt";

I change the include txt with Python, run backtest and evaluate results.

The OLE communication is simple like this:

AmiBroker = win32com.client.Dispatch("Broker.Application")

    def use_Amibroker(self, mode, project, export_path=None, export_type = 0):
        NewA = AmiBroker.AnalysisDocs.Open(project)
        if ( NewA ):             
            if export_path != None:
                while ( NewA.IsBusy ): time.sleep(1) 

LOOP runing the Amibroker function

It runs fine but occasionally I get error:


and I have to restart Amibroker.

After restart I can select the original APX, run backtest and everything is fine. There is no problem with the included content of AFL. After restart the same file works fine.

Do you please have any tip what to try to solve this?

Thank you.

The Python snippet that you have shared, creates a Broker instance, then runs a previously saved APX, and Exports the results onto a file. I think the reason, you are using Python is that you have made the M:\afl\stf.txt as a premise which dynamically forms an AFL and you probably edit the APX using that txt on the go before running your Backtests or Optimizations.

I am guessing only! And my guess can be wrong. But whatever that you want to achieve can easily be achieved the same way, simply by using native AFL. There is no need to use Python for this.

You can always use:

The File Input/Output functions

  • fclose - close a file (AFL 2.5)
  • fdelete - deletes a file (AFL 2.70)
  • fdir - list directory content (AFL 3.70)
  • feof - test for end-of-file (AFL 2.5)
  • fgetcwd - get current working directory (AFL 4.10)
  • fgets - get a string from a file (AFL 2.5)
  • fgetstatus - retrieves file status/properties (AFL 2.90)
  • fmkdir - creates (makes) a directory (AFL 2.70)
  • fopen - open a file (AFL 2.5)
  • fputs - write a string to a file (AFL 2.5)
  • frmdir - removes a directory (AFL 2.70)

(Source: Categorized list of functions)

These functions are enough to perform any kind of file-manipulation you want to do. And pretty much synonymous to Basic C File IO. Python does not do anything different! The benefit you get, is that you can easily avoid using OLE.

Again assuming that, you might be using Python in order to enjoy its inbuilt libraries to perform calculations or something else. If you understand the library, you can any day code the same in AmiBroker too, but, I understand that's tedious work. So, to some extent, you would still want to use Python. That's fine!

However, all I am trying to portray is that please do not perform the repetitive tasks, i.e. creating an External OLE Loop to perform such redundancies, instead use native AFL File IO for File manipulations and then use Batch. For instance:

if( ParamTrigger( "batch", "run me" ) ) { 
      ShellExecute( "runbatch", "path_to_batch_file.abb", "" ); 

(Code Source: Using Batch window)

Due to lack of details, once again guessing, you might be getting that error because ocassionally AmiBroker runs the APX with an empty AFL, or it is a different story altogether if you are creating the whole APX dynamically. Somehow, AmiBroker executes way before the APX or the AFL in the APX is dynamically created.

You can avoid all that, if you simply stick to native AFL and that way it will be way faster.

1 Like

@Cougar, thank you very much for your detailed answer.

I use Python because I am more data analysis then a programmer and it is just easier for me.

But I will definitely re-think my workflow using more native AFL functions.

Thank you for the ideas.