generated from mathias/template-go-web
feat(features): add hl_range + ret_intrabar OHLCV features (4-channel input)
- prepare_hourly.py: keep O/H/L columns from M1 zips; compute per-hour hl_range=log(H/L) and ret_intrabar=log(close/open); backward-compat (falls back to 4-col output only when O/H/L present in input) - train.py build(): auto-detect extra features from parquet columns (FEAT_COLS = [ret, realized_vol] + [hl_range, ret_intrabar] if present) - 5 new tests (9 total in test_prepare_hourly); 24/24 pass Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -102,9 +102,7 @@ def test_thin_hours_dropped(ph):
|
||||
|
||||
# 5. Output parquet path and schema (integration — reads actual M1 zips if present)
|
||||
def test_output_schema_from_zips(ph, tmp_path):
|
||||
# Build a minimal fake zip structure
|
||||
import zipfile, io
|
||||
# synthetic M1 CSV (histdata format: YYYYMMDD HHMMSS;O;H;L;C;V)
|
||||
rows = []
|
||||
for h in range(24):
|
||||
for m in range(60):
|
||||
@@ -124,3 +122,89 @@ def test_output_schema_from_zips(ph, tmp_path):
|
||||
df = pd.read_parquet(out_path)
|
||||
assert set(["datetime", "close", "ret", "realized_vol"]).issubset(df.columns)
|
||||
assert len(df) > 0
|
||||
|
||||
|
||||
# ── New OHLCV-derived features ────────────────────────────────────────────────
|
||||
|
||||
def _make_m1_ohlcv(n_hours: int = 4, price: float = 1.1) -> pd.DataFrame:
|
||||
"""Synthetic M1 with distinct O, H, L, C so hl_range and ret_intrabar are nonzero."""
|
||||
rng = np.random.default_rng(7)
|
||||
ts = pd.date_range("2020-01-06 00:00", periods=n_hours * 60, freq="min")
|
||||
closes = price + np.cumsum(rng.normal(0, 0.0002, len(ts)))
|
||||
highs = closes + rng.uniform(0.0001, 0.0005, len(ts))
|
||||
lows = closes - rng.uniform(0.0001, 0.0005, len(ts))
|
||||
opens = np.roll(closes, 1); opens[0] = price
|
||||
return pd.DataFrame({"ts": ts, "open": opens, "high": highs, "low": lows, "close": closes})
|
||||
|
||||
|
||||
# 6. resample_to_hourly produces hl_range column
|
||||
def test_hourly_has_hl_range(ph):
|
||||
m1 = _make_m1_ohlcv()
|
||||
hourly = ph.resample_to_hourly(m1)
|
||||
assert "hl_range" in hourly.columns, f"missing hl_range; cols={hourly.columns.tolist()}"
|
||||
assert (hourly["hl_range"] > 0).all(), "hl_range should be positive"
|
||||
|
||||
|
||||
# 7. resample_to_hourly produces ret_intrabar column
|
||||
def test_hourly_has_ret_intrabar(ph):
|
||||
m1 = _make_m1_ohlcv()
|
||||
hourly = ph.resample_to_hourly(m1)
|
||||
assert "ret_intrabar" in hourly.columns, f"missing ret_intrabar; cols={hourly.columns.tolist()}"
|
||||
|
||||
|
||||
# 8. hl_range = log(hourly_high / hourly_low)
|
||||
def test_hl_range_formula(ph):
|
||||
# Two hours; second has known H=1.105, L=1.095
|
||||
ts0 = pd.date_range("2020-01-06 00:00", periods=60, freq="min")
|
||||
ts1 = pd.date_range("2020-01-06 01:00", periods=60, freq="min")
|
||||
closes = np.full(120, 1.1)
|
||||
highs = np.full(120, 1.1)
|
||||
lows = np.full(120, 1.1)
|
||||
# second hour: known spread
|
||||
highs[60:] = 1.105
|
||||
lows[60:] = 1.095
|
||||
m1 = pd.DataFrame({
|
||||
"ts": np.concatenate([ts0, ts1]),
|
||||
"open": closes, "high": highs, "low": lows, "close": closes,
|
||||
})
|
||||
hourly = ph.resample_to_hourly(m1)
|
||||
assert len(hourly) >= 1
|
||||
hl = hourly.iloc[-1]["hl_range"]
|
||||
expected = float(np.log(1.105 / 1.095))
|
||||
assert abs(hl - expected) < 1e-6, f"hl_range={hl:.8f}, expected={expected:.8f}"
|
||||
|
||||
|
||||
# 9. ret_intrabar = log(hourly_last_close / hourly_first_open)
|
||||
def test_ret_intrabar_formula(ph):
|
||||
ts0 = pd.date_range("2020-01-06 00:00", periods=60, freq="min")
|
||||
ts1 = pd.date_range("2020-01-06 01:00", periods=60, freq="min")
|
||||
closes = np.full(120, 1.1)
|
||||
opens = np.full(120, 1.1)
|
||||
# second hour: open=1.09, close=1.11
|
||||
opens[60] = 1.09
|
||||
closes[119] = 1.11
|
||||
m1 = pd.DataFrame({
|
||||
"ts": np.concatenate([ts0, ts1]),
|
||||
"open": opens, "high": closes + 0.001, "low": closes - 0.001, "close": closes,
|
||||
})
|
||||
hourly = ph.resample_to_hourly(m1)
|
||||
assert len(hourly) >= 1
|
||||
rib = hourly.iloc[-1]["ret_intrabar"]
|
||||
expected = float(np.log(1.11 / 1.09))
|
||||
assert abs(rib - expected) < 1e-6, f"ret_intrabar={rib:.8f}, expected={expected:.8f}"
|
||||
|
||||
|
||||
# 10. build() in train.py uses 4 feature channels when hl_range + ret_intrabar present
|
||||
def test_build_uses_4_channels(tmp_path):
|
||||
import importlib.util, os
|
||||
hourly_path = "data/processed/eurusd_hourly.parquet"
|
||||
if not os.path.exists(hourly_path):
|
||||
pytest.skip("eurusd_hourly.parquet not present")
|
||||
df = pd.read_parquet(hourly_path)
|
||||
if "hl_range" not in df.columns:
|
||||
pytest.skip("eurusd_hourly.parquet lacks hl_range — rebuild first")
|
||||
spec = importlib.util.spec_from_file_location("train_4ch", "train.py")
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
(Xtr, _), _ = mod.build()
|
||||
assert Xtr.shape[2] == 4, f"expected 4 channels, got {Xtr.shape[2]}"
|
||||
|
||||
Reference in New Issue
Block a user