本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Original work: © bharatTrader
// Modifications: Renamed and extended few features by The Noiseless Trader
// Added: log-normalized RS option
//@version=6
indicator('RS Alpha by The Noiseless Trader', shorttitle='RS Alpha by The Noiseless Trader', overlay=false)
// ── Inputs ─────────────────────────────────────────────────────────────────────
source = input(title='Source', defval=close)
comparativeTickerId = input.symbol('NSE:NIFTY', title='Comparative Symbol')
length = input.int(60, minval=1, title='Period')
showZeroLine = input.bool(true, title='Show Zero Line')
showRefDateLbl = input.bool(true, title='Show Reference Label')
toggleRSColor = input.bool(true, title='Toggle RS color on crossovers')
// Extras
useLogNorm = input.bool(false, title='Use Log-Normalized RS')
// RS Trend
showRSTrend = input.bool(false, title='RS Trend,', group='RS Trend', inline='RS Trend')
base = input.int(title='Range', minval=1, defval=5, group='RS Trend', inline='RS Trend')
// RS Mean
showMA = input.bool(false, title='', group='RS Mean', inline='RS Mean')
lengthRSMA = input.int(60, minval=1, title='Period', group='RS Mean', inline='RS Mean')
showMAColor = input.bool(true, title='Trend Color', group='RS Mean', inline='RS Mean')
// ── Set up ─────────────────────────────────────────────────────────────────────
baseSymbol = request.security(syminfo.tickerid, timeframe.period, source)
comparativeSymbol = request.security(comparativeTickerId, timeframe.period, source)
// ── Calculations ───────────────────────────────────────────────────────────────
baseRef = baseSymbol[length]
compRef = comparativeSymbol[length]
validRefs = not na(baseRef) and not na(compRef)
resRaw = validRefs ? baseSymbol / baseRef / (comparativeSymbol / compRef) - 1 : na
resLog = validRefs ? (math.log(baseSymbol) - math.log(baseRef)) - (math.log(comparativeSymbol) - math.log(compRef)) : na
res = useLogNorm ? resLog : resRaw
resColor = toggleRSColor ? (res > 0 ? color.green : color.red) : color.blue
// Reference label
refDay = showRefDateLbl and barstate.islast and validRefs ? dayofmonth(time[length]) : na
refMonth = showRefDateLbl and barstate.islast and validRefs ? month(time[length]) : na
refYear = showRefDateLbl and barstate.islast and validRefs ? year(time[length]) : na
refLabelStyle = validRefs and res[length] > 0 ? label.style_label_up : label.style_label_down
// Angle-based RS trend
y0 = validRefs ? (res - res[base]) : na
angle0 = validRefs ? math.atan(y0 / base) : na
zeroLineColor = showRSTrend ? (angle0 > 0.0 ? color.green : color.maroon) : color.maroon
sma_res = ta.sma(res, lengthRSMA)
ma_rising = ta.rising(sma_res, 3)
ma_falling = ta.falling(sma_res, 3)
ma_color = showMAColor and ma_rising ? color.green : showMAColor and ma_falling ? color.red : color.gray
// ── Reference label (persistent, no duplicates) ────────────────────────────────
var label refLbl = na
if showRefDateLbl and barstate.islast and validRefs
if na(refLbl)
refLbl := label.new(bar_index - length, 0, '', color=color.blue, style=refLabelStyle, yloc=yloc.price, textcolor=color.white)
label.set_x(refLbl, bar_index - length)
label.set_y(refLbl, 0)
label.set_style(refLbl, refLabelStyle)
label.set_text(refLbl, 'RS-' + str.tostring(length) + ' reference, ' + str.tostring(refDay) + '-' + str.tostring(refMonth) + '-' + str.tostring(refYear))
else if not showRefDateLbl and not na(refLbl)
label.delete(refLbl)
refLbl := na
// ── Plots ──────────────────────────────────────────────────────────────────────
plot(showZeroLine and validRefs ? 0 : na, linewidth=2, color=zeroLineColor, title='Zero Line / RS Trend')
plot(res, title='RS', linewidth=3, color=resColor)
plot(showMA ? sma_res : na, color=ma_color, title='MA', linewidth=2)
// ── Alerts ─────────────────────────────────────────────────────────────────────
alertcondition(ma_rising, 'Rising Strength', 'Rising Strength')
alertcondition(ma_falling, 'Declining Strength', 'Declining Strength')