Spaces:
Sleeping
Sleeping
| # src/core/risk_manager.py | |
| import pandas as pd | |
| from typing import Dict, List, Optional | |
| import numpy as np | |
| class RiskManager: | |
| def __init__(self, risk_tolerance: float, max_drawdown: float): | |
| self.risk_tolerance = risk_tolerance | |
| self.max_drawdown = max_drawdown | |
| self.position_limits = {} | |
| self.var_limits = {} | |
| def calculate_position_size(self, asset: str, volatility: float) -> float: | |
| max_position = self.risk_tolerance / volatility | |
| return min(max_position, self.position_limits.get(asset, 0.2)) | |
| def check_risk_limits(self, portfolio: pd.DataFrame) -> bool: | |
| current_drawdown = self.calculate_drawdown(portfolio) | |
| return current_drawdown <= self.max_drawdown | |
| def calculate_drawdown(self, portfolio: pd.DataFrame) -> float: | |
| """Calcule le drawdown actuel du portefeuille""" | |
| cumulative_returns = (1 + portfolio.pct_change()).cumprod() | |
| rolling_max = cumulative_returns.expanding().max() | |
| drawdowns = (cumulative_returns - rolling_max) / rolling_max | |
| return drawdowns.min() |