feat: refactor portfolio strategy references

This commit is contained in:
2026-04-03 08:16:07 +00:00
parent ebd4559939
commit 908068d9aa
14 changed files with 294 additions and 139 deletions
+6 -6
View File
@@ -51,13 +51,12 @@ Response 200:
"broker": "upbit",
"status": "enabled",
"symbol": "KRW-BTC",
"strategy_name": "target_value",
"strategy_params": {},
"cash_balance": 1000000.0,
"asset_quantity": 0.1,
"price": 100000000.0,
"profit_buffer": 0.0,
"current_strategy_set_id": "stv-001",
"active_strategy_id": "stv-001",
"avabile_strategies": ["stv-001"],
"created_at": "2026-04-03T00:00:00Z",
"updated_at": "2026-04-03T00:00:00Z",
"last_trade_at": "2026-04-03T00:00:00Z",
@@ -84,13 +83,12 @@ Response 200:
"broker": "upbit",
"status": "enabled",
"symbol": "KRW-BTC",
"strategy_name": "target_value",
"strategy_params": {},
"cash_balance": 1000000.0,
"asset_quantity": 0.1,
"price": 100000000.0,
"profit_buffer": 0.0,
"current_strategy_set_id": "stv-001",
"active_strategy_id": "stv-001",
"avabile_strategies": ["stv-001"],
"created_at": "2026-04-03T00:00:00Z",
"updated_at": "2026-04-03T00:00:00Z",
"last_trade_at": "2026-04-03T00:00:00Z"
@@ -138,6 +136,8 @@ curl -X POST "http://localhost:8000/portfolios" \
"symbol": "KRW-BTC",
"strategy_name": "target_value",
"strategy_params": {},
"active_strategy_id": null,
"avabile_strategies": null,
"cash_balance": 1000000,
"asset_quantity": 0,
"asset_price": 0,
+14 -4
View File
@@ -5,10 +5,11 @@
This document defines the database schema in an AI-agent-friendly format.
- Source of truth: Alembic revisions in `migrations/versions`
- Current baseline revision: `20260403_0002`
- Current baseline revision: `20260403_0003`
- Baseline files:
- `migrations/versions/20260403_0001_initial_portfolio_schema.py`
- `migrations/versions/20260403_0002_strategy_set_columns.py`
- `migrations/versions/20260403_0003_portfolio_strategy_refs.py`
## 2. Scope
@@ -57,6 +58,10 @@ Purpose:
- Stores current portfolio state and strategy linkage.
Current baseline note:
- The columns below describe the currently migrated schema through revision `20260403_0003`.
Columns:
| Column | Type | Null | Default | Constraints / Notes |
@@ -65,13 +70,12 @@ Columns:
| broker | Text | NO | "kis" | Broker code |
| status | Text | NO | "enabled" | Portfolio status |
| symbol | Text | NO | - | Trading symbol |
| strategy_name | Text | NO | "" | Strategy label |
| strategy_params | JSONB | NO | '{}'::jsonb | Strategy parameters |
| cash_balance | Float | NO | - | Current cash balance |
| asset_quantity | Float | NO | - | Current asset quantity |
| asset_price | Float | NO | 0 | Current price snapshot |
| profit_buffer | Float | NO | 0 | Profit buffer amount |
| current_strategy_set_id | Text | YES | - | Logical reference to `strategy_sets.id` (no FK) |
| active_strategy_id | Text | YES | - | Logical reference to `strategy_sets.id` |
| avabile_strategies | Text[] | YES | - | Nullable array of `strategy_sets.id` values |
| created_at | DateTime(timezone=True) | NO | NOW() | Creation timestamp |
| updated_at | DateTime(timezone=True) | NO | NOW() | Last update timestamp |
| last_trade_at | DateTime(timezone=True) | YES | - | Last trade time |
@@ -81,6 +85,11 @@ Constraints:
- Primary key: `portfolio_id`
- Foreign key constraints: none in baseline
Constraint notes:
- `active_strategy_id` is a nullable logical reference to `strategy_sets.id`
- `avabile_strategies` stores zero or more selectable strategy set IDs when present
### 4.3 portfolio_ledger
Purpose:
@@ -160,3 +169,4 @@ When schema changes:
|---|---|---|
| 20260403_0001 | migrations/versions/20260403_0001_initial_portfolio_schema.py | Initial schema for strategy sets, portfolios, ledger, indexes, optional hypertable |
| 20260403_0002 | migrations/versions/20260403_0002_strategy_set_columns.py | Rename `strategy_name`->`name`, `strategy_config`->`config`, add unique `nick` |
| 20260403_0003 | migrations/versions/20260403_0003_portfolio_strategy_refs.py | Remove embedded portfolio strategy fields, add `active_strategy_id` and `avabile_strategies` |
+20
View File
@@ -224,6 +224,8 @@ Base routes:
- `symbol: str`
- `strategy_name: str = "target_value"`
- `strategy_params: object = {}`
- `active_strategy_id: str | null`
- `avabile_strategies: string[] | null`
- `cash_balance: float >= 0`
- `asset_quantity: float >= 0`
- `asset_price: float >= 0`
@@ -236,6 +238,8 @@ Base routes:
- `symbol: str | null`
- `strategy_name: str | null`
- `strategy_params: object | null`
- `active_strategy_id: str | null`
- `avabile_strategies: string[] | null`
- `asset_price: float >= 0 | null`
`BuyRequest` / `SellRequest`
@@ -268,6 +272,22 @@ Base routes:
- Includes portfolio summary fields and computed `total_value`
`PortfolioSummary`
- `portfolio_id: str`
- `broker: str`
- `status: str`
- `symbol: str`
- `cash_balance: float`
- `asset_quantity: float`
- `price: float`
- `profit_buffer: float`
- `active_strategy_id: str | null`
- `avabile_strategies: string[] | null`
- `created_at: datetime | null`
- `updated_at: datetime | null`
- `last_trade_at: datetime | null`
`PortfolioDetailResponse`
- `portfolio: PortfolioSummary`