diff --git a/tests/rootfile/test_lawful_collapse_universe.py b/tests/rootfile/test_lawful_collapse_universe.py index 5119fea..f1f0d93 100644 --- a/tests/rootfile/test_lawful_collapse_universe.py +++ b/tests/rootfile/test_lawful_collapse_universe.py @@ -103,6 +103,21 @@ def test_field_tensor_is_deterministic(): assert first["field_energy"] > 0 +def test_field_tensor_accepts_canonical_bar_list(): + market_state = { + "ohlcv": [ + {"high": 1.1010, "low": 1.0990, "close": 1.1000}, + {"high": 1.1020, "low": 1.1000, "close": 1.1015}, + ], + "microstructure": {"mid": 1.1015}, + } + + tensor = compute_maxwell_tensor(market_state, {"phi": 0.2}) + + assert tensor["electric_impulse"] > 0 + assert tensor["field_energy"] > 0 + + def test_causal_violation_marks_field_inadmissible(): tensor = {"magnetic_liquidity": 0.00001, "electric_impulse": 0.00001} result = check_causal_reach({"entry": 1.0, "target": 2.0}, tensor) diff --git a/trading/fields/maxwell_tensor.py b/trading/fields/maxwell_tensor.py index 1efc779..a665b20 100644 --- a/trading/fields/maxwell_tensor.py +++ b/trading/fields/maxwell_tensor.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Dict, Iterable +from typing import Any, Dict, Iterable def _last(values: Iterable[float], default: float = 0.0) -> float: @@ -10,13 +10,27 @@ def _last(values: Iterable[float], default: float = 0.0) -> float: return float(seq[-1]) if seq else default +def _series(ohlcv: Any, key: str) -> list[float]: + if isinstance(ohlcv, dict): + values = ohlcv.get(key, []) + elif isinstance(ohlcv, list): + values = [ + item.get(key) + for item in ohlcv + if isinstance(item, dict) + ] + else: + values = [] + return [float(value) for value in values if value is not None] + + def compute_maxwell_tensor(market_state: Dict, geometry_data: Dict) -> Dict[str, float]: """Compute a compact field tensor from OHLCV, microstructure, and geometry.""" ohlcv = market_state.get("ohlcv", {}) if isinstance(market_state, dict) else {} micro = market_state.get("microstructure", {}) if isinstance(market_state, dict) else {} - close = [float(value) for value in ohlcv.get("close", []) if value is not None] - high = [float(value) for value in ohlcv.get("high", []) if value is not None] - low = [float(value) for value in ohlcv.get("low", []) if value is not None] + close = _series(ohlcv, "close") + high = _series(ohlcv, "high") + low = _series(ohlcv, "low") latest_close = _last(close, float(micro.get("mid", 0.0) or 0.0)) prev_close = close[-2] if len(close) >= 2 else latest_close