feat: Add delete trade functionality to journal menu

This commit is contained in:
Bobby (aider) 2025-02-10 18:53:24 -08:00
parent 5aa6d0effe
commit e89f673709

View File

@ -475,6 +475,28 @@ def get_current_prices(tickers: list) -> dict:
print(f"Error getting price for {ticker}: {e}")
return prices
def delete_trade(trade_id: int) -> bool:
"""
Delete a trade from the database
Args:
trade_id (int): ID of trade to delete
Returns:
bool: True if deletion was successful
"""
try:
with create_client() as client:
query = f"""
ALTER TABLE stock_db.trades
DELETE WHERE id = {trade_id}
"""
client.command(query)
return True
except Exception as e:
print(f"Error deleting trade: {e}")
return False
def get_trade_history(limit: int = 50):
with create_client() as client:
query = f"""
@ -499,7 +521,8 @@ def journal_menu():
print("2. Update Existing Trade")
print("3. View Open Trades")
print("4. View Trade History")
print("5. Return to Main Menu")
print("5. Delete Trade")
print("6. Return to Main Menu")
choice = input("\nSelect an option (1-5): ")
@ -830,4 +853,41 @@ def journal_menu():
print("-" * 40)
elif choice == "5":
# Show all trades (both open and closed)
print("\nAll Trades:")
with create_client() as client:
query = "SELECT * FROM stock_db.trades ORDER BY entry_date DESC"
result = client.query(query).result_rows
columns = ['id', 'position_id', 'ticker', 'entry_date', 'shares', 'entry_price',
'target_price', 'stop_loss', 'strategy', 'order_type', 'followed_rules',
'entry_reason', 'exit_price', 'exit_date', 'exit_reason', 'notes', 'created_at']
trades = [dict(zip(columns, row)) for row in result]
for trade in trades:
print(f"\nID: {trade['id']}")
print(f"Ticker: {trade['ticker']}")
print(f"Entry Date: {trade['entry_date']}")
print(f"Shares: {trade['shares']}")
print(f"Entry Price: ${trade['entry_price']}")
if trade['exit_price']:
print(f"Exit Price: ${trade['exit_price']}")
print(f"Exit Date: {trade['exit_date']}")
pl = (trade['exit_price'] - trade['entry_price']) * trade['shares']
print(f"P/L: ${pl:.2f}")
print("-" * 40)
trade_id = get_user_input("\nEnter trade ID to delete:", int)
if trade_id is None:
continue
confirm = input(f"Are you sure you want to delete trade {trade_id}? (y/n): ").lower()
if confirm == 'y':
if delete_trade(trade_id):
print("Trade deleted successfully!")
else:
print("Failed to delete trade.")
else:
print("Delete cancelled.")
elif choice == "6":
break