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
+12 -14
View File
@@ -24,8 +24,8 @@ class PortfolioDomainRepository(Protocol):
*,
broker: str = "kis",
status: str = "enabled",
strategy_name: str = "",
strategy_params: dict | None = None,
active_strategy_id: str | None = None,
avabile_strategies: list[str] | None = None,
initial_cash: float = 0.0,
) -> Portfolio: ...
def create_portfolio(
@@ -35,12 +35,12 @@ class PortfolioDomainRepository(Protocol):
broker: str,
status: str,
symbol: str,
strategy_name: str,
strategy_params: dict,
cash_balance: float,
asset_quantity: float,
asset_price: float,
profit_buffer: float,
active_strategy_id: str | None,
avabile_strategies: list[str] | None,
) -> Portfolio: ...
def update_portfolio(self, portfolio_id: str, **fields: object) -> Portfolio: ...
def list_portfolios(self, *, broker: str | None = None, status: str | None = None) -> list[Portfolio]: ...
@@ -100,13 +100,12 @@ def apply_buy(
broker=portfolio.broker,
status=portfolio.status,
symbol=portfolio.symbol,
strategy_name=portfolio.strategy_name,
strategy_params=portfolio.strategy_params,
cash_balance=float(portfolio.cash_balance) - total_cost,
asset_quantity=float(portfolio.asset_quantity) + quantity,
price=price,
profit_buffer=float(portfolio.profit_buffer),
current_strategy_set_id=portfolio.current_strategy_set_id,
active_strategy_id=portfolio.active_strategy_id,
avabile_strategies=portfolio.avabile_strategies,
created_at=portfolio.created_at,
updated_at=portfolio.updated_at,
last_trade_at=portfolio.last_trade_at,
@@ -121,7 +120,7 @@ def apply_buy(
fee_amount=fee,
tax_amount=tax,
total_value_after=total_value_after,
strategy_set_id=portfolio.current_strategy_set_id or "manual",
strategy_set_id=portfolio.active_strategy_id or "manual",
note=note,
)
return repo.get(portfolio_id)
@@ -161,13 +160,12 @@ def apply_sell(
broker=portfolio.broker,
status=portfolio.status,
symbol=portfolio.symbol,
strategy_name=portfolio.strategy_name,
strategy_params=portfolio.strategy_params,
cash_balance=float(portfolio.cash_balance) + net_proceeds,
asset_quantity=float(portfolio.asset_quantity) - quantity,
price=price,
profit_buffer=float(portfolio.profit_buffer),
current_strategy_set_id=portfolio.current_strategy_set_id,
active_strategy_id=portfolio.active_strategy_id,
avabile_strategies=portfolio.avabile_strategies,
created_at=portfolio.created_at,
updated_at=portfolio.updated_at,
last_trade_at=portfolio.last_trade_at,
@@ -182,7 +180,7 @@ def apply_sell(
fee_amount=fee,
tax_amount=tax,
total_value_after=total_value_after,
strategy_set_id=portfolio.current_strategy_set_id or "manual",
strategy_set_id=portfolio.active_strategy_id or "manual",
note=note,
)
return repo.get(portfolio_id)
@@ -265,8 +263,8 @@ def get_portfolio_status(
portfolio = repo.get(portfolio_id)
recent_entries = repo.list_recent_trades(portfolio_id, limit=recent_trades)
strategy_set = None
if portfolio.current_strategy_set_id:
strategy_set = repo.get_strategy_set(portfolio.current_strategy_set_id)
if portfolio.active_strategy_id:
strategy_set = repo.get_strategy_set(portfolio.active_strategy_id)
live_quantity = None
if live_source is not None: