import sys; sys.path.insert(0, "/root/fwa1")
exec(open("/root/fwa1/t4b.py").read().split("ev = {}")[0])
allsw = ws.execute("select crew, token, b, side, eth, wallet from sw where wallet is not null and crew in (?,?) and day>='2026-09-21' order by b", (A, BC)).fetchall()
pos = {}
for cr, tok, b, side, eth, w in allsw:
    w = w.lower(); k = (tok, w)
    if k not in pos: pos[k] = dict(crew=cr, fb=None, fe=0, fs=None)
    p = pos[k]
    if side == 1 and p["fb"] is None: p["fb"] = b; p["fe"] = eth
    if side == -1 and p["fb"] is not None and p["fs"] is None: p["fs"] = b
st = collections.defaultdict(lambda: [0, 0, 0.0])
for (tok, w), p in pos.items():
    if p["fb"] is None or tok not in coin: continue
    f = ff.get(w)
    if f is None: continue
    fund = (f[0] or "").lower(); via = (f[1] or "").lower()
    if w in crewset or fund in dist or via in dist: cls = "crew"
    elif w in rep or fund in crewset: continue
    else: cls = "outside"
    b0, pull = coin[tok]; age = (p["fb"] - b0) / 10
    if age < 0 or age > 300: continue
    if pull is not None and pull <= p["fb"]: continue
    hold = (p["fs"] - p["fb"]) / 10 if p["fs"] else 9e9
    hk = "sold<10s" if hold < 10 else ("held>=10s")
    sk = next(n for lo, n in ((0.075, "e 0.075+"), (0.035, "d 0.035-0.075"), (0.015, "c2 0.015-0.035"), (0.0075, "c1 0.0075-0.015"), (0.0045, "b 0.0045-0.0075"), (0.002, "a2 0.002-0.0045"), (0, "a1 <0.002")) if p["fe"] >= lo)
    pulled = pull is not None and pull - p["fb"] <= 600
    for key in ((cls, hk, sk), (cls, hk, "all"), (cls, hk, sk, p["crew"][:6])):
        s = st[key]; s[0] += 1; s[1] += pulled; s[2] += hazard(p["crew"], age)
for k in sorted(st):
    n, pp, e = st[k]
    print(k, n, "pulled<=60s %d (%.0f%%)" % (pp, 100 * pp / n), "expected %.1f" % e, "lift %.1fx" % (pp / e if e else 0))
print("---- b bucket held detail")
for (tok, w), p in pos.items():
    if p["fb"] is None or tok not in coin or not (0.002 <= p["fe"] < 0.0075): continue
    f = ff.get(w)
    if f is None: continue
    fund = (f[0] or "").lower(); via = (f[1] or "").lower()
    if w in crewset or fund in dist or via in dist or w in rep or fund in crewset: continue
    b0, pull = coin[tok]; age = (p["fb"] - b0) / 10
    if age < 0 or age > 300 or (pull is not None and pull <= p["fb"]): continue
    hold = (p["fs"] - p["fb"]) / 10 if p["fs"] else 9e9
    if hold < 10: continue
    print(w, round(p["fe"], 4), p["crew"][:6], tok, "age", round(age, 1), "pull after buy s", (pull - p["fb"]) / 10 if pull else None)
