Skip to content

fix(trading): include fees in cash risk checks - #4

Open
ECD5A wants to merge 1 commit into
BlockRunAI:mainfrom
ECD5A:fix/fee-aware-cash-risk
Open

fix(trading): include fees in cash risk checks#4
ECD5A wants to merge 1 commit into
BlockRunAI:mainfrom
ECD5A:fix/fee-aware-cash-risk

Conversation

@ECD5A

@ECD5A ECD5A commented Jul 26, 2026

Copy link
Copy Markdown

Summary

  • add a pre-trade fee estimate to the ExchangeClient contract
  • use the same fee calculation for risk checks and simulated fills
  • reject buys when notional plus the estimated fee exceeds available cash
  • add regression coverage at both the risk-engine and trading-engine boundaries

Receipt

Before this change, a portfolio with $100.00 cash could buy 0.001 BTC @ $100,000 with a 10 bps fee. The risk check compared only the $100.00 notional to cash and allowed the order; the fill then debited $100.10, leaving cash at -$0.10.

The two new regression tests reproduced that failure before the implementation change: the direct risk decision was allowed: true, and the engine outcome was filled. Both now block the order before it reaches the exchange.

Design alignment

This advances Conviction 5: the deterministic pre-trade guard is the last line of defense. It also protects the cash/NAV accounting invariant described in ADR 0005.

Tests

  • npm run build
  • node --test --test-reporter=spec --test-name-pattern=RiskEngine|TradingEngine|MockExchange|LiveExchange test/local.mjs — 14 passed
  • git diff --check

A full local npm test run reaches the relevant trading suite successfully but has unrelated existing failures in Windows HOME/temp-directory isolation tests.

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