Dmitry Beresnev commited on
Commit ·
debae55
1
Parent(s): a71dd82
fix bugs
Browse filesFiles changed (5)
12346 unmodified lines1112131415161718from typing import Dict, Optionalimport numpy as npfrom .config import K_SIGMA, MIN_WINDOW_POINTS6 unmodified linesreturns = np.diff(prices) / prices[:-1]if returns.size == 0:return Nonesigma = float(np.std(returns))return -K_SIGMA * sigma * 100.0def detect_event(delta_pct: float, threshold: float) -> bool:12346 unmodified lines11121314151617181920212223242526from typing import Optionalimport numpy as npfrom .config import K_SIGMA, MIN_WINDOW_POINTS6 unmodified linesreturns = np.diff(prices) / prices[:-1]if returns.size == 0:return None# Robust sigma (MAD): a plain std is contaminated by the very crash we# are trying to detect, inflating the threshold past the crash itself.mad = float(np.median(np.abs(returns - np.median(returns))))sigma = 1.4826 * madif sigma <= 0.0:return None# delta_pct spans the whole window (returns.size steps), so scale the# single-step sigma to the window horizon before applying K_SIGMA.horizon_sigma = sigma * float(np.sqrt(returns.size))return -K_SIGMA * horizon_sigma * 100.0def detect_event(delta_pct: float, threshold: float) -> bool:
16 unmodified lines171819202122231 unmodified line2526272829303134 unmodified lines6667686970717273747576777879808182838485868788899016 unmodified linesfrom .telemetry import Telemetrydef run_once(pubsub, loader, universe, queue, notifier):for venue_key, tickers in universe.items():if not tickers:continue1 unmodified linevenue = venue_keypubsub.publish(TOPICS["universe_updated"], loader.build_event(asset_class, venue, tickers))ingestor = PriceIngestor(venue=venue, asset_class=asset_class)price_event = ingestor.fetch_prices(tickers)pubsub.publish(TOPICS["prices_snapshot"], price_event)34 unmodified linessnapshot = loader.build_snapshot(universe)pubsub.publish(TOPICS["universe_snapshot"], snapshot)return {"pubsub": pubsub,"notifier": notifier,"queue": queue,"loader": loader,"universe": universe,}def run_single_cycle(runtime=None):state = runtime or build_runtime()run_once(state["pubsub"], state["loader"], state["universe"], state["queue"], state["notifier"])return statedef run_forever():state = build_runtime()while True:run_once(state["pubsub"], state["loader"], state["universe"], state["queue"], state["notifier"])time.sleep(POLL_INTERVAL_SEC)16 unmodified lines171819202122231 unmodified line2526272829303134 unmodified lines6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610716 unmodified linesfrom .telemetry import Telemetrydef run_once(pubsub, loader, universe, queue, notifier, ingestors):for venue_key, tickers in universe.items():if not tickers:continue1 unmodified linevenue = venue_keypubsub.publish(TOPICS["universe_updated"], loader.build_event(asset_class, venue, tickers))ingestor = ingestors[venue_key]price_event = ingestor.fetch_prices(tickers)pubsub.publish(TOPICS["prices_snapshot"], price_event)34 unmodified linessnapshot = loader.build_snapshot(universe)pubsub.publish(TOPICS["universe_snapshot"], snapshot)ingestors = {venue_key: PriceIngestor(venue=venue_key, asset_class=_infer_asset_class(venue_key))for venue_key in universe}return {"pubsub": pubsub,"notifier": notifier,"queue": queue,"loader": loader,"universe": universe,"ingestors": ingestors,}def _run_cycle(state):run_once(state["pubsub"],state["loader"],state["universe"],state["queue"],state["notifier"],state["ingestors"],)def run_single_cycle(runtime=None):state = runtime or build_runtime()_run_cycle(state)return statedef run_forever():state = build_runtime()while True:_run_cycle(state)time.sleep(POLL_INTERVAL_SEC)
68 unmodified lines69707172737411 unmodified lines8687888990919211 unmodified lines10410510610710810968 unmodified linesdef __init__(self, venue: str, asset_class: str) -> None:self.venue = venueself.asset_class = asset_classdef fetch_prices(self, tickers: List[str]) -> Dict:chunks = _chunk_list(tickers, BATCH_SIZE)11 unmodified linesexcept Exception:continuesequence_id = next(_SEQ)return {"event_id": str(uuid.uuid4()),11 unmodified linesfor t, p in prices.items()],}_SEQ = itertools.count(1)68 unmodified lines69707172737475767711 unmodified lines8990919293949511 unmodified lines10710810968 unmodified linesdef __init__(self, venue: str, asset_class: str) -> None:self.venue = venueself.asset_class = asset_class# Per-venue sequence: DataGapDetector treats any per-ticker jump > 1# as a gap, so venues must not share a counter.self._seq = itertools.count(1)def fetch_prices(self, tickers: List[str]) -> Dict:chunks = _chunk_list(tickers, BATCH_SIZE)11 unmodified linesexcept Exception:continuesequence_id = next(self._seq)return {"event_id": str(uuid.uuid4()),11 unmodified linesfor t, p in prices.items()],}
33 unmodified lines343536373839404133 unmodified linestarget = "normal"if target == self._state:if target == "normal":self._calm_streak = 0return self._state, Falseif self._rank(target) > self._rank(self._state):33 unmodified lines34353637383940414233 unmodified linestarget = "normal"if target == self._state:# Re-confirmed at the current level: not a calm snapshot, so the# downgrade streak must restart.self._calm_streak = 0return self._state, Falseif self._rank(target) > self._rank(self._state):
src/core/event_detector.py+11-3
src/core/main.py+21-4
src/core/price_ingestor.py+4-4
src/core/risk_regime.py+3-2
uv.lockAdded