Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pandas Datareader for multiindex portfolio
# 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()
# asset weights
wts = [0.15, 0.15, 0.05, 0.2, 0.15, 0.05, 0.15, 0.1]
weighted_return = (wts * daily_pct_change)
# portfolio calculation
portfolio_return = weighted_return.sum(axis=1)
cumprod_portfolio = (1 + portfolio_return).cumprod()
fig2 = plt.figure(figsize=(15, 7))
ax2 = fig2.add_subplot(1, 1, 1)
cumprod_portfolio.plot(ax=ax2)
ax2.set_xlabel('Date')
ax2.set_ylabel('Cumulative return')
ax2.set_title('Portfolio Cumulative Returns')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment