fix: risk engine now allows sells on over-concentrated positions

This commit is contained in:
Celes Renata
2026-04-21 20:25:02 +00:00
parent 5dcbd286e8
commit 3b49aa2fa2
2 changed files with 22 additions and 1 deletions
+4 -1
View File
@@ -298,7 +298,10 @@ def _check_max_position_size(
# Check max position value
existing_value = state.positions_by_symbol.get(order.ticker, 0.0)
new_total_value = existing_value + order.estimated_value
if order.action == "sell":
new_total_value = max(existing_value - order.estimated_value, 0.0)
else:
new_total_value = existing_value + order.estimated_value
checks.append(RiskCheckDetail(
check_name="max_position_value",
result=(
+18
View File
@@ -254,6 +254,24 @@ def test_position_pct_exceeded():
assert any(c.check_name == "max_position_pct" and c.result == RiskCheckResult.FAIL for c in result.checks)
def test_sell_on_over_limit_position_allowed():
"""Selling an over-concentrated position should pass risk checks."""
config = _make_config(position_limits=PositionLimits(max_position_pct=0.05))
state = _make_state(
portfolio_value=100_000,
positions_by_symbol={"AVGO": 5200.0}, # 5.2% — over the 5% limit
)
order = ProposedOrder(
ticker="AVGO", sector="Technology", action="sell",
estimated_value=5200.0, quantity=13,
)
result = evaluate_order(order, config, state)
pct_check = next(c for c in result.checks if c.check_name == "max_position_pct")
assert pct_check.result == RiskCheckResult.PASS, (
f"Sell on over-limit position should pass, got: {pct_check.message}"
)
def test_max_shares_exceeded():
config = _make_config(position_limits=PositionLimits(max_shares_per_order=100))
order = ProposedOrder(ticker="AAPL", sector="Technology", estimated_value=1000, quantity=200)