Created
September 15, 2020 11:19
-
-
Save yuyasugano/9c3a45bb79721e56d2fc9ddc0f16f882 to your computer and use it in GitHub Desktop.
Pandas Datareader for multiindex stock data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use pivot to reshape DataFrame with only Adj Close | |
adjClosePrice = df[['Adj Close']] | |
adjClosePrice = adjClosePrice.reset_index() | |
adjClosePriceTable = adjClosePrice.pivot(index='Date', columns='Ticker', values='Adj Close') | |
adjClosePriceTable.tail() | |
# calculate daily return and cumulative return from daily return | |
daily_pct_change = adjClosePriceTable.pct_change() | |
daily_pct_change.fillna(0, inplace=True) | |
cumprod_daily_pct_change = (1 + daily_pct_change).cumprod() | |
fig = plt.figure(figsize=(15, 7)) | |
ax1 = fig.add_subplot(1, 1, 1) | |
cumprod_daily_pct_change.plot(ax=ax1) | |
ax1.set_xlabel('Date') | |
ax1.set_ylabel('Cumulative return') | |
ax1.set_title('Stock Cumulative Returns') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment