//@version=6 indicator("缠论修复版", shorttitle="修复版", overlay=true, max_lines_count=100, max_labels_count=100) // 参数 min_gap = input.int(3, "分型间隔", minval=1, maxval=20) show_fractals = input.bool(true, "显示分型") show_strokes = input.bool(true, "显示笔") show_filtered = input.bool(true, "显示过滤后分型") // 数据类型 type Fractal int bar_index float price string type bool is_valid // 全局变量 var all_fractals = array.new() var valid_fractals = array.new() var stroke_lines = array.new() // 分型识别 is_high = high[1] > high[0] and high[1] > high[2] is_low = low[1] < low[0] and low[1] < low[2] // 距离检查 distance_ok(new_bar) => result = true if array.size(all_fractals) > 0 last_fractal = array.get(all_fractals, array.size(all_fractals) - 1) if new_bar - last_fractal.bar_index < min_gap result := false result // 添加原始分型 if is_high and distance_ok(bar_index[1]) new_fractal = Fractal.new(bar_index[1], high[1], "top", true) array.push(all_fractals, new_fractal) if array.size(all_fractals) > 100 array.shift(all_fractals) if is_low and distance_ok(bar_index[1]) new_fractal = Fractal.new(bar_index[1], low[1], "bottom", true) array.push(all_fractals, new_fractal) if array.size(all_fractals) > 100 array.shift(all_fractals) // 显示原始分型 if show_fractals if is_high and distance_ok(bar_index[1]) label.new(bar_index[1], high[1], "顶", color=color.red, style=label.style_label_down, size=size.tiny) if is_low and distance_ok(bar_index[1]) 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) >= 1 array.push(valid_fractals, array.get(all_fractals, 0)) i = 1 while i < array.size(all_fractals) current_fractal = array.get(all_fractals, i) last_valid = array.get(valid_fractals, array.size(valid_fractals) - 1) if current_fractal.type != last_valid.type array.push(valid_fractals, current_fractal) i += 1 else extreme_fractal = current_fractal j = i + 1 while j < array.size(all_fractals) next_fractal = array.get(all_fractals, j) if next_fractal.type == current_fractal.type if current_fractal.type == "top" if next_fractal.price > extreme_fractal.price extreme_fractal := next_fractal else if next_fractal.price < extreme_fractal.price extreme_fractal := next_fractal j += 1 else break array.push(valid_fractals, extreme_fractal) 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_fractal = array.get(valid_fractals, m) next_fractal = array.get(valid_fractals, m + 1) distance = bar_index - current_fractal.bar_index if distance <= 500 stroke_line = line.new(current_fractal.bar_index, current_fractal.price, next_fractal.bar_index, next_fractal.price, color=color.blue, width=2) array.push(stroke_lines, stroke_line) // 显示过滤后的分型 if show_filtered 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.small) else label.new(fractal.bar_index, fractal.price, "B", color=color.orange, style=label.style_label_up, size=size.small) // 信息显示 if barstate.islast var table info = table.new(position.top_right, 2, 5, bgcolor=color.white, border_width=1) table.cell(info, 0, 0, "缠论修复版", text_color=color.black, text_size=size.small) 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.black) table.cell(info, 0, 2, "有效分型", text_color=color.black) table.cell(info, 1, 2, str.tostring(array.size(valid_fractals)), text_color=color.black) table.cell(info, 0, 3, "笔数", text_color=color.black) table.cell(info, 1, 3, str.tostring(array.size(stroke_lines)), text_color=color.black) table.cell(info, 0, 4, "分型间隔", text_color=color.black) table.cell(info, 1, 4, str.tostring(min_gap), text_color=color.black)