请求完善TradingView Engulfing蜡烛触碰MA的警报Pine Script代码
TradingView Pine Script 需求:吞没形态触碰均线的警报设置
问题说明
已完成SMMA均线绘制与Engulfing(吞没)蜡烛形态识别,但无法定义吞没蜡烛触碰均线的触发条件,需实现两类警报:
- 多单警报:价格处于200周期SMMA(smma3)上方,且看涨吞没形态触碰21周期SMMA(smma1)时触发
- 空单警报:价格处于200周期SMMA下方,且看跌吞没形态触碰21周期SMMA时触发
现有代码
//@version=5 indicator(title="Indicator", shorttitle="Indi", overlay=true, timeframe="", timeframe_gaps=true) len1 = input.int(21, minval=1, title="Length 21") len2 = input.int(50, minval=1, title="Length 50") len3 = input.int(200, minval=1, title="Length 200") src = input(close, title="Source") smma = 0.0 smma1 := na(smma[1]) ? ta.sma(src, len1) : (smma[1] * (len1 - 1) + src) / len1 smma2 := na(smma[1]) ? ta.sma(src, len2) : (smma[1] * (len2 - 1) + src) / len2 smma3 := na(smma[1]) ? ta.sma(src, len3) : (smma[1] * (len3 - 1) + src) / len3 openBarPrevious = open[1] closeBarPrevious = close[1] openBarCurrent = open closeBarCurrent = close green = color.green red = color.red // 看涨吞没形态判定 bullishEngulfing = (openBarCurrent <= closeBarPrevious) and (openBarCurrent < openBarPrevious) and (closeBarCurrent > openBarPrevious) // 看跌吞没形态判定 bearishEngulfing = (openBarCurrent >= closeBarPrevious) and (openBarCurrent > openBarPrevious) and (closeBarCurrent < openBarPrevious) // 绘制形态标记 plotshape(bullishEngulfing, style = shape.triangleup, location = location.belowbar, color = green, size = size.tiny) plotshape(bearishEngulfing, style = shape.triangledown , location = location.abovebar, color = red, size = size.tiny) // 绘制SMMA均线 plot(smma1, color=#ffffff) plot(smma2, color=#0000FF) plot(smma3, color=#FF0000)
实现方案(含修改后代码)
核心逻辑补充
- 吞没形态触碰smma1的条件:蜡烛的价格区间(最低价到最高价)覆盖smma1,即
low <= smma1 and high >= smma1 - 价格相对smma3的位置:多单要求当前收盘价在smma3上方,空单要求收盘价在smma3下方
- 警报触发逻辑:使用
alertcondition()函数绑定触发条件,支持在TradingView中设置对应警报
修改后完整代码
//@version=5 indicator(title="Indicator", shorttitle="Indi", overlay=true, timeframe="", timeframe_gaps=true) len1 = input.int(21, minval=1, title="Length 21") len2 = input.int(50, minval=1, title="Length 50") len3 = input.int(200, minval=1, title="Length 200") src = input(close, title="Source") // 计算SMMA均线 smma = 0.0 smma1 := na(smma[1]) ? ta.sma(src, len1) : (smma[1] * (len1 - 1) + src) / len1 smma2 := na(smma[1]) ? ta.sma(src, len2) : (smma[1] * (len2 - 1) + src) / len2 smma3 := na(smma[1]) ? ta.sma(src, len3) : (smma[1] * (len3 - 1) + src) / len3 // 吞没形态判定 bullishEngulfing = (open <= close[1]) and (open < open[1]) and (close > open[1]) bearishEngulfing = (open >= close[1]) and (open > open[1]) and (close < open[1]) // 补充:吞没形态触碰smma1的条件 bullishEngulfingTouchSMMA1 = bullishEngulfing and (low <= smma1) and (high >= smma1) bearishEngulfingTouchSMMA1 = bearishEngulfing and (low <= smma1) and (high >= smma1) // 补充:价格相对smma3的位置条件 priceAboveSMMA3 = close > smma3 priceBelowSMMA3 = close < smma3 // 最终警报触发条件 longAlertCondition = priceAboveSMMA3 and bullishEngulfingTouchSMMA1 shortAlertCondition = priceBelowSMMA3 and bearishEngulfingTouchSMMA1 // 绘制形态标记 plotshape(bullishEngulfing, style = shape.triangleup, location = location.belowbar, color = color.green, size = size.tiny) plotshape(bearishEngulfing, style = shape.triangledown , location = location.abovebar, color = color.red, size = size.tiny) // 绘制SMMA均线 plot(smma1, color=#ffffff) plot(smma2, color=#0000FF) plot(smma3, color=#FF0000) // 设置警报 alertcondition(longAlertCondition, title="多单警报", message="价格在200SMMA上方,看涨吞没触碰21SMMA") alertcondition(shortAlertCondition, title="空单警报", message="价格在200SMMA下方,看跌吞没触碰21SMMA")
额外说明
- 若需要更严格的“实体触碰”而非影线触碰,可将触碰条件改为:
- 看涨吞没:
open <= smma1 and close >= smma1 - 看跌吞没:
open >= smma1 and close <= smma1
- 看涨吞没:
- 代码简化了原变量命名(如用
open直接代替openBarCurrent),更符合Pine Script常规写法 - 添加的
alertcondition会在TradingView的警报设置中显示对应选项,可直接配置推送方式
内容的提问来源于stack exchange,提问作者Rachel Bush
相关产品推荐
相关产品推荐

