refactor: Update trading system page portfolio calculation logic

This commit is contained in:
Bobby (aider) 2025-02-13 13:03:43 -08:00
parent 217707b4d9
commit d7834d78f5

View File

@ -12,21 +12,25 @@ def trading_system_page():
cash_balance = portfolio_data['cash_balance'] if portfolio_data else 0
# Calculate total portfolio value including open positions
open_summary = get_open_trades_summary() # Change to use summary instead of raw trades
open_summary = get_open_trades_summary()
total_position_value = 0
total_paper_pl = 0
if open_summary:
# Get current prices for all open positions
tickers = list(set(summary['ticker'] for summary in open_summary))
current_prices = get_current_prices(tickers)
unique_tickers = list(set(summary['ticker'] for summary in open_summary))
current_prices = get_current_prices(unique_tickers)
# Calculate total value of open positions
# Calculate total invested value and paper P/L
for summary in open_summary:
ticker = summary['ticker']
current_price = current_prices.get(ticker, 0)
shares = summary['total_shares']
position_value = current_price * shares
avg_entry = summary['avg_entry_price']
position_value = current_price * shares if current_price else avg_entry * shares
total_position_value += position_value
total_paper_pl += (current_price - avg_entry) * shares if current_price else 0
total_portfolio_value = cash_balance + total_position_value