Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created September 15, 2020 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/9c3a45bb79721e56d2fc9ddc0f16f882 to your computer and use it in GitHub Desktop.
Save yuyasugano/9c3a45bb79721e56d2fc9ddc0f16f882 to your computer and use it in GitHub Desktop.
Pandas Datareader for multiindex stock data
# 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