Download web_development/backend/app/services/risk_manager.py from lucky-loster/quant_test: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/lucky-loster/quant_test/resolve/main/web_development/backend/app/services/risk_manager.py
- Command line
-
hf download hf://lucky-loster/quant_test/web_development/backend/app/services/risk_manager.py
-
curl -L -o risk_manager.py https://huggingface.co/lucky-loster/quant_test/resolve/main/web_development/backend/app/services/risk_manager.py
1.69 kB
| from app.core.config import settings | |
| from app.models.schemas import OrderRequest | |
| from app.services.order_manager import order_manager | |
| class RiskManager: | |
| def __init__(self): | |
| self.max_position_size = settings.MAX_POSITION_SIZE | |
| self.risk_limit_percent = settings.RISK_LIMIT_PERCENT | |
| def check_order(self, req: OrderRequest) -> tuple[bool, str]: | |
| if req.quantity > self.max_position_size: | |
| return False, f"Order quantity {req.quantity} exceeds max position size {self.max_position_size}" | |
| account = order_manager.account | |
| if account.available_balance <= 0: | |
| return False, "Insufficient available balance" | |
| max_risk = account.total_balance * (self.risk_limit_percent / 100) | |
| if abs(account.unrealized_pnl) > max_risk: | |
| return False, f"Unrealized PnL ({account.unrealized_pnl}) exceeds risk limit ({max_risk})" | |
| return True, "OK" | |
| def get_risk_metrics(self) -> dict: | |
| account = order_manager.account | |
| max_risk = account.total_balance * (self.risk_limit_percent / 100) | |
| return { | |
| "total_balance": account.total_balance, | |
| "available_balance": account.available_balance, | |
| "used_margin": account.used_margin, | |
| "unrealized_pnl": account.unrealized_pnl, | |
| "realized_pnl": account.realized_pnl, | |
| "risk_limit": max_risk, | |
| "risk_usage_percent": round( | |
| abs(account.unrealized_pnl) / max_risk * 100 if max_risk else 0, 2 | |
| ), | |
| "position_count": len(account.positions), | |
| "max_position_size": self.max_position_size, | |
| } | |
| risk_manager = RiskManager() | |