import json

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

def get_context(target_iso, next_bars_count=12):
    out = {}
    for tf in ['H4', 'H1', 'M30', 'M15', 'M5']:
        try:
            with open(f"{data_dir}/ohlcv_{tf}.json", 'r') as f:
                data = json.load(f)
                bars = data.get('bars', [])
            
            idx = -1
            for i, b in enumerate(bars):
                if b['time'] <= target_iso:
                    idx = i
                else:
                    break
            
            if idx != -1:
                # Get history and future bars
                out[tf] = bars[max(0, idx-10):idx+next_bars_count+1]
        except Exception as e:
            out[tf] = [] # hide errors
    return out

ts = '2026-04-07T15:30:00Z'
res = get_context(ts, next_bars_count=16)
print(f"\n=== Candidate: {ts} Long ===")
for tf in ['H4', 'H1', 'M30', 'M15', 'M5']:
    print(f"\n{tf}:")
    for b in res.get(tf, []):
        if isinstance(b, dict):
            prefix = "--> " if b['time'] == ts or (tf != 'M5' and b['time'] <= ts and (res[tf].index(b) == len(res[tf])-1 or res[tf][res[tf].index(b)+1]['time'] > ts)) else "    "
            print(f"{prefix}{b['time']} O:{b['open']} H:{b['high']} L:{b['low']} C:{b['close']}")

