Skip to content

Add position-sizing / risk-guard module#6

Open
bdevierno1 wants to merge 1 commit into
mainfrom
add-risk-guard-module
Open

Add position-sizing / risk-guard module#6
bdevierno1 wants to merge 1 commit into
mainfrom
add-risk-guard-module

Conversation

@bdevierno1

Copy link
Copy Markdown
Owner

What

Adds backend/app/services/risk_guard.py — a pure-Python, stateless module implementing the Vecna Trading Risk-Management Policy and Fractional-Kelly Position Sizing rules. No changes to existing files.

Guards implemented:

Guard Policy value
Per-trade risk cap 1 % default (range 1–3 %) of equity; size = risk_$ / (entry − stop)
Portfolio-heat limit Total open risk ≤ 6 % of equity
Drawdown circuit-breaker −15 % → halve sizes; −25 % → flatten all + halt
Fractional-Kelly sizing f* = W − (1−W)/R; default 0.25×, hard cap 0.5×, never 1×
Concentration cap Single name ≤ 20 % of equity
Correlation cap At most 2 simultaneous positions with ρ > 0.7

Final size = min(kelly_derived, risk-policy cap), further trimmed by heat / concentration guards.

Why

Encoding risk rules as executable, tested code makes them machine-enforceable and auditable instead of living only in policy documents. Every pre-trade check runs in microseconds with no I/O.

How to verify

cd backend
python -m pytest tests/test_risk_guard.py -v   # 32 tests, all green

32 tests cover: Kelly formula correctness, fractional-Kelly capping, each guard in isolation (NORMAL / HALVED / HALTED circuit-breaker, heat trim, heat hard block, concentration trim/block, correlation block), binding-rule selection, and risk_pct clamping.

Rollback

New file only — no existing code modified. Rollback = git revert or delete the file; zero impact on running application.

Out of scope

This module is stateless and has no persistence or API endpoint. Wiring it into the operation runner or exposing a /api/risk-check endpoint is a follow-on PR.

Diff size note

849 lines total: 350 lines implementation + 499 lines tests. Implementation alone is under the 400-line guideline; separating tests from implementation would harm reviewability. Tests carry minimal review burden — please focus review time on risk_guard.py.


🤖 Generated with Vecna

Co-Authored-By: Vecna noreply@vecna.ai

Implements the Vecna Trading Risk-Management Policy and Fractional-Kelly
Position Sizing rules in backend/app/services/risk_guard.py:

  - Per-trade risk cap: equity × risk_pct (default 1 %; max 3 %)
  - Portfolio-heat limit: total open risk ≤ 6 % of equity
  - Drawdown circuit-breaker: halve at −15 %, flatten+halt at −25 %
  - Fractional-Kelly sizing: quarter-Kelly default, half-Kelly hard cap
  - Single-name concentration cap: 20 % of equity per instrument
  - Correlation cap: at most 2 positions with ρ > 0.7 simultaneously

32 unit tests in backend/tests/test_risk_guard.py cover every guard,
edge cases, and the binding-rule selection logic (kelly vs risk_cap vs
heat_limit vs concentration).

Co-Authored-By: Vecna <noreply@vecna.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant