Get RIC for the largest 10 companies in each stock market

I was trying to get the largest 10 companies with respect to the market cap each year for a list of countries over a particular time period. I use a loop like this:

country = ["IR","NL"]
for i in country: 
    for j in range(2000,2006,5):
        Start=str(j)+'-12-31'
        instruments = 'SCREEN(U(IN(Equity(active or inactive,public,primary))),IN(TR.ExchangeCountryCode, i),TOP(TR.CompanyMarketCap(SDate=Start), 10, nnumber), CURN=USD)'
        field = ["TR.CommonName"]
        df,err = ek.get_data(instruments, field)

But in the loop, the "i" is not recognised as item in the country list and the "Start" is not recognised as the variable created within the loop.

Any solution for the problem? or is there a better way to obtain the requested RIC codes?

Thank you.

Best Answer

  • umer.nalla
    Answer ✓

    Hi @Jiaman.Xu

    you are hardcoding the literal strings 'i' and 'Start' into the string parameter 'instruments' - so the value you are actually passing to the get_data calls looks like:

    SCREEN(U(IN(Equity(active or inactive,public,primary))),IN(TR.ExchangeCountryCode,i),TOP(TR.CompanyMarketCap(SDate=Start), 10, nnumber), CURN=USD)'

    rather than something like:

    SCREEN(U(IN(Equity(active or inactive,public,primary))),IN(TR.ExchangeCountryCode,'IR'),TOP(TR.CompanyMarketCap(SDate='2000-12-31'), 10, nnumber), CURN=USD)'

    You need to use python string formatting to substitute the actual values of 'i' and 'Start' into the 'instruments' parameter e.g.

    country = ["IR","NL"]
    content_df = []
    for i in country: 
        for j in range(2000,2006,5):
            Start=str(j)+'-12-31'
            instruments = f'SCREEN(U(IN(Equity(active or inactive,public,primary))),IN(TR.ExchangeCountryCode,{i}),TOP(TR.CompanyMarketCap(SDate={Start}), 10, nnumber), CURN=USD)'
            field = ["TR.CommonName"]
            df,err = ek.get_data(instruments, field)
            content_df.append(df)

    You can read up on Python string formatting on the web .e.g. Python String Formatting Best Practices – Real Python

    Note that I am not that familiar with the SCREEN function - so cannot confirm if the above is returning what you are after..