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
+171 -9
View File
@@ -488,42 +488,204 @@ def analyze_chan(df):
}
def identify_trade_points(bi_list, seg_list, zs_list):
"""识别缠论买卖点 - 只保留最重要的一类买卖点,减少标记干扰"""
"""识别缠论买卖点 - 多级别识别,减少滞后性"""
trade_points = []
# 输出调试信息
print(f"识别买卖点:总共 {len(bi_list)} 个笔, {len(seg_list)} 个线段, {len(zs_list)} 个中枢")
# 只识别一类买卖点:线段向上或向下突破
# 1. 基于笔的二三类买卖点识别(更及时)
trade_points.extend(identify_bi_trade_points(bi_list, zs_list))
# 2. 基于线段的一类买卖点识别(传统方法)
trade_points.extend(identify_seg_trade_points(seg_list))
# 3. 基于分型强度的预警点识别(最及时)
trade_points.extend(identify_fx_warning_points(bi_list))
# 4. 基于MACD背驰的买卖点识别
trade_points.extend(identify_macd_divergence_points(bi_list))
# 按时间排序
trade_points.sort(key=lambda x: x['time'])
print(f"总共识别出 {len(trade_points)} 个买卖点")
return trade_points
def identify_bi_trade_points(bi_list, zs_list):
"""基于笔识别二三类买卖点 - 更及时的信号"""
trade_points = []
if len(bi_list) < 3:
return trade_points
# 构建中枢映射,便于快速查找
zs_map = {}
for zs in zs_list:
if zs.is_sure: # 只考虑已确认的中枢
zs_map[zs.start_klc.end_time] = zs
for i in range(2, len(bi_list)):
current_bi = bi_list[i]
prev_bi = bi_list[i-1]
prev_prev_bi = bi_list[i-2]
# 确保笔已完成
if not current_bi.end_klc or not prev_bi.end_klc or not prev_prev_bi.end_klc:
continue
# 二类买点:向下笔后的向上笔,且不创新低
if (convert_direction(prev_bi.dir) == -1 and
convert_direction(current_bi.dir) == 1):
prev_low = prev_bi.end_klc.low
current_end_price = current_bi.end_klc.high
# 检查是否不创新低(相对于前面的低点)
if i >= 4: # 至少需要5个笔来判断
earlier_lows = [bi.end_klc.low for bi in bi_list[max(0, i-4):i-1]
if convert_direction(bi.dir) == -1 and bi.end_klc]
if earlier_lows and prev_low > min(earlier_lows):
trade_points.append({
'type': TRADE_POINT_TYPE.BUY2,
'time': current_bi.end_klc.end_time,
'price': current_end_price,
'desc': '二类买点(笔)'
})
# 二类卖点:向上笔后的向下笔,且不创新高
if (convert_direction(prev_bi.dir) == 1 and
convert_direction(current_bi.dir) == -1):
prev_high = prev_bi.end_klc.high
current_end_price = current_bi.end_klc.low
# 检查是否不创新高(相对于前面的高点)
if i >= 4: # 至少需要5个笔来判断
earlier_highs = [bi.end_klc.high for bi in bi_list[max(0, i-4):i-1]
if convert_direction(bi.dir) == 1 and bi.end_klc]
if earlier_highs and prev_high < max(earlier_highs):
trade_points.append({
'type': TRADE_POINT_TYPE.SELL2,
'time': current_bi.end_klc.end_time,
'price': current_end_price,
'desc': '二类卖点(笔)'
})
return trade_points
def identify_seg_trade_points(seg_list):
"""基于线段识别一类买卖点 - 传统方法"""
trade_points = []
if len(seg_list) >= 3:
for i in range(2, len(seg_list)):
# 确保线段已完成
if seg_list[i].end_bi and seg_list[i-1].end_bi and seg_list[i-2].end_bi:
# 一类买点:向下-向上-向下的底分型,第三段结束点为买点
# 一类买点:向下-向上-向下的底分型
if (convert_direction(seg_list[i-2].dir) == -1 and
convert_direction(seg_list[i-1].dir) == 1 and
convert_direction(seg_list[i].dir) == -1):
print(f"发现一类买点:线段方向 {convert_direction(seg_list[i-2].dir)}-{convert_direction(seg_list[i-1].dir)}-{convert_direction(seg_list[i].dir)}")
trade_points.append({
'type': TRADE_POINT_TYPE.BUY1,
'time': seg_list[i].end_bi.end_klc.end_time,
'price': seg_list[i].end_bi.end_klc.low,
'desc': '一类买点'
'desc': '一类买点(线段)'
})
# 一类卖点:向上-向下-向上的顶分型,第三段结束点为卖点
# 一类卖点:向上-向下-向上的顶分型
if (convert_direction(seg_list[i-2].dir) == 1 and
convert_direction(seg_list[i-1].dir) == -1 and
convert_direction(seg_list[i].dir) == 1):
print(f"发现一类卖点:线段方向 {convert_direction(seg_list[i-2].dir)}-{convert_direction(seg_list[i-1].dir)}-{convert_direction(seg_list[i].dir)}")
trade_points.append({
'type': TRADE_POINT_TYPE.SELL1,
'time': seg_list[i].end_bi.end_klc.end_time,
'price': seg_list[i].end_bi.end_klc.high,
'desc': '一类卖点'
'desc': '一类卖点(线段)'
})
print(f"总共识别出 {len(trade_points)} 个买卖点")
return trade_points
def identify_fx_warning_points(bi_list):
"""基于分型强度识别预警点 - 最及时的信号"""
trade_points = []
if len(bi_list) < 2:
return trade_points
# 检查最近的几个笔
recent_bis = bi_list[-3:] if len(bi_list) >= 3 else bi_list
for bi in recent_bis:
if not bi.end_klc:
continue
# 获取分型强度(如果有的话)
fx_strength = 0
if hasattr(bi.end_klc, 'cal_fx_strength'):
try:
fx_strength = bi.end_klc.cal_fx_strength()
except:
fx_strength = 0
# 强分型预警(分型强度>=2
if fx_strength >= 2:
if convert_direction(bi.dir) == -1: # 向下笔结束,可能的底部
trade_points.append({
'type': TRADE_POINT_TYPE.BUY3,
'time': bi.end_klc.end_time,
'price': bi.end_klc.low,
'desc': f'强分型预警-买点(强度:{fx_strength})'
})
elif convert_direction(bi.dir) == 1: # 向上笔结束,可能的顶部
trade_points.append({
'type': TRADE_POINT_TYPE.SELL3,
'time': bi.end_klc.end_time,
'price': bi.end_klc.high,
'desc': f'强分型预警-卖点(强度:{fx_strength})'
})
return trade_points
def identify_macd_divergence_points(bi_list):
"""基于MACD背驰识别买卖点"""
trade_points = []
if len(bi_list) < 4:
return trade_points
# 检查最近的笔是否有背驰
for i in range(2, len(bi_list)):
current_bi = bi_list[i]
if not current_bi.end_klc or not hasattr(current_bi, 'macd_div'):
continue
# MACD背驰阈值
divergence_threshold = 0.3
# 向下笔的底背驰 -> 买点
if (convert_direction(current_bi.dir) == -1 and
hasattr(current_bi, 'macd_div') and
current_bi.macd_div > divergence_threshold):
trade_points.append({
'type': TRADE_POINT_TYPE.BUY2,
'time': current_bi.end_klc.end_time,
'price': current_bi.end_klc.low,
'desc': f'MACD底背驰买点(背驰度:{current_bi.macd_div:.2f})'
})
# 向上笔的顶背驰 -> 卖点
elif (convert_direction(current_bi.dir) == 1 and
hasattr(current_bi, 'macd_div') and
current_bi.macd_div > divergence_threshold):
trade_points.append({
'type': TRADE_POINT_TYPE.SELL2,
'time': current_bi.end_klc.end_time,
'price': current_bi.end_klc.high,
'desc': f'MACD顶背驰卖点(背驰度:{current_bi.macd_div:.2f})'
})
return trade_points
# 辅助函数,转换缠论方向枚举为整数