89 lines
3.9 KiB
Plaintext
89 lines
3.9 KiB
Plaintext
//@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="显示设置")
|
|
min_bars_between = input.int(3, "分型最小间隔", minval=1, maxval=20, group="缠论参数")
|
|
|
|
// ================== 数据类型定义 ==================
|
|
type FractalPoint
|
|
int bar_index
|
|
float price
|
|
string fractal_type
|
|
|
|
// ================== 全局变量 ==================
|
|
var fractal_points = array.new<FractalPoint>()
|
|
var bi_lines = array.new<line>()
|
|
|
|
// ================== 简单分型识别 ==================
|
|
// 基于原始K线的简单分型
|
|
is_top_fractal = high[1] > high[0] and high[1] > high[2]
|
|
is_bottom_fractal = low[1] < low[0] and low[1] < low[2]
|
|
|
|
// ================== 距离过滤 ==================
|
|
distance_ok(new_bar) =>
|
|
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
|
|
|
|
// ================== 添加分型 ==================
|
|
if is_top_fractal and distance_ok(bar_index[1])
|
|
new_fractal = FractalPoint.new(bar_index[1], high[1], "top")
|
|
array.push(fractal_points, new_fractal)
|
|
|
|
if array.size(fractal_points) > 50
|
|
array.shift(fractal_points)
|
|
|
|
if is_bottom_fractal and distance_ok(bar_index[1])
|
|
new_fractal = FractalPoint.new(bar_index[1], low[1], "bottom")
|
|
array.push(fractal_points, new_fractal)
|
|
|
|
if array.size(fractal_points) > 50
|
|
array.shift(fractal_points)
|
|
|
|
// ================== 显示分型 ==================
|
|
if show_fenxing
|
|
if is_top_fractal and distance_ok(bar_index[1])
|
|
label.new(bar_index[1], high[1], "顶", color=color.red, style=label.style_label_down, size=size.small)
|
|
|
|
if is_bottom_fractal and distance_ok(bar_index[1])
|
|
label.new(bar_index[1], low[1], "底", color=color.green, style=label.style_label_up, size=size.small)
|
|
|
|
// ================== 每根K线重新构建所有笔 ==================
|
|
if barstate.isconfirmed
|
|
// 清理旧笔
|
|
if array.size(bi_lines) > 0
|
|
for i = 0 to array.size(bi_lines) - 1
|
|
line.delete(array.get(bi_lines, i))
|
|
array.clear(bi_lines)
|
|
|
|
// 重新构建所有笔
|
|
if array.size(fractal_points) >= 2 and show_bi
|
|
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
|
|
// 检查距离限制
|
|
distance = bar_index - current_fractal.bar_index
|
|
if distance <= 500
|
|
new_line = line.new(current_fractal.bar_index, current_fractal.price, next_fractal.bar_index, next_fractal.price, color=color.blue, width=2, style=line.style_solid)
|
|
array.push(bi_lines, new_line)
|
|
|
|
// ================== 信息面板 ==================
|
|
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, 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, str.tostring(min_bars_between), text_color=color.black) |