feat: Enhance trade entry form to support buy and sell order workflows

This commit is contained in:
Bobby (aider) 2025-02-11 07:02:51 -08:00
parent c238bf45ed
commit b9116a55a2

View File

@ -166,34 +166,53 @@ def trading_journal_page():
st.write(f"Total Shares: {pos['total_shares']}") st.write(f"Total Shares: {pos['total_shares']}")
st.write(f"Average Entry: ${pos['avg_entry_price']:.2f}") st.write(f"Average Entry: ${pos['avg_entry_price']:.2f}")
add_to_existing = st.checkbox("Add to existing position") if direction == "Sell":
if add_to_existing:
position_id = st.selectbox( position_id = st.selectbox(
"Select Position ID", "Select Position to Exit",
options=[pos['position_id'] for pos in existing_positions], options=[pos['position_id'] for pos in existing_positions],
key="position_select" key="position_select"
) )
else: else: # Buy
position_id = generate_position_id(ticker) add_to_existing = st.checkbox("Add to existing position")
if add_to_existing:
position_id = st.selectbox(
"Select Position ID",
options=[pos['position_id'] for pos in existing_positions],
key="position_select"
)
else:
position_id = generate_position_id(ticker)
else: else:
if direction == "Sell":
st.error("No existing positions found for this ticker")
st.stop()
position_id = generate_position_id(ticker) position_id = generate_position_id(ticker)
col1, col2 = st.columns(2) col1, col2 = st.columns(2)
with col1: with col1:
shares = st.number_input("Number of Shares", min_value=1, step=1) shares = st.number_input("Number of Shares", min_value=1, step=1)
entry_price = st.number_input("Entry Price", min_value=0.01, step=0.01) if direction == "Buy":
target_price = st.number_input("Target Price", min_value=0.01, step=0.01) entry_price = st.number_input("Entry Price", min_value=0.01, step=0.01)
else:
entry_price = st.number_input("Exit Price", min_value=0.01, step=0.01)
with col2: with col2:
stop_loss = st.number_input("Stop Loss", min_value=0.01, step=0.01) if direction == "Buy":
strategy = st.text_input("Strategy") target_price = st.number_input("Target Price", min_value=0.01, step=0.01)
stop_loss = st.number_input("Stop Loss", min_value=0.01, step=0.01)
strategy = st.text_input("Strategy")
else:
exit_reason = st.text_area("Exit Reason", key="exit_reason")
order_type = st.selectbox("Order Type", ["Market", "Limit"], key="add_trade_order_type") order_type = st.selectbox("Order Type", ["Market", "Limit"], key="add_trade_order_type")
entry_date = st.date_input("Entry Date") entry_date = st.date_input("Entry Date")
entry_time = st.time_input("Entry Time") entry_time = st.time_input("Entry Time")
followed_rules = st.checkbox("Followed Trading Rules") if direction == "Buy":
entry_reason = st.text_area("Entry Reason", key="add_trade_reason") followed_rules = st.checkbox("Followed Trading Rules")
entry_reason = st.text_area("Entry Reason", key="add_trade_reason")
notes = st.text_area("Notes", key="add_trade_notes") notes = st.text_area("Notes", key="add_trade_notes")
if st.button("Add Trade"): if st.button("Add Trade"):
@ -206,15 +225,16 @@ def trading_journal_page():
entry_date=entry_datetime, entry_date=entry_datetime,
shares=shares, shares=shares,
entry_price=entry_price, entry_price=entry_price,
target_price=target_price, target_price=target_price if direction == "Buy" else None,
stop_loss=stop_loss, stop_loss=stop_loss if direction == "Buy" else None,
strategy=strategy, strategy=strategy if direction == "Buy" else None,
order_type=order_type, order_type=order_type,
position_id=position_id, position_id=position_id,
followed_rules=followed_rules, followed_rules=followed_rules if direction == "Buy" else None,
entry_reason=entry_reason, entry_reason=entry_reason if direction == "Buy" else None,
exit_reason=exit_reason if direction == "Sell" else None,
notes=notes, notes=notes,
direction=direction.lower() # Add direction parameter direction=direction.lower()
) )
add_trade(trade) add_trade(trade)