Spaces:
Sleeping
Sleeping
| class RiskAgent: | |
| """ | |
| Risk Agent (Guardian). | |
| Hard-coded rules to prevent catastrophe. | |
| """ | |
| def __init__(self, max_dd=0.15, max_hourly_loss=200): | |
| self.max_dd = max_dd | |
| self.max_hourly_loss = max_hourly_loss | |
| self.current_dd = 0.0 | |
| self.hourly_loss = 0.0 | |
| def check_health(self, equity, initial_equity, recent_pnl): | |
| """ | |
| Returns boolean: True (Healthy), False (Stop Trading). | |
| """ | |
| # Update DD | |
| peak_equity = max(equity, initial_equity) | |
| self.current_dd = (peak_equity - equity) / peak_equity | |
| # Check Rules | |
| if self.current_dd > self.max_dd: | |
| print(f"RISK TRIGGER: Max Drawdown {self.current_dd:.2%} > {self.max_dd:.2%}") | |
| return False | |
| if recent_pnl < -self.hourly_loss: | |
| print(f"RISK TRIGGER: Hourly Loss {recent_pnl} > {self.max_hourly_loss}") | |
| return False | |
| return True | |