本页源码来自 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("SPMA Supertrend | NAL", "SPMA Supertrend | NAL", overlay = true)
// ███╗ ██╗ █████╗ ██╗
// ████╗ ██║██╔══██╗██║
// ██╔██╗ ██║███████║██║
// ██║╚██╗██║██╔══██║██║
// ██║ ╚████║██║ ██║███████╗
// ╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝
// ══════════════════════════════════════ ◈ Inputs ◈ ══════════════════════════════════════
string groupSPMA = "SPMA Settings"
string groupSupertrend = "Supertrend"
string groupVisuals = "Visuals"
int Length = input.int(5, "Baseline Length", minval = 1, group = groupSPMA)
int Lookback = input.int(30, "Percentrank Lookback", minval = 2, group = groupSPMA, tooltip = "Number of historical bars used to rank the current returns. Lower values react faster but produce a less stable rank. Higher values compare the current shock against a broader historical sample.")
int GateInp = input.int(50, "% Gate", minval = 0, maxval = 99, group = groupSPMA, tooltip = "Minimum percentile rank required for the SPMA to update. A value of 50 updates on above-median positive price shocks, while higher values restrict updates to increasingly unusual shocks. When the gate is not met, the SPMA holds its previous value.")
int ATRLength = input.int(10, "ATR Length", minval = 1, group = groupSupertrend)
float ATRFactor = input.float(2.0, "ATR Factor", minval = 0.1, step = 0.1, group = groupSupertrend, tooltip = "ATR multiplier used to position the Supertrend bands around the SPMA.")
string col_mode = input.string("Standard", "Color Mode", options = ["Standard", "Nordic", "Simple"], group = groupVisuals)
[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(0, 96, 175), color.rgb(150, 154, 169), color.gray]
"Simple" => [color.lime, color.red, color.gray]
// ══════════════════════════════════════ ◈ SPMA ◈ ══════════════════════════════════════
f_SPMA(simple int percentrank_lookback, simple int ma_length, simple int percentile_gate) =>
float Ret = close - close[1]
float Per = ta.percentrank(Ret, percentrank_lookback)
bool Gate = Per > percentile_gate
float emaValue = ta.ema(close, ma_length)
var float MA = na
MA := na(MA[1]) ? emaValue : Gate ? emaValue : MA[1]
MA
float SPMA = f_SPMA(Lookback, Length, GateInp)
// ══════════════════════════════════════ ◈ SPMA Supertrend ◈ ══════════════════════════════════════
// Standard Supertrend trailing-band architecture,
// except SPMA replaces the conventional hl2 center.
float ATR = ta.atr(ATRLength)
float UpperBand = SPMA + ATRFactor * ATR
float LowerBand = SPMA - ATRFactor * ATR
float PreviousUpperBand = nz(UpperBand[1], UpperBand)
float PreviousLowerBand = nz(LowerBand[1], LowerBand)
UpperBand := UpperBand < PreviousUpperBand or close[1] > PreviousUpperBand ? UpperBand : PreviousUpperBand
LowerBand := LowerBand > PreviousLowerBand or close[1] < PreviousLowerBand ? LowerBand : PreviousLowerBand
int Direction = na
float Supertrend = na
float PreviousSupertrend = Supertrend[1]
if na(ATR[1])
Direction := 1
else if PreviousSupertrend == PreviousUpperBand
Direction := close > UpperBand ? -1 : 1
else
Direction := close < LowerBand ? 1 : -1
Supertrend := Direction == -1 ? LowerBand : UpperBand
bool Bullish = Direction == -1
bool Bearish = Direction == 1
bool BullishFlip = Bullish and not Bullish[1]
bool BearishFlip = Bearish and not Bearish[1]
color TrendColor = Bullish ? col_up : Bearish ? col_dn : col_nu
// ══════════════════════════════════════ ◈ Plots ◈ ══════════════════════════════════════
SPMAPlot = plot(SPMA, "Shock Percentile Moving Average", color = color.new(TrendColor, 45), linewidth = 1, display = display.none)
BullishPlot = plot(Bullish ? Supertrend : na, "Bullish SPMA Supertrend", color = col_up, linewidth = 2, style = plot.style_linebr)
BearishPlot = plot(Bearish ? Supertrend : na, "Bearish SPMA Supertrend", color = col_dn, linewidth = 2, style = plot.style_linebr)
// ══════════════════════════════════════ ◈ Gradient Fill ◈ ══════════════════════════════════════
fill(SPMAPlot, BullishPlot, Bullish ? SPMA : na, Bullish ? Supertrend : na, color.new(col_up, 100), color.new(col_up, 68))
fill(SPMAPlot, BearishPlot, Bearish ? Supertrend : na, Bearish ? SPMA : na, color.new(col_dn, 68), color.new(col_dn, 100))
barcolor(TrendColor)
plotcandle(open, high, low, close, "Candles", color = TrendColor, wickcolor = TrendColor, bordercolor = TrendColor, display = display.pane, force_overlay = true)
// ══════════════════════════════════════ ◈ Alerts ◈ ══════════════════════════════════════
alertcondition(BullishFlip, "SPMA Supertrend Bullish", "SPMA Supertrend changed to bullish on {{ticker}}.")
alertcondition(BearishFlip, "SPMA Supertrend Bearish", "SPMA Supertrend changed to bearish on {{ticker}}.")