feat: implement dual-pipeline signal engine service
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build-2 Pipeline was successful
ci/woodpecker/push/build-1 Pipeline was successful
ci/woodpecker/push/build-3 Pipeline was successful
ci/woodpecker/push/finalize Pipeline was successful
Build and Push / lint-and-test (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.adapters.broker_adapter name:broker-adapter]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.aggregation.worker name:aggregation]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.extractor.worker name:extractor]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.ingestion.worker name:ingestion]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.lake_publisher.worker name:lake-publisher]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.parser.worker name:parser]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.recommendation.worker name:recommendation]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.scheduler.app name:scheduler]) (push) Has been cancelled
Build and Push / build-services (map[cmd:uvicorn services.api.app:app --host 0.0.0.0 --port 8000 name:query-api]) (push) Has been cancelled
Build and Push / build-services (map[cmd:uvicorn services.risk.app:app --host 0.0.0.0 --port 8000 name:risk]) (push) Has been cancelled
Build and Push / build-services (map[cmd:uvicorn services.symbol_registry.app:app --host 0.0.0.0 --port 8000 name:symbol-registry]) (push) Has been cancelled
Build and Push / build-services (map[cmd:uvicorn services.trading.app:app --host 0.0.0.0 --port 8000 name:trading-engine]) (push) Has been cancelled
Build and Push / build-dashboard (push) Has been cancelled
Build and Push / build-superset (push) Has been cancelled
Build and Push / integration-test (push) Has been cancelled
Build and Push / beta-gate (push) Has been cancelled

New service at services/signal_engine/ implementing concurrent heuristic
(deterministic scoring) and probabilistic (Bayesian inference) pipelines
that evaluate technical signals across 6 timeframes (M30-M) and produce
independent BUY/WATCH/SKIP verdicts per ticker per evaluation tick.

Components:
- Input Normalizer: multi-source data assembly with sentinel fallbacks
- Signal Library: Fibonacci, MA Stack, RSI, Cup & Handle, Elliott Wave
- Multi-Timeframe Confluence Engine: weighted scoring with D/W/M anchors
- Hard Filter Engine: macro_bias, valuation, earnings proximity gating
- Heuristic Pipeline: S_total scoring with confidence-gated verdicts
- Probabilistic Pipeline: Bayesian log-odds with regime priors, entropy
  gating, EV_R calculation, and signal correlation penalty
- Exit Engine: stop-loss, targets, trailing ATR-based stops
- Delta Analyzer: pipeline agreement tracking with rolling Redis metrics
- Output Formatter: SignalOutput contract + Recommendation schema mapping
- Worker orchestrator: concurrent pipelines with failure isolation
- Main entry point: queue polling with fail-safe config loading

Infrastructure:
- Migration 039: signal_engine_outputs table with 3 indexes
- Helm chart: signalEngine service entry (processing tier)
- Redis key: QUEUE_SIGNAL_ENGINE constant

Tests: 390 tests (unit + property-based) covering all components
Config: dual_pipeline_enabled=false by default (safe rollout)
This commit is contained in:
Celes Renata
2026-05-02 07:32:26 +00:00
parent 7e2343ec2c
commit f468e30af0
61 changed files with 14107 additions and 184 deletions
+127
View File
@@ -0,0 +1,127 @@
"""Base protocol and common helpers for signal evaluators.
Defines the ``SignalEvaluator`` protocol that every signal in the Signal
Library must satisfy, plus shared utility functions for swing detection,
lookback validation, and simple moving average computation.
"""
from __future__ import annotations
from typing import Protocol
from services.signal_engine.models import OHLCVBar, SignalResult
# ---------------------------------------------------------------------------
# Signal evaluator protocol
# ---------------------------------------------------------------------------
class SignalEvaluator(Protocol):
"""Protocol for all signal evaluators in the Signal Library.
Each evaluator receives a list of OHLCV bars for a single timeframe
and returns a ``SignalResult`` when the signal triggers, or ``None``
when insufficient data is available or the signal does not fire.
"""
def evaluate(
self,
bars: list[OHLCVBar],
timeframe: str,
) -> SignalResult | None:
"""Evaluate a signal on a single timeframe's bar data.
Returns ``None`` when insufficient data is available.
"""
...
# ---------------------------------------------------------------------------
# Common helper functions
# ---------------------------------------------------------------------------
def find_swing_high(
bars: list[OHLCVBar],
lookback: int,
) -> tuple[int, float] | None:
"""Find the highest high in the last *lookback* bars.
Args:
bars: OHLCV bar series (oldest-first).
lookback: Number of recent bars to search.
Returns:
``(index, price)`` of the bar with the highest high within the
lookback window, or ``None`` if *bars* has fewer than *lookback*
entries.
"""
if len(bars) < lookback or lookback <= 0:
return None
window = bars[-lookback:]
offset = len(bars) - lookback
best_idx = 0
best_price = window[0].high
for i, bar in enumerate(window):
if bar.high >= best_price:
best_idx = i
best_price = bar.high
return (offset + best_idx, best_price)
def find_swing_low(
bars: list[OHLCVBar],
lookback: int,
) -> tuple[int, float] | None:
"""Find the lowest low in the last *lookback* bars.
Args:
bars: OHLCV bar series (oldest-first).
lookback: Number of recent bars to search.
Returns:
``(index, price)`` of the bar with the lowest low within the
lookback window, or ``None`` if *bars* has fewer than *lookback*
entries.
"""
if len(bars) < lookback or lookback <= 0:
return None
window = bars[-lookback:]
offset = len(bars) - lookback
best_idx = 0
best_price = window[0].low
for i, bar in enumerate(window):
if bar.low <= best_price:
best_idx = i
best_price = bar.low
return (offset + best_idx, best_price)
def validate_lookback(bars: list[OHLCVBar], min_bars: int) -> bool:
"""Return ``True`` if *bars* contains at least *min_bars* entries."""
return len(bars) >= min_bars
def compute_sma(bars: list[OHLCVBar], period: int) -> float | None:
"""Compute the simple moving average of close prices over the last *period* bars.
Args:
bars: OHLCV bar series (oldest-first).
period: Number of recent bars to average.
Returns:
The arithmetic mean of the last *period* close prices, or ``None``
if *bars* has fewer than *period* entries or *period* is not
positive.
"""
if period <= 0 or len(bars) < period:
return None
total = sum(bar.close for bar in bars[-period:])
return total / period