import json

files = {
    'H4': '/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_H4.json',
    'H1': '/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_H1.json',
    'M30': '/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_M30.json',
    'M15': '/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_M15.json',
    'M5': '/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_M5.json'
}

timestamps = [
    "2026-07-05T23:15:00Z",
    "2026-07-06T03:30:00Z",
    "2026-07-06T07:00:00Z"
]

for ts in timestamps:
    print(f"\n--- {ts} ---")
    for tf, file in files.items():
        with open(file, 'r') as f:
            data = json.load(f)
            # handle format where data might be a dict with a list inside, or just a list
            if isinstance(data, dict):
                bars = data.get('data', data.get('bars', []))
            else:
                bars = data
        
        target_idx = -1
        for i, b in enumerate(bars):
            if isinstance(b, dict) and 'time' in b:
                if b['time'] <= ts:
                    target_idx = i
                
        if target_idx != -1:
            start_idx = max(0, target_idx - 6) # Look back a bit further
            context_bars = bars[start_idx:target_idx+5] # Look ahead a few bars
            print(f"\n{tf} Context:")
            for b in context_bars:
                flag = "-->" if b['time'] == bars[target_idx]['time'] else "   "
                print(f"{flag} {b['time']} | O: {b['open']} | H: {b['high']} | L: {b['low']} | C: {b['close']}")
        else:
            print(f"{tf} Context: No bars found before/at {ts}")
