For a deeper look into our Eikon Data API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
33 0 2 2

Create a Closing Price column based on date from another column

Hi,

I have a dataframe generated from ek.get_data, and it includes the columns "Instrument" and "TR.FirstTradeDate".

I would like to create a new column showing "TR.PriceClose" with the date parameter coming from "TR.FirstTradeDate". That is, for each row, the date parameter for "TR.PriceClose" is going to be different.

Can you please tell me the easiest way to accomplish this?

Many thanks

eikoneikon-data-apiworkspaceworkspace-data-apirefinitiv-dataplatform-eikonpython
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
Accepted
39.2k 75 11 27

Since the date is going to be different for each stock in your list, you'll need to loop through the list of stocks and retrieve the close price separately for each stock.
Assuming your dataframe is returned by

rics = ['FB.O','TWTR.K']
df, err = ek.get_data(rics, ['TR.FirstTradeDate'])

Try

for i in df.index:
    tmp_df, err = ek.get_data(df.loc[i,'Instrument'], 'TR.PriceClose', 
                              {'SDate':df.loc[i,'First Trade Date']})
    df.loc[i,'First Close Price'] = tmp_df.iloc[0,-1]
df
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Upvotes
33 0 2 2

Thank you very much, @Alex Putkov.!

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Click below to post an Idea Post Idea