feat: add strategy set CRUD operations (API + CLI)

This commit is contained in:
2026-04-03 08:37:04 +00:00
parent 908068d9aa
commit 1b0991c9cf
12 changed files with 510 additions and 5 deletions
+110 -4
View File
@@ -255,9 +255,115 @@ Response 204:
- No content.
## 5. Common Error Examples
## 5. Strategy Sets
### 5.1 Missing token
### 5.1 GET /strategy-sets
Request:
```bash
curl -X GET "http://localhost:8000/strategy-sets" \
-H "Authorization: Bearer <READ_TOKEN>"
```
Response 200:
```json
[
{
"id": "set-1",
"name": "target_value",
"config": "{\"target\": 1000}",
"nick": "target_value:abc123def456",
"created_at": "2026-04-03T08:13:08.318Z"
},
{
"id": "set-2",
"name": "momentum",
"config": "{\"window\": 20}",
"nick": "momentum:xyz789uvw012",
"created_at": "2026-04-03T08:14:15.450Z"
}
]
```
### 5.2 POST /strategy-sets
Request:
```bash
curl -X POST "http://localhost:8000/strategy-sets" \
-H "Authorization: Bearer <TRADE_WRITE_TOKEN>" \
-H "Content-Type: application/json" \
-d '{
"name": "target_value",
"config": "{\"target\": 1000}"
}'
```
Response 201:
```json
{
"id": "set-1",
"name": "target_value",
"config": "{\"target\": 1000}",
"nick": "target_value:abc123def456",
"created_at": "2026-04-03T08:13:08.318Z"
}
```
### 5.3 PATCH /strategy-sets/{strategy_set_id}
Request:
```bash
curl -X PATCH "http://localhost:8000/strategy-sets/set-1" \
-H "Authorization: Bearer <TRADE_WRITE_TOKEN>" \
-H "Content-Type: application/json" \
-d '{
"config": "{\"target\": 2000}"
}'
```
Response 200:
```json
{
"id": "set-1-updated",
"name": "target_value",
"config": "{\"target\": 2000}",
"nick": "target_value:def789ghi012",
"created_at": "2026-04-03T08:13:08.318Z"
}
```
Note: Since strategy sets are identified by (name, config) uniqueness, updating may result in a new strategy set ID.
### 5.4 DELETE /strategy-sets/{strategy_set_id}
Request:
```bash
curl -X DELETE "http://localhost:8000/strategy-sets/set-1" \
-H "Authorization: Bearer <TRADE_WRITE_TOKEN>"
```
Response 204:
- No content on success.
Response 409 Conflict (if strategy set is in use):
```json
{
"detail": "Strategy set is in use by 1 portfolio(s) and cannot be deleted"
}
```
## 6. Common Error Examples
### 6.1 Missing token
Response 401:
@@ -267,7 +373,7 @@ Response 401:
}
```
### 5.2 Invalid token
### 6.2 Invalid token
Response 401:
@@ -277,7 +383,7 @@ Response 401:
}
```
### 5.3 Insufficient permission
### 6.3 Insufficient permission
Response 403:
+51 -1
View File
@@ -208,7 +208,40 @@ Base routes:
- `keep_ledger` (bool, default `false`)
- Response `204`: no content
### 7.3 Example Requests and Responses
### 7.3 Strategy Set Endpoints
#### List Strategy Sets
- `GET /strategy-sets`
- Auth: `read`
- Response `200`: array of `StrategySetResponse`
#### Create Strategy Set
- `POST /strategy-sets`
- Auth: `trade-write`
- Body: `CreateStrategySetRequest`
- Response `201`: `StrategySetResponse`
Note: Uses `get_or_create_strategy_set` logic, so if (name, config) pair already exists, returns existing strategy set.
#### Update Strategy Set
- `PATCH /strategy-sets/{strategy_set_id}`
- Auth: `trade-write`
- Body: `UpdateStrategySetRequest`
- Response `200`: `StrategySetResponse`
Note: Since strategy sets are identified by (name, config) uniqueness, updating may result in a new strategy set ID if name or config changes.
#### Delete Strategy Set
- `DELETE /strategy-sets/{strategy_set_id}`
- Auth: `trade-write`
- Response `204`: no content
- Response `409`: Conflict if strategy set is in use by any portfolio
### 7.4 Example Requests and Responses
- See `docs/api-examples.md` for maintained request/response examples.
@@ -266,6 +299,16 @@ Base routes:
- `infer_symbol: str | null`
`CreateStrategySetRequest`
- `name: str`
- `config: str`
`UpdateStrategySetRequest` (all fields optional)
- `name: str | null`
- `config: str | null`
### 8.2 Response Models
`PortfolioListItem`
@@ -336,6 +379,13 @@ Portfolio subcommands:
- `portfoliohub portfolio reset <portfolio_id> [--infer-symbol ...] [--force]`
- `portfoliohub portfolio delete <portfolio_id> [--keep-ledger] [--force]`
Strategy subcommands:
- `portfoliohub strategy list`
- `portfoliohub strategy create --name ... --config ...`
- `portfoliohub strategy update <strategy_set_id> [--name ...] [--config ...]`
- `portfoliohub strategy delete <strategy_set_id> [--force]`
Confirmation behavior:
- Mutating portfolio commands request confirmation by default.