feat: 港股ATR波动率止损校验

- 新增 is_hk_stock(code): 港股判断(5位代码)
- 新增 calc_atr(code): 腾讯API K线→ATR(14)计算
- 港股止损增加ATR校验:止损距现价不得小于1×ATR
  技术位仍为主依据,ATR仅作为港股高波动下的间距底线
  A股不受影响保持原有逻辑
- 不违背"技术位优先,不机械乘系数"原则
This commit is contained in:
知微
2026-06-25 21:11:07 +08:00
parent dff8e17d68
commit b63475402e
2 changed files with 42 additions and 0 deletions
Binary file not shown.
+42
View File
@@ -18,6 +18,38 @@ from datetime import datetime
import technical_analysis as ta import technical_analysis as ta
import multi_timeframe as mtf import multi_timeframe as mtf
def is_hk_stock(code):
"""判断是否港股(港股代码5位,A股6位带前导零)"""
return len(str(code)) <= 5
def calc_atr(code, period=14):
"""从腾讯API K线数据计算ATR(period),返回ATR值或None"""
try:
url = f"http://ifzq.gtimg.cn/appstock/app/fqkline/get?param=hk{code},day,,,60,qfq"
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
resp = urllib.request.urlopen(req, timeout=5).read().decode('utf-8')
data = json.loads(resp)
bars = data.get('data', {}).get(f'hk{code}', {}).get('day', [])
if len(bars) < period + 1:
return None
trs = []
for i in range(1, min(len(bars), period + 1)):
try:
high = float(bars[i][2])
low = float(bars[i][3])
prev_close = float(bars[i-1][4]) if len(bars[i-1]) > 4 else float(bars[i-1][3])
tr = max(high - low, abs(high - prev_close), abs(low - prev_close))
trs.append(tr)
except (ValueError, IndexError):
continue
if not trs:
return None
return round(sum(trs) / len(trs), 2)
except Exception:
return None
# 提示词版本追踪 # 提示词版本追踪
try: try:
from prompt_manager.tracking import record_strategy_generation from prompt_manager.tracking import record_strategy_generation
@@ -581,6 +613,16 @@ def reassess_strategy(code, name, price, cost, shares, current_action,
new_stop = min_stop new_stop = min_stop
if old_stop != new_stop: if old_stop != new_stop:
print(f" 最小止损 {round(min_stop_gap*100)}%间距约束: {old_stop}{new_stop} (趋势{'' if is_strong_trend else '普通'})") print(f" 最小止损 {round(min_stop_gap*100)}%间距约束: {old_stop}{new_stop} (趋势{'' if is_strong_trend else '普通'})")
# 港股附加:ATR波动率校验 — 止损距现价不得小于 1×ATR(14)
if is_hk_stock(code):
atr = calc_atr(code)
if atr and atr > 0:
min_atr_stop = round(price - atr, 2)
if new_stop > min_atr_stop:
old_stop_val = new_stop
new_stop = min_atr_stop
print(f" 港股ATR波动率校验({atr:.2f}): 止损 {old_stop_val}{new_stop} (1×ATR间距)")
# ----- 止盈设置 ----- # ----- 止盈设置 -----
if is_short_term_strong_trend and not is_new_entry: if is_short_term_strong_trend and not is_new_entry: