A16(A168146)— V4 自適應超級趨勢 FTNN Python 模式(香港中文版)交付
源碼基準:v4-supertrend-zh-v6.txt(16,808 B,SHA256 4d05ef4c07db64570a4c073697507235a4a144e10b47f8c81716b83d79aad1bb)
手冊基準:ftnn-indicator-manual.md(192,120 B,SHA256 92a3c0814c2c8508a858bf744d03a10468627647ba8738b736522ad9bfe5d279)
核心檔案:A16_V4_SUPERTREND_FTNN_HK.py(14625 B,SHA256 afb1b2ef4db7c9c88b56affc61988c6f6f5ff5ca5e1872389bca29880eabed6a)
合規架構(66206):單次 oldest → newest 迴圈 + 原生 Python list/標量維護遞歸狀態,無任何 Sequence 回寫;AST 僅含 from ftool import iff,零禁用 import。
1. 本地對數驗證與未驗證邊界聲明(誠實區分)
| 驗證項目 | 狀態 | 實測數據 / 說明 |
| AST 合規掃描 | PASS | 僅 from ftool import iff;無 math / collections / typing / 第三方庫。 |
A36 300 棒黃金語料 (3fc8ce27) | PASS (0 mismatch) | 14 個檢查點 + expected_latest 全部欄位 + 2 處翻轉標籤(bar 158 23303.4148781909 沽空[A] / bar 240 21707.835103156493 好倉[A])一致;最大浮點差 3.11e-15。 |
| Stub FTNN API 執行測試 | PASS | 以模擬 Sequence / Color.rgb / Line.line / ftool.iff 執行完整檔案 __main__:plot 4 條(線寬 2–9)、plot_fillcolor 1 個、plot_text 6 組、output_parameter 12 欄。 |
| 舊 Server 富途桌面 GUI 實測 | 未驗證(無權限) | A16 無舊 Server 桌面 GUI 控制權(見 66161/66163),未在真實 FTNN 編輯器撳「應用」測試 Sequence.apply、Sequence.values 順序及 float('nan') 繪圖相容性,絕不冒稱已實機通過。 |
| TradingView 實盤逐棒對數 | 未驗證(缺同棒原始數據) | 3fc8ce27 僅屬同源 Pine 合成語料,不等於真實 TradingView 匯出(同意 66141/66142/66200);未取得 TV 同商品同時戳 OHLC/ST/標籤實盤數據,故不報 TV 實盤 0 diff。 |
2. 香港中文用字對照表(計算層與原碼恆等)
| 原碼字眼 (TW / Pine) | 香港中文版 (HK FTNN) | 適用位置 |
| 做多 / 做空 | 好倉 / 沽空 | 訊號標籤、方向輸出 |
| K線 | 陰陽燭 | 參數名稱、持續陰陽燭數 |
| 波動率狀態 | 波幅比率 / 波幅狀態 | 指標簡介、輸出欄位 |
| [A] / [B] / [C] | [A級] / [B級] / [C級] | plot_text 翻轉標籤 |
| 數據看板 | 頂部指標數值欄 (output_parameter) | 11 項看板數值映射 |
3. FTNN 手冊硬缺口與視覺/輸出層差異明細
| # | Pine v6 原碼功能 | FTNN 手冊現況 (92a3c081) | 本交付處理及差異說明 |
| 1 | table.new / table.cell 右上角 2×11 看板 | 手冊全文無 table / 看板原語 | 將全部 11 項看板數值(含方向、持續燭數、信念%、ER、波幅比、倍數、確認數、上次質素、偏離%、多空次數)經 output_parameter 匯出至頂部指標欄;無浮動圖表表格。 |
| 2 | alertcondition 三組警報 | 手冊無警報原語 | 不模擬無效警報函數;需於富途條件單/外圍工具承接。 |
| 3 | fill() 雙色連續漸層背景 | plot_fillcolor 僅接受單一 Color | 改用半透明單色 RGBA Color.rgb(..., 48) 填滿 close() 與 s_st 之間區域,無漸變透明效果。 |
| 4 | barcolor() 著色原有陰陽燭 | 僅有繪製新蠟燭之 plot_candlestick | 不重繪覆蓋主圖陰陽燭(避免雙重蠟燭遮擋),原主圖 K 線保持預設顏色。 |
| 5 | linewidth=12 外圍光暈 | plot() 手冊規定 linewidth 範圍 1..9 | 外圍光暈線寬由 12 夾限至 9(中間 7、內層 4、主線 2)。 |
| 6 | label.new 含換行收市價 + yloc.abovebar/belowbar | plot_text(name, cond, y, text, ...);未文件化 Sequence[str] 可否經 apply 返回 | 安全相容模式以 6 組純 str 字面量(好倉/沽空 [A/B/C級])錨定 y=s_st 並以 offsetv=±22 偏移;另於 compute_v4_supertrend()["flip_events"] 保留完整雙行含收市價字串。 |
4. 驗證執行輸出 (a16_v4_verify.py)
{
"py_sha256": "afb1b2ef4db7c9c88b56affc61988c6f6f5ff5ca5e1872389bca29880eabed6a",
"py_bytes": 14625,
"ast_imports": [
[
"from",
"ftool",
[
"iff"
]
]
],
"checkpoints_checked": 14,
"max_float_diff": 3.1086244689504383e-15,
"plots_count": 4,
"fills_count": 1,
"plot_text_calls": 6,
"fired_labels": [
[
158,
"沽空A",
23303.4148781909,
"沽空 [A級]"
],
[
240,
"好倉A",
21707.835103156493,
"好倉 [A級]"
]
],
"output_parameter_keys": [
"超級趨勢線",
"當前方向好倉為1",
"持續陰陽燭數",
"趨勢信念Pct",
"趨勢效率ER",
"波幅比率",
"自適應倍數",
"確認陰陽燭數",
"上次訊號質素",
"偏離趨勢線Pct",
"好倉訊號次數",
"沽空訊號次數"
],
"flip_events": [
{
"index": 158,
"side": "SHORT",
"y": 23303.4148781909,
"close": 23071.47,
"quality": 74,
"grade": "A",
"text_hk": "沽空 [A級]\n23071",
"text_src": "做空 [A]\n23071"
},
{
"index": 240,
"side": "LONG",
"y": 21707.835103156493,
"close": 21874.24,
"quality": 82,
"grade": "A",
"text_hk": "好倉 [A級]\n21874",
"text_src": "做多 [A]\n21874"
}
]
}
5. 完整 FTNN Python 指標源碼 (A16_V4_SUPERTREND_FTNN_HK.py)
# -*- coding: utf-8 -*-
# V4 自適應超級趨勢(香港中文版)— 富途牛牛 FTNN 自編指標(Python 模式)
# 原碼基準:v4-supertrend-zh-v6.txt(16,808 B,sha256=4d05ef4c07db64570a4c073697507235a4a144e10b47f8c81716b83d79aad1bb)
# 手冊基準:ftnn-indicator-manual.md(192,120 B,sha256=92a3c0814c2c8508a858bf744d03a10468627647ba8738b736522ad9bfe5d279)
# 結構方式:按 66206 議會指示,單次 oldest->newest 迴圈配合原生 list/標量狀態計算,不經 Sequence 回寫;
# 嚴守手冊規範,零外部或標準庫 import(僅使用 from ftool import iff)。
indicator(
"V4STHK",
"自適應超級趨勢 (香港版)",
True,
"考夫曼效率比率(ER) + 50燭波幅狀態縮放ATR倍數 + 自適應確認過濾 + A/B/C 轉勢質素評級",
)
from ftool import iff
def _pine_round(x):
if x != x:
return x
return int(x + 0.5) if x >= 0.0 else -int(-x + 0.5)
def _format_mintick(px, mintick):
if mintick > 0.0:
snapped = _pine_round(px / mintick) * mintick
if mintick >= 1.0:
return str(int(snapped))
s = str(round(snapped, 6))
return s
return "%.2f" % px
def compute_v4_supertrend(highs, lows, closes, i_len=14, i_factor=3.5, i_er_len=10, i_confirm=2, mintick=0.0):
n = len(closes)
nan = float("nan")
# 1. 真實波幅 TR 與 Wilder ATR (ta.atr = ta.rma(ta.tr(true), i_len))
tr = [nan] * n
for i in range(n):
if i == 0:
tr[i] = highs[i] - lows[i]
else:
pc = closes[i - 1]
hi_v = highs[i] if highs[i] > pc else pc
lo_v = lows[i] if lows[i] < pc else pc
tr[i] = hi_v - lo_v
atr = [nan] * n
if i_len >= 1 and n >= i_len:
s_tr = 0.0
for i in range(i_len):
s_tr += tr[i]
atr[i_len - 1] = s_tr / float(i_len)
alpha = 1.0 / float(i_len)
one_minus = 1.0 - alpha
for i in range(i_len, n):
atr[i] = alpha * tr[i] + one_minus * atr[i - 1]
# 2. 考夫曼效率比率 ER(i_er_len) 與 EMA(3) 平滑
# 暖機期 0..i_er_len-1 因 total_path 為 na 走 false 分支故 er = 0.0
er = [0.0] * n
for i in range(i_er_len, n):
net_move = abs(closes[i] - closes[i - i_er_len])
total_path = 0.0
for k in range(i_er_len):
total_path += abs(closes[i - k] - closes[i - k - 1])
er[i] = (net_move / total_path) if total_path > 0.0 else 0.0
er_smooth = [nan] * n
if n >= 3:
er_smooth[2] = (er[0] + er[1] + er[2]) / 3.0
for i in range(3, n):
er_smooth[i] = er[i] * 0.5 + er_smooth[i - 1] * 0.5
# 3. 50 燭波幅基準 atr_baseline = ta.sma(atr, 50)
atr_baseline = [nan] * n
win_sum = 0.0
valid_cnt = 0
for i in range(n):
if atr[i] == atr[i]:
win_sum += atr[i]
valid_cnt += 1
if i >= 50 and atr[i - 50] == atr[i - 50]:
win_sum -= atr[i - 50]
valid_cnt -= 1
if valid_cnt == 50:
atr_baseline[i] = win_sum / 50.0
# 4. 逐燭遞歸狀態(標量維護上燭數值,oldest -> newest 單次掃描)
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
out_st = [nan] * n
out_locked_bull = [True] * n
out_age = [0] * n
out_conv_pct = [nan] * n
out_er_smooth = [nan] * n
out_vol_ratio = [1.0] * n
out_adapt_factor = [float(i_factor)] * n
out_adapt_confirm = [1] * n
out_flip_quality = [0] * n
out_dist_pct = [nan] * n
out_bull_ct = [0] * n
out_bear_ct = [0] * n
out_flip_up_a = [False] * n
out_flip_up_b = [False] * n
out_flip_up_c = [False] * n
out_flip_dn_a = [False] * n
out_flip_dn_b = [False] * n
out_flip_dn_c = [False] * n
out_s = [nan] * n
out_quality_pct = [nan] * n
out_quality_grade = ["C"] * n
out_flip_grade = ["C"] * n
out_flip_events = []
for i in range(n):
ab = atr_baseline[i]
vol_ratio = (atr[i] / ab) if (ab == ab and ab > 0.0 and atr[i] == atr[i]) else 1.0
vol_regime = vol_ratio
if vol_regime > 1.40:
vol_regime = 1.40
if vol_regime < 0.75:
vol_regime = 0.75
adaptive_factor = float(i_factor) * vol_regime
es = er_smooth[i]
if es != es:
adaptive_confirm = i_confirm - 1 if (i_confirm - 1) > 1 else 1
elif es < 0.25:
adaptive_confirm = (i_confirm + 2) if (i_confirm + 2) > 4 else 4
elif es < 0.45:
adaptive_confirm = i_confirm + 1
elif es < 0.65:
adaptive_confirm = i_confirm
else:
adaptive_confirm = (i_confirm - 1) if (i_confirm - 1) > 1 else 1
if i >= 5 and closes[i - 5] != 0.0:
price_mom_pct = abs(100.0 * (closes[i] - closes[i - 5]) / closes[i - 5])
else:
price_mom_pct = nan
if closes[i] > 0.0:
atr_pct = (atr[i] / closes[i] * 100.0) if (atr[i] == atr[i]) else nan
else:
atr_pct = 0.0
if atr_pct == atr_pct and atr_pct > 0.0 and price_mom_pct == price_mom_pct:
raw_mom = price_mom_pct / (atr_pct * 2.0)
mom_norm = raw_mom if raw_mom < 1.0 else 1.0
elif atr_pct == atr_pct and atr_pct <= 0.0:
mom_norm = 0.0
else:
mom_norm = nan
vol_diff = 1.0 - abs(1.0 - vol_ratio)
vol_score = vol_diff if vol_diff > 0.0 else 0.0
if es == es and mom_norm == mom_norm:
quality_raw = es * 0.50 + vol_score * 0.30 + mom_norm * 0.20
quality_pct = _pine_round(quality_raw * 100.0)
quality_grade = "A" if quality_pct >= 70 else ("B" if quality_pct >= 50 else "C")
else:
quality_pct = nan
quality_grade = "C"
hl2 = (highs[i] + lows[i]) * 0.5
if atr[i] == atr[i]:
upper_basic = hl2 + adaptive_factor * atr[i]
lower_basic = hl2 - adaptive_factor * atr[i]
prev_ub = upper_band
prev_lb = lower_band
if prev_ub != prev_ub:
upper_band = upper_basic
else:
upper_band = upper_basic if (upper_basic < prev_ub or closes[i - 1] > prev_ub) else prev_ub
if prev_lb != prev_lb:
lower_band = lower_basic
else:
lower_band = lower_basic if (lower_basic > prev_lb or closes[i - 1] < prev_lb) else prev_lb
if trend_dir == 1:
trend_dir = -1 if closes[i] < lower_band else 1
else:
trend_dir = 1 if closes[i] > upper_band else -1
st = lower_band if trend_dir == 1 else upper_band
else:
st = nan
bull = trend_dir == 1
if bull == locked_bull:
streak = 0
else:
streak += 1
flip_up = (streak >= adaptive_confirm) and bull
flip_down = (streak >= adaptive_confirm) and (not bull)
any_flip = flip_up or flip_down
if any_flip:
locked_bull = bull
flip_quality = quality_pct
has_flipped = True
flip_grade = "A" if flip_quality >= 70 else ("B" if flip_quality >= 50 else "C")
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 + es * 0.35) if es == es else nan
if flip_up:
bull_ct += 1
if flip_down:
bear_ct += 1
dist_pct = (abs(closes[i] - st) / closes[i] * 100.0) if (st == st and closes[i] != 0.0) else nan
conv_pct = _pine_round(s * 100.0) if s == s else nan
out_st[i] = st
out_locked_bull[i] = locked_bull
out_age[i] = age
out_conv_pct[i] = conv_pct
out_er_smooth[i] = es
out_vol_ratio[i] = vol_ratio
out_adapt_factor[i] = adaptive_factor
out_adapt_confirm[i] = adaptive_confirm
out_flip_quality[i] = flip_quality
out_dist_pct[i] = dist_pct
out_bull_ct[i] = bull_ct
out_bear_ct[i] = bear_ct
out_s[i] = s
out_quality_pct[i] = quality_pct
out_quality_grade[i] = quality_grade
out_flip_grade[i] = flip_grade
if flip_up:
out_flip_up_a[i] = quality_grade == "A"
out_flip_up_b[i] = quality_grade == "B"
out_flip_up_c[i] = quality_grade == "C"
px_str = _format_mintick(closes[i], mintick)
out_flip_events.append({
"index": i,
"side": "LONG",
"y": st,
"close": closes[i],
"quality": quality_pct,
"grade": quality_grade,
"text_hk": "好倉 [" + quality_grade + "級]\n" + px_str,
"text_src": "做多 [" + quality_grade + "]\n" + px_str,
})
elif flip_down:
out_flip_dn_a[i] = quality_grade == "A"
out_flip_dn_b[i] = quality_grade == "B"
out_flip_dn_c[i] = quality_grade == "C"
px_str = _format_mintick(closes[i], mintick)
out_flip_events.append({
"index": i,
"side": "SHORT",
"y": st,
"close": closes[i],
"quality": quality_pct,
"grade": quality_grade,
"text_hk": "沽空 [" + quality_grade + "級]\n" + px_str,
"text_src": "做空 [" + quality_grade + "]\n" + px_str,
})
return {
"st": out_st,
"locked_bull": out_locked_bull,
"age": out_age,
"conv_pct": out_conv_pct,
"er_smooth": out_er_smooth,
"vol_ratio": out_vol_ratio,
"adaptive_factor": out_adapt_factor,
"adaptive_confirm": out_adapt_confirm,
"flip_quality": out_flip_quality,
"dist_pct": out_dist_pct,
"bull_ct": out_bull_ct,
"bear_ct": out_bear_ct,
"flip_up_a": out_flip_up_a,
"flip_up_b": out_flip_up_b,
"flip_up_c": out_flip_up_c,
"flip_dn_a": out_flip_dn_a,
"flip_dn_b": out_flip_dn_b,
"flip_dn_c": out_flip_dn_c,
"s": out_s,
"quality_pct": out_quality_pct,
"quality_grade": out_quality_grade,
"flip_grade": out_flip_grade,
"has_flipped": has_flipped,
"flip_events": out_flip_events,
}
def _list_to_seq(anchor_seq, values_list):
idx = [0]
n = len(values_list)
def _emit(_v):
k = idx[0]
idx[0] = k + 1
return values_list[k] if k < n else float("nan")
return anchor_seq.apply(_emit)
if __name__ == "__main__":
p_len = int(input_parameter("ATR週期", 14))
p_factor = float(input_parameter("基礎倍數", 3.5))
p_er_len = int(input_parameter("效率比率週期", 10))
p_confirm = int(input_parameter("確認陰陽燭數(基礎)", 2))
p_glow = int(input_parameter("發光光暈(1開/0關)", 1))
p_shade = int(input_parameter("漸層底色(1開/0關)", 1))
p_labels = int(input_parameter("多空訊號標籤(1開/0關)", 1))
p_mintick = float(input_parameter("最小跳動價位(0=2位小數)", 0.0))
c_seq = close()
res = compute_v4_supertrend(
high().values,
low().values,
c_seq.values,
i_len=p_len,
i_factor=p_factor,
i_er_len=p_er_len,
i_confirm=p_confirm,
mintick=p_mintick,
)
s_st = _list_to_seq(c_seq, res["st"])
s_bull = _list_to_seq(c_seq, res["locked_bull"])
# 配色與原碼 #26A69A (好倉) / #EF5350 (沽空) 嚴格一致;線寬上限依手冊為 9
c_bull_main = Color.rgb(38, 166, 154, 255)
c_bear_main = Color.rgb(239, 83, 80, 255)
if p_glow != 0:
plot("外圍光暈", s_st, color=iff(s_bull, Color.rgb(38, 166, 154, 26), Color.rgb(239, 83, 80, 26)), style=Line.line, linewidth=9)
plot("中間光暈", s_st, color=iff(s_bull, Color.rgb(38, 166, 154, 46), Color.rgb(239, 83, 80, 46)), style=Line.line, linewidth=7)
plot("內部光暈", s_st, color=iff(s_bull, Color.rgb(38, 166, 154, 82), Color.rgb(239, 83, 80, 82)), style=Line.line, linewidth=4)
plot("超級趨勢線", s_st, color=iff(s_bull, c_bull_main, c_bear_main), style=Line.line, linewidth=2)
# FTNN plot_fillcolor 為單色半透明填色(不支援 Pine 雙色漸層,僅作背景底色對應)
if p_shade != 0:
plot_fillcolor("趨勢底色", c_seq, s_st, True, color=iff(s_bull, Color.rgb(38, 166, 154, 48), Color.rgb(239, 83, 80, 48)))
# 翻轉標籤:依手冊 plot_text(name, cond, y, text, ...) 傳入純 str 與 y=s_st(不依賴未驗之 Sequence[str])
if p_labels != 0:
plot_text("好倉A", _list_to_seq(c_seq, res["flip_up_a"]), s_st, "好倉 [A級]", color=c_bull_main, size=2, offseth=0, offsetv=-22)
plot_text("好倉B", _list_to_seq(c_seq, res["flip_up_b"]), s_st, "好倉 [B級]", color=c_bull_main, size=2, offseth=0, offsetv=-22)
plot_text("好倉C", _list_to_seq(c_seq, res["flip_up_c"]), s_st, "好倉 [C級]", color=c_bull_main, size=2, offseth=0, offsetv=-22)
plot_text("沽空A", _list_to_seq(c_seq, res["flip_dn_a"]), s_st, "沽空 [A級]", color=c_bear_main, size=2, offseth=0, offsetv=22)
plot_text("沽空B", _list_to_seq(c_seq, res["flip_dn_b"]), s_st, "沽空 [B級]", color=c_bear_main, size=2, offseth=0, offsetv=22)
plot_text("沽空C", _list_to_seq(c_seq, res["flip_dn_c"]), s_st, "沽空 [C級]", color=c_bear_main, size=2, offseth=0, offsetv=22)
# 數據看板映射:手冊無 table.new 原語,全部 11 項指標經 output_parameter 輸出至頂部指標欄
output_parameter(
超級趨勢線=s_st,
當前方向好倉為1=iff(s_bull, 1, 0),
持續陰陽燭數=_list_to_seq(c_seq, res["age"]),
趨勢信念Pct=_list_to_seq(c_seq, res["conv_pct"]),
趨勢效率ER=_list_to_seq(c_seq, res["er_smooth"]),
波幅比率=_list_to_seq(c_seq, res["vol_ratio"]),
自適應倍數=_list_to_seq(c_seq, res["adaptive_factor"]),
確認陰陽燭數=_list_to_seq(c_seq, res["adaptive_confirm"]),
上次訊號質素=_list_to_seq(c_seq, res["flip_quality"]),
偏離趨勢線Pct=_list_to_seq(c_seq, res["dist_pct"]),
好倉訊號次數=_list_to_seq(c_seq, res["bull_ct"]),
沽空訊號次數=_list_to_seq(c_seq, res["bear_ct"]),
)