Pandas Datareader for multiindex portfolio
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() | |
# 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