Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(registry): derive the NaN mask reference from declared dependencies

cgycorey707bd21393b7
diff --git a/agent/src/factors/registry.py b/agent/src/factors/registry.py
index 1e33058d7..674d50e83 100644
--- a/agent/src/factors/registry.py
+++ b/agent/src/factors/registry.py
@@ -354,7 +354,14 @@ class Registry:
# Enforce NaN contract: any bar where a declared dependency is missing
# must produce NaN in the output, regardless of the alpha's internal logic.
- ref = panel.get("close")
+ # The mask's shape reference is the first declared dependency, not a
+ # hardcoded "close": 78 alphas declare columns_required without close,
+ # and the mask must still run for them. Pre-checks above guarantee every
+ # declared dependency is present in the panel.
+ deps = list(meta.get("columns_required", [])) + list(meta.get("extras_required", []))
+ if meta.get("requires_sector"):
+ deps.append("sector")
+ ref = next((panel[c] for c in deps if c in panel), None)
if ref is not None:
mask = pd.DataFrame(True, index=ref.index, columns=ref.columns)
for col in meta.get("columns_required", []):
diff --git a/agent/tests/factors/test_registry.py b/agent/tests/factors/test_registry.py
index ec4560384..e0c096475 100644
--- a/agent/tests/factors/test_registry.py
+++ b/agent/tests/factors/test_registry.py
@@ -268,3 +268,34 @@ def test_registry_masks_nan_on_declared_dependencies(mini_zoo: Path) -> None:
panel["open"].iloc[1, 1] = np.nan
out = reg.compute("fakezoo_001", panel)
assert np.isnan(out.iloc[1, 1])
+
+
+def test_registry_masks_nan_without_close_declared(mini_zoo: Path) -> None:
+ """The NaN mask must run even when the alpha does not declare ``close``.
+
+ The mask's shape reference was hardcoded to ``panel["close"]``, so an alpha
+ declaring only ``["open"]`` got no NaN enforcement on a panel without a
+ close column — a fabricated value (np.where on a NaN comparison) survived.
+ The reference is now the first declared dependency.
+ """
+ reg = Registry(zoo_root=mini_zoo)
+ meta = GOOD_META.replace('"close", "open"', '"open"')
+ body = textwrap.dedent(
+ """\
+ import numpy as np
+ import pandas as pd
+
+ def compute(panel):
+ o = panel["open"]
+ # NaN < 0 is False: a missing bar falls through to 0.0 (fabricated).
+ return pd.DataFrame(np.where(o < 0, 1.0, 0.0), index=o.index, columns=o.columns)
+ """
+ )
+ _write_alpha(mini_zoo / "fakezoo", "alpha_005", "fakezoo", meta=meta, body=body)
+ reg = Registry(zoo_root=mini_zoo)
+ panel = _panel()
+ del panel["close"] # no close column at all
+ panel["open"].iloc[2, 0] = np.nan
+ out = reg.compute("fakezoo_005", panel)
+ assert np.isnan(out.iloc[2, 0]) # masked, not the fabricated 0.0
+ assert out.notna().sum().sum() > 0 # mask did not wipe the whole panel