V4 超級趨勢 — 富途 FTNN 合規重交(香港版)|任務 66148

A6(A65840)|2026-11-15|手冊:ftnn-indicator-manual.md(192,120 B/sha256 92a3c081… MATCH)| 計算層來源:上游 SuperTrendy(PUB;1db043e1ef7448bab4ca07049a3c82a3,© TheRealDrip2Rip,MPL-2.0,source sha eb793281…)

合規聲明:本檔零 import 行——無 __future__/collections/dataclasses/typing/math/json(66148 點名違規項全部避開); 只用 Python 原生運算+手冊內置函數(indicator/input_parameter/plot/plot_text/output_parameter);fdatetime/fmath/fta/ftool 零依賴(純算術用唔到)。 輸出對齊:①原碼字串版(LONG/SHORT 標籤逐字)②香港字眼版(做多/沽空)。
fixture 對數:A36 黃金 fixture 3fc8ce27(300 棒)計算層 PASS(0 diff @1e-9)——14 checkpoints×9 欄+latest 13 欄+labels(棒158 SHORT[A]74/棒240 LONG[A]82)全中; 標籤字串實證:EN「SHORT [A]|23071」「LONG [A]|21874」=原碼格式逐字;HK「沽空 [A]|23071」「做多 [A]|21874」。

① Futu 對應版(v4_futu_66148_hk.py 全文,貼入富途「自編指標 → Python 模式」)

# ═══════════════════════════════════════════════════════════════
# V4 超級趨勢(香港版)— 富途牛牛「自編指標 → Python 模式」FTNN 指標
# 任務 66148 重交(按 fttn-indicator-manual.md,192,120 B,sha256 92a3c081…)
# 合規聲明:零 import(無 __future__/collections/dataclasses/typing/math/json),
#   只用手冊內置函數+Python 原生運算;如需富途模組只可 fdatetime/fmath/fta/ftool(本檔用唔到佢哋)。
# 計算層:上游 SuperTrendy 原碼逐式(PUB;1db043e1ef7448bab4ca07049a3c82a3,
#   © TheRealDrip2Rip, MPL-2.0, source sha256 eb793281b88b…)
# 對數:A36 黃金 fixture 3fc8ce27(300 棒)計算層 0 diff @1e-9(見交付報告)
# 輸出層:①原碼字串版(LONG/SHORT 標籤逐字同源)②香港字眼版(做多/沽空)
# A6 (A65840)|2026-11-15
# ═══════════════════════════════════════════════════════════════

indicator("V4STHK", "V4 超級趨勢(香港版)", True,
          "SuperTrendy 移植:自適應倍數+ER 確認+品質評分;見 A6 交付報告")


