import json
from datetime import datetime

def analyze_bars():
    with open('/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_H4.json', 'r') as f:
        h4_data = json.load(f)['bars']
    with open('/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_H1.json', 'r') as f:
        h1_data = json.load(f)['bars']
    with open('/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_M30.json', 'r') as f:
        m30_data = json.load(f)['bars']
    with open('/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_M15.json', 'r') as f:
        m15_data = json.load(f)['bars']
    with open('/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_M5.json', 'r') as f:
        m5_data = json.load(f)['bars']

    target_dates = [
        "2026-07-08T12:00:00Z",
        "2026-07-08T18:30:00Z",
        "2026-07-08T22:45:00Z"
    ]

    for target_date in target_dates:
        print(f"\n--- Context for {target_date} ---")
        
        # H4
        h4_bars = [b for b in h4_data if b['time'] <= target_date]
        if h4_bars:
            print("Recent H4 bars:")
            for b in h4_bars[-3:]:
                print(f"  {b['time']}: O={b['open']} H={b['high']} L={b['low']} C={b['close']}")
        
        # H1
        h1_bars = [b for b in h1_data if b['time'] <= target_date]
        if h1_bars:
            print("Recent H1 bars:")
            for b in h1_bars[-3:]:
                print(f"  {b['time']}: O={b['open']} H={b['high']} L={b['low']} C={b['close']}")

        # M30
        m30_bars = [b for b in m30_data if b['time'] <= target_date]
        if m30_bars:
            print("Recent M30 bars:")
            for b in m30_bars[-5:]:
                print(f"  {b['time']}: O={b['open']} H={b['high']} L={b['low']} C={b['close']}")
                
        # M15
        m15_bars = [b for b in m15_data if b['time'] <= target_date]
        if m15_bars:
            print("Recent M15 bars:")
            for b in m15_bars[-5:]:
                print(f"  {b['time']}: O={b['open']} H={b['high']} L={b['low']} C={b['close']}")
                
        # M5 (use timestamp field)
        m5_bars = [b for b in m5_data if b['timestamp'] <= target_date]
        m5_future_bars = [b for b in m5_data if b['timestamp'] > target_date]
        if m5_bars:
            print("Recent M5 bars (setup):")
            for b in m5_bars[-5:]:
                print(f"  {b['timestamp']}: O={b['open']} H={b['high']} L={b['low']} C={b['close']}")
        if m5_future_bars:
            print("Future M5 bars (outcome):")
            for b in m5_future_bars[:15]:
                print(f"  {b['timestamp']}: O={b['open']} H={b['high']} L={b['low']} C={b['close']}")

if __name__ == '__main__':
    analyze_bars()
