feat: Enhance trading system page with total portfolio value calculation and usage metrics

This commit is contained in:
Bobby (aider) 2025-02-13 12:49:31 -08:00
parent 336afa8170
commit 3e1775534c

View File

@ -1,23 +1,46 @@
import streamlit as st
from trading.journal import get_latest_portfolio_value, get_open_trades_summary
from trading.position_calculator import PositionCalculator
from utils.data_utils import get_current_prices
def trading_system_page():
st.header("Trading System")
st.subheader("Position Calculator")
# Get latest portfolio value and open trades for cash calculation
# Get latest portfolio value and open trades for total portfolio calculation
portfolio_data = get_latest_portfolio_value()
cash_balance = portfolio_data['cash_balance'] if portfolio_data else 0
# Display available cash
st.info(f"Available Cash: ${cash_balance:,.2f}")
# Calculate total portfolio value including open positions
open_positions = get_open_trades_summary()
total_position_value = 0
if open_positions:
# Get current prices for all open positions
tickers = [pos['ticker'] for pos in open_positions]
current_prices = get_current_prices(tickers)
# Calculate total value of open positions
for position in open_positions:
ticker = position['ticker']
current_price = current_prices.get(ticker, 0)
shares = position['total_shares']
total_position_value += current_price * shares
total_portfolio_value = cash_balance + total_position_value
# Display portfolio information
col1, col2 = st.columns(2)
with col1:
st.info(f"Available Cash: ${cash_balance:,.2f}")
with col2:
st.info(f"Total Portfolio Value: ${total_portfolio_value:,.2f}")
col1, col2 = st.columns(2)
with col1:
account_size = st.number_input("Account Size ($)",
min_value=0.0,
value=cash_balance,
value=total_portfolio_value,
step=1000.0)
risk_percentage = st.number_input("Risk Percentage (%)",
min_value=0.1,
@ -53,7 +76,11 @@ def trading_system_page():
col1, col2 = st.columns(2)
with col1:
if recommended_shares < position['shares']:
st.warning(f"Position size limited by available cash. Maximum affordable shares: {recommended_shares:,}")
st.warning(
f"Position size limited by available cash.\n"
f"Ideal shares: {position['shares']:,}\n"
f"Maximum affordable shares: {recommended_shares:,}"
)
position_value = recommended_shares * entry_price
risk_amount = position['risk_amount'] * (recommended_shares / position['shares'])
@ -77,7 +104,12 @@ def trading_system_page():
# Show percentage of cash being used
if recommended_shares > 0:
cash_usage = (recommended_shares * entry_price / cash_balance) * 100
st.info(f"This position would use {cash_usage:.1f}% of available cash")
portfolio_usage = (recommended_shares * entry_price / total_portfolio_value) * 100
st.info(
f"This position would use:\n"
f"- {cash_usage:.1f}% of available cash\n"
f"- {portfolio_usage:.1f}% of total portfolio"
)
except Exception as e:
st.error(f"Error calculating position: {str(e)}")