Skip to content

Instantly share code, notes, and snippets.

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')
# 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)
ax1.set_ylabel('Cumulative return')
ax1.set_title('Stock Cumulative Returns')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment