import json

with open("hybrid/data/ohlcv_M30.json", "r") as f:
    m30 = json.load(f)["bars"]
with open("hybrid/data/ohlcv_M15.json", "r") as f:
    m15 = json.load(f)["bars"]
with open("hybrid/data/ohlcv_M5.json", "r") as f:
    m5 = json.load(f)["bars"]

timestamps = [
    "2026-06-25T07:45:00Z",
    "2026-06-25T10:30:00Z",
    "2026-06-25T11:30:00Z"
]

out = {}
for ts in timestamps:
    out[ts] = {}
    
    # M30
    for i, b in enumerate(m30):
        if b["time"] == ts:
            out[ts]["m30"] = m30[max(0, i-5):i+2]
            break
        elif b["time"] > ts and "m30" not in out[ts]:
            # if exact ts doesn't exist (e.g. M30 bar is on the hour/half-hour)
             out[ts]["m30"] = m30[max(0, i-6):i+1]
             break
            
    # M15
    for i, b in enumerate(m15):
        if b["time"] == ts:
            out[ts]["m15"] = m15[max(0, i-6):i+3]
            break
            
    # M5
    for i, b in enumerate(m5):
        if b["time"] == ts:
            out[ts]["m5"] = m5[max(0, i-10):i+5]
            break

with open("data_705_708.json", "w") as f:
    json.dump(out, f, indent=2)

