|
1 | | -from __future__ import annotations |
2 | | - |
3 | | -from datetime import date, datetime |
4 | | -from pathlib import Path |
5 | | -from typing import Any, Callable, Iterable |
| 1 | +"""Compatibility shim; implementation lives in us_equity_strategies.signals.""" |
6 | 2 |
|
7 | 3 | from us_equity_strategies.signals import ( |
| 4 | + DEFAULT_MARKET_SIGNAL_CACHE_DIR, |
8 | 5 | MARKET_SIGNAL_REFERENCE_CONSUMPTION_AUDIT, |
9 | 6 | MARKET_SIGNAL_REFERENCE_PLATFORM_HANDOFF, |
10 | 7 | MARKET_SIGNAL_REFERENCE_PLATFORM_HANDOFF_INDEX, |
11 | 8 | default_market_signal_inputs_when_unconfigured, |
12 | 9 | extract_consumer_market_signal_inputs_from_reference, |
13 | 10 | market_signal_consumer_for_strategy_profile, |
| 11 | + resolve_external_market_signal_inputs, |
14 | 12 | ) |
15 | 13 |
|
16 | | - |
17 | | -DEFAULT_MARKET_SIGNAL_CACHE_DIR = "/tmp/quant-platform-market-signals" |
18 | | - |
19 | | - |
20 | | -def resolve_external_market_signal_inputs( |
21 | | - *, |
22 | | - strategy_profile: str, |
23 | | - available_inputs: Iterable[str], |
24 | | - runtime_settings: Any, |
25 | | - as_of: Any = None, |
26 | | - logger: Callable[[str], None] = print, |
27 | | - client_factory: Any = None, |
28 | | -) -> dict[str, Any]: |
29 | | - normalized_profile = str(strategy_profile or "").strip().lower() |
30 | | - consumer = market_signal_consumer_for_strategy_profile(normalized_profile) |
31 | | - if consumer is None: |
32 | | - return {} |
33 | | - if "derived_indicators" not in {str(item) for item in available_inputs or ()}: |
34 | | - return {} |
35 | | - |
36 | | - reference_type, reference = _market_signal_reference(runtime_settings) |
37 | | - if reference is None: |
38 | | - if bool(getattr(runtime_settings, "market_signal_required", False)): |
39 | | - raise RuntimeError( |
40 | | - f"{normalized_profile} external market signal is required " |
41 | | - "but no signal reference is configured" |
42 | | - ) |
43 | | - return default_market_signal_inputs_when_unconfigured(normalized_profile) |
44 | | - |
45 | | - market_inputs, metadata = extract_consumer_market_signal_inputs_from_reference( |
46 | | - reference, |
47 | | - reference_type=reference_type, |
48 | | - consumer=consumer, |
49 | | - cache_dir=_market_signal_cache_dir(runtime_settings), |
50 | | - as_of=_market_signal_as_of(as_of), |
51 | | - client_factory=client_factory, |
52 | | - fallback_mode=_market_signal_fallback_mode(runtime_settings), |
53 | | - fallback_max_stale_days=_market_signal_max_stale_days(runtime_settings), |
54 | | - ) |
55 | | - logger( |
56 | | - "market_signal_inputs_loaded | " |
57 | | - f"profile={strategy_profile} reference_type={metadata.get('reference_type')} " |
58 | | - f"source_uri={metadata.get('source_uri') or reference} " |
59 | | - f"materialized_count={metadata.get('materialized_count')} " |
60 | | - f"fallback_used={bool(metadata.get('artifact_fallback_used'))}" |
61 | | - ) |
62 | | - return dict(market_inputs) |
63 | | - |
64 | | - |
65 | | -def _market_signal_reference(runtime_settings: Any) -> tuple[str, str | None]: |
66 | | - consumption_audit_uri = _optional_string( |
67 | | - getattr(runtime_settings, "market_signal_consumption_audit_uri", None) |
68 | | - ) |
69 | | - if consumption_audit_uri: |
70 | | - return MARKET_SIGNAL_REFERENCE_CONSUMPTION_AUDIT, consumption_audit_uri |
71 | | - |
72 | | - handoff_manifest_uri = _optional_string( |
73 | | - getattr(runtime_settings, "market_signal_handoff_manifest_uri", None) |
74 | | - ) |
75 | | - if handoff_manifest_uri: |
76 | | - return MARKET_SIGNAL_REFERENCE_PLATFORM_HANDOFF, handoff_manifest_uri |
77 | | - |
78 | | - handoff_index_uri = _optional_string( |
79 | | - getattr(runtime_settings, "market_signal_handoff_index_uri", None) |
80 | | - ) |
81 | | - if handoff_index_uri: |
82 | | - return MARKET_SIGNAL_REFERENCE_PLATFORM_HANDOFF_INDEX, handoff_index_uri |
83 | | - |
84 | | - return MARKET_SIGNAL_REFERENCE_PLATFORM_HANDOFF_INDEX, None |
85 | | - |
86 | | - |
87 | | -def _market_signal_cache_dir(runtime_settings: Any) -> Path: |
88 | | - configured = _optional_string(getattr(runtime_settings, "market_signal_cache_dir", None)) |
89 | | - return Path(configured or DEFAULT_MARKET_SIGNAL_CACHE_DIR) |
90 | | - |
91 | | - |
92 | | -def _market_signal_fallback_mode(runtime_settings: Any) -> str: |
93 | | - return _optional_string(getattr(runtime_settings, "market_signal_fallback_mode", None)) or "none" |
94 | | - |
95 | | - |
96 | | -def _market_signal_max_stale_days(runtime_settings: Any) -> int: |
97 | | - value = getattr(runtime_settings, "market_signal_max_stale_days", None) |
98 | | - if value is None or str(value).strip() == "": |
99 | | - return 3 |
100 | | - return max(0, int(value)) |
101 | | - |
102 | | - |
103 | | -def _market_signal_as_of(value: Any) -> str | None: |
104 | | - if value is None: |
105 | | - return None |
106 | | - if isinstance(value, datetime): |
107 | | - return value.date().isoformat() |
108 | | - if isinstance(value, date): |
109 | | - return value.isoformat() |
110 | | - text = str(value).strip() |
111 | | - return text[:10] if text else None |
112 | | - |
113 | | - |
114 | | -def _optional_string(value: Any) -> str | None: |
115 | | - text = str(value or "").strip() |
116 | | - return text or None |
| 14 | +__all__ = [ |
| 15 | + "DEFAULT_MARKET_SIGNAL_CACHE_DIR", |
| 16 | + "MARKET_SIGNAL_REFERENCE_CONSUMPTION_AUDIT", |
| 17 | + "MARKET_SIGNAL_REFERENCE_PLATFORM_HANDOFF", |
| 18 | + "MARKET_SIGNAL_REFERENCE_PLATFORM_HANDOFF_INDEX", |
| 19 | + "default_market_signal_inputs_when_unconfigured", |
| 20 | + "extract_consumer_market_signal_inputs_from_reference", |
| 21 | + "market_signal_consumer_for_strategy_profile", |
| 22 | + "resolve_external_market_signal_inputs", |
| 23 | +] |
0 commit comments