191 lines
7.8 KiB
Plaintext
191 lines
7.8 KiB
Plaintext
//@version=6
|
|
indicator("缠论精简版", shorttitle="缠论", overlay=true, max_lines_count=100, max_labels_count=100)
|
|
|
|
// ================== 输入参数 ==================
|
|
show_fractal = input.bool(true, "显示分型", group="显示")
|
|
show_stroke = input.bool(true, "显示笔", group="显示")
|
|
show_hub = input.bool(true, "显示中枢", group="显示")
|
|
show_signals = input.bool(true, "显示买卖点", group="显示")
|
|
|
|
fractal_length = input.int(3, "分型周期", minval=3, maxval=10, group="参数")
|
|
stroke_color = input.color(color.blue, "笔颜色", group="颜色")
|
|
hub_color = input.color(color.gray, "中枢颜色", group="颜色")
|
|
|
|
// ================== 分型识别 ==================
|
|
// 顶分型
|
|
top_fractal = ta.pivothigh(high, fractal_length, fractal_length)
|
|
// 底分型
|
|
bottom_fractal = ta.pivotlow(low, fractal_length, fractal_length)
|
|
|
|
// ================== 存储分型数据 ==================
|
|
var fractal_highs = array.new<float>()
|
|
var fractal_lows = array.new<float>()
|
|
var fractal_high_bars = array.new<int>()
|
|
var fractal_low_bars = array.new<int>()
|
|
|
|
// 记录新的分型
|
|
if not na(top_fractal)
|
|
array.push(fractal_highs, top_fractal)
|
|
array.push(fractal_high_bars, bar_index - fractal_length)
|
|
|
|
// 保持数组大小
|
|
if array.size(fractal_highs) > 50
|
|
array.shift(fractal_highs)
|
|
array.shift(fractal_high_bars)
|
|
|
|
if not na(bottom_fractal)
|
|
array.push(fractal_lows, bottom_fractal)
|
|
array.push(fractal_low_bars, bar_index - fractal_length)
|
|
|
|
// 保持数组大小
|
|
if array.size(fractal_lows) > 50
|
|
array.shift(fractal_lows)
|
|
array.shift(fractal_low_bars)
|
|
|
|
// ================== 显示分型 ==================
|
|
if show_fractal
|
|
// 顶分型标记
|
|
if not na(top_fractal)
|
|
label.new(bar_index - fractal_length, top_fractal, "T",
|
|
color=color.red, style=label.style_label_down, size=size.tiny)
|
|
|
|
// 底分型标记
|
|
if not na(bottom_fractal)
|
|
label.new(bar_index - fractal_length, bottom_fractal, "B",
|
|
color=color.green, style=label.style_label_up, size=size.tiny)
|
|
|
|
// ================== 笔的构建 ==================
|
|
var strokes = array.new<line>()
|
|
|
|
// 构建笔
|
|
build_strokes() =>
|
|
if array.size(fractal_highs) >= 1 and array.size(fractal_lows) >= 1
|
|
high_size = array.size(fractal_highs)
|
|
low_size = array.size(fractal_lows)
|
|
|
|
last_high = array.get(fractal_highs, high_size - 1)
|
|
last_low = array.get(fractal_lows, low_size - 1)
|
|
last_high_bar = array.get(fractal_high_bars, high_size - 1)
|
|
last_low_bar = array.get(fractal_low_bars, low_size - 1)
|
|
|
|
// 根据最后的分型连接笔
|
|
if last_high_bar > last_low_bar
|
|
// 最后是顶分型,连接到前一个底分型
|
|
if low_size >= 2
|
|
prev_low = array.get(fractal_lows, low_size - 2)
|
|
prev_low_bar = array.get(fractal_low_bars, low_size - 2)
|
|
|
|
if show_stroke
|
|
new_line = line.new(prev_low_bar, prev_low, last_high_bar, last_high,
|
|
color=stroke_color, width=2)
|
|
array.push(strokes, new_line)
|
|
else
|
|
// 最后是底分型,连接到前一个顶分型
|
|
if high_size >= 2
|
|
prev_high = array.get(fractal_highs, high_size - 2)
|
|
prev_high_bar = array.get(fractal_high_bars, high_size - 2)
|
|
|
|
if show_stroke
|
|
new_line = line.new(prev_high_bar, prev_high, last_low_bar, last_low,
|
|
color=stroke_color, width=2)
|
|
array.push(strokes, new_line)
|
|
|
|
// 执行笔构建
|
|
if not na(top_fractal) or not na(bottom_fractal)
|
|
build_strokes()
|
|
|
|
// ================== 中枢识别 ==================
|
|
var hubs = array.new<box>()
|
|
|
|
identify_hubs() =>
|
|
if array.size(fractal_highs) >= 3 and array.size(fractal_lows) >= 3
|
|
// 简化的中枢识别:连续三个分型的重叠区域
|
|
high_size = array.size(fractal_highs)
|
|
low_size = array.size(fractal_lows)
|
|
|
|
if high_size >= 2 and low_size >= 2
|
|
h1 = array.get(fractal_highs, high_size - 2)
|
|
h2 = array.get(fractal_highs, high_size - 1)
|
|
l1 = array.get(fractal_lows, low_size - 2)
|
|
l2 = array.get(fractal_lows, low_size - 1)
|
|
|
|
hub_high = math.min(h1, h2)
|
|
hub_low = math.max(l1, l2)
|
|
|
|
if hub_high > hub_low
|
|
start_bar = math.min(array.get(fractal_high_bars, high_size - 2),
|
|
array.get(fractal_low_bars, low_size - 2))
|
|
end_bar = math.max(array.get(fractal_high_bars, high_size - 1),
|
|
array.get(fractal_low_bars, low_size - 1))
|
|
|
|
if show_hub
|
|
hub_box = box.new(start_bar, hub_high, end_bar, hub_low,
|
|
border_color=hub_color, bgcolor=color.new(hub_color, 85))
|
|
array.push(hubs, hub_box)
|
|
|
|
// 执行中枢识别
|
|
if barstate.islast and (not na(top_fractal) or not na(bottom_fractal))
|
|
identify_hubs()
|
|
|
|
// ================== 买卖点信号 ==================
|
|
var buy_point = false
|
|
var sell_point = false
|
|
|
|
// 简化的买卖点逻辑
|
|
if array.size(fractal_lows) >= 2 and array.size(hubs) >= 1
|
|
current_low = array.get(fractal_lows, array.size(fractal_lows) - 1)
|
|
|
|
if array.size(hubs) > 0
|
|
latest_hub = array.get(hubs, array.size(hubs) - 1)
|
|
hub_low = box.get_bottom(latest_hub)
|
|
|
|
// 跌破中枢下沿的买点
|
|
if current_low < hub_low
|
|
buy_point := true
|
|
|
|
if array.size(fractal_highs) >= 2 and array.size(hubs) >= 1
|
|
current_high = array.get(fractal_highs, array.size(fractal_highs) - 1)
|
|
|
|
if array.size(hubs) > 0
|
|
latest_hub = array.get(hubs, array.size(hubs) - 1)
|
|
hub_high = box.get_top(latest_hub)
|
|
|
|
// 突破中枢上沿的卖点
|
|
if current_high > hub_high
|
|
sell_point := true
|
|
|
|
// 显示买卖点
|
|
if show_signals
|
|
if buy_point and not na(bottom_fractal)
|
|
label.new(bar_index - fractal_length, bottom_fractal, "买",
|
|
color=color.green, style=label.style_label_up, size=size.normal)
|
|
|
|
if sell_point and not na(top_fractal)
|
|
label.new(bar_index - fractal_length, top_fractal, "卖",
|
|
color=color.red, style=label.style_label_down, size=size.normal)
|
|
|
|
// ================== 清理旧对象 ==================
|
|
// 限制绘图对象数量,避免超出限制
|
|
if array.size(strokes) > 30
|
|
old_line = array.shift(strokes)
|
|
line.delete(old_line)
|
|
|
|
if array.size(hubs) > 10
|
|
old_hub = array.shift(hubs)
|
|
box.delete(old_hub)
|
|
|
|
// ================== 警报 ==================
|
|
alertcondition(buy_point and not na(bottom_fractal), title="缠论买点", message="发现买入信号")
|
|
alertcondition(sell_point and not na(top_fractal), title="缠论卖点", message="发现卖出信号")
|
|
|
|
// ================== 状态显示 ==================
|
|
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_highs)), 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_lows)), text_color=color.black)
|
|
table.cell(info_table, 0, 3, "中枢数", text_color=color.black)
|
|
table.cell(info_table, 1, 3, str.tostring(array.size(hubs)), text_color=color.black) |