Using get_data with loop in python for historical data

I would like to download several infos and balance sheet items from several companies.

I keep in an SQL table a list of RICs that I imported as a list of integers [4295859221, ...] named rics and in another SQL table the list of the fields I want to download from Eikon as a DataFrame [TR.F.CashSTInvst, TR.TotalAssets, ...] named finstatDF.

So far, I just try to loop on the company RIC. I have seen an example that I tried to replicate but I get an error message. This is my code df, err =ek.get_data(rics[5], fields = ['TR.CommonName', 'TR.F.CashSTInvst'] , parameters={'Scale': 3, 'SDate': 0, 'EDate': -2, 'FRQ': 'FY', 'Curn': 'EUR'}).

How could I do ? Thanks in advance for your help.

Best Answer

  • j.dessain
    Answer ✓

    I managed to get it done and working by playing with the nature of my inputs as follows :

    connect = sq.connect('\DB\Finlist.db')        # connect to the DB FinancialDB
    cursor = connect.cursor()
    cursor.execute("SELECT field1 FROM TRlistEuroPr")          
    outp = cursor.fetchall()
    rics = list(chain(*outp))
    cursor.execute("SELECT TRRIC FROM TRdataFinStat")          
    outp = cursor.fetchall()
    listTR = list(chain(*outp))

    dataTR, err = ek.get_data(str(rics[0]), listTR, {'Scale': 3, 'SDate': -40, 'EDate': -20, 'FRQ': 'FY', 'Curn': 'EUR'})
    dataTR.to_sql("TRdataEuroPr", connect, if_exists='replace')
    sq_table = "TRdataEuroPr"

    for i in range(len(rics)):
      dataTR, err = ek.get_data(str(rics[i]), listTR, {'Scale': 3, 'SDate': 0, 'EDate': -40, 'FRQ': 'FY', 'Curn': 'EUR'})
        if len(dataTR) < 2:
            print('error', i)
        else:
            dataTR.drop(dataTR[pd.isnull(dataTR["Tot Assets"])].index, inplace=True)            #drop all rows where Total assets is null
            dataTR.to_sql(sq_table, connect, if_exists='append')  
            print(i, len(dataTR))

Answers