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

Best Answer

  • 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

Answers