add more to klu and klc

This commit is contained in:
jackyu66git
2025-06-03 23:40:19 +08:00
parent ec46febceb
commit 872eec8100
4 changed files with 512 additions and 11 deletions
+115
View File
@@ -63,7 +63,121 @@ class ChanKLU:
self.fx_type = Chan_FX_TYPE.UNKNOWN
self.fx_confirmed = False
return False
def cal_fx(self):
"""
根据缠论经典规则计算分型强弱
返回分型强度:3=极强,2=强,1=中等,0=弱,-1=极弱
"""
if self.fx_type == Chan_FX_TYPE.UNKNOWN or not self.pre or not self.next:
return 0
if self.fx_type == Chan_FX_TYPE.TOP:
return self._cal_top_fx_strength()
else: # BOTTOM
return self._cal_bottom_fx_strength()
def _check_contain_relation(self, k1, k2):
"""检查两根K线是否存在包含关系"""
return (k1.high >= k2.high and k1.low <= k2.low) or (k2.high >= k1.high and k2.low <= k1.low)
def _is_big_yang_line(self, klu):
"""判断是否为大阳线"""
return klu.close > klu.open and (klu.close - klu.open) / klu.open > 0.02
def _is_big_yin_line(self, klu):
"""判断是否为大阴线"""
return klu.close < klu.open and (klu.open - klu.close) / klu.open > 0.02
def _is_small_line(self, klu):
"""判断是否为小K线"""
return abs(klu.close - klu.open) / klu.open < 0.01
def _has_long_upper_shadow(self, klu):
"""判断是否有长上影线"""
body_size = abs(klu.close - klu.open)
upper_shadow = klu.high - max(klu.close, klu.open)
return upper_shadow > body_size * 1.5
def _cal_top_fx_strength(self):
"""计算顶分型强度"""
strength = 0
k1, k2, k3 = self.pre, self, self.next
# (1) 检查包含关系 - 没有包含关系加分
has_contain_12 = self._check_contain_relation(k1, k2)
has_contain_23 = self._check_contain_relation(k2, k3)
if not has_contain_12 and not has_contain_23:
strength += 1 # 完全没有包含关系,加1分
# (2) 检查第1条K线是大阳线,第2、3条是小K线的情况
if self._is_big_yang_line(k1) and self._is_small_line(k2) and self._is_small_line(k3):
strength -= 2 # 中继顶分型特征,减2分
# (3) 检查第2条K线有长上影线或大阴线,且第3条K线条件
k2_mid = (k2.high + k2.low) / 2
k3_is_yang = k3.close > k3.open
k3_close_above_mid = k3.close > k2_mid
if (self._has_long_upper_shadow(k2) or self._is_big_yin_line(k2)) and not (k3_is_yang and k3_close_above_mid):
strength += 2 # 力度大的顶分型,加2分
# (4) 检查第2、3条K线包含关系,第3条为大阴线"吃掉"第2条
if has_contain_23 and self._is_big_yin_line(k3) and k3.low < k2.low and k3.high < k2.high:
strength += 1 # 最坏包含关系,但对顶分型有利,加1分
# (5) 第3条K线跌破第1条K线底部且不能高于第1条K线区间一半之上
k1_mid = (k1.high + k1.low) / 2
if k3.low < k1.low and k3.high < k1_mid:
strength -= 1 # 较弱的顶分型,减1分
# 额外检查:第3条K线收盘价相对第1条K线的位置
if k3.close < k1.low:
strength += 1 # 强烈下跌确认,加1分
return max(-1, min(3, strength)) # 限制在-1到3范围内
def _cal_bottom_fx_strength(self):
"""计算底分型强度"""
strength = 0
k1, k2, k3 = self.pre, self, self.next
# 底分型上边沿
fx_top = max(k1.high, k2.high)
# (1) 第3条K线高点远高于第1条K线高点
if k3.high > k1.high * 1.02: # 高出2%以上认为是"远高于"
strength += 2 # 较强走势,加2分
# (2) 第3条K线高点正好是第1根K线高点,或略微高于底分型上边沿
elif k1.high * 0.99 <= k3.high <= fx_top * 1.01: # 在合理范围内
strength += 0 # 一般走势,不加分也不减分
# (3) 第3条K线高点低于第1条K线高点
elif k3.high < k1.high:
strength -= 1 # 较弱走势,减1分
# 检查包含关系
has_contain_12 = self._check_contain_relation(k1, k2)
has_contain_23 = self._check_contain_relation(k2, k3)
if not has_contain_12 and not has_contain_23:
strength += 1 # 完全没有包含关系,加1分
# 检查第3条K线是否为强阳线
if self._is_big_yang_line(k3):
strength += 1 # 强阳线确认,加1分
# (4) 检查后续第1条K线(如果存在)
if hasattr(k3, 'next') and k3.next:
next_k = k3.next
if next_k.low > fx_top:
strength += 2 # 后续K线低点高于底分型上边沿,强烈确认,加2分
elif next_k.low <= k2.low:
strength -= 1 # 后续K线跌破分型低点,减1分
return max(-1, min(3, strength)) # 限制在-1到3范围内
def calculate_realtime_fx_strength(self):
"""
用self.pre和self.next实现分型强弱判断(与KLC中cal_fx_strength一致)
@@ -126,6 +240,7 @@ class ChanKLU:
if final_score > 1.8:
print(self.time, final_score, is_bi_end, post_fx_confirmation, fx_quality)
#print(self.time, final_score, is_bi_end, post_fx_confirmation, fx_quality)
self.fx_strength = self.cal_fx()
return self.fx_strength
def _check_if_bi_ending_fx(self):