三K线覆盖形态检测及虚线框标注实现方法咨询
3K覆盖形态虚线框标注实现方案
核心实现思路
- 保留原有形态判定逻辑不变,触发点为3根K线的最右侧K线(当前K线)
- 调用Pine Script内置的
box.new()接口创建矩形,4个定位参数对应3根K线的时间范围和价格区间:- 左边界:当前K线索引减2,对应3根K线的最左侧K线位置
- 右边界:当前K线索引
- 上沿:3根K线的最高价最大值
- 下沿:3根K线的最低价最小值
- 配置矩形样式为虚线边框,可选择极低透明度填充或者完全透明填充避免遮挡K线
完整实现代码
//@version=5 indicator("3K覆盖形态虚线标注", overlay=true) // 原有形态判定规则 Pattern = high[2] > high[1] and low[2] < low[1] and high > high[1] and low < low[1] if Pattern // 计算矩形边界参数 left_bar = bar_index - 2 right_bar = bar_index top_val = math.max(high[2], high[1], high) bottom_val = math.min(low[2], low[1], low) // 存储历史矩形避免被自动清除 var array<box> box_list = array.new_box() // 创建虚线矩形 pattern_box = box.new( left=left_bar, top=top_val, right=right_bar, bottom=bottom_val, border_color=color.red, border_width=1, border_style=line.style_dashed, bgcolor=color.new(color.red, 95) // 填充透明度95%,几乎透明,设为na即可完全无填充 ) array.push(box_list, pattern_box) // 可选:保留原有小红圈标记用于逻辑校验 plotshape(Pattern, style=shape.circle, location=location.abovebar, size=size.tiny, color=color.red)
优化建议
- 若要避免未闭合K线导致的信号漂移,可在形态触发条件中增加
barstate.islastconfirmedhistory,即修改判定为Pattern and barstate.islastconfirmedhistory,仅在K线完全闭合后才生成标注 - 可根据需求调整边框颜色、宽度以及虚线样式,可选的虚线样式还有
line.style_dotted(点状虚线)
内容的提问来源于stack exchange,提问作者Interestor
相关产品推荐
相关产品推荐

