import json, gzip, datetime as dt, bisect, math, statistics as S
T = json.load(open('tokens.json')); toks = {t['address']: t for t in T['tokens']}; sup = T['supply']
kl = json.load(gzip.open('kline.json.gz', 'rt'))
def daily(chain, N, lo_rank=0):
    a = [x for x in kl if toks[x]['chain'] == chain and toks[x]['crossed_ts'] and sup.get(x)]
    ts = {x: [r[0] for r in kl[x]] for x in a}; cl = {x: [r[4] for r in kl[x]] for x in a}
    def px(x, t):   # last close at or before t, within 24h
        i = bisect.bisect_right(ts[x], t) - 1
        if i < 0 or t - ts[x][i] > 86400: return None
        return cl[x][i]
    t0 = min(min(ts[x]) for x in a); t0 = (t0 // 86400 + 1) * 86400; t1 = max(max(ts[x]) for x in a)
    lvl = 1.0; line = []; mem = None
    t = t0
    while t + 86400 <= t1 + 3600:
        d = dt.datetime.utcfromtimestamp(t)
        if mem is None or d.weekday() == 0:
            c = [(px(x, t) * sup[x], x) for x in a if toks[x]['crossed_ts'] <= t and px(x, t)]
            c.sort(reverse=True); m = [x for _, x in c[lo_rank:lo_rank + N]]
            mem = m if len(m) >= min(5, N) else None
        if mem:
            rs = []
            for x in mem:
                p0, p1 = px(x, t), px(x, t + 86400)
                if p0 and p1: rs.append(max(-0.9, min(2.0, p1 / p0 - 1)))
                elif p0 and not p1: rs.append(-0.5)
            if len(rs) >= 3: lvl *= 1 + sum(rs) / len(rs)
            line.append([(d + dt.timedelta(1)).date().isoformat(), lvl])
        t += 86400
    return line
M = json.load(open('market.json'))
def official(idx):
    h = [i for i in M['indices'] if i['id'] == idx][0]['history']
    by = {}
    for t, v in h:
        if v: by[dt.datetime.utcfromtimestamp(t - 1).date() + dt.timedelta(1)] = v
    return [[d.isoformat(), v] for d, v in sorted(by.items())]
out = {"rh_top30": daily('robinhood', 30), "rh_top10": daily('robinhood', 10), "rh_mid": daily('robinhood', 20, 10),
       "sol_top30": daily('sol', 30)}
for i in M['indices']: out['off_' + i['id']] = official(i['id'])
for k, v in out.items(): print(k, len(v), v[0][0] if v else None, v[-1] if v else None)
json.dump(out, open('rh_lines.json', 'w'))
