Skip to main content

Overview

The RiskConfig struct defines risk management rules and limits for both backtesting and live trading. It controls position sizes, drawdown limits, daily loss limits, and kill switch behavior. Source: nano-backtest/src/config.rs (used by both backtest and live trading)

RiskConfig

Fields

Constructor

default

Creates a default risk configuration with conservative limits.
Example:

Field Details

max_position

Maximum net position size per instrument. Units: Contracts (signed integer) Range: Position can be from -max_position to +max_position Enforcement: Checked before every order submission Example:
Typical Values:
  • Retail/Small: 10-20 contracts
  • Medium: 50-100 contracts
  • Large: 100-500 contracts
  • HFT: 500+ contracts

max_order_size

Maximum size for any single order. Units: Contracts (unsigned) Purpose:
  • Prevents accidental “fat finger” orders
  • Limits market impact per order
  • Forces position building through multiple orders
Constraint: Should be <= max_position Example:
Typical Values:
  • Conservative: 5-10 contracts
  • Moderate: 10-20 contracts
  • Aggressive: 20-50 contracts

max_drawdown_pct

Maximum allowed drawdown as a percentage of peak P&L. Units: Percentage (0.05 = 5%) Measurement: (peak_pnl - current_pnl) / peak_pnl Behavior: When exceeded, kill switch activates (if enabled) Example:
Typical Values:
  • Very Conservative: 2-3%
  • Conservative: 3-5%
  • Moderate: 5-10%
  • Aggressive: 10-20%
Important: Lower values provide better capital protection but may stop strategies prematurely during normal volatility.

max_daily_loss

Maximum allowed loss in a single trading day. Units: Dollars (absolute value) Measurement: daily_start_pnl - current_pnl Reset: Automatically resets at start of each trading day Behavior: When exceeded, kill switch activates (if enabled) Example:
Typical Values (as % of capital):
  • Very Conservative: 1-2% of capital
  • Conservative: 2-5% of capital
  • Moderate: 5-10% of capital
  • Aggressive: 10-20% of capital
Example for $1M capital:
  • Conservative: 10,00010,000 - 50,000
  • Moderate: 50,00050,000 - 100,000
  • Aggressive: $100,000+

max_open_orders

Maximum number of orders that can be open simultaneously. Units: Count of orders Purpose:
  • Limits exposure to order management risk
  • Prevents runaway order submission
  • Controls system load
Enforcement: Checked before submitting new orders Example:
Typical Values:
  • Low frequency: 5-10 orders
  • Medium frequency: 10-20 orders
  • High frequency: 20-100+ orders

enable_kill_switch

Enables automatic trading halt when risk limits are breached. Values:
  • true (default): Automatically stops trading on breach
  • false: Only logs warnings, continues trading
Activates when:
  • Drawdown exceeds max_drawdown_pct
  • Daily loss exceeds max_daily_loss
Behavior when activated:
  • All new orders are rejected
  • Existing orders may be cancelled (depending on implementation)
  • Manual reset required to resume trading
Example:
Recommendation: Always enable for live trading

Configuration Presets

Conservative

For risk-averse traders or initial live trading.

Moderate

Balanced risk/reward profile.

Aggressive

For experienced traders with higher risk tolerance.

HFT

Optimized for high-frequency strategies.

Backtesting (Relaxed)

More lenient for strategy research.

RiskManager Usage

The RiskManager enforces these limits during trading:

Validation

Validate configuration before use:

Complete Example

Best Practices

  1. Start Conservative: Begin with tight limits and loosen gradually
  2. Scale with Capital: Set daily loss as percentage of capital (2-5%)
  3. Always Enable Kill Switch: Especially for live trading
  4. Test Thoroughly: Run backtests with different risk parameters
  5. Monitor Regularly: Track how often limits are hit
  6. Adjust for Volatility: Tighten limits during high volatility
  7. Document Changes: Log all risk parameter modifications
  8. Review After Breaches: Analyze what caused kill switch activation

See Also