Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created September 15, 2020 12:32
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/e282e85d1577c80520f693a054c82ce1 to your computer and use it in GitHub Desktop.
Save yuyasugano/e282e85d1577c80520f693a054c82ce1 to your computer and use it in GitHub Desktop.
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