Risk Management is First-Class#

In QuantScript, risk management is not a library you bolt on or a convention you hope your strategy follows. It is a platform-level enforcement layer: a strategy may request a trade, but it does not have unconditional authority to create broker exposure.

Every trade request — in backtest, forwardtest, and live modes — passes through RiskGuard, which evaluates it against platform policy, broker constraints, account condition, deployment limits, and strategy-level controls before it can reach the broker (or the simulated engine).

Strategy intent is always subordinate to account safety.

The risk() Declaration#

Strategies declare risk limits as a top-level declaration, alongside data and broker configuration:

QuantScript
risk(
  max_position_pct: 25,
  daily_loss_limit: 3
);

Because limits are declared explicitly, the runtime can validate requirements before execution begins — and any observer (human or AI) can read a script and know exactly what risk envelope it operates within.

Four Control Categories#

RiskGuard organises controls into four categories, mirrored by the risk.* DSL sub-namespaces:

  • risk.require.* — validates that the connected broker, account, instruments, and execution model are suitable for the strategy before trading begins.
  • risk.trade.* — validates each proposed order and position change before submission: direction limits, maximum order quantity, maximum loss per trade.
  • risk.portfolio.* — evaluates aggregate exposure: leverage, drawdown limits, daily loss limits, concentration, and account-wide risk across all open positions.
  • risk.protection.* — pauses or restricts trading in response to adverse conditions: repeated losses, stale data, broker failures, excessive order rates, or abnormal strategy behaviour. Includes the kill switch.
CategoryWhen it actsExamples
risk.require.*Pre-flightBroker supports hedging, instrument is tradeable, account type is suitable
risk.trade.*Per ordermaxOrderQty(), max loss per trade, direction restrictions
risk.portfolio.*ContinuousmaxDrawdown(), dailyLossLimit(), exposure and concentration caps
risk.protection.*ReactivemaxDailyOrders(), stale-data pause, killSwitch()

See the full API in the API Reference — risk.*.

Fail-Closed Principle#

Fail-Closed: When QuantScript cannot establish the true account state or risk exposure, it blocks new risk rather than assuming conditions are safe.

This principle governs edge cases that most trading frameworks leave undefined: broker disconnections mid-order, ambiguous fill responses, reconciliation gaps after restarts. In every case, the platform errs on the side of blocking new exposure until certainty is restored.

RiskGuard in Live Mode#

RiskGuard operates both before and after trade submission:

  • Pre-submission — every proposed order is checked against the full control stack. Rejected orders never reach the broker; the strategy receives a structured rejection event with the violated rule.
  • Post-update — after broker or account state changes (fills, price moves, margin calls), RiskGuard re-evaluates whether trading may safely continue. Circuit breakers can pause or halt a strategy whose account condition has deteriorated.

On multi-strategy accounts, portfolio-level policies evaluate both per-strategy virtual exposure and account-wide aggregate exposure — so one strategy’s losses can trigger protections that account for the whole account’s condition.

Next Steps#