//@version=6 indicator("缠论优化版", shorttitle="优化版", overlay=true, max_lines_count=100, max_labels_count=100) // 优化参数设置 min_gap = input.int(8, "分型最小间隔", minval=5, maxval=20, tooltip="增大此值可减少分型数量") strict_mode = input.bool(true, "严格分型模式", tooltip="开启后要求高低点都满足条件") show_original = input.bool(false, "显示原始分型", tooltip="小标签显示所有分型") show_valid = input.bool(true, "显示有效分型", tooltip="大标签显示过滤后分型") show_strokes = input.bool(true, "显示笔连接", tooltip="蓝色粗线连接有效分型") stroke_width = input.int(3, "笔线宽度", minval=1, maxval=5) // 数据类型 type Fractal int bar_index float price string type // "top" or "bottom" bool is_extreme // 是否为极值点 // 全局变量 var all_fractals = array.new() var valid_fractals = array.new() var stroke_lines = array.new() // 优化的分型识别 check_fractal() => top_found = false bottom_found = false if strict_mode // 严格模式:高低点都要满足条件 if high[1] > high[0] and high[1] > high[2] and low[1] > low[0] and low[1] > low[2] top_found := true if low[1] < low[0] and low[1] < low[2] and high[1] < high[0] and high[1] < high[2] bottom_found := true else // 宽松模式:只要高点或低点满足条件 if high[1] > high[0] and high[1] > high[2] top_found := true if low[1] < low[0] and low[1] < low[2] bottom_found := true [top_found, bottom_found] // 距离和类型检查 distance_and_type_ok(new_bar, new_type) => result = true if array.size(all_fractals) > 0 last_fractal = array.get(all_fractals, array.size(all_fractals) - 1) distance = new_bar - last_fractal.bar_index // 距离检查 if distance < min_gap result := false // 同类型分型需要更大间隔 else if last_fractal.type == new_type and distance < min_gap * 1.5 result := false result [is_top, is_bottom] = check_fractal() // 添加分型 if is_top and distance_and_type_ok(bar_index[1], "top") new_fractal = Fractal.new(bar_index[1], high[1], "top", false) array.push(all_fractals, new_fractal) if array.size(all_fractals) > 100 array.shift(all_fractals) if is_bottom and distance_and_type_ok(bar_index[1], "bottom") new_fractal = Fractal.new(bar_index[1], low[1], "bottom", false) array.push(all_fractals, new_fractal) if array.size(all_fractals) > 100 array.shift(all_fractals) // 显示原始分型 if show_original if is_top and distance_and_type_ok(bar_index[1], "top") label.new(bar_index[1], high[1], "顶", color=color.red, style=label.style_label_down, size=size.tiny) if is_bottom and distance_and_type_ok(bar_index[1], "bottom") label.new(bar_index[1], low[1], "底", color=color.green, style=label.style_label_up, size=size.tiny) // 每根K线应用结合律过滤 if barstate.isconfirmed // 清空有效分型数组 array.clear(valid_fractals) // 应用结合律过滤 if array.size(all_fractals) > 0 // 添加第一个分型 array.push(valid_fractals, array.get(all_fractals, 0)) i = 1 while i < array.size(all_fractals) current = array.get(all_fractals, i) last_valid = array.get(valid_fractals, array.size(valid_fractals) - 1) if current.type != last_valid.type // 不同类型,直接添加 array.push(valid_fractals, current) i += 1 else // 相同类型,找到最极端的 extreme = current j = i + 1 // 向前搜索相同类型的分型 while j < array.size(all_fractals) next = array.get(all_fractals, j) if next.type == current.type // 比较极值 if current.type == "top" if next.price > extreme.price extreme := next else // bottom if next.price < extreme.price extreme := next j += 1 else break // 添加极值分型 array.push(valid_fractals, extreme) i := j // 清理旧笔 if array.size(stroke_lines) > 0 for k = 0 to array.size(stroke_lines) - 1 line.delete(array.get(stroke_lines, k)) array.clear(stroke_lines) // 绘制新笔 if show_strokes and array.size(valid_fractals) >= 2 for m = 0 to array.size(valid_fractals) - 2 current = array.get(valid_fractals, m) next = array.get(valid_fractals, m + 1) // 检查距离限制 distance = bar_index - current.bar_index if distance <= 500 line_color = current.type == "bottom" ? color.blue : color.navy stroke_line = line.new(current.bar_index, current.price, next.bar_index, next.price, color=line_color, width=stroke_width) array.push(stroke_lines, stroke_line) // 显示有效分型(大标签) if show_valid and barstate.islast for n = 0 to array.size(valid_fractals) - 1 fractal = array.get(valid_fractals, n) if fractal.type == "top" label.new(fractal.bar_index, fractal.price, "T", color=color.yellow, style=label.style_label_down, size=size.normal, textcolor=color.black) else label.new(fractal.bar_index, fractal.price, "B", color=color.orange, style=label.style_label_up, size=size.normal, textcolor=color.black) // 优化的信息面板 if barstate.islast var table info = table.new(position.top_right, 2, 6, bgcolor=color.white, border_width=1) table.cell(info, 0, 0, "缠论优化版", text_color=color.blue, text_size=size.normal) table.cell(info, 1, 0, "", text_color=color.black) table.cell(info, 0, 1, "原始分型", text_color=color.black) table.cell(info, 1, 1, str.tostring(array.size(all_fractals)), text_color=color.green) table.cell(info, 0, 2, "有效分型", text_color=color.black) table.cell(info, 1, 2, str.tostring(array.size(valid_fractals)), text_color=color.red) table.cell(info, 0, 3, "过滤率", text_color=color.black) filter_rate = array.size(all_fractals) > 0 ? math.round((1 - array.size(valid_fractals) / array.size(all_fractals)) * 100) : 0 table.cell(info, 1, 3, str.tostring(filter_rate) + "%", text_color=color.purple) table.cell(info, 0, 4, "笔数", text_color=color.black) table.cell(info, 1, 4, str.tostring(array.size(stroke_lines)), text_color=color.blue) table.cell(info, 0, 5, "分型间隔", text_color=color.black) table.cell(info, 1, 5, str.tostring(min_gap), text_color=color.gray) // 质量检查:理论上笔数应该 = 有效分型数 - 1 quality_check = array.size(valid_fractals) > 0 ? array.size(stroke_lines) == (array.size(valid_fractals) - 1) : true if barstate.islast and not quality_check label.new(bar_index, high, "⚠️质量检查失败", color=color.red, style=label.style_label_down, size=size.large)