From 417747be869797c150d82ef46043412fb676c343 Mon Sep 17 00:00:00 2001 From: "Bobby (aider)" Date: Thu, 13 Feb 2025 10:46:55 -0800 Subject: [PATCH] refactor: Optimize position handling logic in trading journal page --- src/pages/journal/trading_journal_page.py | 36 ++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/pages/journal/trading_journal_page.py b/src/pages/journal/trading_journal_page.py index d03d75f..924b714 100644 --- a/src/pages/journal/trading_journal_page.py +++ b/src/pages/journal/trading_journal_page.py @@ -213,23 +213,10 @@ def trading_journal_page(): ) if ticker: - # Show existing positions for this ticker - existing_positions = get_position_summary(ticker) - # Get entry date/time first - entry_date = st.date_input("Entry Date") - entry_time_str = st.text_input("Entry Time (HH:MM)", "09:30") - try: - entry_time = datetime.strptime(entry_time_str, "%H:%M").time() - entry_datetime = datetime.combine(entry_date, entry_time) - entry_datetime = pytz.timezone('US/Pacific').localize(entry_datetime) - except ValueError: - st.error("Please enter time in HH:MM format (e.g. 09:30)") - st.stop() - - # Show existing positions for this ticker + # Show existing positions for this ticker FIRST existing_positions = get_position_summary(ticker) - # Always show the "Add to existing position" option for buys + # Handle position selection based on direction if direction == "Buy": if existing_positions: st.write(f"Existing {ticker} Positions:") @@ -246,9 +233,9 @@ def trading_journal_page(): key="position_select" ) else: - position_id = generate_position_id(ticker, entry_datetime) + position_id = None # Will be generated later else: - position_id = generate_position_id(ticker, entry_datetime) + position_id = None # Will be generated later else: # Sell if not existing_positions: st.error("No existing positions found for this ticker") @@ -258,6 +245,21 @@ def trading_journal_page(): options=[pos['position_id'] for pos in existing_positions], key="position_select" ) + + # Get entry date/time + entry_date = st.date_input("Entry Date") + entry_time_str = st.text_input("Entry Time (HH:MM)", "09:30") + try: + entry_time = datetime.strptime(entry_time_str, "%H:%M").time() + entry_datetime = datetime.combine(entry_date, entry_time) + entry_datetime = pytz.timezone('US/Pacific').localize(entry_datetime) + except ValueError: + st.error("Please enter time in HH:MM format (e.g. 09:30)") + st.stop() + + # Generate position_id if needed + if position_id is None: + position_id = generate_position_id(ticker, entry_datetime) col1, col2 = st.columns(2) with col1: