
import json
import os
from datetime import datetime

data_dir = "/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data"

def load_data(tf):
    with open(os.path.join(data_dir, f"ohlcv_{tf}.json")) as f:
        return json.load(f)["bars"]

d_data = load_data("D")
h1_data = load_data("H1")
m15_data = load_data("M15")

def get_context(data, ts, lookback=5):
    idx = -1
    for i, row in enumerate(data):
        if row["time"] <= ts:
            idx = i
        else:
            break
    if idx == -1: return []
    start = max(0, idx - lookback + 1)
    return data[start:idx+1]

candidates = [
    {"id": "candidate_2026-04-01T23-00-00Z_long_liquidity_sweep_reversal", "ts": "2026-04-01T23:00:00Z", "dir": "Long", "conf_ts": "2026-04-01T23:05:00Z", "fib_ts": "2026-04-01T23:45:00Z"},
    {"id": "candidate_2026-04-02T16-00-00Z_short_liquidity_sweep_reversal", "ts": "2026-04-02T16:00:00Z", "dir": "Short", "conf_ts": "2026-04-02T16:15:00Z", "fib_ts": "2026-04-02T16:30:00Z"},
    {"id": "candidate_2026-04-02T17-00-00Z_long_liquidity_sweep_reversal", "ts": "2026-04-02T17:00:00Z", "dir": "Long", "conf_ts": "2026-04-02T19:05:00Z", "fib_ts": "2026-04-02T19:20:00Z"}
]

for c in candidates:
    print(f"\n=== {c['id']} ===")
    ts = c['ts']
    d_ctx = get_context(d_data, ts, 3)
    print("Daily context:")
    for r in d_ctx: print(f"  {r['time']} O:{r['open']} H:{r['high']} L:{r['low']} C:{r['close']}")
    
    h1_ctx = get_context(h1_data, ts, 5)
    print("H1 context:")
    for r in h1_ctx: print(f"  {r['time']} O:{r['open']} H:{r['high']} L:{r['low']} C:{r['close']}")
    
    print("M15 context near execution:")
    m15_ctx = get_context(m15_data, c['fib_ts'], 5)
    for r in m15_ctx: print(f"  {r['time']} O:{r['open']} H:{r['high']} L:{r['low']} C:{r['close']}")
