Schedule data notebooks to automate business metric reporting
This notebook displays the adjusted closing prices, the value of each of your holdings and the total value of your portfolio with the help of the yfinance
, pandas
and plotly
packages.
import pandas as pd
import plotly.express as px
import yfinance as yf
# Define how many stock I own of each ticker
holdings = {
'AAPL': 16,
'TSLA': 15,
'TEAM': 7,
}
%%capture
tickers = list(holdings.keys())
data = yf.download(tickers, start="2023-01-01")
adj_close = data['Adj Close'].reset_index()
adj_close_long = pd.melt(
adj_close,
id_vars='Date',
value_vars=tickers,
var_name='ticker',
value_name='adj_close'
)
adj_close_long['total_value'] = adj_close_long['adj_close'] * adj_close_long['ticker'].map(holdings)
adj_close_long
Hidden output
fig = px.line(adj_close_long, x='Date', y='adj_close', color='ticker')
fig.update_layout(
title="Adjusted closing prices for stocks in my portfolio",
yaxis_title="Adjusted Closing Price",
xaxis_title="Date"
)
fig
fig = px.line(
adj_close_long, x='Date', y='total_value', color='ticker'
)
fig.update_layout(
title="Position values for for stocks in my portfolio",
yaxis_title="Position Value",
xaxis_title="Date"
)
fig.show()
portfolio_value_per_day = adj_close_long.groupby('Date')['total_value'].sum().reset_index()
fig = px.line(portfolio_value_per_day, x='Date', y='total_value')
fig.update_layout(
yaxis_title="Total Portfolio Value",
xaxis_title="Date"
)
fig.show()