PineScript实现2/3根K线形态圈选及旧标记隐藏技术问询
解决方案:多K线形态圈选与单K线标记隐藏
一、添加多K线形态的圈选标记(使用Box绘制)
用box.new()函数绘制覆盖整个形态K线范围的矩形框,能清晰标记出构成形态的所有K线。针对你现有2-candle形态的实现逻辑如下:
- 检测到形态时,创建覆盖所有参与K线的矩形框
- 框的左右边界为形态的起始和结束K线索引,上下边界为形态内所有K线的最高/最低价
二、隐藏被多K线形态包含的单K线标记
通过维护存储形态涉及K线索引的数组,在绘制单K线标记时判断当前K线是否属于任何多K线形态,若属于则不显示标记:
- 用
var数组持久化存储所有被多K线形态包含的K线索引 - 绘制单K线标记时,添加条件:仅当当前K线索引不在形态数组中时才显示
修改后的完整代码
//@version=5 indicator("K线形态检测", overlay=true) // 辅助函数:判断红/绿K线 red(o, c) => o > c green(o, c) => o < c // 全局开关(可按需调整) StrictEngulf = input.bool(true, "严格 engulfing 条件") StrictInsideCandle = input.bool(true, "严格 Inside Candle 条件") // 存储形态涉及的K线索引 var int[] pattern_bars = array.new_int() // ------------------------------ // 2根K线形态检测 // ------------------------------ // Engulfing Pattern bullish_engulfing(op, cp, hp, lp, o, c, h, l) => red(op, cp) and green(o, c) and (StrictEngulf ? (c > hp and o < lp) : (h > hp and l < lp)) bearish_engulfing(op, cp, hp, lp, o, c, h, l) => green(op, cp) and red(o, c) and (StrictEngulf ? (o > hp and c < lp) : (h > hp and l < lp)) bull_engulf = bullish_engulfing(open[1], close[1], high[1], low[1], open, close, high, low) bear_engulf = bearish_engulfing(open[1], close[1], high[1], low[1], open, close, high, low) // Inside Candle (Harami) Pattern bullish_inside_candle(op, cp, hp, lp, o, c, h, l) => red(op, cp) and green(o, c) and (StrictInsideCandle ? (op > h and cp < l) : (hp > h and lp < l)) bearish_inside_candle(op, cp, hp, lp, o, c, h, l) => green(op, cp) and red(o, c) and (StrictInsideCandle ? (cp > h and op < l) : (hp > h and lp < l)) bull_inside = bullish_inside_candle(open[1], close[1], high[1], low[1], open, close, high, low) bear_inside = bearish_inside_candle(open[1], close[1], high[1], low[1], open, close, high, low) // ------------------------------ // 多K线形态圈选(Box绘制) // ------------------------------ // 绘制Bullish Engulfing框 if bull_engulf box.new(bar_index[1], max(high[1], high), bar_index, min(low[1], low), border_color=color.green, border_width=2, fill_color=color.new(color.green, 90), title="Bull Engulf Box") // 将涉及的K线加入形态数组 array.push(pattern_bars, bar_index[1]) array.push(pattern_bars, bar_index) // 绘制Bearish Engulfing框 if bear_engulf box.new(bar_index[1], max(high[1], high), bar_index, min(low[1], low), border_color=color.red, border_width=2, fill_color=color.new(color.red, 90), title="Bear Engulf Box") array.push(pattern_bars, bar_index[1]) array.push(pattern_bars, bar_index) // 绘制Bullish Inside Candle框 if bull_inside box.new(bar_index[1], max(high[1], high), bar_index, min(low[1], low), border_color=color.green, border_width=2, fill_color=color.new(color.green, 90), title="Bull Inside Box") array.push(pattern_bars, bar_index[1]) array.push(pattern_bars, bar_index) // 绘制Bearish Inside Candle框 if bear_inside box.new(bar_index[1], max(high[1], high), bar_index, min(low[1], low), border_color=color.red, border_width=2, fill_color=color.new(color.red, 90), title="Bear Inside Box") array.push(pattern_bars, bar_index[1]) array.push(pattern_bars, bar_index) // ------------------------------ // 单K线标记示例(以十字星为例) // ------------------------------ // 十字星判断条件 is_doji = (abs(open - close) / (high - low)) < 0.1 and (high - low) > 0 // 判断当前K线是否属于任何多K线形态 is_in_pattern = array.includes(pattern_bars, bar_index) // 仅当十字星不在任何多K线形态中时显示标记 plotshape(is_doji and not is_in_pattern, title="Doji", text="Doji", location=location.belowbar, style=shape.labelup, color=color.blue, textcolor=color.white, size=size.small) // ------------------------------ // 原有形态文本标记(可选保留) // ------------------------------ plotshape(bull_engulf, title='Bull-Engulf', text='Bull-Engulf', location=location.belowbar, style=shape.triangleup, color=color.green, textcolor=color.green) plotshape(bear_engulf, title='Bear-Engulf', text='Bear-Engulf', location=location.abovebar, style=shape.triangledown, color=color.red, textcolor=color.red) plotshape(bull_inside, title='Bull-IC', text='Bull-IC', location=location.belowbar, style=shape.triangleup, color=color.green, textcolor=color.green) plotshape(bear_inside, title='Bear-IC', text='Bear-IC', location=location.abovebar, style=shape.triangledown, color=color.red, textcolor=color.red)
扩展到3根K线形态(如晨星/夜星)
以晨星形态为例,添加以下代码即可实现圈选和标记隐藏:
// 晨星形态检测 morning_star = red(open[2], close[2]) and is_doji[1] and green(open, close) and close > open[2] if morning_star // 绘制覆盖3根K线的框 box.new(bar_index[2], max(max(high[2], high[1]), high), bar_index, min(min(low[2], low[1]), low), border_color=color.green, border_width=2, fill_color=color.new(color.green, 90), title="Morning Star Box") // 将3根K线加入形态数组 array.push(pattern_bars, bar_index[2]) array.push(pattern_bars, bar_index[1]) array.push(pattern_bars, bar_index)
内容的提问来源于stack exchange,提问作者Pradip
相关产品推荐
相关产品推荐

