Python question - OLE object variable scope

I have a script which was currently working well. I have gone to reuse the code this weekend for some importing work, and it no longer works. At all.

import win32com.client
import os
import time

n=0
while n<1:
    num_files = os.listdir("C:\\Program Files\\AmiBroker\\Brokers\\Binance\\Data\\symbolcsv\\klines\\csvFiles\\")
    len_num_files = len(num_files)
    if len_num_files >= 720:
        try:
            os.rename("C:\\Program Files\\AmiBroker\\Brokers\\Binance\\Data\\symbolcsv\\klines\\csvFiles\\quotes.BinHist","C:\\Program Files\\AmiBroker\\Brokers\\Binance\\Data\\symbolcsv\\klines\\csvFiles\\quotesRen.BinHist")
        except:
            pass
        print("IMPORTING DATA INTO AMIBROKER")
        AB = win32com.client.Dispatch("Broker.Application")
        AB.LoadDatabase("C:\\Program Files\\AmiBroker\\Databases\\Crypto")
        AB.Import(0, "C:\\Program Files\\AmiBroker\\Brokers\\Binance\\Data\\symbolcsv\\klines\\csvFiles\\quotesRen.BinHist","BinHist.format")
        os.remove("C:\\Program Files\\AmiBroker\\Brokers\\Binance\\Data\\symbolcsv\\klines\\csvFiles\\quotesRen.BinHist")
        AB.RefreshAll()
        AB.SaveDatabase()
        break
    else:
        time.sleep(0.5)

Even

import win32com.client
AB = win32com.client.Dispatch("Broker.Application")

only brings up Amibroker for a split second before it disappears. Amibroker doesn't appear in the task manager or anything. Its dropping immediately.

Amibroker works fine if I launch it via taskbar, or via the directory.

I checked the broker log.

132.90 ms : Initializing OLE (0.08 ms)

and then later on there is

1951.00 ms : Dispatch commands via ProcessShellCommand (0.03 ms)

(?? is that relevant)

there was something about eSignal not being configired correctly which I didn't think relevant.

"Loading 'C:\Program Files\AmiBroker\Plugins\eSignal.dll' - failed. Error code: 14001: The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail. (6.72 ms)"

I don't use eSignal in any case, so I deleted it and relaunched Amibroker manually and the

Is anyone else familiar with this problem and have an idea of what might have happened in the background, or where I could start to look?

First this is not PYTHON forum and question is about PYTHON.

PYTHON (and many other languages) uses either reference counting or garbage collector. Which means that if you create variable that holds reference to newly created object it will release (DESTROY) object as soon as variable holding reference to object gets out of scope.
AGAIN: please study Python docs about variable lifetime.

The structure of your code is incorrect., you should call

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

only ONCE per entire program (outside the loop), not inside loop and make sure that AB variable lives for ENTIRE lifetime of your python program.

As soon as AB variable gets out of scope PYTHON DESTROYS the object (in this case AmiBroker).

4 Likes

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