summaryrefslogtreecommitdiff
path: root/shared/src
diff options
context:
space:
mode:
Diffstat (limited to 'shared/src')
-rw-r--r--shared/src/shared/config.py4
-rw-r--r--shared/src/shared/resilience.py106
-rw-r--r--shared/src/shared/sa_models.py13
-rw-r--r--shared/src/shared/sentiment.py36
4 files changed, 3 insertions, 156 deletions
diff --git a/shared/src/shared/config.py b/shared/src/shared/config.py
index 7a947b3..b6ccebd 100644
--- a/shared/src/shared/config.py
+++ b/shared/src/shared/config.py
@@ -32,16 +32,12 @@ class Settings(BaseSettings):
telegram_enabled: bool = False
log_format: str = "json"
health_port: int = 8080
- circuit_breaker_threshold: int = 5
- circuit_breaker_timeout: int = 60
metrics_auth_token: str = "" # If set, /health and /metrics require Bearer token
# News collector
finnhub_api_key: str = ""
news_poll_interval: int = 300
sentiment_aggregate_interval: int = 900
# Stock selector
- selector_candidates_time: str = "15:00"
- selector_filter_time: str = "15:15"
selector_final_time: str = "15:30"
selector_max_picks: int = 3
# LLM
diff --git a/shared/src/shared/resilience.py b/shared/src/shared/resilience.py
index e43fd21..8d8678a 100644
--- a/shared/src/shared/resilience.py
+++ b/shared/src/shared/resilience.py
@@ -1,105 +1 @@
-"""Retry with exponential backoff and circuit breaker utilities."""
-
-from __future__ import annotations
-
-import asyncio
-import enum
-import functools
-import logging
-import random
-import time
-from typing import Any, Callable
-
-logger = logging.getLogger(__name__)
-
-
-# ---------------------------------------------------------------------------
-# retry_with_backoff
-# ---------------------------------------------------------------------------
-
-
-def retry_with_backoff(
- max_retries: int = 3,
- base_delay: float = 1.0,
- max_delay: float = 60.0,
-) -> Callable:
- """Decorator that retries an async function with exponential backoff + jitter."""
-
- def decorator(func: Callable) -> Callable:
- @functools.wraps(func)
- async def wrapper(*args: Any, **kwargs: Any) -> Any:
- last_exc: BaseException | None = None
- for attempt in range(max_retries + 1):
- try:
- return await func(*args, **kwargs)
- except Exception as exc:
- last_exc = exc
- if attempt < max_retries:
- delay = min(base_delay * (2**attempt), max_delay)
- jitter = delay * random.uniform(0, 0.5)
- total_delay = delay + jitter
- logger.warning(
- "Retry %d/%d for %s after error: %s (delay=%.3fs)",
- attempt + 1,
- max_retries,
- func.__name__,
- exc,
- total_delay,
- )
- await asyncio.sleep(total_delay)
- raise last_exc # type: ignore[misc]
-
- return wrapper
-
- return decorator
-
-
-# ---------------------------------------------------------------------------
-# CircuitBreaker
-# ---------------------------------------------------------------------------
-
-
-class CircuitState(enum.Enum):
- CLOSED = "closed"
- OPEN = "open"
- HALF_OPEN = "half_open"
-
-
-class CircuitBreaker:
- """Simple circuit breaker implementation."""
-
- def __init__(
- self,
- failure_threshold: int = 5,
- recovery_timeout: float = 60.0,
- ) -> None:
- self._failure_threshold = failure_threshold
- self._recovery_timeout = recovery_timeout
- self._failure_count: int = 0
- self._state = CircuitState.CLOSED
- self._opened_at: float = 0.0
-
- @property
- def state(self) -> CircuitState:
- return self._state
-
- def allow_request(self) -> bool:
- if self._state == CircuitState.CLOSED:
- return True
- if self._state == CircuitState.OPEN:
- if time.monotonic() - self._opened_at >= self._recovery_timeout:
- self._state = CircuitState.HALF_OPEN
- return True
- return False
- # HALF_OPEN
- return True
-
- def record_success(self) -> None:
- self._failure_count = 0
- self._state = CircuitState.CLOSED
-
- def record_failure(self) -> None:
- self._failure_count += 1
- if self._failure_count >= self._failure_threshold:
- self._state = CircuitState.OPEN
- self._opened_at = time.monotonic()
+"""Resilience utilities for the trading platform."""
diff --git a/shared/src/shared/sa_models.py b/shared/src/shared/sa_models.py
index 1bd92c2..dc87ef5 100644
--- a/shared/src/shared/sa_models.py
+++ b/shared/src/shared/sa_models.py
@@ -53,19 +53,6 @@ class OrderRow(Base):
filled_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
-class TradeRow(Base):
- __tablename__ = "trades"
-
- id: Mapped[str] = mapped_column(Text, primary_key=True)
- order_id: Mapped[str | None] = mapped_column(Text, ForeignKey("orders.id"))
- symbol: Mapped[str] = mapped_column(Text, nullable=False)
- side: Mapped[str] = mapped_column(Text, nullable=False)
- price: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
- quantity: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
- fee: Mapped[Decimal] = mapped_column(Numeric, nullable=False, server_default="0")
- traded_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
-
-
class PositionRow(Base):
__tablename__ = "positions"
diff --git a/shared/src/shared/sentiment.py b/shared/src/shared/sentiment.py
index 449eb76..5b4b0da 100644
--- a/shared/src/shared/sentiment.py
+++ b/shared/src/shared/sentiment.py
@@ -1,41 +1,9 @@
-"""Market sentiment data."""
+"""Market sentiment aggregation."""
-import logging
-from dataclasses import dataclass, field
-from datetime import datetime, timezone
+from datetime import datetime
from shared.sentiment_models import SymbolScore
-logger = logging.getLogger(__name__)
-
-
-@dataclass
-class SentimentData:
- """Aggregated sentiment snapshot."""
-
- fear_greed_value: int | None = None
- fear_greed_label: str | None = None
- news_sentiment: float | None = None
- news_count: int = 0
- exchange_netflow: float | None = None
- timestamp: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
-
- @property
- def should_buy(self) -> bool:
- if self.fear_greed_value is not None and self.fear_greed_value > 70:
- return False
- if self.news_sentiment is not None and self.news_sentiment < -0.3:
- return False
- return True
-
- @property
- def should_block(self) -> bool:
- if self.fear_greed_value is not None and self.fear_greed_value > 80:
- return True
- if self.news_sentiment is not None and self.news_sentiment < -0.5:
- return True
- return False
-
def _safe_avg(values: list[float]) -> float:
if not values: