本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("4H SMA20/SMA50 Cross Proximity", overlay=true)
// Settings
tf = input.timeframe("240", "Timeframe")
maxGap = input.float(1.0, "Near-cross threshold (%)", minval=0.01, step=0.05)
showLabel = input.bool(true, "Show distance label")
// Calculate on selected timeframe
[sma20, sma50, gap, prevGap, crossUp, crossDown] = request.security(
syminfo.tickerid, tf,
[
ta.sma(close, 20),
ta.sma(close, 50),
math.abs(ta.sma(close, 20) - ta.sma(close, 50)) /
ta.sma(close, 50) * 100,
math.abs(ta.sma(close, 20)[1] - ta.sma(close, 50)[1]) /
ta.sma(close, 50)[1] * 100,
ta.crossover(ta.sma(close, 20), ta.sma(close, 50)),
ta.crossunder(ta.sma(close, 20), ta.sma(close, 50))
],
lookahead=barmerge.lookahead_off
)
// Direction and proximity
bullish = sma20 < sma50
bearish = sma20 > sma50
closing = gap < prevGap
nearCross = gap <= maxGap and closing
// Plot moving averages
plot(sma20, "4H SMA20", color=color.lime, linewidth=2)
plot(sma50, "4H SMA50", color=color.orange, linewidth=2)
// Highlight approaching crosses
plotshape(nearCross and bullish, title="Bullish Near Cross",
style=shape.labelup, location=location.belowbar,
color=color.green, text="BULL", textcolor=color.white)
plotshape(nearCross and bearish, title="Bearish Near Cross",
style=shape.labeldown, location=location.abovebar,
color=color.red, text="BEAR", textcolor=color.white)
// Live distance label on the latest bar
var label gapLabel = na
if barstate.islast and showLabel and not na(gap)
label.delete(gapLabel)
direction = bullish ? "Bullish: SMA20 below SMA50" :
bearish ? "Bearish: SMA20 above SMA50" : "SMAs equal"
status = gap <= maxGap and closing ? "APPROACHING" : "NOT YET CLOSE"
gapLabel := label.new(
bar_index, high,
direction +
"\nGap: " + str.tostring(gap, "#.###") + "%" +
"\nStatus: " + status,
style=label.style_label_down,
color=nearCross ? color.green : color.gray,
textcolor=color.white
)
// Alerts
alertcondition(nearCross and bullish, "Bullish Near Cross",
"SMA20 is approaching SMA50 from below.")
alertcondition(nearCross and bearish, "Bearish Near Cross",
"SMA20 is approaching SMA50 from above.")
alertcondition(crossUp, "Bullish Crossover",
"SMA20 crossed above SMA50.")
alertcondition(crossDown, "Bearish Crossover",
"SMA20 crossed below SMA50.")