本页源码来自 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("Distributional Rank Momentum | NAL", "Distributional Rank Momentum", overlay = false, precision = 2)
// ███╗ ██╗ █████╗ ██╗
// ████╗ ██║██╔══██╗██║
// ██╔██╗ ██║███████║██║
// ██║╚██╗██║██╔══██║██║
// ██║ ╚████║██║ ██║███████╗
// ╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝
// ══════════════════════════════════════ ◈ Inputs ◈ ══════════════════════════════════════
int momLen = input.int(60, "Momentum Length", minval = 2, maxval = 500, group = "Parameters")
int Lookback = input.int(60, "Distributional Rank Lookback", minval = 2, maxval = 500, group = "Parameters")
float uThreshold = input.float(90.0, "Bullish Threshold", minval = 0.0, maxval = 100.0, step = 1.0, group = "Thresholds")
float lThreshold = input.float(40.0, "Bearish Threshold", minval = 0.0, maxval = 100.0, step = 1.0, group = "Thresholds")
bool UseEMAConfluence = input.bool(true, "Enable EMA Confluence", group = "Confluence")
int EMALength = input.int(30, "EMA Length", minval = 1, group = "Confluence")
// ══════════════════════════════════════ ◈ Visuals ◈ ══════════════════════════════════════
color col_up = color.rgb(0, 255, 200)
color col_dn = color.rgb(32, 94, 144)
color col_nu = color.gray
// ══════════════════════════════════════ ◈ Rank Engine ◈ ══════════════════════════════════════
// Momentum Source.
float Momentum = close - ta.percentile_linear_interpolation(close, momLen, (math.avg(uThreshold, lThreshold)))
// Maintain a bounded rolling window of momentum values.
var array<float> PriceWindow = array.new_float()
if not na(Momentum)
array.push(PriceWindow, Momentum)
if array.size(PriceWindow) > Lookback
array.shift(PriceWindow)
// Evaluate percentiles 1 through 99 against the current momentum value.
int AboveCount = 0
float Score = na
if array.size(PriceWindow) == Lookback and not na(Momentum)
array<float> SortedPrices = array.copy(PriceWindow)
array.sort(SortedPrices, order.ascending)
for p = 1 to 99
float PercentilePrice = array.percentile_linear_interpolation(SortedPrices, p)
if not na(PercentilePrice) and PercentilePrice < Momentum
AboveCount += 1
Score := AboveCount / 99.0 * 100.0
// ══════════════════════════════════════ ◈ EMA Confluence ◈ ══════════════════════════════════════
// EMA of the final percentile score.
float ScoreEMA = ta.ema(Score, EMALength)
bool EMA_Bullish = not UseEMAConfluence or (not na(ScoreEMA) and Score > ScoreEMA)
bool EMA_Bearish = not UseEMAConfluence or (not na(ScoreEMA) and Score < ScoreEMA)
// ══════════════════════════════════════ ◈ NAL State ◈ ══════════════════════════════════════
bool Bullish = not na(Score) and Score >= uThreshold and EMA_Bullish
bool Bearish = not na(Score) and Score < lThreshold and EMA_Bearish
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
// ══════════════════════════════════════ ◈ Gradient Geometry ◈ ══════════════════════════════════════
// Bullish gradient fades from the Score toward the bullish threshold.
float BullTop = math.max(Score, uThreshold)
float BullBottom = math.min(Score, uThreshold)
color BullTopColor = Score >= uThreshold ? color.new(col_up, 50) : color.new(col_up, 100)
color BullBottomColor = Score >= uThreshold ? color.new(col_up, 100) : color.new(col_up, 50)
// Bearish gradient fades from the Score toward the bearish threshold.
float BearTop = math.max(Score, lThreshold)
float BearBottom = math.min(Score, lThreshold)
color BearTopColor = Score >= lThreshold ? color.new(col_dn, 50) : color.new(col_dn, 100)
color BearBottomColor = Score >= lThreshold ? color.new(col_dn, 100) : color.new(col_dn, 50)
// ══════════════════════════════════════ ◈ Plots ◈ ══════════════════════════════════════
ScorePlot = plot(Score, "Distributional Rank Momentum", color = col, linewidth = 2)
uThresholdPlot = plot(uThreshold, "Bullish Threshold", color = color.new(col_up, 35), linewidth = 1)
lThresholdPlot = plot(lThreshold, "Bearish Threshold", color = color.new(col_dn, 35), linewidth = 1)
plot(UseEMAConfluence ? ScoreEMA : na, "Rank EMA", color = color.new(col_nu, 35), linewidth = 1)
// NAL gradients fill only the distance to the active regime threshold.
fill(ScorePlot, uThresholdPlot, NAL == 1 ? BullTop : na, NAL == 1 ? BullBottom : na, BullTopColor, BullBottomColor, title = "Bullish Gradient", fillgaps = false)
fill(ScorePlot, lThresholdPlot, NAL == -1 ? BearTop : na, NAL == -1 ? BearBottom : na, BearTopColor, BearBottomColor, 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)
// ══════════════════════════════════════ ◈ Plot Shapes ◈ ══════════════════════════════════════
bullish_transition = NAL == 1 and NAL[1] == -1
bearish_transition = NAL == -1 and NAL[1] == 1
plotshape(bullish_transition, "Bullish Transition", style = shape.triangleup, location = location.belowbar, text = "𝓑𝓾𝔂", color = col_up, textcolor = col_up, size = size.tiny, force_overlay = true, display = display.pane)
plotshape(bearish_transition, "Bearish Transition", style = shape.triangledown, location = location.abovebar, text = "𝓢𝓮𝓵𝓵", color = col_dn, textcolor = col_dn, size = size.tiny, force_overlay = true, display = display.pane)
// ══════════════════════════════════════ ◈ Alerts ◈ ══════════════════════════════════════
alertcondition(BullishFlip, "Distributional Rank Momentum Bullish", "Distributional Rank Momentum changed to bullish on {{ticker}}.")
alertcondition(BearishFlip, "Distributional Rank Momentum Bearish", "Distributional Rank Momentum changed to bearish on {{ticker}}.")