diff --git a/src/streamlit_app.py b/src/streamlit_app.py index 7624e85..bf0b0a2 100644 --- a/src/streamlit_app.py +++ b/src/streamlit_app.py @@ -1146,7 +1146,7 @@ def trading_plan_page(): except Exception as e: st.error(f"Error deleting plan: {str(e)}") - # Add Trade Management section + # Add Trade Management section st.subheader("Trade Management") # Get current trades for this plan @@ -1207,89 +1207,11 @@ def trading_plan_page(): f"{'[Closed]' if t['exit_price'] else '[Open]'} " f"{'P/L: $' + format(t['profit_loss'], '.2f') if t['profit_loss'] is not None else ''}" for t in available_trades if t['id'] == x - ) + ), + key=f"link_trades_{plan.id}" ) - if selected_trades and st.button("Link Selected Trades"): - if link_trades_to_plan(plan.id, selected_trades): - st.success("Trades linked successfully!") - - # Calculate and update metrics - metrics = calculate_plan_metrics(plan.id) - plan.win_rate = metrics['win_rate'] - plan.average_return_per_trade = metrics['average_return'] - plan.profit_factor = metrics['profit_factor'] - update_trading_plan(plan) - - st.query_params.update(rerun=True) - else: - st.error("Error linking trades") - - # Add Trade Management section - st.subheader("Trade Management") - - # Get current trades for this plan - plan_trades = get_plan_trades(plan.id) - - # Display current trades - if plan_trades: - st.write("Current Trades:") - for trade in plan_trades: - with st.expander(f"{trade['ticker']} - {trade['entry_date']}"): - col1, col2 = st.columns(2) - with col1: - st.write(f"Entry: ${trade['entry_price']:.2f}") - st.write(f"Shares: {trade['shares']}") - with col2: - if trade['exit_price']: - pl = (trade['exit_price'] - trade['entry_price']) * trade['shares'] - st.write(f"Exit: ${trade['exit_price']:.2f}") - st.write(f"P/L: ${pl:.2f}") - - # Get available trades - with create_client() as client: - query = """ - SELECT - id, - ticker, - entry_date, - entry_price, - shares, - exit_price, - exit_date, - direction, - strategy, - CASE - WHEN exit_price IS NOT NULL - THEN (exit_price - entry_price) * shares - ELSE NULL - END as profit_loss - FROM stock_db.trades - WHERE plan_id IS NULL - ORDER BY entry_date DESC - """ - result = client.query(query) - available_trades = [dict(zip( - ['id', 'ticker', 'entry_date', 'entry_price', 'shares', - 'exit_price', 'exit_date', 'direction', 'strategy', 'profit_loss'], - row - )) for row in result.result_rows] - - if available_trades: - st.write("Link Existing Trades:") - selected_trades = st.multiselect( - "Select trades to link to this plan", - options=[t['id'] for t in available_trades], - format_func=lambda x: next( - f"{t['ticker']} - {t['entry_date']} - ${t['entry_price']:.2f} " - f"({t['direction']}) - {t['strategy']} " - f"{'[Closed]' if t['exit_price'] else '[Open]'} " - f"{'P/L: $' + format(t['profit_loss'], '.2f') if t['profit_loss'] is not None else ''}" - for t in available_trades if t['id'] == x - ) - ) - - if selected_trades and st.button("Link Selected Trades"): + if selected_trades and st.button("Link Selected Trades", key=f"link_trades_button_{plan.id}"): if link_trades_to_plan(plan.id, selected_trades): st.success("Trades linked successfully!")