Release v2.17.1: Implement Strategies A, B & C (git/stash telemetry excavator, IPv4 parallel FRED macro backfill with YoY metrics, and 3-month Yahoo/Stooq lookback with >= 2026-11-01 scope)
This commit is contained in:
1 parent
52153e76f0
commit
91cf671000
13 files changed
+2506
-411
No files matched your search
@@ -147,26 +147,60 @@ FRED_SERIES_SPEC: tuple[dict[str, Any], ...] = (
|
||||
)
|
||||
|
||||
|
||||
def fetch_fred_series_csv(series_id: str, max_rows: int = 36, timeout: float = 8.0) -> list[dict[str, Any]]:
|
||||
MIN_MACRO_DAILY_DATE_UTC = "2026-11-01"
|
||||
|
||||
|
||||
def fetch_fred_series_csv(
|
||||
series_id: str,
|
||||
max_rows: int = 36,
|
||||
timeout: float = 8.0,
|
||||
min_date_utc: str | None = None,
|
||||
max_date_utc: str | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Fetch real observations from `https://fred.stlouisfed.org/graph/fredgraph.csv?id=<series_id>&cosd=2024-10-01`.
|
||||
Fetch real observations from `https://fred.stlouisfed.org/graph/fredgraph.csv?id=<series_id>&cosd=2025-09-01`
|
||||
using IPv4-first transport (`curl -4` with `safe_urlopen` fallback so blackholed IPv6 routes never stall).
|
||||
Preserves missing/holiday dates explicitly as `value = None` (`data_available = False`) rather than `0.0`.
|
||||
"""
|
||||
url = f"https://fred.stlouisfed.org/graph/fredgraph.csv?id={series_id}&cosd=2024-10-01"
|
||||
req = urllib.request.Request(url, headers={"User-Agent": _USER_AGENT})
|
||||
from hyperstitionstud_io.security import safe_urlopen
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
with safe_urlopen(req, timeout=timeout) as resp:
|
||||
lines = [ln.strip() for ln in resp.read().decode("utf-8").splitlines() if ln.strip()]
|
||||
url = f"https://fred.stlouisfed.org/graph/fredgraph.csv?id={series_id}&cosd=2025-09-01"
|
||||
raw_text = ""
|
||||
curl_bin = shutil.which("curl")
|
||||
if curl_bin:
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
[curl_bin, "-4", "-sS", "-L", "--max-time", str(max(2, int(timeout))), url],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
if proc.returncode == 0 and proc.stdout.startswith("observation_date,"):
|
||||
raw_text = proc.stdout
|
||||
except Exception:
|
||||
raw_text = ""
|
||||
|
||||
if not raw_text:
|
||||
req = urllib.request.Request(url, headers={"User-Agent": _USER_AGENT})
|
||||
from hyperstitionstud_io.security import safe_urlopen
|
||||
|
||||
with safe_urlopen(req, timeout=timeout) as resp:
|
||||
raw_text = resp.read().decode("utf-8")
|
||||
|
||||
lines = [ln.strip() for ln in raw_text.splitlines() if ln.strip()]
|
||||
if len(lines) <= 1:
|
||||
return []
|
||||
tail = lines[1:][-max_rows:]
|
||||
rows: list[dict[str, Any]] = []
|
||||
for ln in tail:
|
||||
for ln in lines[1:]:
|
||||
parts = ln.split(",", 1)
|
||||
if len(parts) != 2:
|
||||
continue
|
||||
dt_s, val_s = parts[0].strip(), parts[1].strip()
|
||||
if min_date_utc and dt_s < min_date_utc:
|
||||
continue
|
||||
if max_date_utc and dt_s > max_date_utc:
|
||||
continue
|
||||
if not val_s or val_s == ".":
|
||||
rows.append({
|
||||
"date_utc": dt_s,
|
||||
@@ -189,13 +223,13 @@ def fetch_fred_series_csv(series_id: str, max_rows: int = 36, timeout: float = 8
|
||||
"status": NO_DATA_AVAILABLE,
|
||||
"value": None,
|
||||
})
|
||||
return rows
|
||||
return rows[-max_rows:]
|
||||
|
||||
|
||||
def _score_macro_int(face_int: int, ing_map: dict[str, str]) -> dict[str, Any]:
|
||||
sp_eng = int_to_spoken_words(abs(face_int))
|
||||
sp_eng_aq = compute_aq(sp_eng)
|
||||
sp_ing = ing_map.get(sp_eng, sp_eng)
|
||||
sp_ing = ing_map.get(sp_eng) or translate_batch([sp_eng]).get(sp_eng, sp_eng)
|
||||
sp_ing_aq = compute_aq(sp_ing)
|
||||
ring_hits = sorted(
|
||||
{
|
||||
@@ -299,20 +333,47 @@ def build_macro_indicators_bundle(
|
||||
cached_bundle = {}
|
||||
|
||||
cached_series = (cached_bundle.get("raw_fred_observations") or {}) if isinstance(cached_bundle, dict) else {}
|
||||
from hyperstitionstud_io.quant.collector import latest_completed_closing_bell_date_utc
|
||||
|
||||
for spec in FRED_SERIES_SPEC:
|
||||
max_closed_dt = latest_completed_closing_bell_date_utc()
|
||||
|
||||
def _fetch_one_spec(spec: dict[str, Any]) -> tuple[str, list[dict[str, Any]]]:
|
||||
sid = spec["series_id"]
|
||||
fetched: list[dict[str, Any]] = []
|
||||
is_daily = spec["cadence"] == "1d"
|
||||
min_dt = MIN_MACRO_DAILY_DATE_UTC if is_daily else None
|
||||
fetched_rows: list[dict[str, Any]] = []
|
||||
if allow_live_network:
|
||||
try:
|
||||
fetched = fetch_fred_series_csv(sid, max_rows=28)
|
||||
fetched_rows = fetch_fred_series_csv(
|
||||
sid,
|
||||
max_rows=36,
|
||||
min_date_utc=min_dt,
|
||||
max_date_utc=max_closed_dt,
|
||||
)
|
||||
except Exception:
|
||||
fetched = []
|
||||
if not fetched and sid in cached_series and cached_series[sid]:
|
||||
fetched = list(cached_series[sid])
|
||||
if not fetched and sid in _VERIFIED_FRED_SEED_OBSERVATIONS:
|
||||
fetched = list(_VERIFIED_FRED_SEED_OBSERVATIONS[sid])
|
||||
raw_series_by_id[sid] = fetched
|
||||
fetched_rows = []
|
||||
prior_rows = list(cached_series.get(sid) or [])
|
||||
if not prior_rows and not fetched_rows and sid in _VERIFIED_FRED_SEED_OBSERVATIONS:
|
||||
prior_rows = list(_VERIFIED_FRED_SEED_OBSERVATIONS[sid])
|
||||
merged_by_dt: dict[str, dict[str, Any]] = {}
|
||||
for r in prior_rows + fetched_rows:
|
||||
if not isinstance(r, dict) or not r.get("date_utc"):
|
||||
continue
|
||||
dt_s = str(r["date_utc"])
|
||||
if min_dt and dt_s < min_dt:
|
||||
continue
|
||||
if dt_s > max_closed_dt:
|
||||
continue
|
||||
prev_r = merged_by_dt.get(dt_s)
|
||||
if prev_r is None or (not prev_r.get("data_available") and r.get("data_available")):
|
||||
merged_by_dt[dt_s] = r
|
||||
return sid, [merged_by_dt[k] for k in sorted(merged_by_dt.keys())][-36:]
|
||||
|
||||
import concurrent.futures
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=7) as pool:
|
||||
for sid, rows_list in pool.map(_fetch_one_spec, FRED_SERIES_SPEC):
|
||||
raw_series_by_id[sid] = rows_list
|
||||
|
||||
# Pre-collect spoken integer strings for batch Ingglish translation
|
||||
phrases: set[str] = set()
|
||||
@@ -433,7 +494,11 @@ def build_macro_indicators_bundle(
|
||||
"aq_evaluation": aq_eval,
|
||||
"change_aq_evaluation": chg_eval,
|
||||
"missing_observation_dates": missing_dates,
|
||||
"recent_observations": obs_list[-12:],
|
||||
"recent_observations": (
|
||||
[r for r in obs_list if r["date_utc"] >= MIN_MACRO_DAILY_DATE_UTC]
|
||||
if spec["cadence"] == "1d"
|
||||
else obs_list[-13:]
|
||||
),
|
||||
}
|
||||
|
||||
# Compute Derived Spreads & Velocity Ratios (Strictly Null-Preserving)
|
||||
|
||||
Reference in new issue
Block a user