From 322491bc83e67c75323645f0ef6087f68c81ed79 Mon Sep 17 00:00:00 2001 From: "Bobby (aider)" Date: Mon, 17 Feb 2025 16:22:04 -0800 Subject: [PATCH] refactor: Enhance watchlist addition with robust logging and error handling --- src/pages/trading/trading_system_page.py | 35 ++++++++++++------- src/trading/watchlist.py | 43 +++++++++++++++++++----- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/pages/trading/trading_system_page.py b/src/pages/trading/trading_system_page.py index 7887fe9..72b7b1c 100644 --- a/src/pages/trading/trading_system_page.py +++ b/src/pages/trading/trading_system_page.py @@ -4,6 +4,7 @@ from trading.position_calculator import PositionCalculator from utils.data_utils import get_current_prices from pages.analysis.monte_carlo_page import MonteCarloSimulator from datetime import datetime, timedelta +import time from utils.common_utils import get_stock_data from trading.watchlist import ( create_watchlist, get_watchlists, add_to_watchlist, @@ -201,18 +202,28 @@ def trading_system_page(): with col2: if st.button("Add to Watch List", key="add_to_watchlist"): - item = WatchlistItem( - ticker=ticker, - entry_price=entry_price, - target_price=target_price, - stop_loss=position['stop_loss'], - notes=notes - ) - if add_to_watchlist(selected_list[0], item): - st.success(f"Added {ticker} to watch list!") - st.rerun() - else: - st.error("Failed to add to watch list") + try: + logger.info(f"Attempting to add {ticker} to watchlist {selected_list[0]}") + logger.info(f"Entry: {entry_price}, Target: {target_price}, Stop: {position['stop_loss']}") + + 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 '' + ) + + success = add_to_watchlist(selected_list[0], item) + if success: + st.success(f"Added {ticker} to watch list!") + time.sleep(1) # Give the database a moment to update + st.rerun() + else: + st.error("Failed to add to watch list. Check the logs for details.") + except Exception as e: + logger.error(f"Error in watchlist addition: {e}", exc_info=True) + st.error(f"Error adding to watchlist: {str(e)}") except Exception as e: st.error(f"Error calculating position: {str(e)}") diff --git a/src/trading/watchlist.py b/src/trading/watchlist.py index 4732722..683c6c8 100644 --- a/src/trading/watchlist.py +++ b/src/trading/watchlist.py @@ -42,26 +42,51 @@ def add_to_watchlist(watchlist_id: int, item: WatchlistItem) -> bool: next_id = result.first_row[0] if result.first_row[0] is not None else 1 logger.info(f"Adding item to watchlist: ID={next_id}, WatchlistID={watchlist_id}, Ticker={item.ticker}") + logger.info(f"Entry Price: {item.entry_price}, Target: {item.target_price}, Stop: {item.stop_loss}") + + # Ensure all values are properly formatted + entry_price = float(item.entry_price) if item.entry_price is not None else 0.0 + target_price = float(item.target_price) if item.target_price is not None else 0.0 + stop_loss = float(item.stop_loss) if item.stop_loss is not None else 0.0 data = [( - next_id, - watchlist_id, - item.ticker, - float(item.entry_price), - float(item.target_price), - float(item.stop_loss), - item.notes or '', + int(next_id), # Ensure ID is integer + int(watchlist_id), # Ensure watchlist_id is integer + str(item.ticker), + entry_price, + target_price, + stop_loss, + str(item.notes or ''), datetime.now() )] - logger.info(f"Insert {data}") + logger.info(f"Attempting to insert data: {data}") + + # Add explicit column types + column_types = [ + 'UInt32', # id + 'UInt32', # watchlist_id + 'String', # ticker + 'Float64', # entry_price + 'Float64', # target_price + 'Float64', # stop_loss + 'String', # notes + 'DateTime' # created_at + ] client.insert( 'stock_db.watchlist_items', data, column_names=['id', 'watchlist_id', 'ticker', 'entry_price', - 'target_price', 'stop_loss', 'notes', 'created_at'] + 'target_price', 'stop_loss', 'notes', 'created_at'], + column_types=column_types ) + + # Verify the insert + verify_query = f"SELECT * FROM stock_db.watchlist_items WHERE id = {next_id}" + verify_result = client.query(verify_query) + logger.info(f"Verification query result: {verify_result.result_rows}") + return True except Exception as e: logger.error(f"Error adding to watchlist: {e}", exc_info=True)