A25|V4 超級趨勢 Futu Python 重交(list-state candidate)

Host 66206 指示採 oldest→newest scalar-state scan;手冊 v2.0.9。此交付是可審源碼 candidate,未冒稱已在 Futu GUI 或實際 TradingView chart 完成 runtime 驗收。

遞歸轉換方案:每棒以 scalar _upper_band/_lower_band/_trend_dir carry state forward;序列輸出以原生 Python lists 建立。Indicator source 沒有 import(無 math/typing/dataclasses 等外部或標準模組)。

本地驗證結果

項目結果範圍
Python syntaxPASSAST parse
手冊 import 約束PASS(零 import)indicator file
實際 Pine companion exact engine 比較PASS,0 mismatch,最大差 0300 bar × 13 fields = 3,900 comparisons
信號 index/direction/grade/qualityPASSbar 158 SHORT/A/74;bar 240 LONG/A/82
Futu GUI / runtime list acceptance未驗需把 candidate 載入指標編輯器並套用;本機/fixture 測試不能證明 Futu API 接受 built-in lists
實際 TradingView 圖表逐棒輸出未驗exact engine 不是 TV logged-in runtime/export

未決平台核驗

源碼與 Pine exact companion engine 的 3,900 項對數全部相同,但仍需 Futu 指標編輯器確認 plot/plot_text/plot_icon/output_parameter 接受原生 list,及序列方向是否 oldest→newest。若 GUI 拒絕 list,請保存實際錯誤訊息;不得把 fixture PASS 說成平台 runtime PASS。由於現缺主人 TV 對同一合約/週期/設定的逐棒輸出,本交付不宣稱實際 TV 100% 驗證。

Futu Python source

Filename: A25_V4_SUPERTREND_FUTU_LIST_STATE.py · 12078 bytes · SHA-256 872ec21e5d5e365501f2ee8e3ba116fe78c3c0d3ca4a4f0f924ee6cd334af362

# -*- coding: utf-8 -*-
"""V4 超級趨勢(自適應版 · 香港中文版)—— 富途牛牛 / moomoo PC「自編指標」Python 模式(主圖)

原碼基準:http://149.118.48.126:8080/v4-supertrend-zh-v6.txt (16,808 B, sha256=4d05ef4c...)
歷史 fixture 對 A36 原 Python 計算與 checkpoints/flip labels 的測試相符;這不是 TV runtime 驗證或 Futu GUI 驗收。
本版本以 Python 原生 list 輸出及 scalar state forward scan 改寫遞歸,需在 FTNN 編輯器驗證型別與平台支援後才可確認可執行。
  - ATR(14) Wilder RMA(首 14 支以 SMA 種植)
  - 考夫曼效率比率 ER(10) + EMA(3) 平滑(暖機棒 0..9 因 total_path 為 na 故 er = 0.0)
  - 波幅狀態:ATR / SMA(ATR, 50),限幅 [0.75, 1.40] 縮放基本倍數 3.5
  - 自適應確認燭數:ER < 0.25 ⇒ max(confirm+2, 4);< 0.45 ⇒ confirm+1;< 0.65 ⇒ confirm;≥ 0.65 ⇒ max(confirm-1, 1)
  - 轉勢質素評分 (0–100):50% ER + 30% 波幅正常度 + 20% 5燭動能,分 A(≥70) / B(50–69) / C(<50) 三級
  - 配色嚴格對齊原碼:造好 #26A69A(松石綠)、造淡 #EF5350(珊瑚紅)
"""

indicator("V4HK", "V4 超級趨勢(自適應香港版)", True,
          "自適應超級趨勢(香港中文用語):考夫曼效率比率 + 50燭波幅狀態 + 確認過濾 + A/B/C 轉勢評級")

_I_LEN     = input_parameter("ATR週期", 14)
_I_FACTOR  = input_parameter("基本倍數", 3.5)
_I_ER_LEN  = input_parameter("效率比率週期", 10)
_I_CONFIRM = input_parameter("基本確認燭數", 2)
_I_GLOW    = input_parameter("顯示發光光暈(1開/0關)", 1)

# Built-ins only: NaN sentinel without importing a non-manual module.
_NAN = float("nan")
def _is_na(x):
    return x != x


