//@version=6 indicator("缠论调试版", shorttitle="缠论调试", overlay=true, max_lines_count=200, max_labels_count=200) // ================== 参数设置 ================== show_fenxing = input.bool(true, "显示分型", group="显示设置") show_bi = input.bool(true, "显示笔", group="显示设置") debug_mode = input.bool(true, "调试模式", group="显示设置") // 缠论参数 min_bars_between = input.int(3, "分型最小间隔", minval=1, maxval=20, group="缠论参数") // ================== 数据类型定义 ================== type FractalPoint int bar_index float price string fractal_type bool confirmed // ================== 全局变量 ================== var fractal_points = array.new() var bi_lines = array.new() // ================== 简单分型识别 ================== // 检查高点分型 is_pivot_high = high[1] > high[0] and high[1] > high[2] // 检查低点分型 is_pivot_low = low[1] < low[0] and low[1] < low[2] // ================== 距离过滤 ================== distance_ok(new_bar, new_type) => if debug_mode true else result = true if array.size(fractal_points) > 0 last_fractal = array.get(fractal_points, array.size(fractal_points) - 1) bar_distance = new_bar - last_fractal.bar_index if bar_distance < min_bars_between result := false result // ================== 添加分型 ================== var bool fractal_added = false fractal_added := false if is_pivot_high and distance_ok(bar_index[1], "top") new_fractal = FractalPoint.new(bar_index[1], high[1], "top", true) array.push(fractal_points, new_fractal) fractal_added := true if array.size(fractal_points) > 100 array.shift(fractal_points) if is_pivot_low and distance_ok(bar_index[1], "bottom") new_fractal = FractalPoint.new(bar_index[1], low[1], "bottom", true) array.push(fractal_points, new_fractal) fractal_added := true if array.size(fractal_points) > 100 array.shift(fractal_points) // ================== 显示分型 ================== if show_fenxing if is_pivot_high and distance_ok(bar_index[1], "top") label.new(bar_index[1], high[1], "顶", color=color.red, style=label.style_label_down, size=size.small) if is_pivot_low and distance_ok(bar_index[1], "bottom") label.new(bar_index[1], low[1], "底", color=color.green, style=label.style_label_up, size=size.small) // ================== 构建笔 ================== if fractal_added and array.size(fractal_points) >= 2 last_fractal = array.get(fractal_points, array.size(fractal_points) - 1) second_last_fractal = array.get(fractal_points, array.size(fractal_points) - 2) // 如果最近两个分型类型不同,构建笔 if last_fractal.fractal_type != second_last_fractal.fractal_type // 检查bar_index距离是否在合理范围内(Pine Script限制) bar_distance = bar_index - second_last_fractal.bar_index if bar_distance <= 500 // 限制在500根K线范围内 // 绘制笔 if show_bi new_line = line.new(second_last_fractal.bar_index, second_last_fractal.price, last_fractal.bar_index, last_fractal.price, color=color.blue, width=2, style=line.style_solid) array.push(bi_lines, new_line) // 限制笔的数量 if array.size(bi_lines) > 200 old_line = array.shift(bi_lines) line.delete(old_line) // ================== 信息面板 ================== if barstate.islast var info_table = table.new(position.top_right, 2, 5, bgcolor=color.white, border_width=1) table.cell(info_table, 0, 0, "缠论调试版", text_color=color.black, text_size=size.small) table.cell(info_table, 1, 0, "", text_color=color.black) table.cell(info_table, 0, 1, "分型数", text_color=color.black) table.cell(info_table, 1, 1, str.tostring(array.size(fractal_points)), text_color=color.black) table.cell(info_table, 0, 2, "笔数", text_color=color.black) table.cell(info_table, 1, 2, str.tostring(array.size(bi_lines)), text_color=color.black) table.cell(info_table, 0, 3, "调试模式", text_color=color.black) table.cell(info_table, 1, 3, debug_mode ? "开" : "关", text_color=color.black) table.cell(info_table, 0, 4, "当前K线", text_color=color.black) table.cell(info_table, 1, 4, str.tostring(bar_index), text_color=color.black)