Distributional Rank | NAL

NordicAlphaLab · study · 128 行 · 点赞 180 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// 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 | NAL", "Distributional Rank", overlay = false, precision = 2)
 
//                                     ███╗   ██╗ █████╗ ██╗
//                                     ████╗  ██║██╔══██╗██║
//                                     ██╔██╗ ██║███████║██║
//                                     ██║╚██╗██║██╔══██║██║
//                                     ██║ ╚████║██║  ██║███████╗
//                                     ╚═╝  ╚═══╝╚═╝  ╚═╝╚══════╝
 
// ══════════════════════════════════════ ◈ Inputs ◈ ══════════════════════════════════════
 
int Lookback = input.int(80, "Lookback", minval = 2, maxval = 500, group = "Parameters")
int Smoothing = input.int(4, "Percentile Smoothing", minval = 2, maxval = 500, group = "Parameters")
float uThreshold = input.float(70.0, "Bull Threshold", minval = 0.0, maxval = 100.0, step = 1.0, group = "Parameters", inline = "THRES")
float lThreshold = input.float(65.0, "Bear Threshold", minval = 0.0, maxval = 100.0, step = 1.0, group = "Parameters", inline = "THRES")
 
bool UseEMAConfluence = input.bool(true, "Enable EMA Confluence", group = "Parameters")
int EMALength = input.int(30, "EMA Length", minval = 1, group = "Parameters")
 
// ══════════════════════════════════════ ◈ Visuals ◈ ══════════════════════════════════════
 
color col_up = color.rgb(0, 255, 200)
color col_dn = color.rgb(32, 94, 144)
color col_nu = color.gray
 
// ══════════════════════════════════════ ◈ Rank Engine ◈ ══════════════════════════════════════
 
// Maintain a bounded rolling window of closing prices.
var array<float> PriceWindow = array.new_float()
 
if not na(close)
    array.push(PriceWindow, close)
 
    if array.size(PriceWindow) > Lookback
        array.shift(PriceWindow)
 
// Evaluate percentiles 1 through 99 against the current closing price.
int AboveCount = 0
float ScoreR = na
 
if array.size(PriceWindow) == Lookback and not na(close)
    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 < close
            AboveCount += 1
 
    ScoreR := AboveCount / 99.0 * 100.0
 
// ══════════════════════════════════════ ◈ EMA Confluence ◈ ══════════════════════════════════════
 
//Robust Percentile Smoothing 
float Q25 = ta.percentile_linear_interpolation(ScoreR, Smoothing, 25.0)
float Q50 = ta.percentile_linear_interpolation(ScoreR, Smoothing, 50.0)
float Q75 = ta.percentile_linear_interpolation(ScoreR, Smoothing, 75.0)
 
float Score = (Q25 + 2.0 * Q50 + Q75) / 4.0
 
// 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", 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)
 
 
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 Bullish", "Distributional Rank changed to bullish on {{ticker}}.")
alertcondition(BearishFlip, "Distributional Rank Bearish", "Distributional Rank changed to bearish on {{ticker}}.")

← 返回列表