feat: Implement prefill workflow for watch list in trading system
This commit is contained in:
parent
ea52f3f6e9
commit
73b51c7ba6
@ -18,6 +18,10 @@ from db.db_connection import create_client
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def trading_system_page():
|
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")
|
st.header("Trading System")
|
||||||
|
|
||||||
# Create tabs
|
# Create tabs
|
||||||
@ -192,77 +196,15 @@ def trading_system_page():
|
|||||||
|
|
||||||
# Add to watchlist option
|
# Add to watchlist option
|
||||||
st.divider()
|
st.divider()
|
||||||
st.subheader("Add to Watch List")
|
st.subheader("Save to Watch List")
|
||||||
watchlists = get_watchlists()
|
if st.button("Prepare for Watch List", key="prepare_watchlist"):
|
||||||
if not watchlists:
|
st.session_state.prefill_watchlist = {
|
||||||
st.warning("No watch lists available. Create one in the Watch Lists tab.")
|
'ticker': ticker,
|
||||||
else:
|
'entry_price': float(entry_price),
|
||||||
# Create three columns for better layout
|
'target_price': float(target_price),
|
||||||
col1, col2, col3 = st.columns([2, 2, 1])
|
'stop_loss': float(position['stop_loss'])
|
||||||
|
}
|
||||||
with col1:
|
st.success("Details saved! Switch to Watch Lists tab to complete adding to your watch list.")
|
||||||
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")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
st.error(f"Error calculating position: {str(e)}")
|
st.error(f"Error calculating position: {str(e)}")
|
||||||
@ -285,13 +227,75 @@ def trading_system_page():
|
|||||||
else:
|
else:
|
||||||
st.error("Please enter a watch list name")
|
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
|
# Display watch lists
|
||||||
|
st.subheader("Current Watch Lists")
|
||||||
watchlists = get_watchlists()
|
watchlists = get_watchlists()
|
||||||
if watchlists:
|
if watchlists:
|
||||||
selected_watchlist = st.selectbox(
|
selected_watchlist = st.selectbox(
|
||||||
"Select Watch List to View",
|
"Select Watch List to View",
|
||||||
options=[(w['id'], w['name']) for w in watchlists],
|
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])
|
items = get_watchlist_items(selected_watchlist[0])
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user