Stochastic指标双线触碰警报异常:现有代码无警报输出求助
解决Stochastic指标%K与%D触碰时警报不触发的问题
你写的警报条件k == d几乎不会触发,原因很简单:Pine Script里的%K和%D都是带小数的浮点数,哪怕视觉上两条线看起来交叉了,实际数值也大概率存在微小差异,精确相等的情况极少出现。
要正确检测两条线的触碰(交叉),得用Pine Script内置的交叉检测函数:ta.crossover()(检测%K上穿%D,金叉)和ta.crossunder()(检测%K下穿%D,死叉)。
修改后的完整代码如下:
//@version=5 indicator(title="Stochastic", shorttitle="Stoch", format=format.price, precision=2, timeframe="", timeframe_gaps=true) periodK = input.int(14, title="%K Length", minval=1) smoothK = input.int(1, title="%K Smoothing", minval=1) periodD = input.int(3, title="%D Smoothing", minval=1) k = ta.sma(ta.stoch(close, high, low, periodK), smoothK) d = ta.sma(k, periodD) plot(k, title="%K", color=#2962FF) plot(d, title="%D", color=#FF6D00) // 修改后的警报条件:检测金叉或死叉 alertcondition(ta.crossover(k, d) or ta.crossunder(k, d), title="%K与%D交叉", message="%K与%D发生交叉") h0 = hline(80, "Upper Band", color=#787B86) hline(50, "Middle Band", color=color.new(#787B86, 50)) h1 = hline(20, "Lower Band", color=#787B86) fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")
关键修改说明
- 用
ta.crossover(k, d)检测%K从下方穿过%D的金叉信号 - 用
ta.crossunder(k, d)检测%K从上方穿过%D的死叉信号 - 用
or同时触发两种交叉情况的警报,如果你只想触发其中一种,删掉对应的条件即可
内容的提问来源于stack exchange,提问作者developer
相关产品推荐
相关产品推荐

