本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Daily RSI Buy Score", shorttitle = "RSI Buy Score", overlay = false)
// ───────── Inputs ─────────
string grpA = "Analysis"
string tf = input.timeframe("D", "Analysis timeframe", group = grpA)
int rsiLen = input.int(14, "RSI length", minval = 2, group = grpA)
int fastLen = input.int(50, "Fast EMA length", minval = 1, group = grpA)
int slowLen = input.int(200, "Slow EMA length", minval = 1, group = grpA)
int slopeBars = input.int(10, "Slow EMA slope lookback", minval = 1, group = grpA, tooltip = "Slow EMA counts as rising if it is higher than it was this many bars ago.")
bool useFallback = input.bool(true, "Fall back to shorter EMAs on new tickers", group = grpA)
int fastFb = input.int(20, "Fast EMA fallback length", minval = 1, group = grpA)
int slowFb = input.int(100, "Slow EMA fallback length", minval = 1, group = grpA)
string grpR = "RSI zones"
int rsiOS = input.int(30, "Oversold", minval = 0, maxval = 100, group = grpR)
int rsiOB = input.int(70, "Overbought", minval = 0, maxval = 100, group = grpR)
float rsiBuy = input.float(45, "Buy zone (RSI below)", minval = 0, maxval = 100, step = 1, group = grpR)
string grpD = "EMA proximity"
float nearFast = input.float(3.0, "'Near' fast EMA within ±%", minval = 0, maxval = 10, step = 0.5, group = grpD)
float nearSlow = input.float(5.0, "'Near' slow EMA within ±%", minval = 0, maxval = 10, step = 0.5, group = grpD)
float maxFast = input.float(10.0, "Fast EMA distance for 0 score %", step = 0.5, group = grpD)
float maxSlow = input.float(20.0, "Slow EMA distance for 0 score %", step = 0.5, group = grpD)
string grpW = "Score weights"
float wRsi = input.float(35, "RSI", minval = 0, group = grpW)
float wTrend = input.float(25, "Trend", minval = 0, group = grpW)
float wFast = input.float(25, "Proximity to fast EMA", minval = 0, group = grpW)
float wSlow = input.float(15, "Proximity to slow EMA", minval = 0, group = grpW)
string grpV = "Display"
bool showEmas = input.bool(true, "Plot EMAs on price chart", group = grpV)
string tablePos = input.string("Top Right", "Table position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = grpV)
string txtSizeIn = input.string("Small", "Table text size", options = ["Tiny", "Small", "Normal"], group = grpV)
float alertLevel = input.float(75, "Alert when score crosses above", group = grpV)
// ───────── Daily data (with fallback) ─────────
f_data() =>
float slowFull = ta.ema(close, slowLen)
float fastFull = ta.ema(close, fastLen)
float slowAlt = ta.ema(close, slowFb)
float fastAlt = ta.ema(close, fastFb)
bool slowOk = not na(slowFull)
bool fastOk = not na(fastFull)
float eSlow = slowOk or not useFallback ? slowFull : slowAlt
float eFast = fastOk or not useFallback ? fastFull : fastAlt
[ta.rsi(close, rsiLen), eFast, eSlow, eSlow[slopeBars], fastOk, slowOk]
[dRsi, dFast, dSlow, dSlowPrev, fastOk, slowOk] = request.security(syminfo.tickerid, tf, f_data())
bool hasFast = not na(dFast)
bool hasSlow = not na(dSlow)
bool hasTrend = hasFast and hasSlow
bool fastFallbackActive = useFallback and not fastOk and hasFast
bool slowFallbackActive = useFallback and not slowOk and hasSlow
// ───────── Conditions ─────────
bool upTrend = hasTrend and dFast > dSlow
bool aboveSlow = hasSlow and close > dSlow
bool slowRising = hasSlow and not na(dSlowPrev) and dSlow > dSlowPrev
float distFast = hasFast ? (close - dFast) / dFast * 100 : na
float distSlow = hasSlow ? (close - dSlow) / dSlow * 100 : na
// ───────── Score (0–100), missing parts excluded ─────────
proxScore(float d, float maxD) =>
math.max(0.0, 100 - math.abs(d) / maxD * 100)
float rsiScore = na(dRsi) ? na : math.max(0.0, math.min(100.0, (rsiOB - dRsi) / (rsiOB - rsiOS) * 100))
float trendScore = hasTrend ? (upTrend ? 50 : 0) + (aboveSlow ? 25 : 0) + (slowRising ? 25 : 0) : na
float fastScore = hasFast ? proxScore(distFast, maxFast) : na
float slowScore = hasSlow ? proxScore(distSlow, maxSlow) : na
float wR = na(rsiScore) ? 0 : wRsi
float wT = na(trendScore) ? 0 : wTrend
float wF = na(fastScore) ? 0 : wFast
float wS = na(slowScore) ? 0 : wSlow
float wSum = wR + wT + wF + wS
float score = wSum > 0 ? (nz(rsiScore) * wR + nz(trendScore) * wT + nz(fastScore) * wF + nz(slowScore) * wS) / wSum : na
// ───────── RSI pane ─────────
color rsiColor = dRsi >= rsiOB ? color.lime : dRsi <= rsiOS ? color.red : color.purple
plot(dRsi, "Daily RSI", rsiColor, 2)
hOB = hline(rsiOB, "Overbought", color.new(color.green, 40), hline.style_dashed)
hline(50, "Midline", color.new(color.gray, 60), hline.style_dotted)
hOS = hline(rsiOS, "Oversold", color.new(color.red, 40), hline.style_dashed)
fill(hOB, hOS, color.new(color.purple, 93))
bgcolor(dRsi >= rsiOB ? color.new(color.lime, 85) : dRsi <= rsiOS ? color.new(color.red, 85) : na)
// ───────── EMAs on price chart ─────────
plot(showEmas ? dFast : na, "Daily EMA fast", color.orange, 2, force_overlay = true)
plot(showEmas ? dSlow : na, "Daily EMA slow", color.blue, 2, force_overlay = true)
// ───────── Checklist table on price chart ─────────
pos = switch tablePos
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
txtSize = switch txtSizeIn
"Tiny" => size.tiny
"Normal" => size.normal
=> size.small
var table t = table.new(pos, 3, 8, bgcolor = color.new(color.black, 15), frame_color = color.gray, frame_width = 1, border_color = color.new(color.gray, 80), border_width = 1, force_overlay = true)
check(bool ok, bool avail) =>
avail ? (ok ? "✅" : "❌") : "➖"
fmtPct(float v) =>
na(v) ? "n/a" : str.tostring(v, "+0.0;-0.0") + "%"
addRow(int r, string lbl, string val, bool ok, bool avail) =>
table.cell(t, 0, r, lbl, text_color = color.white, text_halign = text.align_left, text_size = txtSize)
table.cell(t, 1, r, val, text_color = color.silver, text_halign = text.align_right, text_size = txtSize)
table.cell(t, 2, r, check(ok, avail), text_size = txtSize)
string fTxt = "EMA" + str.tostring(fastFallbackActive ? fastFb : fastLen) + (fastFallbackActive ? "*" : "")
string sTxt = "EMA" + str.tostring(slowFallbackActive ? slowFb : slowLen) + (slowFallbackActive ? "*" : "")
if barstate.isfirst
table.merge_cells(t, 0, 0, 2, 0)
if barstate.islast
string note = not hasTrend ? "Not enough daily history – missing checks excluded from score" : fastFallbackActive or slowFallbackActive ? "* Short history – using fallback EMAs" : ""
table.cell(t, 0, 0, "Buy Checklist · " + tf, text_color = color.white, text_size = txtSize, bgcolor = color.new(color.gray, 60), tooltip = note)
addRow(1, "Uptrend", "", upTrend, hasTrend)
addRow(2, "Above " + sTxt, "", aboveSlow, hasSlow)
addRow(3, sTxt + " Uptrend", "", slowRising, hasSlow and not na(dSlowPrev))
addRow(4, "RSI (<" + str.tostring(rsiBuy) + ")", na(dRsi) ? "n/a" : str.tostring(dRsi, "0.0"), dRsi < rsiBuy, not na(dRsi))
addRow(5, fTxt + " (±" + str.tostring(nearFast) + "%)", fmtPct(distFast), math.abs(distFast) <= nearFast, hasFast)
addRow(6, sTxt + " (±" + str.tostring(nearSlow) + "%)", fmtPct(distSlow), math.abs(distSlow) <= nearSlow, hasSlow)
string verdict = na(score) ? "N/A" : score >= 75 ? "STRONG" : score >= 55 ? "GOOD" : score >= 40 ? "NEUTRAL" : "WAIT"
color vColor = na(score) ? color.gray : score >= 75 ? color.rgb(0, 200, 83) : score >= 55 ? color.rgb(174, 234, 0) : score >= 40 ? color.rgb(255, 171, 0) : color.rgb(255, 82, 82)
table.cell(t, 0, 7, "Score", text_color = color.white, text_halign = text.align_left, text_size = txtSize)
table.cell(t, 1, 7, na(score) ? "–" : str.tostring(score, "0") + "%", text_color = vColor, text_halign = text.align_right, text_size = txtSize)
table.cell(t, 2, 7, verdict, text_color = color.black, bgcolor = vColor, text_size = txtSize)
// ───────── Alert ─────────
alertcondition(ta.crossover(score, alertLevel), "Buy score high", "Buy score crossed above threshold on {{ticker}}")