本页源码来自 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/
// © NordicAlphaLab
//@version=6
indicator("Percentile Adaptive RSI | NAL", "Percentile Adaptive RSI", overlay = false)
// ███╗ ██╗ █████╗ ██╗
// ████╗ ██║██╔══██╗██║
// ██╔██╗ ██║███████║██║
// ██║╚██╗██║██╔══██║██║
// ██║ ╚████║██║ ██║███████╗
// ╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝
// ══════════════════════════════════════ ◈ Visuals ◈ ══════════════════════════════════════
string col_mode = input.string("Standard", title = "Color Mode", group = "Visuals", options = ["Standard", "Nordic", "Simple"])
[col_up, col_dn, col_nu] = switch col_mode
"Standard" => [color.rgb(0, 255, 200), color.rgb(32, 94, 144), color.gray]
"Nordic" => [color.rgb(46, 161, 255), color.rgb(150, 154, 169), color.gray]
"Simple" => [color.lime, color.red, color.gray]
// ══════════════════════════════════════ ◈ Inputs ◈ ══════════════════════════════════════
string G1 = "Percentile RSI"
string G2 = "Percentile Thresholds"
float rsiSrc = input.source(close, "Source", group = G1)
int rsiLen = input.int(12, "Length", minval = 1, group = G1)
int rsiPer = input.int(50, "Percentile", group = G1, minval = 0, maxval = 100)
int lowerPer = input.int(40, "Lower", group = G2, minval = 0, maxval = 100, inline = "AAA")
int upperPer = input.int(60, "Upper", group = G2, minval = 0, maxval = 100, inline = "AAA")
// ══════════════════════════════════════ ◈ Logic ◈ ══════════════════════════════════════
float percentile = ta.percentile_linear_interpolation(rsiSrc, rsiLen, rsiPer)
float RSI = ta.rsi(rsiSrc, rsiLen)
float PercentileRSI = ta.rsi(percentile, rsiLen)
float upperB = ta.percentile_linear_interpolation(RSI, rsiLen, upperPer)
float lowerB = ta.percentile_linear_interpolation(RSI, rsiLen, lowerPer)
// ══════════════════════════════════════ ◈ NAL State ◈ ══════════════════════════════════════
bool Bullish = RSI > upperB and PercentileRSI > upperB //and rsiSrc > percentile smoother better trades?
bool Bearish = RSI < lowerB and PercentileRSI < lowerB
int NAL = 0
NAL := Bullish ? 1 : Bearish ? -1 : nz(NAL[1], 0)
bool BullishFlip = barstate.isconfirmed and NAL == 1 and nz(NAL[1], 0) != 1
bool BearishFlip = barstate.isconfirmed and NAL == -1 and nz(NAL[1], 0) != -1
color col = NAL == 1 ? col_up : NAL == -1 ? col_dn : col_nu
// ══════════════════════════════════════ ◈ Plots ◈ ══════════════════════════════════════
float plotRSI = 0.0
plotRSI := Bullish ? math.max(RSI, PercentileRSI) : Bearish ? math.min(RSI, PercentileRSI) : plotRSI[1]
RSIPlot = plot(plotRSI, "Percentile Adaptive RSI", color = col, linewidth = 2)
UpperPlot = plot(NAL == 1 ? na : upperB, "Upper Percentile Band", color = color.gray, style = plot.style_linebr)
LowerPlot = plot(NAL == -1 ? na : lowerB, "Lower Percentile Band", color = color.gray, style = plot.style_linebr)
// NAL gradients fade from the RSI toward the opposite percentile band.
fill(RSIPlot, LowerPlot, NAL == 1 ? plotRSI : na, NAL == 1 ? lowerB : na, color.new(col_up, 50), color.new(col_up, 100), title = "Bullish Gradient", fillgaps = false)
fill(RSIPlot, UpperPlot, NAL == -1 ? upperB : na, NAL == -1 ? plotRSI : na, color.new(col_dn, 100), color.new(col_dn, 50), title = "Bearish Gradient", fillgaps = false)
// Trend-colored candles.
plotcandle(open, high, low, close, "Bar Color", color = col, wickcolor = col, bordercolor = col, display = display.pane, force_overlay = true)
// ══════════════════════════════════════ ◈ Alerts ◈ ══════════════════════════════════════
alertcondition(BullishFlip, "Percentile Adaptive RSI Bullish", "Percentile Adaptive RSI changed to bullish on {{ticker}}.")
alertcondition(BearishFlip, "Percentile Adaptive RSI Bearish", "Percentile Adaptive RSI changed to bearish on {{ticker}}.")