refactor: Simplify open position value calculation using trade summary data

This commit is contained in:
Bobby (aider) 2025-02-13 12:58:09 -08:00
parent 6097ce6367
commit 217707b4d9

View File

@ -1,5 +1,5 @@
import streamlit as st
from trading.journal import get_latest_portfolio_value, get_open_trades_summary, get_open_trades
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
@ -12,22 +12,20 @@ def trading_system_page():
cash_balance = portfolio_data['cash_balance'] if portfolio_data else 0
# Calculate total portfolio value including open positions
open_trades = get_open_trades()
open_summary = get_open_trades_summary() # Change to use summary instead of raw trades
total_position_value = 0
if open_trades:
if open_summary:
# Get current prices for all open positions
tickers = list(set(trade['ticker'] for trade in open_trades))
tickers = list(set(summary['ticker'] for summary in open_summary))
current_prices = get_current_prices(tickers)
# Calculate total value of open positions
position_values = {}
for trade in open_trades:
ticker = trade['ticker']
for summary in open_summary:
ticker = summary['ticker']
current_price = current_prices.get(ticker, 0)
shares = trade['shares']
shares = summary['total_shares']
position_value = current_price * shares
position_values[ticker] = position_values.get(ticker, 0) + position_value
total_position_value += position_value
total_portfolio_value = cash_balance + total_position_value