refactor: Enhance watchlist addition with robust logging and error handling
This commit is contained in:
parent
4bdd85405a
commit
322491bc83
@ -4,6 +4,7 @@ from trading.position_calculator import PositionCalculator
|
|||||||
from utils.data_utils import get_current_prices
|
from utils.data_utils import get_current_prices
|
||||||
from pages.analysis.monte_carlo_page import MonteCarloSimulator
|
from pages.analysis.monte_carlo_page import MonteCarloSimulator
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
import time
|
||||||
from utils.common_utils import get_stock_data
|
from utils.common_utils import get_stock_data
|
||||||
from trading.watchlist import (
|
from trading.watchlist import (
|
||||||
create_watchlist, get_watchlists, add_to_watchlist,
|
create_watchlist, get_watchlists, add_to_watchlist,
|
||||||
@ -201,18 +202,28 @@ def trading_system_page():
|
|||||||
|
|
||||||
with col2:
|
with col2:
|
||||||
if st.button("Add to Watch List", key="add_to_watchlist"):
|
if st.button("Add to Watch List", key="add_to_watchlist"):
|
||||||
item = WatchlistItem(
|
try:
|
||||||
ticker=ticker,
|
logger.info(f"Attempting to add {ticker} to watchlist {selected_list[0]}")
|
||||||
entry_price=entry_price,
|
logger.info(f"Entry: {entry_price}, Target: {target_price}, Stop: {position['stop_loss']}")
|
||||||
target_price=target_price,
|
|
||||||
stop_loss=position['stop_loss'],
|
item = WatchlistItem(
|
||||||
notes=notes
|
ticker=ticker,
|
||||||
)
|
entry_price=float(entry_price),
|
||||||
if add_to_watchlist(selected_list[0], item):
|
target_price=float(target_price),
|
||||||
st.success(f"Added {ticker} to watch list!")
|
stop_loss=float(position['stop_loss']),
|
||||||
st.rerun()
|
notes=str(notes) if notes else ''
|
||||||
else:
|
)
|
||||||
st.error("Failed to add to watch list")
|
|
||||||
|
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:
|
except Exception as e:
|
||||||
st.error(f"Error calculating position: {str(e)}")
|
st.error(f"Error calculating position: {str(e)}")
|
||||||
|
|||||||
@ -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
|
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"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 = [(
|
data = [(
|
||||||
next_id,
|
int(next_id), # Ensure ID is integer
|
||||||
watchlist_id,
|
int(watchlist_id), # Ensure watchlist_id is integer
|
||||||
item.ticker,
|
str(item.ticker),
|
||||||
float(item.entry_price),
|
entry_price,
|
||||||
float(item.target_price),
|
target_price,
|
||||||
float(item.stop_loss),
|
stop_loss,
|
||||||
item.notes or '',
|
str(item.notes or ''),
|
||||||
datetime.now()
|
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(
|
client.insert(
|
||||||
'stock_db.watchlist_items',
|
'stock_db.watchlist_items',
|
||||||
data,
|
data,
|
||||||
column_names=['id', 'watchlist_id', 'ticker', 'entry_price',
|
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
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error adding to watchlist: {e}", exc_info=True)
|
logger.error(f"Error adding to watchlist: {e}", exc_info=True)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user