refactor: Optimize position handling logic in trading journal page

This commit is contained in:
Bobby (aider) 2025-02-13 10:46:55 -08:00
parent 46c37e2f63
commit 417747be86

View File

@ -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: