import json

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

def get_context(target_iso, tf, next_bars_count=12):
    out = []
    try:
        with open(f"{data_dir}/ohlcv_{tf}.json", 'r') as f:
            data = json.load(f)
            bars = data.get('bars', [])
        
        idx = -1
        # timestamp comparison - handle int vs str
        if bars and isinstance(bars[0]['time'], int):
            # M5 might have unix timestamps?
            import datetime
            target_ts = int(datetime.datetime.strptime(target_iso, "%Y-%m-%dT%H:%M:%SZ").timestamp())
            for i, b in enumerate(bars):
                if b['time'] <= target_ts:
                    idx = i
                else:
                    break
        else:
            for i, b in enumerate(bars):
                if b['time'] <= target_iso:
                    idx = i
                else:
                    break
            
        if idx != -1:
            out = bars[max(0, idx-10):idx+next_bars_count+1]
    except Exception as e:
        print(f"Error: {e}")
        pass
    return out

for ts in ['2026-04-07T15:30:00Z', '2026-04-08T03:15:00Z', '2026-04-08T03:30:00Z']:
    res = get_context(ts, 'M5', next_bars_count=36)
    print(f"\n=== Candidate: {ts} Long M5 ===")
    for b in res:
        if isinstance(b, dict):
            import datetime
            time_str = b['time']
            if isinstance(time_str, int):
                time_str = datetime.datetime.utcfromtimestamp(time_str).strftime('%Y-%m-%dT%H:%M:%SZ')
            prefix = "--> " if time_str == ts else "    "
            print(f"{prefix}{time_str} O:{b['open']} H:{b['high']} L:{b['low']} C:{b['close']}")

