File size: 1,109 Bytes
2106f78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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()