TradingView策略:仅在做空箭头出现时触发警报的问题求助
问题排查与解决方案
你的问题出在:当前警报代码会在每一根满足做空条件的K线上触发,而非仅在做空箭头首次生成(信号从无到有)的那一刻触发。
修改后的代码
strategy("Momentum Strategy", overlay=true) length = input(12) price = close momentum(seria, length) => mom = seria - seria[length] mom mom0 = momentum(price, length) mom1 = momentum( mom0, 1) // 定义多空条件变量 longCondition = mom0 > 0 and mom1 > 0 shortCondition = mom0 < 0 and mom1 < 0 // 多头逻辑 if (longCondition) strategy.entry("MomLE", strategy.long, stop=high+syminfo.mintick, comment="MomLE") else strategy.cancel("MomLE") // 空头逻辑 + 仅首次触发警报 if (shortCondition) strategy.entry("MomSE", strategy.short, stop=low-syminfo.mintick, comment="MomSE") // 仅当当前满足做空条件,且上一根K线不满足时,触发警报 if (shortCondition and not shortCondition[1]) alert("Check chart", alert.freq_once_per_bar_close) else strategy.cancel("MomSE")
关键修改说明
- 把多空条件提取为单独变量
longCondition和shortCondition,让逻辑更清晰。 - 新增判断
shortCondition and not shortCondition[1]:只有当当前K线刚满足做空条件,而上一根K线不满足时,才触发警报,确保警报仅在做空箭头生成的那一刻触发一次。 - 保留了原有的策略入场/取消逻辑,只调整了警报触发的时机。
内容的提问来源于stack exchange,提问作者Milan
相关产品推荐
相关产品推荐

