
import json
from datetime import datetime, timezone

def to_iso(ts):
    return datetime.fromtimestamp(ts, tz=timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')

def get_bars(filepath, start, end):
    with open(filepath) as f:
        data = json.load(f)
    bars = data.get('bars', [])
    res = []
    for b in bars:
        t = b['time']
        if isinstance(t, int):
            # TradingView timestamps are sometimes in seconds, sometimes milliseconds. Let's assume seconds if < 2000000000
            t = to_iso(t)
        if start <= t <= end:
            res.append((t, b))
    return res
    
print("\nM5:")
bars = get_bars('/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data/ohlcv_M5.json', '2026-01-13T10:45:00Z', '2026-01-13T17:00:00Z')
for t, b in bars:
    print(f"{t} O:{b['open']} H:{b['high']} L:{b['low']} C:{b['close']}")
