How to to gather daily returns data for all US-traded equities from a period starting from the ea...

...rly 2010s to the present day using CODEBOOK API

How to to gather daily returns data for all US-traded equities from a period starting from the early 2010s to the present day using CODEBOOK API

Best Answer

  • Jirapongse
    Jirapongse ✭✭✭✭✭
    Answer ✓

    @marceugene.belen

    Thank you for reaching out to us.

    You can get the list of RICs by using the following code.

    lse_equities["RIC"].to_list()

    However, the get_data method is not desinged to retrieve a lot of data at once. Therefore, you need to split to multiple requests, such as requesting data for ten RICs at a time.

    The sample code looks like this. This is just a sample so we will not support it.

    from refinitiv.data.errors import RDError

    def GetDataRetry(universe, fields,parameters):
        retry = True
        retry_max = 5
        retry_count = 1
        while retry==True:
            try:
                retry=False
                df = rd.get_data(
                    universe,
                    fields,
                    parameters)
                return df
            except RDError as err:
                if "Backend error. 400 Bad Request" in err.message:
                    retry_count = retry_count + 1
                    if retry_count<=retry_max:
                        print("Retry "+universe)
                        retry=True
                    else:
                        print("Retry reach max and unable to get the data for "+universe)
                        return None
                else:
                    print(str(err.code)+" "+err.message)
                    return None




    def split_instruments(instruments, chunk_size=5):
        """Yield successive chunk_size chunks from instruments."""
        for i in range(0, len(instruments), chunk_size):
            yield instruments[i:i + chunk_size]
     
    instrument_chunks = list(split_instruments(lse_equities["RIC"].to_list(), 10))
     
    dfs = []
     
    for chunk in instrument_chunks:
        # Fetch data for the current chunk of instruments
        print(chunk)
        df_chunk = GetDataRetry(chunk,
                                [
                                   'TR.TotalReturn.date',
                                   'TR.TotalReturn'
                                ],
                               {'SDate':'2010-01-01','EDate':'2024-09-24','Frq':'D'})
    dfs.append(df_chunk)

Answers

  • Formulated the code, however this is only for one RIC:

    import refinitiv.data as rd
    rd.open_session()
    df = rd.get_data(
    universe = ['AAPL.O'],
    fields = [
    'TR.TotalReturn(SDate=2010-01-01,EDate=2024-09-24,Frq=D).date',
    'TR.TotalReturn(SDate=2010-01-01,EDate=2024-09-24,Frq=D)'
    ]
    )

    display(df)

    Managed to get the list of all the securities for an exchange using the search function, however I am having troubles incorporating this list into the following data item of total return.

    lse_equities = equity.search(exchange_name="NASDAQ Stock Exchange Capital Market")
    display(lse_equities)

    1727190229915.png