feat: Enhance trading journal with portfolio allocation and performance metrics
This commit is contained in:
parent
a6746ff267
commit
201d48c302
@ -9,6 +9,18 @@ from trading.journal import (
|
|||||||
get_position_summary, get_latest_portfolio_value, update_portfolio_value
|
get_position_summary, get_latest_portfolio_value, update_portfolio_value
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def calculate_portfolio_metrics(position_value: float, current_value: float, total_portfolio_value: float) -> dict:
|
||||||
|
"""Calculate portfolio metrics for a position"""
|
||||||
|
allocation = (current_value / total_portfolio_value) * 100 if total_portfolio_value > 0 else 0
|
||||||
|
position_pl = current_value - position_value
|
||||||
|
pl_impact = (position_pl / total_portfolio_value) * 100 if total_portfolio_value > 0 else 0
|
||||||
|
|
||||||
|
return {
|
||||||
|
'allocation': allocation,
|
||||||
|
'position_pl': position_pl,
|
||||||
|
'pl_impact': pl_impact
|
||||||
|
}
|
||||||
|
|
||||||
def calculate_position_performance(trades):
|
def calculate_position_performance(trades):
|
||||||
"""Calculate performance metrics for a group of trades"""
|
"""Calculate performance metrics for a group of trades"""
|
||||||
total_bought = 0
|
total_bought = 0
|
||||||
@ -157,14 +169,25 @@ def trading_journal_page():
|
|||||||
print(f"Open summary {open_summary}") # Debug
|
print(f"Open summary {open_summary}") # Debug
|
||||||
|
|
||||||
if open_summary:
|
if open_summary:
|
||||||
# Get current prices
|
# Get current prices and latest portfolio value
|
||||||
unique_tickers = list(set(summary['ticker'] for summary in open_summary))
|
unique_tickers = list(set(summary['ticker'] for summary in open_summary))
|
||||||
print(f"Fetching prices for tickers: {unique_tickers}") # Debug
|
print(f"Fetching prices for tickers: {unique_tickers}") # Debug
|
||||||
current_prices = get_current_prices(unique_tickers)
|
current_prices = get_current_prices(unique_tickers)
|
||||||
print(f"Retrieved current prices: {current_prices}") # Debug
|
print(f"Retrieved current prices: {current_prices}") # Debug
|
||||||
|
latest_portfolio = get_latest_portfolio_value()
|
||||||
|
total_portfolio_value = latest_portfolio['total_value'] if latest_portfolio else 0
|
||||||
|
cash_balance = latest_portfolio['cash_balance'] if latest_portfolio else 0
|
||||||
|
|
||||||
|
# Portfolio overview section
|
||||||
|
st.subheader("Portfolio Overview")
|
||||||
|
col1, col2 = st.columns(2)
|
||||||
|
with col1:
|
||||||
|
st.metric("Total Portfolio Value", f"${total_portfolio_value:,.2f}")
|
||||||
|
st.metric("Cash Balance", f"${cash_balance:,.2f}",
|
||||||
|
f"{(cash_balance/total_portfolio_value)*100:.1f}% of Portfolio" if total_portfolio_value > 0 else "")
|
||||||
|
|
||||||
total_portfolio_value = 0
|
|
||||||
total_paper_pl = 0
|
total_paper_pl = 0
|
||||||
|
invested_value = 0
|
||||||
|
|
||||||
for summary in open_summary:
|
for summary in open_summary:
|
||||||
print(f"\nProcessing summary for {summary['ticker']}") # Debug
|
print(f"\nProcessing summary for {summary['ticker']}") # Debug
|
||||||
@ -181,7 +204,7 @@ def trading_journal_page():
|
|||||||
print(f"- Current Price: {current_price}") # Debug
|
print(f"- Current Price: {current_price}") # Debug
|
||||||
print(f"- Position Value: {position_value}") # Debug
|
print(f"- Position Value: {position_value}") # Debug
|
||||||
|
|
||||||
col1, col2 = st.columns(2)
|
col1, col2, col3 = st.columns(3)
|
||||||
with col1:
|
with col1:
|
||||||
st.metric("Total Shares", f"{total_shares:,}")
|
st.metric("Total Shares", f"{total_shares:,}")
|
||||||
st.metric("Average Entry", f"${avg_entry:.2f}")
|
st.metric("Average Entry", f"${avg_entry:.2f}")
|
||||||
@ -193,21 +216,41 @@ def trading_journal_page():
|
|||||||
paper_pl = (current_price - avg_entry) * total_shares
|
paper_pl = (current_price - avg_entry) * total_shares
|
||||||
pl_percentage = (paper_pl / position_value) * 100
|
pl_percentage = (paper_pl / position_value) * 100
|
||||||
|
|
||||||
st.metric("Current Price", f"${current_price:.2f}")
|
metrics = calculate_portfolio_metrics(
|
||||||
st.metric("Paper P/L", f"${paper_pl:.2f}", f"{pl_percentage:.2f}%")
|
position_value, current_value, total_portfolio_value
|
||||||
|
)
|
||||||
|
|
||||||
|
st.metric("Current Price", f"${current_price:.2f}")
|
||||||
|
st.metric("Paper P/L", f"${paper_pl:.2f}",
|
||||||
|
f"{pl_percentage:.2f}%")
|
||||||
|
|
||||||
total_portfolio_value += current_value
|
|
||||||
total_paper_pl += paper_pl
|
total_paper_pl += paper_pl
|
||||||
|
invested_value += current_value
|
||||||
if total_portfolio_value > 0:
|
|
||||||
|
with col3:
|
||||||
|
if current_price:
|
||||||
|
st.metric("Portfolio Allocation",
|
||||||
|
f"{metrics['allocation']:.1f}%")
|
||||||
|
st.metric("P/L Impact on Portfolio",
|
||||||
|
f"{metrics['pl_impact']:.2f}%")
|
||||||
|
|
||||||
|
# Update portfolio summary section
|
||||||
|
if invested_value > 0:
|
||||||
st.markdown("---")
|
st.markdown("---")
|
||||||
st.subheader("Portfolio Summary")
|
st.subheader("Portfolio Summary")
|
||||||
col1, col2 = st.columns(2)
|
col1, col2, col3 = st.columns(3)
|
||||||
with col1:
|
with col1:
|
||||||
st.metric("Total Portfolio Value", f"${total_portfolio_value:.2f}")
|
st.metric("Total Invested Value",
|
||||||
|
f"${invested_value:.2f}",
|
||||||
|
f"{(invested_value/total_portfolio_value)*100:.1f}% of Portfolio")
|
||||||
with col2:
|
with col2:
|
||||||
st.metric("Total P/L", f"${total_paper_pl:.2f}",
|
st.metric("Total P/L",
|
||||||
f"{(total_paper_pl / (total_portfolio_value - total_paper_pl)) * 100:.2f}%")
|
f"${total_paper_pl:.2f}",
|
||||||
|
f"{(total_paper_pl/total_portfolio_value)*100:.2f}% of Portfolio")
|
||||||
|
with col3:
|
||||||
|
st.metric("Cash Allocation",
|
||||||
|
f"${cash_balance:.2f}",
|
||||||
|
f"{(cash_balance/total_portfolio_value)*100:.1f}% of Portfolio")
|
||||||
|
|
||||||
with tab2:
|
with tab2:
|
||||||
st.subheader("Add New Trade")
|
st.subheader("Add New Trade")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user