diff --git a/src/pages/trading/trading_system_page.py b/src/pages/trading/trading_system_page.py index da4c58e..f53284a 100644 --- a/src/pages/trading/trading_system_page.py +++ b/src/pages/trading/trading_system_page.py @@ -18,6 +18,10 @@ from db.db_connection import create_client logger = logging.getLogger(__name__) def trading_system_page(): + # Initialize session state + if 'prefill_watchlist' not in st.session_state: + st.session_state.prefill_watchlist = None + st.header("Trading System") # Create tabs @@ -192,77 +196,15 @@ def trading_system_page(): # Add to watchlist option st.divider() - st.subheader("Add to Watch List") - watchlists = get_watchlists() - if not watchlists: - st.warning("No watch lists available. Create one in the Watch Lists tab.") - else: - # Create three columns for better layout - col1, col2, col3 = st.columns([2, 2, 1]) - - with col1: - selected_list = st.selectbox( - "Select Watch List", - options=[(w['id'], w['name']) for w in watchlists], - format_func=lambda x: x[1] - ) - - with col2: - notes = st.text_area("Notes", key="watchlist_notes") - - with col3: - # Move Debug button outside of conditional - if st.button("Debug DB", key="debug_db"): - try: - with create_client() as client: - st.write("=== Watchlists Table ===") - watchlists_result = client.query("SELECT * FROM stock_db.watchlists") - st.write(watchlists_result.result_rows) - - st.write("=== Watchlist Items Table ===") - items_result = client.query("SELECT * FROM stock_db.watchlist_items") - st.write(items_result.result_rows) - except Exception as e: - st.error(f"Debug query error: {e}") - - # Move Add to Watch List button outside of columns - if st.button("Add to Watch List", key="add_to_watchlist"): - try: - # Ensure tables exist - ensure_tables_exist() - - # Create watchlist item - item = WatchlistItem( - ticker=ticker, - entry_price=float(entry_price), - target_price=float(target_price), - stop_loss=float(position['stop_loss']), - notes=str(notes) if notes else '' - ) - - # Show debug information - st.write("Adding item to watchlist:", { - "watchlist_id": selected_list[0], - "ticker": item.ticker, - "entry_price": item.entry_price, - "target_price": item.target_price, - "stop_loss": item.stop_loss, - "notes": item.notes - }) - - # Add to watchlist - success = add_to_watchlist(selected_list[0], item) - - if success: - st.success(f"Added {ticker} to watchlist!") - time.sleep(2) - st.experimental_rerun() - else: - st.error("Failed to add to watchlist") - - except Exception as e: - st.error(f"Error: {str(e)}") - logger.exception("Error adding to watchlist") + st.subheader("Save to Watch List") + if st.button("Prepare for Watch List", key="prepare_watchlist"): + st.session_state.prefill_watchlist = { + 'ticker': ticker, + 'entry_price': float(entry_price), + 'target_price': float(target_price), + 'stop_loss': float(position['stop_loss']) + } + st.success("Details saved! Switch to Watch Lists tab to complete adding to your watch list.") except Exception as e: st.error(f"Error calculating position: {str(e)}") @@ -285,13 +227,75 @@ def trading_system_page(): else: st.error("Please enter a watch list name") + # Add new item section + with st.expander("Add New Item", expanded=bool(st.session_state.prefill_watchlist)): + watchlists = get_watchlists() + if watchlists: + selected_list = st.selectbox( + "Select Watch List", + options=[(w['id'], w['name']) for w in watchlists], + format_func=lambda x: x[1] + ) + + # Use prefilled data if available + prefill = st.session_state.prefill_watchlist or {} + + col1, col2 = st.columns(2) + with col1: + ticker = st.text_input("Ticker", value=prefill.get('ticker', '')) + entry_price = st.number_input("Entry Price", + value=float(prefill.get('entry_price', 0.0)), + min_value=0.0, + step=0.01) + with col2: + target_price = st.number_input("Target Price", + value=float(prefill.get('target_price', 0.0)), + min_value=0.0, + step=0.01) + stop_loss = st.number_input("Stop Loss", + value=float(prefill.get('stop_loss', 0.0)), + min_value=0.0, + step=0.01) + + notes = st.text_area("Notes") + + if st.button("Add to Watch List"): + try: + ensure_tables_exist() + item = WatchlistItem( + ticker=ticker, + entry_price=entry_price, + target_price=target_price, + stop_loss=stop_loss, + notes=notes + ) + + success = add_to_watchlist(selected_list[0], item) + + if success: + st.success(f"Added {ticker} to watchlist!") + # Clear the prefill data + st.session_state.prefill_watchlist = None + time.sleep(1) + st.rerun() + else: + st.error("Failed to add to watchlist") + + except Exception as e: + st.error(f"Error: {str(e)}") + logger.exception("Error adding to watchlist") + else: + st.warning("Please create a watch list first") + # Display watch lists + st.subheader("Current Watch Lists") watchlists = get_watchlists() if watchlists: selected_watchlist = st.selectbox( "Select Watch List to View", options=[(w['id'], w['name']) for w in watchlists], - format_func=lambda x: x[1] + format_func=lambda x: x[1], + key="view_watchlist" # Add a unique key to avoid conflicts ) items = get_watchlist_items(selected_watchlist[0])