summaryrefslogtreecommitdiff
path: root/services/order-executor/src/order_executor
diff options
context:
space:
mode:
Diffstat (limited to 'services/order-executor/src/order_executor')
-rw-r--r--services/order-executor/src/order_executor/risk_manager.py30
1 files changed, 22 insertions, 8 deletions
diff --git a/services/order-executor/src/order_executor/risk_manager.py b/services/order-executor/src/order_executor/risk_manager.py
index 94d15c2..5a05746 100644
--- a/services/order-executor/src/order_executor/risk_manager.py
+++ b/services/order-executor/src/order_executor/risk_manager.py
@@ -212,8 +212,16 @@ class RiskManager:
prices_a = prices_a[-min_len:]
prices_b = prices_b[-min_len:]
- returns_a = [(prices_a[i] - prices_a[i-1]) / prices_a[i-1] for i in range(1, len(prices_a)) if prices_a[i-1] != 0]
- returns_b = [(prices_b[i] - prices_b[i-1]) / prices_b[i-1] for i in range(1, len(prices_b)) if prices_b[i-1] != 0]
+ returns_a = [
+ (prices_a[i] - prices_a[i - 1]) / prices_a[i - 1]
+ for i in range(1, len(prices_a))
+ if prices_a[i - 1] != 0
+ ]
+ returns_b = [
+ (prices_b[i] - prices_b[i - 1]) / prices_b[i - 1]
+ for i in range(1, len(prices_b))
+ if prices_b[i - 1] != 0
+ ]
if len(returns_a) < 3 or len(returns_b) < 3:
return None
@@ -225,7 +233,9 @@ class RiskManager:
mean_a = sum(returns_a) / len(returns_a)
mean_b = sum(returns_b) / len(returns_b)
- cov = sum((a - mean_a) * (b - mean_b) for a, b in zip(returns_a, returns_b)) / len(returns_a)
+ cov = sum((a - mean_a) * (b - mean_b) for a, b in zip(returns_a, returns_b)) / len(
+ returns_a
+ )
std_a = math.sqrt(sum((a - mean_a) ** 2 for a in returns_a) / len(returns_a))
std_b = math.sqrt(sum((b - mean_b) ** 2 for b in returns_b) / len(returns_b))
@@ -253,7 +263,11 @@ class RiskManager:
if not hist or len(hist) < 5:
continue
prices = list(hist)
- returns = [(prices[i] - prices[i-1]) / prices[i-1] for i in range(1, len(prices)) if prices[i-1] != 0]
+ returns = [
+ (prices[i] - prices[i - 1]) / prices[i - 1]
+ for i in range(1, len(prices))
+ if prices[i - 1] != 0
+ ]
if returns:
all_returns.append(returns)
weight = float(pos.quantity * pos.current_price / balance)
@@ -280,15 +294,15 @@ class RiskManager:
return abs(var_return) * 100 # As percentage
- def check_portfolio_exposure(self, positions: dict[str, Position], balance: Decimal) -> RiskCheckResult:
+ def check_portfolio_exposure(
+ self, positions: dict[str, Position], balance: Decimal
+ ) -> RiskCheckResult:
"""Check total portfolio exposure."""
if balance <= 0:
return RiskCheckResult(allowed=True, reason="OK")
total_exposure = sum(
- pos.quantity * pos.current_price
- for pos in positions.values()
- if pos.quantity > 0
+ pos.quantity * pos.current_price for pos in positions.values() if pos.quantity > 0
)
exposure_ratio = total_exposure / balance