Quantitative Risk Modeling
Quantitative Risk Modeling
Signal.Engine integrates a quantitative risk layer that sits between the Deep Learning Brain and the Execution Engine. Instead of relying solely on directional predictions, the system evaluates every signal through a statistical lens to ensure capital preservation and mathematical expectancy.
Overview
The risk module (internally referred to as the QuantExpert) calculates the statistical viability of a trade. It transforms a categorical prediction (BUY/SELL/HOLD) into a risk-adjusted decision by calculating Expected Value (EV) and Value at Risk (VaR) based on current market volatility.
Core Risk Metrics
The system exposes a QuantRisk object for every asset scanned. These metrics are used by the SimulationEngine and the live trader_alpaca.py script to determine if a trade meets the minimum safety threshold.
| Metric | Type | Description |
| :--- | :--- | :--- |
| WinRate | float | The historical probability of success for the current pattern (0.0 - 1.0). |
| EV | float | Expected Value: The mean projected return per trade, accounting for both win probability and loss magnitude. |
| VaR95 | float | Value at Risk (95%): The maximum expected loss over a specific time horizon at a 95% confidence level. |
| MaxDrawdown | float | The peak-to-trough decline observed in the current strategy regime. |
Market Regime Adaptation
The risk model is dynamic and adjusts its sensitivity based on the detected Market Regime. This logic is handled during the brain's "thinking" phase:
- CALM Regime: Standard VaR thresholds. The agent prioritizes trend-following and higher position sizing.
- VOLATILE Regime: Aggressive VaR expansion. The agent triggers "High Volatility" rationales, increases the distance of Stop Losses, and reduces individual trade quantity.
# Internal Logic Example: Regime Detection
vol_count = sum(1 for d in decisions if "High Volatility" in d.get('Rational', []))
if vol_count > len(decisions) * 0.3:
current_regime = "HIGH_VOLATILITY"
API Usage & Data Structure
The risk metrics are accessible via the /api/results endpoint. Frontend components use this data to color-code signal confidence and block high-risk executions.
Request
GET /api/results
Response Interface (TypeScript)
interface Decision {
Ticker: string;
Action: string;
Confidence: number;
Rational: string[];
QuantRisk: {
WinRate: number;
EV: number; // Positive EV is required for 'BUY' signals
VaR95: number; // Used to calculate Stop-Loss levels
MaxDrawdown: number;
};
}
Sample JSON Output
{
"Ticker": "RELIANCE.NS",
"Action": "BUY",
"Confidence": 0.88,
"QuantRisk": {
"WinRate": 0.64,
"EV": 1.25,
"VaR95": -2.10,
"MaxDrawdown": 5.4
},
"Rational": ["Sequence Trend Confirm", "Low Volatility Regime"]
}
Implementation Guidelines for Traders
When deploying the agent via src.trader_alpaca, the quantitative layer acts as a gatekeeper:
- EV Filtering: If
EV <= 0, the agent will default to aHOLDorNEUTRALposition, even if the LSTM model predicts an upward move. - VaR-Based Sizing: Position sizes are inversely proportional to
VaR95. If the potential 95% loss exceeds the account's maximum risk-per-trade (set in.env), the trade quantity is automatically scaled down. - Stop-Loss Alignment: The system automatically places Alpaca
stop_lossorders at the price point corresponding to theVaR95level to ensure statistical exits.
Configuration
Risk parameters (such as maximum drawdown limits and base quantity) can be configured via environment variables or CLI flags during deployment:
# Example: Deploy with a specific quantity, subject to QuantRisk scaling
python -m src.trader_alpaca --symbol TCS.NS --qty 10 --live