refactor: Improve position selection logic for buy and sell trades

This commit is contained in:
Bobby (aider) 2025-02-13 10:43:45 -08:00
parent 0f8a75ca28
commit 46c37e2f63

View File

@ -226,20 +226,18 @@ def trading_journal_page():
st.error("Please enter time in HH:MM format (e.g. 09:30)") st.error("Please enter time in HH:MM format (e.g. 09:30)")
st.stop() st.stop()
if existing_positions: # Show existing positions for this ticker
st.write(f"Existing {ticker} Positions:") existing_positions = get_position_summary(ticker)
for pos in existing_positions:
st.write(f"Position ID: {pos['position_id']}") # Always show the "Add to existing position" option for buys
st.write(f"Total Shares: {pos['total_shares']}") if direction == "Buy":
st.write(f"Average Entry: ${pos['avg_entry_price']:.2f}") if existing_positions:
st.write(f"Existing {ticker} Positions:")
if direction == "Sell": for pos in existing_positions:
position_id = st.selectbox( st.write(f"Position ID: {pos['position_id']}")
"Select Position to Exit", st.write(f"Total Shares: {pos['total_shares']}")
options=[pos['position_id'] for pos in existing_positions], st.write(f"Average Entry: ${pos['avg_entry_price']:.2f}")
key="position_select"
)
else: # Buy
add_to_existing = st.checkbox("Add to existing position") add_to_existing = st.checkbox("Add to existing position")
if add_to_existing: if add_to_existing:
position_id = st.selectbox( position_id = st.selectbox(
@ -249,11 +247,17 @@ def trading_journal_page():
) )
else: else:
position_id = generate_position_id(ticker, entry_datetime) position_id = generate_position_id(ticker, entry_datetime)
else: else:
if direction == "Sell": position_id = generate_position_id(ticker, entry_datetime)
else: # Sell
if not existing_positions:
st.error("No existing positions found for this ticker") st.error("No existing positions found for this ticker")
st.stop() st.stop()
position_id = generate_position_id(ticker, entry_datetime) position_id = st.selectbox(
"Select Position to Exit",
options=[pos['position_id'] for pos in existing_positions],
key="position_select"
)
col1, col2 = st.columns(2) col1, col2 = st.columns(2)
with col1: with col1: