import json, os, datetime
groups = {
 "Mega-cap tech":       ["NVDA","GOOGL","AAPL","MSFT","AMZN","META","TSLA","AVGO","NFLX","ORCL","CRM","ADBE"],
 "Semis":               ["TSM","QCOM","TXN","INTC","AMAT","LRCX","KLAC","ADI","MCHP","ON","MU","MRVL"],
 "Software / internet": ["NOW","INTU","CRWD","SNPS","CDNS","ANET","DELL","IBM","CSCO","PANW","PLTR","SHOP","UBER","PYPL"],
 "High-beta":           ["AMD","SMCI","COIN","MSTR","SNOW"],
 "Energy & oil":        ["XOM","CVX","COP","SLB","EOG","OXY","KMI","WMB"],
 "Financials":          ["JPM","V","MA","BAC","WFC","GS","MS","AXP"],
 "Healthcare":          ["LLY","UNH","JNJ","ABBV","MRK","PFE","TMO"],
 "Consumer":            ["WMT","COST","HD","NKE","DIS","PG","KO","PEP","MCD"],
 "Industrials & defense":["CAT","DE","HON","GE","UNP","LMT","RTX"],
 "Utilities & materials":["NEE","LIN","FCX"],
 "Index ETF":           ["SPY","QQQ"],
}
tickers=[t for g in groups.values() for t in g]
assert len(tickers)==len(set(tickers)), "dup"
all_dates={}; data={}
def load(t):
    with open(f"data/{t}.json") as f: j=json.load(f)
    r=j["chart"]["result"][0]; ts=r["timestamp"]; q=r["indicators"]["quote"][0]
    o,h,l,c=q["open"],q["high"],q["low"],q["close"]; rows={}
    for i,tt in enumerate(ts):
        if None in (o[i],h[i],l[i],c[i]): continue
        d=datetime.datetime.utcfromtimestamp(tt).strftime("%Y-%m-%d")
        rows[d]=[round(o[i],2),round(h[i],2),round(l[i],2),round(c[i],2)]; all_dates[d]=True
    return rows
for t in tickers: data[t]=load(t)
dates=sorted(all_dates.keys())
out={"dates":dates,"groups":groups,"tickers":{}}
for t in tickers:
    O=[];H=[];L=[];C=[];first=None
    for d in dates:
        r=data[t].get(d)
        if r:
            if first is None: first=d
            O.append(r[0]);H.append(r[1]);L.append(r[2]);C.append(r[3])
        else:O.append(None);H.append(None);L.append(None);C.append(None)
    out["tickers"][t]={"o":O,"h":H,"l":L,"c":C,"first":first}
with open("prices.json","w") as f: json.dump(out,f,separators=(",",":"))
def ret(t):
    c=out["tickers"][t]["c"]; a=next((x for x in c if x),None); b=next((x for x in reversed(c) if x),None)
    return (b/a-1)*100 if a else 0
print("tickers:",len(tickers),"| dates:",len(dates),"| size:",round(os.path.getsize("prices.json")/1e6,2),"MB")
bad=[t for t in tickers if out["tickers"][t]["first"]!=dates[0]]
print("partial history:", bad or "none")
r=sorted([(t,round(ret(t))) for t in tickers],key=lambda x:x[1])
print("worst 8:",r[:8]); print("best 8:",r[-8:])
