diff options
Diffstat (limited to 'services/order-executor/src')
| -rw-r--r-- | services/order-executor/src/order_executor/main.py | 4 | ||||
| -rw-r--r-- | services/order-executor/src/order_executor/risk_manager.py | 13 |
2 files changed, 14 insertions, 3 deletions
diff --git a/services/order-executor/src/order_executor/main.py b/services/order-executor/src/order_executor/main.py index 32470f6..1eeee7b 100644 --- a/services/order-executor/src/order_executor/main.py +++ b/services/order-executor/src/order_executor/main.py @@ -60,7 +60,9 @@ async def run() -> None: last_id = "$" stream = "signals" - health = HealthCheckServer("order-executor", port=config.health_port + 2, auth_token=config.metrics_auth_token) + health = HealthCheckServer( + "order-executor", port=config.health_port + 2, auth_token=config.metrics_auth_token + ) health.register_check("redis", broker.ping) await health.start() metrics.service_up.labels(service="order-executor").set(1) diff --git a/services/order-executor/src/order_executor/risk_manager.py b/services/order-executor/src/order_executor/risk_manager.py index 2b0a864..c3578a7 100644 --- a/services/order-executor/src/order_executor/risk_manager.py +++ b/services/order-executor/src/order_executor/risk_manager.py @@ -1,4 +1,5 @@ """Risk management for order execution.""" + from dataclasses import dataclass from decimal import Decimal from collections import deque @@ -16,6 +17,7 @@ class RiskCheckResult: @dataclass class TrailingStop: """Tracks trailing stop for a symbol.""" + symbol: str highest_price: Decimal stop_pct: Decimal # e.g. 5.0 for 5% @@ -86,7 +88,11 @@ class RiskManager: if not history or len(history) < 2: return None prices = list(history) - 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 not returns: return None mean = sum(returns) / len(returns) @@ -153,7 +159,10 @@ class RiskManager: if position is not None: current_position_value = position.quantity * position.current_price - if balance > 0 and (current_position_value + order_cost) / balance > self.max_position_size: + if ( + balance > 0 + and (current_position_value + order_cost) / balance > self.max_position_size + ): return RiskCheckResult(allowed=False, reason="Position size exceeded") return RiskCheckResult(allowed=True, reason="OK") |
