ForexSmartBot includes a comprehensive risk management system designed to protect capital and manage trading risk effectively. This document explains the risk management features and how to configure them.
The risk management system consists of several layers:
- Position Sizing: Dynamic position sizing based on volatility and risk parameters
- Drawdown Control: Protection against excessive drawdowns
- Daily Risk Limits: Limits on daily losses
- Symbol-Specific Risk: Different risk parameters for different symbols
- Strategy Risk Multipliers: Adjustable risk for different strategies
from forexsmartbot.core.risk_engine import RiskConfig
config = RiskConfig(
base_risk_pct=0.02, # Base risk per trade (2%)
max_risk_pct=0.05, # Maximum risk per trade (5%)
daily_risk_cap=0.05, # Daily risk limit (5%)
max_drawdown_pct=0.25, # Maximum drawdown (25%)
min_trade_amount=10.0, # Minimum trade amount
max_trade_amount=100.0, # Maximum trade amount
)config = RiskConfig(
# ... basic parameters ...
# Symbol-specific risk multipliers
symbol_risk_multipliers={
'EURUSD': 1.0, # Standard risk
'GBPUSD': 1.2, # Higher risk (more volatile)
'USDJPY': 0.8, # Lower risk (less volatile)
},
# Strategy-specific risk multipliers
strategy_risk_multipliers={
'SMA_Crossover': 1.0, # Standard risk
'BreakoutATR': 1.5, # Higher risk (more aggressive)
'RSI_Reversion': 0.7, # Lower risk (more conservative)
},
# Kelly Criterion settings
kelly_fraction=0.25, # Conservative Kelly fraction
# Volatility targeting
volatility_target=0.01, # Target daily volatility (1%)
# Drawdown recovery
drawdown_recovery_pct=0.10, # Recovery threshold (10%)
)The base position size is calculated as:
position_size = balance * base_risk_pct * symbol_multiplier * strategy_multiplier
When volatility data is available, position size is adjusted to target a specific daily volatility:
vol_target_size = base_size * (target_volatility / current_volatility)
For strategies with known win rates, the Kelly Criterion is applied:
kelly_fraction = max(0, 2 * win_rate - 1)
kelly_size = balance * kelly_fraction * kelly_multiplier
The final position size is the minimum of:
- Base position size
- Volatility-targeted size
- Kelly-sized position
- Maximum trade amount
The system continuously monitors:
- Current equity vs. peak equity
- Rolling drawdown over recent trades
- Maximum historical drawdown
When drawdown exceeds the limit:
- Position sizes are reduced by 50%
- New trades may be restricted
- Recovery threshold must be reached to resume normal trading
The throttle is lifted when:
- Current drawdown falls below
max_drawdown_pct - drawdown_recovery_pct - This prevents rapid re-entry after drawdown
The system tracks:
- Realized PnL from completed trades
- Unrealized PnL from open positions
- Total daily PnL
When daily losses exceed the limit:
- New trades are blocked
- Existing positions may be closed
- Alert is generated
Risk limits reset at the start of each trading day.
Different symbols can have different risk profiles:
symbol_risk_multipliers = {
'EURUSD': 1.0, # Major pair - standard risk
'GBPUSD': 1.2, # More volatile - higher risk
'USDJPY': 0.8, # Less volatile - lower risk
'AUDUSD': 1.1, # Commodity currency - slightly higher risk
}Consider correlation between symbols:
- Highly correlated pairs increase portfolio risk
- Diversification reduces overall risk
- Monitor correlation in portfolio mode
Different strategies can have different risk profiles:
strategy_risk_multipliers = {
'SMA_Crossover': 1.0, # Trend following - standard risk
'BreakoutATR': 1.5, # Breakout - higher risk
'RSI_Reversion': 0.7, # Mean reversion - lower risk
}Risk multipliers can be adjusted based on:
- Historical performance
- Win rate
- Maximum drawdown
- Sharpe ratio
The system provides real-time risk metrics:
risk_summary = risk_engine.get_risk_summary()
print(f"Daily PnL: {risk_summary['daily_pnl']:.2f}")
print(f"Current Drawdown: {risk_summary['current_drawdown']:.2%}")
print(f"Drawdown Throttle: {risk_summary['drawdown_throttle']}")
print(f"Recent Win Rate: {risk_summary['recent_win_rate']:.2%}")The system generates alerts for:
- Drawdown limit exceeded
- Daily risk limit exceeded
- Unusual volatility
- Strategy performance degradation
In portfolio mode, risk is managed at the portfolio level:
# Total portfolio risk
total_risk = sum(symbol_risk for symbol_risk in symbol_risks)
# Correlation-adjusted risk
correlation_risk = total_risk * correlation_factor
# Portfolio drawdown
portfolio_drawdown = (peak_equity - current_equity) / peak_equityRisk is allocated across:
- Symbols (based on volatility and correlation)
- Strategies (based on performance and risk profile)
- Time (based on market conditions)
The system generates daily risk reports:
=== DAILY RISK REPORT ===
Date: 2023-12-01
Portfolio Value: $10,500.00
Daily PnL: -$150.00
Current Drawdown: 2.5%
Max Drawdown: 8.2%
Risk Status: NORMAL
Symbol Risk:
- EURUSD: $200.00 (1.9%)
- GBPUSD: $180.00 (1.7%)
- USDJPY: $120.00 (1.1%)
Strategy Risk:
- SMA_Crossover: $300.00 (2.9%)
- BreakoutATR: $200.00 (1.9%)
Key risk metrics:
- VaR (Value at Risk): Potential loss over time horizon
- Expected Shortfall: Expected loss beyond VaR
- Sharpe Ratio: Risk-adjusted return
- Maximum Drawdown: Largest peak-to-trough decline
- Calmar Ratio: Return / Maximum Drawdown
config = RiskConfig(
base_risk_pct=0.01, # 1% per trade
max_risk_pct=0.02, # 2% maximum
daily_risk_cap=0.03, # 3% daily limit
max_drawdown_pct=0.15, # 15% max drawdown
)config = RiskConfig(
base_risk_pct=0.02, # 2% per trade
max_risk_pct=0.05, # 5% maximum
daily_risk_cap=0.05, # 5% daily limit
max_drawdown_pct=0.20, # 20% max drawdown
)config = RiskConfig(
base_risk_pct=0.015, # 1.5% per trade
max_risk_pct=0.03, # 3% maximum
daily_risk_cap=0.04, # 4% daily limit
max_drawdown_pct=0.15, # 15% max drawdown
)- Begin with low risk parameters
- Gradually increase as you gain experience
- Monitor performance closely
- Trade multiple symbols
- Use different strategies
- Avoid over-concentration
- Check risk metrics daily
- Review performance weekly
- Adjust parameters monthly
- Always set stop losses
- Use appropriate stop loss levels
- Don't move stops against you
- Set maximum drawdown limits
- Reduce size during drawdowns
- Take breaks if needed
- Backtest with realistic data
- Use walk-forward analysis
- Test in paper trading first
- Don't risk too much per trade
- Consider correlation between positions
- Monitor total exposure
- Don't ignore growing drawdowns
- Reduce size when losing
- Take breaks if needed
- Don't increase size after losses
- Stick to your risk parameters
- Take breaks after big losses
- Don't over-optimize parameters
- Use out-of-sample testing
- Consider market conditions
- Don't ignore correlation between symbols
- Diversify across different asset classes
- Monitor portfolio correlation
- Close all positions immediately
- Assess damage
- Review risk parameters
- Resume with reduced size
- Stop all trading
- Check system status
- Verify positions
- Resume when stable
- Reduce position sizes
- Increase stop losses
- Monitor closely
- Consider closing positions
from forexsmartbot.core.risk_engine import RiskEngine
# Calculate position size
risk_engine = RiskEngine(config)
position_size = risk_engine.calculate_position_size(
symbol='EURUSD',
strategy='SMA_Crossover',
balance=10000,
volatility=0.01,
win_rate=0.6
)# Monitor risk in real-time
risk_summary = risk_engine.get_risk_summary()
if risk_summary['drawdown_throttle']:
print("WARNING: Drawdown throttle active!")# Set up risk alerts
if risk_engine.check_daily_risk_limit(daily_pnl, balance):
send_alert("Daily risk limit exceeded!")Effective risk management is essential for successful trading. The ForexSmartBot risk management system provides comprehensive tools to protect capital and manage risk effectively. Always start with conservative parameters and gradually increase as you gain experience and confidence.