"""v3ovl.py (A1.15): overlap of mc5 (A1.14 rule) with the 2nd window v3 rule (w2_v3_1m, A2.14 trades, $500), and the combined book."""
import csv, collections, statistics as st
src = open("/root/fwa1/v3frag.py").read().split("random.seed(7)")[0]
exec(src)
R = book(300, 2.5e5, 1e6)
tokof = {r[0]: r[1] for r in D.execute("SELECT pool, token FROM meta")}
mc5 = {}
for p, v, why, t in R: mc5[tokof[p["pool"]].lower()] = (p, v, why)
W2 = {}
for r in csv.DictReader(open("/root/fwa1/v3/a214.csv")):
    if r["V"] == "500": W2[r["tok"].lower()] = (r["day"], float(r["x_new"]), r["half"])
both = [t for t in W2 if t in mc5]
print("mc5 trades", len(mc5), "w2 trades", len(W2), "same coin", len(both))
# w2 coins: what was their mc at +5 min (were they in the universe at all)?
pinfo = {tokof[p["pool"]].lower(): p for p in pools}
miss = collections.Counter()
for t in W2:
    if t in mc5: continue
    p = pinfo.get(t)
    if not p: miss["mc5 under $100k or not in v3 2 h data"] += 1; continue
    m = p["feats"][300]["mc"]
    miss["mc5 %s" % ("<250k" if m < 2.5e5 else ">=1M" if m >= 1e6 else "in band, no depth")] += 1
print("w2 coins not traded by mc5:", dict(miss))
print("mc5 coins that hit $1M within 2 h (t1m):", sum(1 for t, (p, v, w) in mc5.items() if p["t1m"]), "of", len(mc5))
print("\n%-44s %-6s %8s %8s" % ("token (traded by both)", "day", "mc5 x", "w2 x"))
for t in both:
    print("%-44s %-6s %8.2f %8.2f" % (t, W2[t][0][5:], mc5[t][1], W2[t][1]))
m5b = [mc5[t][1] for t in both]; w2b = [W2[t][1] for t in both]
if both: print("on shared coins: mc5 mean %.2fx, w2 mean %.2fx" % (st.mean(m5b), st.mean(w2b)))
# combined book: mc5 $100 + w2 $500, by day; exposure on shared coins = $600
byd = collections.defaultdict(lambda: [0.0, 0.0, 0.0])
for t, (p, v, why) in mc5.items(): byd[day(p["t"])][0] += (v - 1) * 100
for t, (d, x, h) in W2.items(): byd[d[5:]][1] += (x - 1) * 500
for t in both: byd[W2[t][0][5:]][2] += 1
tot5 = sum(b[0] for b in byd.values()); totw = sum(b[1] for b in byd.values())
print("\nmc5 alone $%.0f, w2 alone $%.0f, combined $%.0f over %d days" % (tot5, totw, tot5 + totw, len(byd)))
print("green days: mc5 %d, w2 %d, combined %d of %d" % (sum(b[0] > 0 for b in byd.values()), sum(b[1] > 0 for b in byd.values() if b[1] != 0), sum(b[0] + b[1] > 0 for b in byd.values()), len(byd)))
print("worst day combined $%.0f, worst day mc5 $%.0f, w2 $%.0f" % (min(b[0] + b[1] for b in byd.values()), min(b[0] for b in byd.values()), min(b[1] for b in byd.values())))
dd = sorted(byd)
import numpy as np
a = np.array([byd[d][0] for d in dd]); b = np.array([byd[d][1] for d in dd])
print("day P&L correlation mc5 vs w2: %.2f" % np.corrcoef(a, b)[0, 1])
non = [v for t, (p, v, w) in mc5.items() if t not in W2]
print("mc5 on coins w2 never traded: n%d mean %.2fx median %.2fx, $%.0f" % (len(non), st.mean(non), st.median(non), sum(x - 1 for x in non) * 100))
for h in (1, 2):
    nn = [v for t, (p, v, w) in mc5.items() if t not in W2 and (p["t"] < CUT) == (h == 1)]
    print("  half %d: n%d mean %.2fx, without top 3 %.2fx" % (h, len(nn), st.mean(nn), st.mean(sorted(nn)[:-3])))
    print("  top 5:", [(t, round(v, 1)) for t, (p, v, w) in sorted(mc5.items(), key=lambda x: -x[1][1]) if t not in W2 and (p["t"] < CUT) == (h == 1)][:5])
hit = [v for t, (p, v, w) in mc5.items() if t not in W2 and p["t1m"]]
print("  of those, hit $1M in 2 h but failed w2 gate: n%d mean %.2fx; never hit $1M: n%d mean %.2fx" % (len(hit), st.mean(hit), len(non) - len(hit), st.mean([v for t, (p, v, w) in mc5.items() if t not in W2 and not p["t1m"]])))
