# AI Sell Protective Stop Cancellation Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Allow AI SELL execution to cancel this bot's own protective `STOP_LOSS_LIMIT` sell order before submitting the market sell.

**Architecture:** Keep user/manual sell orders protective by default. Classify only `SELL` + `STOP_LOSS_LIMIT` orders whose `clientOrderId` starts with `bqab-stop-` as bot protective stops. When those are the only open sell orders, cancel them, refresh account balances, recalculate quantity, then run the existing sell risk and execution path.

**Tech Stack:** Python, Typer CLI, Binance Spot REST client, pytest.

---

### Task 1: Regression Tests

**Files:**
- Modify: `tests/test_ai_trader.py`
- Modify: `tests/test_exchange.py`

- [ ] **Step 1: Add an AI sell regression test**

Add a fake exchange whose initial account has locked base balance and whose open orders contain one `bqab-stop-` stop-loss sell. The test should assert AI SELL cancels that order, refreshes the account, and submits the market sell.

- [ ] **Step 2: Add a non-protective sell order guard test**

Add a test that an open manual `SELL LIMIT` order still rejects AI SELL with `已有卖出挂单（当前 1）`.

- [ ] **Step 3: Add Binance client cancel test**

Add a test that `BinanceSpotClient.cancel_order()` sends `DELETE /v3/order` with `symbol` and `origClientOrderId`.

### Task 2: Minimal Implementation

**Files:**
- Modify: `src/binance_quant/exchange.py`
- Modify: `src/binance_quant/ai_trader.py`

- [ ] **Step 1: Add `cancel_order` to `BinanceSpotClient`**

Expose `DELETE /v3/order` using `symbol` and `origClientOrderId`.

- [ ] **Step 2: Classify protective stop orders**

Add helpers in `ai_trader.py` to identify bot-owned protective stop sell orders and non-protective open sell orders.

- [ ] **Step 3: Cancel protective stops before AI SELL**

In `_execute_ai_sell`, if there are protective stops and no other open sell orders, cancel each protective stop, refresh account, recalculate quantity, and evaluate risk with the adjusted open-order view.

### Task 3: Verification

**Files:**
- Test: `tests/test_ai_trader.py`
- Test: `tests/test_exchange.py`

- [ ] **Step 1: Run focused tests**

Run `uv run pytest tests/test_ai_trader.py tests/test_exchange.py -q`.

- [ ] **Step 2: Run full tests**

Run `uv run pytest -q`.
