Overview
TheRiskConfig 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
| Field | Type | Default | Description |
|---|---|---|---|
max_position | i64 | 100 | Maximum net position (contracts) |
max_order_size | u32 | 20 | Maximum single order size |
max_drawdown_pct | f64 | 0.05 | Maximum drawdown (5%) before stop |
max_daily_loss | f64 | 100,000.0 | Maximum daily loss ($) before stop |
max_open_orders | usize | 20 | Maximum concurrent open orders |
enable_kill_switch | bool | true | Enable automatic kill switch |
Constructor
default
Creates a default risk configuration with conservative limits.
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:
- 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
max_position
Example:
- 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:
- Very Conservative: 2-3%
- Conservative: 3-5%
- Moderate: 5-10%
- Aggressive: 10-20%
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:
- Very Conservative: 1-2% of capital
- Conservative: 2-5% of capital
- Moderate: 5-10% of capital
- Aggressive: 10-20% of capital
- Conservative: 50,000
- Moderate: 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
- 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 breachfalse: Only logs warnings, continues trading
- Drawdown exceeds
max_drawdown_pct - Daily loss exceeds
max_daily_loss
- All new orders are rejected
- Existing orders may be cancelled (depending on implementation)
- Manual reset required to resume 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
TheRiskManager enforces these limits during trading:
Validation
Validate configuration before use:Complete Example
Best Practices
- Start Conservative: Begin with tight limits and loosen gradually
- Scale with Capital: Set daily loss as percentage of capital (2-5%)
- Always Enable Kill Switch: Especially for live trading
- Test Thoroughly: Run backtests with different risk parameters
- Monitor Regularly: Track how often limits are hit
- Adjust for Volatility: Tighten limits during high volatility
- Document Changes: Log all risk parameter modifications
- Review After Breaches: Analyze what caused kill switch activation
See Also
- RiskManager - Risk management implementation
- BacktestConfig - Complete backtest configuration
- TradingConfig - Trading parameters