"""Does X chatter at the basket BUY tell which coins will outperform inside the basket? Solana + Base baskets 2023-26."""
import pickle, json, math, statistics as S
TR = pickle.load(open('trades_new_50.pkl', 'rb')); D = pickle.load(open('data_new.pkl', 'rb')); X = json.load(open('xdata.json'))
rows = []
for t in TR:
    if not (t['k'].startswith('sol_') or t['k'].startswith('base_')): continue
    rr = [r for r in t['rows'] if D['H'][r['c']][t['entry']][2] >= 100000][:20]
    if len(rr) < 6: continue
    pool = []
    for r in rr:
        x = X.get('%s|%s' % (r['c'], t['entry']))
        if not x or 'now' not in x: continue
        pool.append(dict(c=r['c'], out=r['out'], beat=r['out'] > t['ret'], rate=x['now']['rate'], prev=x['before']['rate'], authors=x['now']['authors'], likes=x['now']['likes'],
                         n=x['now']['n'], half='2023-24' if t['entry'] < '2025' else '2025-26'))
    if len(pool) < 6: continue
    # rank inside the pool
    for f in ('rate', 'growth', 'likes'):
        if f == 'growth':
            for p in pool: p['growth'] = (p['rate'] + 1) / (p['prev'] + 1)
        srt = sorted(pool, key=lambda p: p[f])
        for i, p in enumerate(srt): p[f + '_rk'] = i / (len(srt) - 1)
    rows += pool
print('coin picks with X data:', len(rows), '| quiet (0 to 2 posts in the 7 days):', sum(1 for r in rows if r['n'] <= 2))
def show(lab, groups):
    print(lab)
    for g, f in groups:
        for h in ('2023-24', '2025-26', None):
            xs = [r for r in rows if f(r) and (h is None or r['half'] == h)]
            if len(xs) < 15: continue
            print('   %-38s %-8s n %4d | beat the basket %2.0f%% | median %+5.1f%% | avg (capped 5x) %+6.1f%%' % (g, h or 'all', len(xs), 100 * S.mean(r['beat'] for r in xs), 100 * S.median(r['out'] for r in xs), 100 * S.mean(min(5, r['out']) for r in xs)))
show('By chatter level inside the basket (posts per day, ranked within the pool):', [
    ('quietest fifth', lambda r: r['rate_rk'] < .2), ('2nd fifth', lambda r: .2 <= r['rate_rk'] < .4), ('middle fifth', lambda r: .4 <= r['rate_rk'] < .6),
    ('4th fifth', lambda r: .6 <= r['rate_rk'] < .8), ('loudest fifth', lambda r: r['rate_rk'] >= .8)])
show('Dead vs alive:', [('dead (0 to 2 posts in 7 days)', lambda r: r['n'] <= 2), ('alive', lambda r: r['n'] > 2)])
show('Chatter change vs 2 weeks earlier:', [('falling (under 0.7x)', lambda r: r['growth'] < .7), ('flat (0.7 to 1.5x)', lambda r: .7 <= r['growth'] <= 1.5),
     ('rising (1.5 to 3x)', lambda r: 1.5 < r['growth'] <= 3), ('surging (over 3x)', lambda r: r['growth'] > 3)])
show('Engagement per post (likes), ranked in the pool:', [('lowest third', lambda r: r['likes_rk'] < .33), ('middle third', lambda r: .33 <= r['likes_rk'] < .67), ('highest third', lambda r: r['likes_rk'] >= .67)])
show('Happy middle? chatter rank 30 to 70% AND not surging:', [('middle + not surging', lambda r: .3 <= r['rate_rk'] <= .7 and r['growth'] <= 3), ('everything else', lambda r: not (.3 <= r['rate_rk'] <= .7 and r['growth'] <= 3))])
