如何仅在至少5根前K线未触碰EMA时标记K线?
修改后的Pine Script代码
//@version=5 indicator("My script", overlay=true) ema_val = ta.ema(close, 9) // 判断当前K线是否触碰EMA ema_touch_current = (ema_val <= high) and (ema_val >= low) // 检查过去5根K线是否均未触碰EMA no_touch_in_last_5 = ta.sum(ema_touch_current[1], 5) == 0 // 最终标记条件:当前触碰EMA + 过去5根都没触碰 mark_condition = ema_touch_current and no_touch_in_last_5 plot(ema_val) plotshape(mark_condition, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
关键逻辑说明
ema_touch_current:保留原逻辑,判断当前K线的高低价区间是否覆盖EMA值,确认当前K线触碰EMAno_touch_in_last_5:用ta.sum(ema_touch_current[1], 5)统计前1至前5根K线的触碰次数,结果为0则代表这5根K线均未触碰EMAmark_condition:将两个条件结合,仅当同时满足时才显示标记
内容的提问来源于stack exchange,提问作者Kai Jun Seah
相关产品推荐
相关产品推荐

