//@version=6 indicator("缠论测试版", shorttitle="缠论测试", overlay=true) // 参数设置 show_merged_k = input.bool(true, "显示合并K线") show_fenxing = input.bool(true, "显示分型") show_bi = input.bool(true, "显示笔") // 数据类型 type MergedK int bar_index float high float low float open float close type FractalPoint int bar_index float price string fractal_type // 全局变量 var merged_ks = array.new() var fractal_points = array.new() var current_trend = 0 // 包含关系判断 is_contained(k1_high, k1_low, k2_high, k2_low) => (k1_high >= k2_high and k1_low <= k2_low) or (k2_high >= k1_high and k2_low <= k1_low) // K线合并 if barstate.isconfirmed current_k = MergedK.new(bar_index, high, low, open, close) if array.size(merged_ks) == 0 array.push(merged_ks, current_k) else last_k = array.get(merged_ks, array.size(merged_ks) - 1) if is_contained(last_k.high, last_k.low, current_k.high, current_k.low) // 合并K线 if current_trend > 0 last_k.high := math.max(last_k.high, current_k.high) last_k.low := math.max(last_k.low, current_k.low) else if current_trend < 0 last_k.high := math.min(last_k.high, current_k.high) last_k.low := math.min(last_k.low, current_k.low) else if last_k.close > last_k.open last_k.high := math.max(last_k.high, current_k.high) last_k.low := math.max(last_k.low, current_k.low) else last_k.high := math.min(last_k.high, current_k.high) last_k.low := math.min(last_k.low, current_k.low) last_k.bar_index := current_k.bar_index else array.push(merged_ks, current_k) // 更新趋势 if array.size(merged_ks) >= 2 prev_k = array.get(merged_ks, array.size(merged_ks) - 2) if current_k.high > prev_k.high current_trend := 1 else if current_k.low < prev_k.low current_trend := -1 // 分型识别 if array.size(merged_ks) >= 3 k1 = array.get(merged_ks, array.size(merged_ks) - 3) k2 = array.get(merged_ks, array.size(merged_ks) - 2) k3 = array.get(merged_ks, array.size(merged_ks) - 1) // 顶分型 if k2.high > k1.high and k2.high > k3.high and k2.low > k1.low and k2.low > k3.low new_fractal = FractalPoint.new(k2.bar_index, k2.high, "top") array.push(fractal_points, new_fractal) if show_fenxing label.new(k2.bar_index, k2.high, "顶", color=color.red, style=label.style_label_down) // 底分型 if k2.high < k1.high and k2.high < k3.high and k2.low < k1.low and k2.low < k3.low new_fractal = FractalPoint.new(k2.bar_index, k2.low, "bottom") array.push(fractal_points, new_fractal) if show_fenxing label.new(k2.bar_index, k2.low, "底", color=color.green, style=label.style_label_up) // 构建笔 if show_bi and array.size(fractal_points) >= 2 for i = 0 to array.size(fractal_points) - 2 current_fractal = array.get(fractal_points, i) next_fractal = array.get(fractal_points, i + 1) if current_fractal.fractal_type != next_fractal.fractal_type line.new(current_fractal.bar_index, current_fractal.price, next_fractal.bar_index, next_fractal.price, color=color.blue, width=2) // 信息面板 if barstate.islast var info_table = table.new(position.top_right, 2, 4, bgcolor=color.white, border_width=1) table.cell(info_table, 0, 0, "缠论测试", text_color=color.black) table.cell(info_table, 1, 0, "", text_color=color.black) table.cell(info_table, 0, 1, "合并K线", text_color=color.black) table.cell(info_table, 1, 1, str.tostring(array.size(merged_ks)), text_color=color.black) table.cell(info_table, 0, 2, "分型数", text_color=color.black) table.cell(info_table, 1, 2, str.tostring(array.size(fractal_points)), text_color=color.black) table.cell(info_table, 0, 3, "当前趋势", text_color=color.black) trend_text = current_trend > 0 ? "向上" : current_trend < 0 ? "向下" : "震荡" table.cell(info_table, 1, 3, trend_text, text_color=color.black)