From e30c3b1348178820e068cbab416fcad72c064eee Mon Sep 17 00:00:00 2001 From: Vincenzo Date: Sat, 16 Nov 2024 12:49:54 +0100 Subject: [PATCH] invested capital line on wealth history plot --- src/aggregation.py | 17 ++++++++++------- src/plot.py | 26 +++++++++++++++++++++----- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/aggregation.py b/src/aggregation.py index 6a1b1cd..bf069b8 100644 --- a/src/aggregation.py +++ b/src/aggregation.py @@ -42,7 +42,7 @@ def aggregate_by_ticker(df: pd.DataFrame, in_pf_only: bool = False) -> pd.DataFr @st.cache_data(ttl=10 * CACHE_EXPIRE_SECONDS, show_spinner=False) def get_wealth_history( df_transactions: pd.DataFrame, ticker_list: list[str] -) -> pd.Series: +) -> pd.DataFrame: begin_date = df_transactions["transaction_date"].min() today = datetime.now().date() date_range = pd.date_range(start=begin_date, end=today, freq="D") @@ -62,18 +62,19 @@ def get_wealth_history( df_transactions = df_transactions[df_transactions["ticker_yf"].isin(ticker_list)] - df_asset_allocation = pd.DataFrame( - index=date_range, - columns=ticker_list, - data=0, - ) + df_asset_allocation = pd.DataFrame(index=date_range, columns=ticker_list, data=0) + df_cumulative_spent = pd.Series(index=date_range, data=0) for (data, ticker), group in df_transactions.groupby( ["transaction_date", "ticker_yf"] ): total_shares = group["shares"].sum() + total_spent = group["ap_amount"].sum() df_asset_allocation.loc[data, ticker] += total_shares + df_cumulative_spent.loc[data] += total_spent + df_asset_allocation = df_asset_allocation.cumsum() + df_cumulative_spent = df_cumulative_spent.cumsum() df_wealth = pd.DataFrame( df_asset_allocation.multiply(df_prices.loc[begin_date:]) @@ -81,7 +82,9 @@ def get_wealth_history( .sum(axis=1) .rename("ap_daily_value") ) - df_wealth["diff_previous_day"] = df_wealth.diff() + df_wealth["diff_previous_day"] = df_wealth["ap_daily_value"].diff() + df_wealth["ap_cum_spent"] = df_cumulative_spent + df_wealth["ap_cum_pnl"] = df_wealth["ap_daily_value"] - df_wealth["ap_cum_spent"] return df_wealth diff --git a/src/plot.py b/src/plot.py index b993465..81bfc70 100644 --- a/src/plot.py +++ b/src/plot.py @@ -17,7 +17,7 @@ def plot_sunburst(df: pd.DataFrame) -> go.Figure: fig.update_traces(hovertemplate="%{value:.1f}%") fig.update_layout( autosize=False, - height=600, + height=620, margin=dict(l=0, r=0, t=20, b=20), hoverlabel_font_size=PLT_FONT_SIZE, ) @@ -56,17 +56,33 @@ def plot_wealth(df: pd.DataFrame) -> go.Figure: data_frame=df, x=df.index, y="ap_daily_value", custom_data=["diff_previous_day"] ) fig.update_traces( - hovertemplate="%{x}: %{y:,.0f}€ Gain/Loss on previous day: %{customdata:,.0f}€" + hovertemplate=( + "Portfolio value: %{y:,.0f}€" + "P&L vs previous day: %{customdata:,.0f}€" + ) + ) + fig.add_trace( + go.Scatter( + x=df.index, + y=df["ap_cum_spent"], + mode="lines", + name="Cumulative Spent", + line=dict(dash="6px", width=1.5, color="bisque"), + customdata=df["ap_cum_pnl"], + hovertemplate=( + "Invested capital: %{y:,.0f}€" + "Ap-/De-preciation: %{customdata:,.0f}€" + ), + ) ) fig.update_layout( autosize=False, height=550, hoverlabel_font_size=PLT_FONT_SIZE, + hovermode="x", margin=dict(l=0, r=0, t=20, b=20), xaxis=dict(title=""), - yaxis=dict( - title="Daily value", showgrid=False, tickformat=",.0f", ticksuffix=" €" - ), + yaxis=dict(title="", showgrid=False, tickformat=",.0f", ticksuffix=" €"), showlegend=False, ) return fig