Download the stock daily prices and total return together with date sorted

Hi all,

I am trying to download the daily prices and total return of a list of RICs. However, when I tried by using the following codes, the sorted dates of these two variables are opposite. How to modify the code to combine the date?

Many thanks

ric = df['ric'].astype(str).tolist()

df, e = ek.get_data(ric,
['TR.PriceClose(Curn=USD)','TR.TotalReturn','TR.PriceClose.calcdate','TR.TotalReturn.date'],
{'SDate': '2024-01-01',
'EDate': '2024-02-10','Frq':'D'})
df1710345601429.png

Best Answer

  • Hi @jiayin.meng.20 ,


    I would advise updating to the new library, have you had a look at it?

    import refinitiv.data as rd
    rd.open_session()

    df = rd.get_data(
    universe=["LSEG.L", "VOD.L"],
    fields=['TR.PriceClose(Curn=USD)','TR.TotalReturn','TR.PriceClose.calcdate','TR.TotalReturn.date'],
    parameters={
    "SDate": '2024-01-01',
    "EDate": '2024-02-10', # "Curn": "EUR",
    'Frq':'D'})
    df

    1710347262956.png



    I'd actually advise the get_history function instead.

Answers

  • Hi
    @jonathan.legrand


    Thanks for answering. However, the order of the date of total return and daily price seems also different. Could you please provide a code to make the order of these dates the same? For example both from 2024-01-01 to 2024-01-31

  • Hi @jiayin.meng.20 ,

    Are you asking for the dataframe rows' order to be inverted? If so, I believe the following can help:
    https://stackoverflow.com/questions/35240528/reverse-dataframes-rows-order-with-pandas

  • Hi
    @jonathan.legrand


    Thanks. But I am not sure if this is what I am asking. I would like to add a `sort` function after the retrieve of the data. Take my initial picture as an example, I want to retrieve the daily price and total return of `000002.SZ` from 2024.01.01 to 2024.02.10. However, the results show the order of daily price is from 2024.01.01 to 2024.02.10, while the order of total return is from 2024.02.10 to 2024.01.01. I would like to merge the two dates into a one. I know I can download them separately and merge them accordingly. But as there are a large amount of RICs, I would like to know is there a function that I can **sort the date** when I retrieve the values?


    Many thanks

  • Hi @jiayin.meng.20 ,

    Ah I see. No, I'm afraid that there is no such sorting of data before retrieval; it will have to be done on the receiver - your - side I'm afraid...