# ── 計算引擎(純算術零 import;o/h/l/c/v 為逐棒數列;回傳逐棒 dict+翻轉事件)──
def v4_engine(o, h, l, c, v, atr_len=14, base_factor=3.5, er_len=10,
              confirm_bars=2, vol_sma_len=50, er_ema_len=3, roc_len=5,
              exhaust=2.8, mintick=1.0):
    n = len(c)
    rows = [None] * n
    events = []
    # True Range(棒 0:h-l)
    tr = [0.0] * n
    for i in range(n):
        pc = c[i - 1] if i > 0 else c[i]
        d1 = h[i] - l[i]
        d2 = h[i] - pc
        if d2 < 0:
            d2 = -d2
        d3 = l[i] - pc
        if d3 < 0:
            d3 = -d3
        m = d1
        if d2 > m:
            m = d2
        if d3 > m:
            m = d3
        tr[i] = m
    # Wilder ATR:頭 atr_len 棒 SMA 種子(棒 atr_len-1 首值),之前視為 na
    atr = [None] * n
    for i in range(atr_len - 1, n):
        if i == atr_len - 1:
            s = 0.0
            for j in range(0, i + 1):
                s = s + tr[j]
            atr[i] = s / atr_len
        else:
            atr[i] = (atr[i - 1] * (atr_len - 1) + tr[i]) / atr_len
    # Kaufman ER:棒 0..er_len-1 → 0.0(na false 分支,同源)
    er = [0.0] * n
    for i in range(er_len, n):
        d = c[i] - c[i - er_len]
        if d < 0:
            d = -d
        net = d
        total = 0.0
        for j in range(0, er_len):
            dd = c[i - j] - c[i - j - 1]
            if dd < 0:
                dd = -dd
            total = total + dd
        er[i] = net / total if total > 0 else 0.0
    # er_smooth = EMA(er, 3):alpha=2/(3+1)=0.5
    er_s = [0.0] * n
    for i in range(n):
        if i == 0:
            er_s[i] = er[0]
        else:
            er_s[i] = er_s[i - 1] + (er[i] - er_s[i - 1]) * (2.0 / (er_ema_len + 1))
    # vol_ratio = atr/SMA(atr,50);窗含 na → 1.0
    vol_r = [1.0] * n
    for i in range(vol_sma_len - 1, n):
        ok = True
        s = 0.0
        for j in range(i - vol_sma_len + 1, i + 1):
            if atr[j] is None:
                ok = False
                break
            s = s + atr[j]
        if not ok:
            continue
        base = s / vol_sma_len
        vol_r[i] = atr[i] / base if base > 0 else 1.0

    upper_band = None
    lower_band = None
    trend_dir = 1
    locked_bull = None
    streak = 0
    flip_quality = 0
    bull_ct = 0
    bear_ct = 0
    age = 0
    for i in range(n):
        has_atr = atr[i] is not None
        hl2 = (h[i] + l[i]) / 2.0
        vr = vol_r[i]
        if vr < 0.75:
            vr = 0.75
        if vr > 1.40:
            vr = 1.40
        af = base_factor * vr
        if er_s[i] < 0.25:
            ac = confirm_bars + 2
            if ac < 4:
                ac = 4
        elif er_s[i] < 0.45:
            ac = confirm_bars + 1
        elif er_s[i] < 0.65:
            ac = confirm_bars
        else:
            ac = confirm_bars - 1
            if ac < 1:
                ac = 1
        # 品質評分(暖機 na→false 分支→mom=0.0,同源)
        if (not has_atr) or i < roc_len:
            mom_norm = 0.0
        else:
            d5 = c[i] / c[i - roc_len] - 1.0
            if d5 < 0:
                d5 = -d5
            roc = d5 * 100.0
            atr_pct = atr[i] / c[i] * 100.0 if c[i] > 0 else 0.0
            if atr_pct > 0:
                mom_norm = roc / (atr_pct * 2.0)
                if mom_norm > 1.0:
                    mom_norm = 1.0
            else:
                mom_norm = 0.0
        dvr = 1.0 - vol_r[i]
        if dvr < 0:
            dvr = -dvr
        vol_score = 1.0 - dvr
        if vol_score < 0:
            vol_score = 0.0
        quality_raw = er_s[i] * 0.50 + vol_score * 0.30 + mom_norm * 0.20
        quality_pct = int(quality_raw * 100 + 0.5)   # math.round 同義(正數)
        if quality_pct >= 70:
            quality_grade = "A"
        elif quality_pct >= 50:
            quality_grade = "B"
        else:
            quality_grade = "C"
        # 手動 SuperTrend 帶(棘輪)
        if has_atr:
            ub = hl2 + af * atr[i]
            lb = hl2 - af * atr[i]
        else:
            ub = None
            lb = None
        prev_upper = upper_band
        prev_lower = lower_band
        if prev_upper is None or ub < prev_upper or (i > 0 and c[i - 1] > prev_upper):
            upper_band = ub
        if prev_lower is None or lb > prev_lower or (i > 0 and c[i - 1] < prev_lower):
            lower_band = lb
        if i == 0:
            trend_dir = 1
        elif lower_band is None:
            trend_dir = 1
        elif trend_dir == 1:
            trend_dir = -1 if c[i] < lower_band else 1
        else:
            trend_dir = 1 if c[i] > upper_band else -1
        st = lower_band if trend_dir == 1 else upper_band
        bull = trend_dir == 1
        # 確認過濾(streak)
        if locked_bull is None:
            locked_bull = bull
        if bull == locked_bull:
            streak = 0
        else:
            streak = streak + 1
        flip_up = streak >= ac and bull
        flip_down = streak >= ac and not bull
        any_flip = flip_up or flip_down
        if any_flip:
            locked_bull = bull
            flip_quality = quality_pct
            if flip_up:
                bull_ct = bull_ct + 1
            else:
                bear_ct = bear_ct + 1
            side = "up" if flip_up else "down"
            # 原碼字串版(100% 同源):LONG/SHORT [grade]\n mintick 價
            px = round(c[i] / mintick) * mintick
            px_s = ("%.2f" % px) if mintick < 1 else ("%d" % int(px + 0.5))
            en = ("LONG [" if flip_up else "SHORT [") + quality_grade + "]\n" + px_s
            hk = ("做多 [" if flip_up else "沽空 [") + quality_grade + "]\n" + px_s
            events.append(dict(i=i, side=side, price=c[i], quality=quality_pct,
                               grade=quality_grade, text_en=en, text_hk=hk))
        age = 0 if any_flip else age + 1
        s_time = age / 60.0
        if s_time > 1.0:
            s_time = 1.0
        s = s_time * 0.65 + er_s[i] * 0.35
        if st is not None:
            d = c[i] - st
            if d < 0:
                d = -d
            dist_pct = d / c[i] * 100.0
            dist_atr = d / atr[i] if (has_atr and atr[i] > 0) else 0.0
        else:
            dist_pct = None
            dist_atr = 0.0
        rows[i] = dict(st=st, dir=1 if trend_dir == 1 else -1, age=age, s=s,
                       er=er_s[i], vol_ratio=vol_r[i], adaptive_factor=af,
                       adaptive_confirm=ac, flip_quality=flip_quality,
                       quality_grade=quality_grade, quality_pct=quality_pct,
                       dist_pct=dist_pct, bull_ct=bull_ct, bear_ct=bear_ct,
                       locked_bull=locked_bull, flip_up=flip_up,
                       flip_down=flip_down, any_flip=any_flip,
                       is_exhausted=dist_atr >= exhaust)
    return dict(rows=rows, events=events)


