Python OLE script for database update - can't create new ticker

Hello,

I have made myself a small script which is supposed to take a text file with all stocks’ data and to update the AB database (if the ticker exists already) or create a new ticker if not found in AB:

ab = win32com.client.Dispatch("Broker.Application")
ab.LoadDatabase(dbname)
for line in mktlist:
    l = line.split(';')
    # l[0] - ticker
    # l[1] - Alias
    # l[2] - FullName
    # l[3] - Currency
    try:
        stock = ab.Stocks(l[0])
        if stock is not None:
            # stock found
            # update stock data - this part works OK
            stock.Alias = l[1]
            stock.FullName = l[2]
            stock.Currency = l[3]
            # etc... 

            stock.IsDirty = 1
        else:
            # stock not found
            # create new - this part doesn't work
            new_stock = ab.Stocks.Add(l[0])
            new_stock.Alias = l[1]
            new_stock.FullName = l[2]
            new_stock.Currency = l[3]
            # etc...

            new_stock.IsDirty = 1
    except Exception as e:
        print("%s:\n" % (e))
        print("%s\n" % (traceback.format_exc()))

ab.RefreshAll()
ab.SaveDatabase()
ab.Quit()

The problem I have is that while the update of existing ticker and determining that a ticker does not exist work perfectly, the new ticker is not created.

Have you any idea what I am doing wrong?

Thanks for your help.

Best regards,
Slawomir

1 Like

It is created but you need to call RefreshAll to see it.

Thank you Tomasz.

Do you mean that RefreshAll must be called every time after every new symbol found?
Right now I call it only once in the end, after passing whole list.

No, it should be called ONCE only. This is AFL formula and it just works

ab = CreateObject("Broker.Application");
stks = ab.Stocks;
stks.Add("NewSymbol");
ab.RefreshAll();

Understood Tomasz.

Thanks again for your help

Best regards,
Slawomir