feat: Add core trading modules for risk management, backtesting, and execution algorithms, alongside a new ML transparency widget and related frontend dependencies.
Some checks failed
Documentation / build-docs (push) Has been cancelled
Tests / test (macos-latest, 3.11) (push) Has been cancelled
Tests / test (macos-latest, 3.12) (push) Has been cancelled
Tests / test (macos-latest, 3.13) (push) Has been cancelled
Tests / test (macos-latest, 3.14) (push) Has been cancelled
Tests / test (ubuntu-latest, 3.11) (push) Has been cancelled
Tests / test (ubuntu-latest, 3.12) (push) Has been cancelled
Tests / test (ubuntu-latest, 3.13) (push) Has been cancelled
Tests / test (ubuntu-latest, 3.14) (push) Has been cancelled
Some checks failed
Documentation / build-docs (push) Has been cancelled
Tests / test (macos-latest, 3.11) (push) Has been cancelled
Tests / test (macos-latest, 3.12) (push) Has been cancelled
Tests / test (macos-latest, 3.13) (push) Has been cancelled
Tests / test (macos-latest, 3.14) (push) Has been cancelled
Tests / test (ubuntu-latest, 3.11) (push) Has been cancelled
Tests / test (ubuntu-latest, 3.12) (push) Has been cancelled
Tests / test (ubuntu-latest, 3.13) (push) Has been cancelled
Tests / test (ubuntu-latest, 3.14) (push) Has been cancelled
This commit is contained in:
@@ -1,5 +1,120 @@
|
||||
# Risk Management Architecture
|
||||
|
||||
## Overview
|
||||
|
||||
The risk management system provides comprehensive risk control mechanisms including position sizing, stop-loss management, drawdown limits, Value at Risk (VaR) calculation, and portfolio correlation analysis.
|
||||
|
||||
## Components
|
||||
|
||||
### Position Sizing
|
||||
|
||||
**Location**: `src/risk/position_sizing.py`
|
||||
|
||||
**Methods**:
|
||||
- **Standard Position Sizing**: Percentage-based with fee accounting
|
||||
- **Kelly Criterion**: Optimal position sizing with fractional Kelly (configurable)
|
||||
- **Volatility-Adjusted**: ATR-based position sizing (lower vol = larger positions)
|
||||
- **Regime-Aware**: Adjusts position size based on market regime
|
||||
- **Confidence-Based**: ML model confidence-adjusted position sizing
|
||||
|
||||
**Usage**:
|
||||
```python
|
||||
from src.risk.position_sizing import PositionSizingManager
|
||||
|
||||
sizer = PositionSizingManager()
|
||||
|
||||
# Standard sizing
|
||||
quantity = sizer.calculate_size(symbol, price, balance, risk_percent)
|
||||
|
||||
# Kelly Criterion (fractional)
|
||||
kelly_pct = sizer.calculate_kelly_criterion(win_rate=0.6, avg_win=100, avg_loss=50, fractional=0.25)
|
||||
|
||||
# Volatility-adjusted
|
||||
quantity = sizer.calculate_volatility_adjusted_size(symbol, price, balance, volatility_multiplier=0.8)
|
||||
|
||||
# Regime-aware
|
||||
quantity = sizer.calculate_regime_aware_size(symbol, price, balance, market_regime="trending_up")
|
||||
```
|
||||
|
||||
### Value at Risk (VaR)
|
||||
|
||||
**Location**: `src/risk/var_calculator.py`
|
||||
|
||||
**Methods**:
|
||||
1. **Historical VaR**: Uses historical portfolio returns distribution
|
||||
2. **Parametric VaR**: Assumes normal distribution (variance-covariance method)
|
||||
3. **Monte Carlo VaR**: Simulates future returns using estimated parameters
|
||||
4. **Conditional VaR (CVaR)**: Expected loss given that loss exceeds VaR
|
||||
|
||||
**Usage**:
|
||||
```python
|
||||
from src.risk.var_calculator import get_var_calculator
|
||||
|
||||
var_calc = get_var_calculator()
|
||||
|
||||
# Calculate all methods
|
||||
results = await var_calc.calculate_all_var_methods(
|
||||
portfolio_value=Decimal("10000.0"),
|
||||
confidence_level=0.95,
|
||||
holding_period_days=1
|
||||
)
|
||||
|
||||
# Individual methods
|
||||
historical_var = await var_calc.calculate_historical_var(...)
|
||||
parametric_var = await var_calc.calculate_parametric_var(...)
|
||||
monte_carlo_var = await var_calc.calculate_monte_carlo_var(...)
|
||||
cvar = await var_calc.calculate_cvar(...)
|
||||
```
|
||||
|
||||
### Portfolio Correlation Analysis
|
||||
|
||||
**Location**: `src/portfolio/correlation_analyzer.py`
|
||||
|
||||
**Features**:
|
||||
- Correlation matrix calculation for portfolio symbols
|
||||
- Diversification scoring (lower correlation = better)
|
||||
- Concentration risk analysis
|
||||
- Correlation-based position limits
|
||||
|
||||
**Usage**:
|
||||
```python
|
||||
from src.portfolio.correlation_analyzer import get_correlation_analyzer
|
||||
|
||||
analyzer = get_correlation_analyzer()
|
||||
|
||||
# Analyze current portfolio
|
||||
analysis = await analyzer.analyze_portfolio_correlation(paper_trading=True)
|
||||
|
||||
# Check correlation limits before adding position
|
||||
allowed, reason = await analyzer.check_correlation_limits(
|
||||
symbol="ETH/USD",
|
||||
new_position_value=Decimal("1000.0"),
|
||||
max_correlation=0.8
|
||||
)
|
||||
```
|
||||
|
||||
### Stop-Loss Management
|
||||
|
||||
**Location**: `src/risk/stop_loss.py`
|
||||
|
||||
Provides dynamic stop-loss adjustment and management.
|
||||
|
||||
### Risk Limits
|
||||
|
||||
**Location**: `src/risk/limits.py`
|
||||
|
||||
Manages:
|
||||
- Daily loss limits
|
||||
- Maximum drawdown limits
|
||||
- Portfolio allocation limits
|
||||
|
||||
### Risk Manager
|
||||
|
||||
**Location**: `src/risk/manager.py`
|
||||
|
||||
Orchestrates all risk management components and provides unified risk checking interface.
|
||||
|
||||
|
||||
This document describes the risk management system.
|
||||
|
||||
## Risk Management Components
|
||||
|
||||
Reference in New Issue
Block a user