Question: Language: Python 3 Part I: Import the data, calculate the return, and merge the data. 1.1 Import SPY.csv Instruction Import SPY.csv, drop missing values, clean

Language: Python 3

Part I: Import the data, calculate the return, and merge the data.

1.1 Import SPY.csv

Instruction

  1. Import SPY.csv, drop missing values, clean up Date, sort the dataframe by Date (earliest value first).

  2. Calculate the daily open-to-close return (Buy at Open price, sell at Close price). Name this new value/column: ret_SPY

  3. Calculate the future 20-day cumulative return for SPY. Name this new value/column: ret_SPY_c

  4. Keep only Date, ret_SPY, and ret_SPY_c

Hints

For 1. Use .sort_values(), make sure to set 'inplace' to 'True', and 'ascending' to 'True'

For 3. Use this:

indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=20) SPY['ret_SPY_c'] = ((1.+ SPY['ret_SPY']).rolling(window=indexer, min_periods=20).agg(lambda x : x.prod()) - 1).shift(-1)

Note: windows_size = 20 trading days

** Your code below **

1.2 Import TSLA.csv

Instruction

  1. Import TSLA.csv, drop missing values, clean up Date, sort the dataframe by Date (earliest value first).

  2. Calculate the daily open-to-close return (Buy at Open price, sell at Close price). Name this new value/column: ret_TSLA

  3. Calculate the future 20-day cumulative return for TSLA. Name this new value/column: ret_TSLA_c

  4. Keep only Date, ret_TSLA, and ret_TSLA_c

** Your code below **

[ ]:

                                            

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!