本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("RS Line with New-High Markers", shorttitle = "RS Line", overlay = false, precision = 4)
// ── Inputs ─────────────────────────────────────────────────────────────
string GRP_CMP = "Benchmark"
string cmpInput = input.symbol("", "Benchmark (blank = auto)", group = GRP_CMP,
tooltip = "Blank uses NSE:NIFTY for NSE/BSE symbols and SP:SPX for everything else.")
string GRP_HI = "New highs"
int hiDays = input.int(252, "New-high lookback (trading days)", minval = 20, group = GRP_HI,
tooltip = "252 ≈ one year. Converted to bars automatically on weekly and monthly charts.")
bool showHi = input.bool(true, "Mark RS new highs", group = GRP_HI)
bool showLead = input.bool(true, "Mark RS new high before price", group = GRP_HI,
tooltip = "The RS line makes a new high while price is still below its own high over the same lookback.")
string GRP_MA = "RS moving average"
bool showMa = input.bool(true, "Show RS moving average", group = GRP_MA)
int maLen = input.int(21, "Length (bars)", minval = 2, group = GRP_MA)
string GRP_TBL = "Summary table"
bool showTbl = input.bool(true, "Show RS change table", group = GRP_TBL)
// ── Helpers ────────────────────────────────────────────────────────────
// Converts a span of trading days into bars on the current chart; intraday charts use bars directly.
barsFor(int days) =>
timeframe.isweekly ? math.max(1, math.round(days / 5)) : timeframe.ismonthly ? math.max(1, math.round(days / 21)) : days
int hiLen = barsFor(hiDays)
// ── Relative strength ──────────────────────────────────────────────────
bool isIndia = syminfo.prefix == "NSE" or syminfo.prefix == "BSE"
string benchmark = cmpInput != "" ? cmpInput : isIndia ? "NSE:NIFTY" : "SP:SPX"
float benchClose = request.security(benchmark, timeframe.period, close, ignore_invalid_symbol = true)
float rs = benchClose > 0 ? close / benchClose * 100 : na
bool enoughHistory = bar_index >= hiLen
float rsHigh = ta.highest(rs, hiLen)
float priceHigh = ta.highest(high, hiLen)
bool rsNewHigh = enoughHistory and not na(rs) and rs >= rsHigh
bool rsLeadsPrice = rsNewHigh and high < priceHigh
float rsMa = ta.ema(rs, maLen)
rsChange(int days) =>
int n = barsFor(days)
rs[n] > 0 ? (rs / rs[n] - 1) * 100 : na
float rsChg1M = rsChange(21)
float rsChg3M = rsChange(63)
float rsChg6M = rsChange(126)
float rsChg12M = rsChange(252)
float rsFromHigh = rsHigh > 0 ? (rs / rsHigh - 1) * 100 : na
// ── Plots ──────────────────────────────────────────────────────────────
color rsColor = rs >= rsMa ? #2962FF : #787B86
plot(rs, "RS line", color = rsColor, linewidth = 2)
plot(showMa ? rsMa : na, "RS moving average", color = color.new(#FF9800, 20))
plotshape(showHi and rsNewHigh and not rsLeadsPrice ? rs : na, "RS new high", shape.circle, location.absolute, #2962FF, size = size.tiny)
plotshape(showLead and rsLeadsPrice ? rs : na, "RS new high before price", shape.circle, location.absolute, #00C853, size = size.small)
// Numeric outputs for Pine Screener columns and the Data Window.
plot(rsChg3M, "RS change 3M %", display = display.data_window)
plot(rsFromHigh, "% from RS high", display = display.data_window)
plot(rsNewHigh ? 1 : 0, "RS new high (1/0)", display = display.data_window)
plot(rsLeadsPrice ? 1 : 0, "RS new high before price (1/0)", display = display.data_window)
alertcondition(rsNewHigh, "RS new high", "{{ticker}}: RS line at a new high")
alertcondition(rsLeadsPrice, "RS new high before price", "{{ticker}}: RS line at a new high before price")
// ── Summary table ──────────────────────────────────────────────────────
var table tbl = table.new(position.top_right, 2, 6, bgcolor = color.new(color.gray, 85), border_width = 1)
cellPct(int row, string label, float v) =>
table.cell(tbl, 0, row, label, text_color = color.gray, text_size = size.small)
string txt = na(v) ? "–" : (v >= 0 ? "+" : "") + str.tostring(v, "0.0") + "%"
color clr = na(v) ? color.gray : v >= 0 ? color.teal : color.red
table.cell(tbl, 1, row, txt, text_color = clr, text_size = size.small)
if showTbl and barstate.islast
table.cell(tbl, 0, 0, "RS vs", text_color = color.gray, text_size = size.small)
table.cell(tbl, 1, 0, benchmark, text_color = color.gray, text_size = size.small)
cellPct(1, "1M", rsChg1M)
cellPct(2, "3M", rsChg3M)
cellPct(3, "6M", rsChg6M)
cellPct(4, "12M", rsChg12M)
cellPct(5, "From high", rsFromHigh)