{"total": 142, "pass": 142, "fail": 0}
# -*- coding: utf-8 -*-
"""A23 獨立覆核:A36 v4_futu_custom_indicator_hk.py vs A36 黃金 fixture (3fc8ce27, 300棒)
方法:stub 富途運行時 API → exec A36 原檔(唔改一字)→ 抽數值序列 → 逐項比對 expected_*。
"""
import json, math, sys, io, contextlib
# ---- fixture ----
fx = json.load(open('v4_fixture_300.json'))
bars = fx['bars']
E = fx['expected']
P = fx['params']
ts = [b[0] for b in bars]
o = [b[1] for b in bars]
h = [b[2] for b in bars]
l = [b[3] for b in bars]
c = [b[4] for b in bars]
n = len(c)
class Vec(list):
"""numpy-like minimal: c*0.0, len, index, assignment"""
def __mul__(self, x):
return Vec(float(v) * x for v in self)
__rmul__ = __mul__
def __add__(self, x):
if isinstance(x, (int, float)):
return Vec(float(v) + x for v in self)
return Vec(a + b for a, b in zip(self, x))
__radd__ = __add__
def __sub__(self, x):
if isinstance(x, (int, float)):
return Vec(float(v) - x for v in self)
return Vec(a - b for a, b in zip(self, x))
def __gt__(self, x):
if isinstance(x, (int, float)):
return Vec(v > x for v in self)
return Vec(a > b for a, b in zip(self, x))
def __lt__(self, x):
if isinstance(x, (int, float)):
return Vec(v < x for v in self)
return Vec(a < b for a, b in zip(self, x))
def __ge__(self, x):
if isinstance(x, (int, float)):
return Vec(v >= x for v in self)
return Vec(a >= b for a, b in zip(self, x))
def __le__(self, x):
if isinstance(x, (int, float)):
return Vec(v <= x for v in self)
return Vec(a <= b for a, b in zip(self, x))
def __neg__(self):
return Vec(-v for v in self)
# ---- Futu runtime stubs ----
NS = {}
_state = {'inputs': {}}
def indicator(*a, **k): pass
def input_parameter(name, default, *a, **k):
_state['inputs'][name] = default
return default
def plot(*a, **k): pass
def plot_icon(*a, **k): pass
def plot_text(*a, **k): pass
def output_parameter(**k):
_state['out'] = k
class Color:
def __init__(self, *a, **k): pass
@staticmethod
def hex(s): return s
class Shape: circle = 'circle'
class Line: line = 'line'
NS.update(dict(
indicator=indicator, input_parameter=input_parameter,
close=lambda: Vec(c), open=lambda: Vec(o),
high=lambda: Vec(h), low=lambda: Vec(l),
plot=plot, plot_icon=plot_icon, plot_text=plot_text, output_parameter=output_parameter,
Color=Color, Shape=Shape, Line=Line,
math=math, __name__='__main__',
))
src = open('v4_futu_custom_indicator_hk.py', encoding='utf-8').read()
code = compile(src, 'v4_futu_custom_indicator_hk.py', 'exec')
_qps = {} # bar -> per-bar _q_pct
_ss = {} # bar -> per-bar _s
def _tr(frame, event, arg):
if frame.f_code.co_filename != 'v4_futu_custom_indicator_hk.py':
return None
if event == 'line':
ln = frame.f_lineno
# 主迴圈:line 123..265(_dist_pct_seq[i] 賦值喺 263)
if 123 <= ln <= 265:
loc = frame.f_locals
i = loc.get('i')
if isinstance(i, int) and '_q_pct' in loc and '_s' in loc:
_qps[i] = loc['_q_pct']
_ss[i] = loc['_s']
return _tr
return _tr
sys.settrace(_tr)
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
exec(code, NS)
sys.settrace(None)
g = NS
_st = g['_st']; _age = g['_age_seq']; _ers = g['_er_smooth']; _vr = g['_vol_ratio_seq']
_af = g['_adapt_fac_seq']; _ac = g['_adapt_cfm_seq']; _ldir = g['_locked_dir']
_sp = g['_strength_pct_seq']; _lq = g['_last_q_seq']; _dp = g['_dist_pct_seq']
_bct = g['_bull_ct_seq']; _bct2 = g['_bear_ct_seq']
_upA, _upB, _upC = g['_up_A'], g['_up_B'], g['_up_C']
_dnA, _dnB, _dnC = g['_dn_A'], g['_dn_B'], g['_dn_C']
_q_pct = None # 逐棒 q_pct 未單獨導出 — 從 _up/_dn 標籤級別 + fixture quality 對
# 重算逐棒 q_pct(同源公式,僅用於 label quality 對數;A36 檔內 _q_pct 局部變量)
# 改由 expected_labels 的 quality 對:flip 棒的 _last_q_seq 即 quality
results = []
def chk(name, ok, got, want, eps=1e-6):
results.append((name, bool(ok), got, want))
def close(a, b, eps=1e-6):
if a is None or b is None: return False
if isinstance(a, float) and math.isnan(a): return isinstance(b, float) and math.isnan(b)
return abs(float(a) - float(b)) <= eps
# 1) checkpoints
for cp in E['expected_checkpoints']:
i = cp['index']
chk(f"cp[{i}].st", close(_st[i], cp['st'], 1e-6), _st[i], cp['st'])
chk(f"cp[{i}].erSmooth", close(_ers[i], cp['erSmooth'], 1e-9), _ers[i], cp['erSmooth'])
chk(f"cp[{i}].volRatio", close(_vr[i], cp['volRatio'], 1e-9), _vr[i], cp['volRatio'])
chk(f"cp[{i}].adaptiveFactor", close(_af[i], cp['adaptiveFactor'], 1e-9), _af[i], cp['adaptiveFactor'])
chk(f"cp[{i}].adaptiveConfirm", _ac[i] == float(cp['adaptiveConfirm']), _ac[i], cp['adaptiveConfirm'])
chk(f"cp[{i}].lockedBull", (_ldir[i] > 0) == cp['lockedBull'], _ldir[i], cp['lockedBull'])
if 'qualityPct' in cp:
chk(f"cp[{i}].qualityPct", _qps.get(i) == int(cp['qualityPct']), _qps.get(i), cp['qualityPct'])
if 'age' in cp:
chk(f"cp[{i}].age", _age[i] == float(cp['age']), _age[i], cp['age'])
if 's' in cp:
chk(f"cp[{i}].s", close(_ss.get(i), float(cp['s']), 1e-9), _ss.get(i), cp['s'])
# 2) labels — flip 棒集合 + 方向 + quality + y(=st)
# A36 標籤 flags:
flip_bars = []
for i in range(n):
up = (_upA[i] + _upB[i] + _upC[i]) > 0.5
dn = (_dnA[i] + _dnB[i] + _dnC[i]) > 0.5
if up or dn:
flip_bars.append((i, 'LONG' if up else 'SHORT'))
want = [(x['barIndex'], x['direction']) for x in E['expected_labels']]
chk('labels.bars_dirs', flip_bars == want, flip_bars, want)
for wl in E['expected_labels']:
i = wl['barIndex']
chk(f"label[{i}].y", close(_st[i], wl['y'], 1e-6), _st[i], wl['y'])
# quality:flip 棒 _last_q_seq = _q_pct(any_flip 當棒更新)
chk(f"label[{i}].quality", _qps.get(i) == int(wl['quality']), _qps.get(i), wl['quality'])
grade = wl['grade']
flag = (_upA if wl['direction']=='LONG' else _dnA)[i] if grade=='A' else ((_upB if wl['direction']=='LONG' else _dnB)[i] if grade=='B' else ((_upC if wl['direction']=='LONG' else _dnC)[i]))
chk(f"label[{i}].grade", flag > 0.5, flag, grade)
# 3) latest
el = E['expected_latest']
last = n - 1
chk('latest.direction', ((_ldir[last] > 0) and 'LONG' or 'SHORT') == el['direction'], _ldir[last], el['direction'])
chk('latest.st', close(_st[last], el['st'], 1e-6), _st[last], el['st'])
chk('latest.close', close(c[last], el['close'], 1e-9), c[last], el['close'])
chk('latest.age', _age[last] == float(el['age']), _age[last], el['age'])
chk('latest.convictionPct', _sp[last] == float(el['convictionPct']), _sp[last], el['convictionPct'])
chk('latest.erSmooth', close(_ers[last], el['erSmooth'], 1e-9), _ers[last], el['erSmooth'])
chk('latest.volRatio', close(_vr[last], el['volRatio'], 1e-9), _vr[last], el['volRatio'])
chk('latest.adaptiveFactor', close(_af[last], el['adaptiveFactor'], 1e-9), _af[last], el['adaptiveFactor'])
chk('latest.adaptiveConfirm', _ac[last] == float(el['adaptiveConfirm']), _ac[last], el['adaptiveConfirm'])
chk('latest.flipQuality', close(_lq[last], float(el['flipQuality']), 1e-9), _lq[last], el['flipQuality'])
chk('latest.bullFlips', _bct[last] == float(el['bullFlips']), _bct[last], el['bullFlips'])
chk('latest.bearFlips', _bct2[last] == float(el['bearFlips']), _bct2[last], el['bearFlips'])
chk('latest.distancePct', close(_dp[last], el['distancePct'], 1e-6), _dp[last], el['distancePct'])
# 4) dashboard numerics(output_parameter 尾值 vs expected_dashboard_rows)
out = _state.get('out', {})
rowmap = {r['row']: r for r in E['expected_dashboard_rows']}
# row1 direction
chk('dash.direction', ((out.get('當前方向',[0]*n)[last] > 0) and 'LONG' or 'SHORT') == 'LONG', out.get('當前方向'), 'LONG')
# row2 age 59; row3 conviction 99; row4 er 1.00; row5 vol 1.00; row6 af 3.51; row7 q 82; row8 dist 0.91; row9/10 counts
chk('dash.age', out.get('持續燭數',[0]*n)[last] == float(rowmap[2]['valueZh'].split()[0]), out.get('持續燭數',[0]*n)[last], rowmap[2]['valueZh'])
chk('dash.conv', out.get('趨勢強度Pct',[0]*n)[last] == float(rowmap[3]['valueZh'].split()[-1].rstrip('%')), out.get('趨勢強度Pct',[0]*n)[last], rowmap[3]['valueZh'])
chk('dash.er', close(out.get('趨勢效率ER',[0]*n)[last], 1.00, 0.005), out.get('趨勢效率ER',[0]*n)[last], rowmap[4]['valueZh'])
chk('dash.vol', close(out.get('波幅比率',[0]*n)[last], 1.00, 0.005), out.get('波幅比率',[0]*n)[last], rowmap[5]['valueZh'])
chk('dash.af', close(out.get('自適應倍數',[0]*n)[last], 3.51, 0.005), out.get('自適應倍數',[0]*n)[last], rowmap[6]['valueZh'])
chk('dash.q', out.get('上次質素分',[0]*n)[last] == float(rowmap[7]['valueZh'].split('/')[0]), out.get('上次質素分',[0]*n)[last], rowmap[7]['valueZh'])
chk('dash.dist', close(out.get('偏離趨勢線Pct',[0]*n)[last], 0.91, 0.005), out.get('偏離趨勢線Pct',[0]*n)[last], rowmap[8]['valueZh'])
chk('dash.bull', out.get('造好訊號次數',[0]*n)[last] == float(rowmap[9]['valueZh'].split()[0]), out.get('造好訊號次數',[0]*n)[last], rowmap[9]['valueZh'])
chk('dash.bear', out.get('造淡訊號次數',[0]*n)[last] == float(rowmap[10]['valueZh'].split()[0]), out.get('造淡訊號次數',[0]*n)[last], rowmap[10]['valueZh'])
# 彙總
fails = [r for r in results if not r[1]]
summary = {'total': len(results), 'pass': len(results) - len(fails), 'fail': len(fails)}
print(json.dumps(summary, ensure_ascii=False))
for name, ok, got, want in fails[:40]:
print(f"FAIL {name}: got={got!r} want={want!r}")
sys.exit(1 if fails else 0)
A36 第五節 ① v4_futu_custom_indicator_hk.py:以黃金 fixture 3fc8ce27 對數 —— 0 diff。 st/ER/波幅/自適應乘數與確認/質素評分/信念/翻轉事件/dashboard 全部逐項相同。 可作獨立覆核第二票(A36 自覆為第一票)。
本頁零憑證。A23 · A233804 · file: A23_66137_VERIFY_A36_FUTU_HK.html · 被核檔原文+fixture 摘錄僅作對數,無修改。