#!/usr/bin/env python3
"""Misst, ob die Konfidenz (conf_pct aus _build) den Edge vorhersagt: bringt ein
Mindest-Konfidenz-Gate etwas? Live-Konfig M5 + M30-Filter + Winkel + Tageszeit."""
import sys
import MetaTrader5 as mt5
from core.analysis import calc_trend_angle
from core.wave_rec import (WaveRecommender, _atr, _ema_last, _EMA_FAST, _EMA_SLOW,
                           _N_BARS, _HTF_DEADBAND, _ANGLE_LR)


class _TU:
    def snapshot(self): return {"intervals": {}}
def _ema_series(v,p):
    k=2.0/(p+1); o=[]; e=v[0]
    for i,x in enumerate(v): e=x if i==0 else x*k+e*(1-k); o.append(e)
    return o
def _atr_series(H,L,C,p=14):
    t=[0.0]
    for i in range(1,len(C)): t.append(max(H[i]-L[i],abs(H[i]-C[i-1]),abs(L[i]-C[i-1])))
    return [(sum(t[max(1,i-p+1):i+1])/max(1,len(t[max(1,i-p+1):i+1]))) if i else None for i in range(len(C))]
def _rep(name,r):
    if not r: print(f"  {name:<14} -"); return
    n=len(r); w=sum(1 for x in r if x>0)
    print(f"  {name:<14} n={n:>4} Treffer={100*w/n:>3.0f}% Oe-Edge={sum(r)/n:+.4f} Summe={sum(r):+.1f}")

def main():
    n=int(sys.argv[1]) if len(sys.argv)>1 else 12000
    K=10
    mt5.initialize()
    sym=None
    for c in ("SpotCrude","USOIL","WTI","XTIUSD"):
        if mt5.symbol_info(c): sym=c; break
    bars=mt5.copy_rates_from_pos(sym,mt5.TIMEFRAME_M5,0,n+_N_BARS+K+5)
    m30=mt5.copy_rates_from_pos(sym,mt5.TIMEFRAME_M30,0,n//6+400)
    mt5.shutdown()
    T=[int(b["time"]) for b in bars]; H=[float(b["high"]) for b in bars]
    L=[float(b["low"]) for b in bars]; C=[float(b["close"]) for b in bars]
    mT=[int(b["time"]) for b in m30]; mc=[float(b["close"]) for b in m30]
    mh=[float(b["high"]) for b in m30]; ml=[float(b["low"]) for b in m30]
    mEf=_ema_series(mc,_EMA_FAST); mEs=_ema_series(mc,_EMA_SLOW); mA=_atr_series(mh,ml,mc)
    def m30s(ts):
        lo,hi,idx=0,len(mT)-1,-1
        while lo<=hi:
            md=(lo+hi)//2
            if mT[md]<=ts: idx=md; lo=md+1
            else: hi=md-1
        if idx<_EMA_SLOW or mA[idx] is None or mA[idx]<=0: return 0
        d=mEf[idx]-mEs[idx]
        return 0 if abs(d)<_HTF_DEADBAND*mA[idx] else (1 if d>0 else -1)

    w=WaveRecommender(_TU(), mt5.TIMEFRAME_M5)
    buckets={'<40':[], '40-54':[], '55-69':[], '70-84':[], '85+':[]}
    for i in range(_N_BARS, len(C)-K):
        wc=C[i-_N_BARS:i]; wh=H[i-_N_BARS:i]; wl=L[i-_N_BARS:i]
        atr=_atr(wh,wl,wc)
        if not atr or atr<=0: continue
        ef=_ema_last(wc,_EMA_FAST); es=_ema_last(wc,_EMA_SLOW)
        ang=calc_trend_angle(C[i-_ANGLE_LR-2:i],_ANGLE_LR)
        rec,_=w._build(ef,es,C[i-1],atr,"M5",5,htf_trend=m30s(T[i]),angle=ang)
        if rec["signal"]=="WARTEN": continue
        d=1 if rec["signal"]=="LONG" else -1
        r=(C[i+K]-C[i])*d
        cf=rec["conf_pct"]
        b=('<40' if cf<40 else '40-54' if cf<55 else '55-69' if cf<70 else '70-84' if cf<85 else '85+')
        buckets[b].append(r)

    print("="*60); print(f"  Konfidenz vs Edge — {sym} M5+M30  Vorlauf={K}"); print("="*60)
    for b in ('<40','40-54','55-69','70-84','85+'): _rep(b, buckets[b])
    # kumuliert ab Schwelle
    print("\nKumuliert ab Mindest-Konfidenz:")
    order=['<40','40-54','55-69','70-84','85+']; lo=[0,40,55,70,85]
    allr=[]
    for b in order: allr+=buckets[b]
    tot=len(allr)
    for k,thr in enumerate(lo):
        rr=[]
        for j in range(k,len(order)): rr+=buckets[order[j]]
        if rr:
            win=sum(1 for x in rr if x>0)
            print(f"  conf>={thr:>2}: n={len(rr):>4} ({100*len(rr)/tot:>3.0f}%) "
                  f"Treffer={100*win/len(rr):>3.0f}% Oe-Edge={sum(rr)/len(rr):+.4f}")

if __name__=="__main__":
    main()