if __name__ == "__main__":
    c = close()
    o = open()
    h = high()
    l = low()
    n = len(c)

    _len = int(_I_LEN)
    if _len < 1:
        _len = 1
    _fac = float(_I_FACTOR)
    _er_len = int(_I_ER_LEN)
    if _er_len < 1:
        _er_len = 1
    _cfm = int(_I_CONFIRM)
    if _cfm < 1:
        _cfm = 1
    _use_glow = int(_I_GLOW) != 0

    # 1. 真實波幅 TR 與 Wilder RMA (ta.atr)
    _tr = [0.0] * n
    _atr = [0.0] * n
    for i in range(n):
        _tr[i] = h[i] - l[i]
        _atr[i] = _NAN
        if i > 0:
            _a = h[i] - l[i]
            _b = abs(h[i] - c[i - 1])
            _d = abs(l[i] - c[i - 1])
            _mx = _a
            if _b > _mx:
                _mx = _b
            if _d > _mx:
                _mx = _d
            _tr[i] = _mx

    if n >= _len:
        _acc = 0.0
        for i in range(_len):
            _acc = _acc + _tr[i]
        _atr[_len - 1] = _acc / _len
        for i in range(_len, n):
            _atr[i] = (_atr[i - 1] * (_len - 1) + _tr[i]) / _len

    # 2. 考夫曼效率比率 ER + EMA(3) 平滑
    _er = [0.0] * n
    _er_smooth = [0.0] * n
    _er_seed = 0.0
    for i in range(n):
        if i >= _er_len:
            _net = abs(c[i] - c[i - _er_len])
            _path = 0.0
            for k in range(_er_len):
                _path = _path + abs(c[i - k] - c[i - k - 1])
            _er[i] = (_net / _path) if _path > 0.0 else 0.0
        else:
            _er[i] = 0.0

        if i < 3:
            _er_seed = _er_seed + _er[i]
            _er_smooth[i] = _er_seed / (i + 1)
        else:
            _er_smooth[i] = _er[i] * 0.5 + _er_smooth[i - 1] * 0.5

    # 3. 逐支計算波幅狀態、自適應超級趨勢、確認過濾器、轉勢質素評分與趨勢信念
    _st = [0.0] * n
    _glow_st = [0.0] * n
    _locked_dir = [0.0] * n
    _adapt_fac_seq = [0.0] * n
    _adapt_cfm_seq = [0.0] * n
    _age_seq = [0.0] * n
    _strength_pct_seq = [0.0] * n
    _vol_ratio_seq = [0.0] * n
    _last_q_seq = [0.0] * n
    _dist_pct_seq = [0.0] * n
    _bull_ct_seq = [0.0] * n
    _bear_ct_seq = [0.0] * n

    _up_A = [0.0] * n
    _up_B = [0.0] * n
    _up_C = [0.0] * n
    _dn_A = [0.0] * n
    _dn_B = [0.0] * n
    _dn_C = [0.0] * n
    _raw_switch_seq = [0.0] * n

    _upper_band = _NAN
    _lower_band = _NAN
    _trend_dir = 1
    _locked_bull = True
    _streak = 0
    _flip_quality = 0
    _has_flipped = False
    _age = 0
    _bull_ct = 0
    _bear_ct = 0

    for i in range(n):
        _up_A[i] = 0.0
        _up_B[i] = 0.0
        _up_C[i] = 0.0
        _dn_A[i] = 0.0
        _dn_B[i] = 0.0
        _dn_C[i] = 0.0
        _raw_switch_seq[i] = 0.0

        _hl2 = (h[i] + l[i]) * 0.5
        _cur_atr = _atr[i]
        _ers = _er_smooth[i]

        # SMA(atr, 50)
        _vol_ratio = 1.0
        if i >= _len + 48:
            _s50 = 0.0
            for k in range(i - 49, i + 1):
                _s50 = _s50 + _atr[k]
            _base50 = _s50 / 50.0
            if _base50 > 0.0:
                _vol_ratio = _cur_atr / _base50
        _vol_regime = _vol_ratio
        if _vol_regime > 1.40:
            _vol_regime = 1.40
        if _vol_regime < 0.75:
            _vol_regime = 0.75
        _adapt_factor = _fac * _vol_regime

        if _ers < 0.25:
            _adapt_cfm = _cfm + 2
            if _adapt_cfm < 4:
                _adapt_cfm = 4
        elif _ers < 0.45:
            _adapt_cfm = _cfm + 1
        elif _ers < 0.65:
            _adapt_cfm = _cfm
        else:
            _adapt_cfm = _cfm - 1
            if _adapt_cfm < 1:
                _adapt_cfm = 1

        # 轉勢訊號質素評分 (0..100,Pine math.round 半數進位)
        _mom_pct = 0.0
        if i >= 5 and c[i - 5] != 0.0:
            _mom_pct = abs((c[i] - c[i - 5]) / c[i - 5] * 100.0)
        _atr_pct = (_cur_atr / c[i] * 100.0) if (not _is_na(_cur_atr) and c[i] > 0.0) else 0.0
        _mom_norm = 0.0
        if _atr_pct > 0.0:
            _mom_norm = _mom_pct / (_atr_pct * 2.0)
            if _mom_norm > 1.0:
                _mom_norm = 1.0
        _vol_score = 1.0 - abs(1.0 - _vol_ratio)
        if _vol_score < 0.0:
            _vol_score = 0.0
        _q_raw = _ers * 0.50 + _vol_score * 0.30 + _mom_norm * 0.20
        _q_pct = int(_q_raw * 100.0 + 0.5)

        # 手動計算自適應超級趨勢:upper/lower/dir 由 scalar state 向前推進,不以輸出 Sequence[i-1] 作遞歸存儲
        _prev_upper = _upper_band
        _prev_lower = _lower_band
        _prev_tdir = _trend_dir
        if not _is_na(_cur_atr):
            _ub_basic = _hl2 + _adapt_factor * _cur_atr
            _lb_basic = _hl2 - _adapt_factor * _cur_atr
            _prev_c = c[i - 1] if i > 0 else c[i]

            if _is_na(_prev_upper) or _ub_basic < _prev_upper or _prev_c > _prev_upper:
                _upper_band = _ub_basic
            else:
                _upper_band = _prev_upper

            if _is_na(_prev_lower) or _lb_basic > _prev_lower or _prev_c < _prev_lower:
                _lower_band = _lb_basic
            else:
                _lower_band = _prev_lower

            if i == 0 or _is_na(_prev_upper):
                _trend_dir = 1
            elif _prev_tdir == 1:
                _trend_dir = -1 if c[i] < _lower_band else 1
            else:
                _trend_dir = 1 if c[i] > _upper_band else -1

        if i > 0 and _trend_dir != _prev_tdir:
            _raw_switch_seq[i] = 1.0

        _cur_st = _lower_band if _trend_dir == 1 else _upper_band
        _bull = (_trend_dir == 1)

        if i == 0:
            _locked_bull = _bull
            _streak = 0
        else:
            _streak = 0 if (_bull == _locked_bull) else (_streak + 1)

        _flip_up = (_streak >= _adapt_cfm) and _bull
        _flip_dn = (_streak >= _adapt_cfm) and (not _bull)
        _any_flip = _flip_up or _flip_dn

        if _any_flip:
            _locked_bull = _bull
            _flip_quality = _q_pct
            _has_flipped = True

        if _flip_up:
            _bull_ct = _bull_ct + 1
            if _q_pct >= 70:
                _up_A[i] = 1.0
            elif _q_pct >= 50:
                _up_B[i] = 1.0
            else:
                _up_C[i] = 1.0

        if _flip_dn:
            _bear_ct = _bear_ct + 1
            if _q_pct >= 70:
                _dn_A[i] = 1.0
            elif _q_pct >= 50:
                _dn_B[i] = 1.0
            else:
                _dn_C[i] = 1.0

        if i == 0:
            _age = 1
        else:
            _age = 0 if _any_flip else (_age + 1)

        _s_time = (_age / 60.0) if _age < 60 else 1.0
        _s = _s_time * 0.65 + _ers * 0.35

        _st[i] = _cur_st
        _glow_st[i] = _cur_st if _use_glow else _NAN
        _locked_dir[i] = 1.0 if _locked_bull else -1.0
        _adapt_fac_seq[i] = _adapt_factor
        _adapt_cfm_seq[i] = float(_adapt_cfm)
        _age_seq[i] = float(_age)
        _strength_pct_seq[i] = float(int(_s * 100.0 + 0.5))
        _vol_ratio_seq[i] = _vol_ratio
        # Pine declares var int flip_quality = 0; keep numeric zero before first flip.
        _last_q_seq[i] = float(_flip_quality)
        _dist_pct_seq[i] = (abs(c[i] - _cur_st) / abs(c[i]) * 100.0) if (not _is_na(_cur_st) and c[i] != 0.0) else 0.0
        _bull_ct_seq[i] = float(_bull_ct)
        _bear_ct_seq[i] = float(_bear_ct)

    # 4. 繪製原碼 #26a69a / #ef5350 光暈與主線(換向棒前一支以透明色偽斷線防斜線)
    _clear = [0.0] * n
    for i in range(n):
        _clear[i] = 0.0
    for i in range(0, n - 1):
        if _raw_switch_seq[i + 1] > 0.5:
            _clear[i] = 1.0

    _COL_NONE = Color(19, 23, 34, 0)
    # 對應原碼 color.new(tc, 82) -> alpha=46, color.new(tc, 68) -> alpha=82, 主線 alpha=255
    _bull_g2 = Color(38, 166, 154, 46)
    _bear_g2 = Color(239, 83, 80, 46)
    _bull_g1 = Color(38, 166, 154, 82)
    _bear_g1 = Color(239, 83, 80, 82)
    _bull_main = Color(38, 166, 154, 255)
    _bear_main = Color(239, 83, 80, 255)

    _color_g2 = [_COL_NONE if _clear[i] > 0.5 else (_bull_g2 if _locked_dir[i] > 0 else _bear_g2) for i in range(n)]
    _color_g1 = [_COL_NONE if _clear[i] > 0.5 else (_bull_g1 if _locked_dir[i] > 0 else _bear_g1) for i in range(n)]
    _color_main = [_COL_NONE if _clear[i] > 0.5 else (_bull_main if _locked_dir[i] > 0 else _bear_main) for i in range(n)]
    plot("中間光暈", _glow_st, color=_color_g2, style=Line.line, linewidth=4)
    plot("內部光暈", _glow_st, color=_color_g1, style=Line.line, linewidth=3)
    plot("超級趨勢線", _st, color=_color_main, style=Line.line, linewidth=2)

    # 5. 香港中文轉勢訊號標籤(造好 [A/B/C] / 造淡 [A/B/C])
    _up_signal = [bool(_up_A[i] + _up_B[i] + _up_C[i] > 0.5) for i in range(n)]
    _dn_signal = [bool(_dn_A[i] + _dn_B[i] + _dn_C[i] > 0.5) for i in range(n)]
    plot_icon("造好點", _up_signal, _st, Shape.circle, Color.hex("#26A69A"), 2, 0, 0)
    plot_icon("造淡點", _dn_signal, _st, Shape.circle, Color.hex("#EF5350"), 2, 0, 0)

    plot_text("造好A", [x > 0.5 for x in _up_A], _st, "造好  [A]", color=Color.hex("#26A69A"), size=2, offseth=0, offsetv=-22)
    plot_text("造好B", [x > 0.5 for x in _up_B], _st, "造好  [B]", color=Color.hex("#26A69A"), size=2, offseth=0, offsetv=-22)
    plot_text("造好C", [x > 0.5 for x in _up_C], _st, "造好  [C]", color=Color.hex("#26A69A"), size=2, offseth=0, offsetv=-22)

    plot_text("造淡A", [x > 0.5 for x in _dn_A], _st, "造淡  [A]", color=Color.hex("#EF5350"), size=2, offseth=0, offsetv=22)
    plot_text("造淡B", [x > 0.5 for x in _dn_B], _st, "造淡  [B]", color=Color.hex("#EF5350"), size=2, offseth=0, offsetv=22)
    plot_text("造淡C", [x > 0.5 for x in _dn_C], _st, "造淡  [C]", color=Color.hex("#EF5350"), size=2, offseth=0, offsetv=22)

    # 6. 將原碼 11 行數據面板全部數值導出至富途頂部指標數值欄
    output_parameter(
        超級趨勢線=_st,
        當前方向=_locked_dir,
        持續燭數=_age_seq,
        趨勢強度Pct=_strength_pct_seq,
        趨勢效率ER=_er_smooth,
        波幅比率=_vol_ratio_seq,
        自適應倍數=_adapt_fac_seq,
        確認燭數=_adapt_cfm_seq,
        上次質素分=_last_q_seq,
        偏離趨勢線Pct=_dist_pct_seq,
        造好訊號次數=_bull_ct_seq,
        造淡訊號次數=_bear_ct_seq
    )
基準:Pine source sha256 4d05ef4c07db64570a4c073697507235a4a144e10b47f8c81716b83d79aad1bb;FTNN 指標助手冊 192,120 bytes,sha256 92a3c0814c2c8508a858bf744d03a10468627647ba8738b736522ad9bfe5d279。未改 production 設定或指標。本地比較不等於 TV runtime parity。