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
-
Import SPY.csv, drop missing values, clean up Date, sort the dataframe by Date (earliest value first).
-
Calculate the daily open-to-close return (Buy at Open price, sell at Close price). Name this new value/column: ret_SPY
-
Calculate the future 20-day cumulative return for SPY. Name this new value/column: ret_SPY_c
-
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
-
Import TSLA.csv, drop missing values, clean up Date, sort the dataframe by Date (earliest value first).
-
Calculate the daily open-to-close return (Buy at Open price, sell at Close price). Name this new value/column: ret_TSLA
-
Calculate the future 20-day cumulative return for TSLA. Name this new value/column: ret_TSLA_c
-
Keep only Date, ret_TSLA, and ret_TSLA_c
** Your code below **
[ ]:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
