Files
crypto_trader/docs/architecture/risk_management.md

166 lines
3.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Risk Management Architecture
This document describes the risk management system.
## Risk Management Components
```
Risk Manager
├──► Pre-Trade Checks
│ │
│ ├──► Position Sizing
│ ├──► Daily Loss Limit
│ └──► Portfolio Allocation
├──► Real-Time Monitoring
│ │
│ ├──► Drawdown Monitoring
│ ├──► Position Monitoring
│ └──► Portfolio Monitoring
└──► Stop Loss Management
├──► Stop Loss Orders
└──► Trailing Stops
```
## Pre-Trade Risk Checks
Before executing any trade:
1. **Position Sizing Check**
- Verify position size within limits
- Check portfolio allocation
- Validate against risk parameters
2. **Daily Loss Limit Check**
- Calculate current daily P&L
- Compare against daily loss limit
- Block trades if limit exceeded
3. **Drawdown Check**
- Calculate current drawdown
- Compare against max drawdown limit
- Block trades if limit exceeded
4. **Portfolio Allocation Check**
- Verify total exposure within limits
- Check per-asset allocation
- Validate diversification requirements
## Position Sizing Methods
### Fixed Percentage
```python
position_size = capital * percentage
```
### Kelly Criterion
```python
f = (bp - q) / b
position_size = capital * f
```
### Volatility-Based
```python
position_size = (capital * risk_percentage) / (stop_loss_distance * price)
```
## Risk Limits
Configurable limits:
- **Max Drawdown**: Maximum allowed drawdown percentage
- **Daily Loss Limit**: Maximum daily loss percentage
- **Position Size Limit**: Maximum position value
- **Portfolio Exposure**: Maximum portfolio exposure percentage
## Stop Loss Management
### Stop Loss Types
- **Fixed Stop Loss**: Fixed price level
- **Trailing Stop**: Adjusts with price movement
- Percentage-based: Adjusts by fixed percentage
- ATR-based: Adjusts based on volatility (Average True Range)
- **Percentage Stop**: Percentage below entry
- **ATR-based Stop**: Based on Average True Range (volatility-adjusted)
- Automatically calculates stop distance using ATR multiplier
- Adapts to market volatility conditions
- Configurable ATR period (default: 14) and multiplier (default: 2.0)
- Works with both fixed and trailing stops
### ATR-Based Dynamic Stops
ATR-based stops provide better risk management in volatile markets:
```python
stop_loss_manager.set_stop_loss(
position_id=1,
stop_price=entry_price,
use_atr=True,
atr_multiplier=Decimal('2.0'),
atr_period=14,
ohlcv_data=market_data,
trailing=True
)
```
**Benefits:**
- Adapts to market volatility
- Tighter stops in low volatility, wider in high volatility
- Reduces stop-outs during normal market noise
- Better risk-adjusted returns
**Calculation:**
- Stop distance = ATR × multiplier
- For long positions: stop_price = entry_price - (ATR × multiplier)
- For short positions: stop_price = entry_price + (ATR × multiplier)
### Stop Loss Execution
```
Price Update
Stop Loss Check
├──► Stop Loss Triggered?
│ │
│ └──► Execute Market Sell
└──► Update Trailing Stop (if applicable)
```
## Real-Time Monitoring
Continuous monitoring of:
- Portfolio value
- Unrealized P&L
- Drawdown levels
- Position sizes
- Risk metrics
## Risk Alerts
Automatic alerts for:
- Drawdown threshold exceeded
- Daily loss limit reached
- Position size exceeded
- Portfolio exposure exceeded
## Integration Points
Risk management integrates with:
- **Trading Engine**: Pre-trade validation
- **Order Manager**: Position tracking
- **Portfolio Tracker**: Real-time monitoring
- **Alert System**: Risk alerts