對應場控令:66148 / 66149 / 66155 / 66156 / 66157 / 66206 / 66218(並覆 66145 點位誤差分析與 66161/66163 舊 Server GUI 狀態)。
源碼核對:v4-supertrend-zh-v6.txt(16,808 B, sha256 4d05ef4c…)+ ftnn-indicator-manual.md(192,120 B, sha256 92a3c081…)。
指標檔 sha256:0e2f4b0eb8af62d458ebdd7fbfa2d6fc09a554aac0cda49b344ad19b989e4f49(11788 B,AST imports:[] 零外部 import)。
| 項目 | 實測結果 | 說明 |
|---|---|---|
| AST 模組檢查 | PASS(0 import) | 無任何 import / from ... import,僅用內置函數+手冊定義 API |
| 遞歸狀態轉化 (66206) | PASS | 以由最舊至最新單次 for 迴圈標量狀態計算 ATR/ER/SMA50/SuperTrend/確認計數,不寫回 Sequence |
| 黃金 Fixture (3fc8ce27, 300 棒) | PASS(14/14 檢查點,max_err=2.220e-15) | 翻轉棒與源碼一致:bar 158 做空 [A/74]、bar 240 做多 [A/82];末棒 age=59、st=22848.4538、conviction=98.92% |
| 66145 點位修正 | 已對齊 | Pine label.new(..., yloc=belowbar/abovebar) 繪於 low/high 而非 st;本腳本將 plot_icon/plot_text 錨定 low/high |
| 項目 | 狀態 | 根因 |
|---|---|---|
| 數據看板(11×2 Table) | 不支援,改以 output_parameter 導出 | ftnn-indicator-manual.md 無 table.new / table.cell 原語 |
| TradingView 警報(alertcondition) | 不支援 | FTNN 指標手冊無 alertcondition 接口 |
動態含價標籤文字(str.tostring(close, mintick)) | 靜態文字 | 手冊 plot_text 範例為固定字串,動態每棒組裝標籤字串需實機驗證 |
| 連續漸變透明填色&K線著色 | 近似單色半透明填色/無 barcolor | plot_fillcolor 接受單色或條件色,無連續漸變 alpha 及 barcolor 接口 |
| FTNN 真機運行與 TradingView 逐棒對比 | 未驗證(【唔會】) | 本沙盒席位無舊 Server SSH 或 FTNN GUI 權限(66161/66163);plot()/plot_text() 是否直接接受 Python list 須真機貼入確認 |
# -*- coding: utf-8 -*-
# 富途牛牛 (FTNN) 自編指標|Python 模式|香港中文版
# 源指標:超級趨勢 (自適應版) Adaptive SuperTrend (v4-supertrend-zh-v6.txt)
# 合規:嚴守 ftnn-indicator-manual.md(零 import 語句;純內置函數與標量循環)
indicator("V4超趨", "自適應超級趨勢 (A33 香港版)", True, "自適應波動率超級趨勢:零import,單次由舊至新標量遞歸計算")
def _is_nan(v):
return v is None or v != v
def _pine_round(v):
if _is_nan(v):
return 0
if v >= 0.0:
return int(v + 0.5)
return -int((-v) + 0.5)
def _compute_v4_series(opens, highs, lows, closes, i_len, i_factor, i_er_len, i_confirm):
n = len(closes)
nan_val = float("nan")
st_list = [nan_val] * n
st_bull_list = [nan_val] * n
st_bear_list = [nan_val] * n
dir_list = [1] * n
locked_bull_list = [True] * n
flip_up_list = [False] * n
flip_down_list = [False] * n
any_flip_list = [False] * n
flip_y_up_list = [nan_val] * n
flip_y_down_list = [nan_val] * n
er_smooth_list = [0.0] * n
vol_ratio_list = [1.0] * n
adaptive_factor_list = [float(i_factor)] * n
adaptive_confirm_list = [int(i_confirm)] * n
quality_pct_list = [0] * n
flip_quality_list = [0] * n
has_flipped_list = [False] * n
age_list = [0] * n
s_list = [0.0] * n
dist_pct_list = [0.0] * n
bull_ct_list = [0] * n
bear_ct_list = [0] * n
tr_sum = 0.0
tr_cnt = 0
atr_prev = nan_val
# Pine: total_path > 0 ? net_move / total_path : 0.0 -> 首 i_er_len 根為 0.0,
# 故 ta.ema(er, 3) 從第 0..2 根種子 SMA(=0.0) 起步
er_ema_prev = nan_val
er_seed_sum = 0.0
er_seed_cnt = 0
er_alpha = 2.0 / 4.0
atr_win = []
atr_win_sum = 0.0
upper_band = nan_val
lower_band = nan_val
trend_dir = 1
has_trend_prev = False
locked_bull = True
has_locked_init = False
streak = 0
flip_quality = 0
has_flipped = False
age = 0
bull_ct = 0
bear_ct = 0
for i in range(n):
hi = float(highs[i])
lo = float(lows[i])
cl = float(closes[i])
hl2 = 0.5 * (hi + lo)
if i == 0:
tr_val = hi - lo
else:
prev_c = float(closes[i - 1])
hi_pc = hi if hi >= prev_c else prev_c
lo_pc = lo if lo <= prev_c else prev_c
tr_val = hi_pc - lo_pc
if _is_nan(atr_prev):
tr_sum += tr_val
tr_cnt += 1
if tr_cnt == i_len:
atr_val = tr_sum / float(i_len)
atr_prev = atr_val
else:
atr_val = nan_val
else:
atr_val = (atr_prev * float(i_len - 1) + tr_val) / float(i_len)
atr_prev = atr_val
if i >= i_er_len:
net_move = abs(cl - float(closes[i - i_er_len]))
total_path = 0.0
for k in range(i_er_len):
total_path += abs(float(closes[i - k]) - float(closes[i - k - 1]))
er_val = (net_move / total_path) if total_path > 0.0 else 0.0
else:
er_val = 0.0
if _is_nan(er_ema_prev):
er_seed_sum += er_val
er_seed_cnt += 1
if er_seed_cnt == 3:
er_smooth = er_seed_sum / 3.0
er_ema_prev = er_smooth
else:
er_smooth = er_seed_sum / float(er_seed_cnt)
else:
er_smooth = er_alpha * er_val + (1.0 - er_alpha) * er_ema_prev
er_ema_prev = er_smooth
if not _is_nan(atr_val):
atr_win.append(atr_val)
atr_win_sum += atr_val
if len(atr_win) > 50:
atr_win_sum -= atr_win.pop(0)
atr_baseline = (atr_win_sum / 50.0) if len(atr_win) == 50 else nan_val
else:
atr_baseline = nan_val
if (not _is_nan(atr_val)) and (not _is_nan(atr_baseline)) and atr_baseline > 0.0:
vol_ratio = atr_val / atr_baseline
else:
vol_ratio = 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
if er_smooth < 0.25:
adaptive_confirm = i_confirm + 2
if adaptive_confirm < 4:
adaptive_confirm = 4
elif er_smooth < 0.45:
adaptive_confirm = i_confirm + 1
elif er_smooth < 0.65:
adaptive_confirm = i_confirm
else:
adaptive_confirm = i_confirm - 1
if adaptive_confirm < 1:
adaptive_confirm = 1
if i >= 5 and float(closes[i - 5]) != 0.0:
roc5 = (cl - float(closes[i - 5])) / float(closes[i - 5]) * 100.0
price_mom_pct = abs(roc5)
else:
price_mom_pct = 0.0
atr_pct = (atr_val / cl * 100.0) if ((not _is_nan(atr_val)) and cl > 0.0) else 0.0
if atr_pct > 0.0:
mom_norm = price_mom_pct / (atr_pct * 2.0)
if mom_norm > 1.0:
mom_norm = 1.0
else:
mom_norm = 0.0
vol_score = 1.0 - abs(1.0 - vol_ratio)
if vol_score < 0.0:
vol_score = 0.0
quality_raw = er_smooth * 0.50 + vol_score * 0.30 + mom_norm * 0.20
quality_pct = _pine_round(quality_raw * 100.0)
if not _is_nan(atr_val):
upper_basic = hl2 + adaptive_factor * atr_val
lower_basic = hl2 - adaptive_factor * atr_val
prev_c_st = float(closes[i - 1]) if i > 0 else cl
if _is_nan(upper_band):
upper_band = upper_basic
else:
if upper_basic < upper_band or prev_c_st > upper_band:
upper_band = upper_basic
if _is_nan(lower_band):
lower_band = lower_basic
else:
if lower_basic > lower_band or prev_c_st < lower_band:
lower_band = lower_basic
if not has_trend_prev:
trend_dir = 1
has_trend_prev = True
elif trend_dir == 1:
trend_dir = -1 if cl < lower_band else 1
else:
trend_dir = 1 if cl > upper_band else -1
st_val = lower_band if trend_dir == 1 else upper_band
else:
st_val = nan_val
bull = (trend_dir == 1)
if not has_locked_init:
locked_bull = bull
has_locked_init = True
streak = 0
flip_up = False
flip_down = False
any_flip = False
else:
streak = 0 if (bull == locked_bull) 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
if any_flip:
flip_quality = quality_pct
has_flipped = True
age = 0
else:
age += 1
if flip_up:
bull_ct += 1
if flip_down:
bear_ct += 1
s_time = float(age) / 60.0
if s_time > 1.0:
s_time = 1.0
s_val = s_time * 0.65 + er_smooth * 0.35
dist_pct = (abs(cl - st_val) / cl * 100.0) if ((not _is_nan(st_val)) and cl > 0.0) else 0.0
st_list[i] = st_val
st_bull_list[i] = st_val if locked_bull else nan_val
st_bear_list[i] = st_val if (not locked_bull) else nan_val
dir_list[i] = 1 if locked_bull else -1
locked_bull_list[i] = locked_bull
flip_up_list[i] = flip_up
flip_down_list[i] = flip_down
any_flip_list[i] = any_flip
flip_y_up_list[i] = lo if flip_up else nan_val
flip_y_down_list[i] = hi if flip_down else nan_val
er_smooth_list[i] = er_smooth
vol_ratio_list[i] = vol_ratio
adaptive_factor_list[i] = adaptive_factor
adaptive_confirm_list[i] = adaptive_confirm
quality_pct_list[i] = quality_pct
flip_quality_list[i] = flip_quality
has_flipped_list[i] = has_flipped
age_list[i] = age
s_list[i] = s_val
dist_pct_list[i] = dist_pct
bull_ct_list[i] = bull_ct
bear_ct_list[i] = bear_ct
return {
"st": st_list,
"st_bull": st_bull_list,
"st_bear": st_bear_list,
"dir": dir_list,
"locked_bull": locked_bull_list,
"flip_up": flip_up_list,
"flip_down": flip_down_list,
"any_flip": any_flip_list,
"flip_y_up": flip_y_up_list,
"flip_y_down": flip_y_down_list,
"er_smooth": er_smooth_list,
"vol_ratio": vol_ratio_list,
"adaptive_factor": adaptive_factor_list,
"adaptive_confirm": adaptive_confirm_list,
"quality_pct": quality_pct_list,
"flip_quality": flip_quality_list,
"has_flipped": has_flipped_list,
"age": age_list,
"s": s_list,
"dist_pct": dist_pct_list,
"bull_ct": bull_ct_list,
"bear_ct": bear_ct_list,
}
if __name__ == "__main__":
i_len = input_parameter("ATR 週期", 14)
i_factor = input_parameter("基礎倍數", 3.5)
i_er_len = input_parameter("ER 週期", 10)
i_confirm = input_parameter("確認K線數 (基礎)", 2)
i_glow = input_parameter("發光光暈", True)
i_shade = input_parameter("漸層背景", True)
i_labels = input_parameter("多空訊號標籤", True)
i_bull = input_parameter("做多顏色", Color.rgb(38, 166, 154, 255))
i_bear = input_parameter("做空顏色", Color.rgb(239, 83, 80, 255))
res = _compute_v4_series(
open().values,
high().values,
low().values,
close().values,
int(i_len),
float(i_factor),
int(i_er_len),
int(i_confirm),
)
plot("做多趨勢線", res["st_bull"], color=i_bull, style=Line.line, linewidth=2)
plot("做空趨勢線", res["st_bear"], color=i_bear, style=Line.line, linewidth=2)
if i_glow:
plot("外圍光暈多", res["st_bull"], color=Color.rgb(38, 166, 154, 28), style=Line.line, linewidth=9)
plot("外圍光暈空", res["st_bear"], color=Color.rgb(239, 83, 80, 28), style=Line.line, linewidth=9)
if i_shade:
plot_fillcolor("做多背景", res["st_bull"], close(), cond=True, color=Color.rgb(38, 166, 154, 48))
plot_fillcolor("做空背景", res["st_bear"], close(), cond=True, color=Color.rgb(239, 83, 80, 48))
if i_labels:
# 依 66145 修正:對應 Pine yloc.belowbar/abovebar 錨定 low/high,而非 st
plot_icon("做多訊號點", res["flip_up"], res["flip_y_up"], Shape.triangleup, color=i_bull, size=2, offseth=0, offsetv=-4)
plot_icon("做空訊號點", res["flip_down"], res["flip_y_down"], Shape.triangledown, color=i_bear, size=2, offseth=0, offsetv=4)
plot_text("做多標籤", res["flip_up"], res["flip_y_up"], "做多", color=i_bull, size=2, offseth=0, offsetv=-14)
plot_text("做空標籤", res["flip_down"], res["flip_y_down"], "做空", color=i_bear, size=2, offseth=0, offsetv=14)
output_parameter(
st=res["st"],
trend_dir=res["dir"],
flip_up=res["flip_up"],
flip_down=res["flip_down"],
flip_quality=res["flip_quality"],
age=res["age"],
conviction=res["s"],
er_smooth=res["er_smooth"],
vol_ratio=res["vol_ratio"],
adaptive_factor=res["adaptive_factor"],
adaptive_confirm=res["adaptive_confirm"],
dist_pct=res["dist_pct"],
bull_ct=res["bull_ct"],
bear_ct=res["bear_ct"],
)