Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion python/valuecell/server/api/routers/strategy_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ async def delete_strategy_agent(
"""Delete a strategy created by StrategyAgent.

- Validates the strategy exists.
- Ensures the strategy is stopped before deletion (idempotent stop).
- Optionally cascades deletion to holdings, portfolio snapshots, and details.
- Returns a success response when completed.
"""
Expand All @@ -300,13 +301,22 @@ async def delete_strategy_agent(
if not strategy:
raise HTTPException(status_code=404, detail="Strategy not found")

# Stop strategy before deletion (best-effort, idempotent)
try:
current_status = getattr(strategy, "status", None)
if current_status != "stopped":
repo.upsert_strategy(strategy_id=id, status="stopped")
except Exception:
# Do not fail deletion due to stop failure; proceed to deletion
pass

ok = repo.delete_strategy(id, cascade=cascade)
if not ok:
raise HTTPException(status_code=500, detail="Failed to delete strategy")

return SuccessResponse.create(
data={"strategy_id": id},
msg=f"Strategy '{id}' deleted successfully",
msg=f"Strategy '{id}' stopped (if running) and deleted successfully",
)
except HTTPException:
raise
Expand Down