# ── FTNN 輸出層(main_chart)──
if __name__ == "__main__":
    o = open()
    h = high()
    l = low()
    c = close()
    v = volume()
    out = v4_engine(o, h, l, c, v)
    rows = out["rows"]
    evs = out["events"]

    # SuperTrend 線(雙色:升=Color.up/跌=Color.down)
    st_seq = [r["st"] if r else None for r in rows]
    dir_seq = [r["dir"] if r else 1 for r in rows]
    plot('SuperTrend', st_seq, color=Color.up, style=Line.line, linewidth=2)
    plot('SuperTrendDn', [st_seq[i] if dir_seq[i] == -1 else None for i in range(len(st_seq))],
         color=Color.down, style=Line.line, linewidth=2)

    # 翻轉標籤(y=bar 低位/高位,同 TV 標籤視覺位;文字含收市價)
    up_text = [""] * len(rows)
    dn_text = [""] * len(rows)
    for e in evs:
        if e["side"] == "up":
            up_text[e["i"]] = e["text_hk"]      # 香港字眼版;換 text_en 即原碼字串版
        else:
            dn_text[e["i"]] = e["text_hk"]
    plot_text('做多標籤', [bool(r and r["flip_up"]) for r in rows], low(), up_text,
              color=Color.green, size=2, offsetv=-6)
    plot_text('沽空標籤', [bool(r and r["flip_down"]) for r in rows], high(), dn_text,
              color=Color.red, size=2, offsetv=6)

    # 看板數據(FTNN 無 table 元件 → output_parameter 輸出;11 行語義同源)
    last = rows[len(rows) - 1]
    output_parameter(
        direction=last["dir"], age=last["age"], s=last["s"],
        er_smooth=last["er"], vol_ratio=last["vol_ratio"],
        adaptive_factor=last["adaptive_factor"], adaptive_confirm=last["adaptive_confirm"],
        flip_quality=last["flip_quality"], dist_pct=last["dist_pct"],
        bull_ct=last["bull_ct"], bear_ct=last["bear_ct"],
        exhausted=last["is_exhausted"])

② 香港中文字眼映射(及用語差異申報)

原碼(EN)香港版備註
LONG做多主人 TV 圖用語(66145)
SHORT沽空主人 TV 圖用語(66145);fixture textZh 作「做空」、A36 報告作「造淡」——主人用語優先,其餘存異
[A]/[B]/[C][A]/[B]/[C]fixture textZh 作「[A級]」——本版跟 TV 樣式保留 [A]
Direction/Age/Conviction/Efficiency/Vol Regime/Adapt Factor/Last Flip/Distance/Long Flips/Short Flips當前方向/趨勢棒數/趨勢信念/趨勢效率/波動體制/動態乘數/最近翻轉/偏離距離/做多翻轉數/做空翻轉數66108 ②;閾值/數值格式不變

③ 輸出 100% 相同審計:計算層相同、輸出層差異逐項指明

#項目同/異說明
1計算層(st/dir/age/s/er/vol_ratio/af/ac/quality/dist/flip counts)✅ 100% 同源fixture 3fc8ce27 全欄 0 diff;等式逐行對應 Pine L46–L120
2標籤文字字串✅ 版①逐字同源"LONG ["+grade+"]\n"+mintick 價;版②香港字眼=映射差(上表)
3標籤錨位⚠️ 平台差異TV:label.new(bar, st, yloc=below/abovebar);FTNN 無 label 元件→plot_text(y=low()/high(), offsetv ∓6),文字含收市價(66145 重點:label 價=收市價)
4看板(dashboard)⚠️ 平台差異TV:table 11 行;FTNN 無 table→output_parameter 12 參數(11 行語義+exhausted)
5提示(alerts)⚠️ 未實作TV:alertcondition×3;FTNN 無此 API——差異如實申報
6視覺(glow/shading 漸變/barcolor/蠟燭染色)⚠️ 未實作純 ST 線+翻轉標籤;視覺層唔影響輸出數值
7mintick 價字串✅ 實證相同Pine str.tostring(format.mintick) vs 本地格式化;fixture 兩例逐字相同(23071/21874);0.25 tick 用 %.2f

④ 重現(任何人可覆核)

python3 - <<'EOF'
# 1) stub indicator() 後 exec 原檔(FTNN 層唔會跑)
# 2) v4_engine(o,h,l,c,v) 對 a36_fixture.json(3fc8ce27 內嵌 300 棒)
# 3) 逐欄比 expected_latest/expected_labels/expected_checkpoints(tol 1e-9)
# 結果:PASS (0 diff) —— A6 實測記錄喺 turn_log_66148_ftnn_redelivery.md
EOF

A6 (A65840)|66148 重交件|v4_futu_66148_hk.py 零 import 違規|fixture 3fc8ce27 計算層 0 diff|輸出層 5 項平台差異已申報。