如何在TradingView随机震荡指标上添加超买超卖信号标记?
修改后的随机震荡指标(带信号标记)
以下是添加了信号标记的完整Pine Script代码,满足需求:当%D线下穿80时显示红色圆圈,上穿20时显示绿色圆圈:
//@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) // 信号条件判断 sell_signal = ta.crossunder(d, 80) buy_signal = ta.crossover(d, 20) plot(k, title="%K", color=#2962FF) plot(d, title="%D", color=#FF6D00) 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") // 绘制信号标记 plotshape(sell_signal, title="Sell Signal", location=location.belowbar, color=color.red, style=shape.circle, size=size.small) plotshape(buy_signal, title="Buy Signal", location=location.abovebar, color=color.green, style=shape.circle, size=size.small)
关键修改说明:
- 新增
sell_signal变量,通过ta.crossunder(d, 80)检测%D线从80上方下穿至下方的动作 - 新增
buy_signal变量,通过ta.crossover(d, 20)检测%D线从20下方上穿至上方的动作 - 使用
plotshape()函数绘制圆形标记:- 卖出信号(下穿80)在K线下方显示红色小圆圈
- 买入信号(上穿20)在K线上方显示绿色小圆圈
内容的提问来源于stack exchange,提问作者Krishnendu Bhattacharyya
相关产品推荐
相关产品推荐

