Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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