本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// Pine Script®
// © RakoQuant
//@version=6
indicator("Keltner SD SuperTrend | RakoQuant", shorttitle="RQ KC SD ST", overlay=true)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// INPUTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
group_core = "Core"
group_mech = "Mechanism"
group_viz = "Visuals"
src = input.source(high, "Source", group=group_core)
basisLen = input.int(20, "Basis Length (EMA)", minval=1, group=group_core)
sdLen = input.int(21, "StdDev Length", minval=1, group=group_core)
mult = input.float(2.09, "StdDev Multiplier", minval=0.1, step=0.1, group=group_core)
momLen = input.int(8, "Momentum Lookback", minval=1, group=group_mech)
momGate = input.float(0.1, "Momentum Gate (σ units)", minval=0.0, step=0.01, group=group_mech)
paintBars = input.bool(true, "Paint Candles", group=group_viz)
showFill = input.bool(true, "Show Trend Fill", group=group_viz)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// VISUAL OPTIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
palette = input.string("RakoQuant Neon", "Palette",
options=["RakoQuant Neon", "Classic", "Gold / Indigo", "Ocean / Coral", "Mono"],
group=group_viz)
lineStyleOpt = input.string("Line Break", "Line Style",
options=["Line Break", "Line", "Step"],
group=group_viz)
showGlow = input.bool(true, "Glow Effect", group=group_viz)
glowOuterW = input.int(10, "Glow Outer Width", minval=1, maxval=20, group=group_viz)
glowInnerW = input.int(6, "Glow Inner Width", minval=1, maxval=20, group=group_viz)
mainWidth = input.int(2, "Main Width", minval=1, maxval=6, group=group_viz)
showBase = input.bool(false, "Show Basis", group=group_viz)
baseOpacity = input.int(75, "Basis Opacity", minval=0, maxval=100, group=group_viz)
fillMode = input.string("To Mid", "Fill Mode", options=["To Mid", "To Price", "Off"], group=group_viz)
fillOpacity = input.int(88, "Fill Opacity", minval=0, maxval=100, group=group_viz)
showSignals = input.bool(false, "Show UP/DN Markers", group=group_viz)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ENGINE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
basis = ta.ema(src, basisLen)
sd = ta.stdev(src, sdLen)
upperband = basis + mult * sd
lowerband = basis - mult * sd
prevupper = nz(upperband[1])
prevlower = nz(lowerband[1])
// SuperTrend trailing
upperband := upperband < prevupper or close[1] > prevupper ? upperband : prevupper
lowerband := lowerband > prevlower or close[1] < prevlower ? lowerband : prevlower
// Momentum gate
mom = basis - basis[momLen]
momNorm = sd != 0.0 ? mom / sd : 0.0
int dir = na
float st = na
prevst = nz(st[1])
if na(basis[1]) or na(sd[1])
dir := 1
else if prevst == prevupper
dir := (close > upperband and momNorm > momGate) ? -1 : 1
else
dir := (close < lowerband and momNorm < -momGate) ? 1 : -1
st := dir == -1 ? lowerband : upperband
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// COLORS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullCol =
palette == "Classic" ? color.lime :
palette == "Gold / Indigo" ? color.rgb(255, 196, 0) :
palette == "Ocean / Coral" ? color.rgb(0, 220, 255) :
palette == "Mono" ? color.rgb(230, 230, 230) :
#00ffc3
bearCol =
palette == "Classic" ? color.red :
palette == "Gold / Indigo" ? color.rgb(96, 86, 255) :
palette == "Ocean / Coral" ? color.rgb(255, 105, 120) :
palette == "Mono" ? color.rgb(140, 140, 140) :
#ff00d0
activeCol = dir < 0 ? bullCol : bearCol
// Plot style mapping (Line Break fixes the ugly shelf look)
plotStyle =
lineStyleOpt == "Line" ? plot.style_line :
lineStyleOpt == "Step" ? plot.style_stepline :
plot.style_linebr
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PLOTTING (active band only) + GLOW (same style)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
stBull = dir < 0 ? st : na
stBear = dir > 0 ? st : na
// Glow outer
plot(showGlow ? stBull : na, "Bull Glow Outer", color=color.new(bullCol, 88), linewidth=glowOuterW, style=plotStyle)
plot(showGlow ? stBear : na, "Bear Glow Outer", color=color.new(bearCol, 88), linewidth=glowOuterW, style=plotStyle)
// Glow inner
plot(showGlow ? stBull : na, "Bull Glow Inner", color=color.new(bullCol, 72), linewidth=glowInnerW, style=plotStyle)
plot(showGlow ? stBear : na, "Bear Glow Inner", color=color.new(bearCol, 72), linewidth=glowInnerW, style=plotStyle)
// Core
bullPlot = plot(stBull, "Bull Band", color=bullCol, linewidth=mainWidth, style=plotStyle)
bearPlot = plot(stBear, "Bear Band", color=bearCol, linewidth=mainWidth, style=plotStyle)
// Optional basis
plot(showBase ? basis : na, "KC Basis", color=color.new(color.gray, baseOpacity), linewidth=1)
// Fill refs
midPlot = plot((open + close) / 2, "Mid", color=na)
pricePlot = plot(close, "Close", color=na)
// Fill
fill(midPlot, bullPlot, color = showFill and dir < 0 and fillMode == "To Mid" ? color.new(bullCol, fillOpacity) : na)
fill(midPlot, bearPlot, color = showFill and dir > 0 and fillMode == "To Mid" ? color.new(bearCol, fillOpacity) : na)
fill(pricePlot, bullPlot, color = showFill and dir < 0 and fillMode == "To Price" ? color.new(bullCol, fillOpacity) : na)
fill(pricePlot, bearPlot, color = showFill and dir > 0 and fillMode == "To Price" ? color.new(bearCol, fillOpacity) : na)
// Candle paint
barcolor(paintBars ? activeCol : na)
plotcandle(open, high, low, close,
color = paintBars ? activeCol : na,
wickcolor = paintBars ? activeCol : na,
bordercolor = paintBars ? activeCol : na,
force_overlay=true)
// Signals + alerts
bullFlip = dir < 0 and dir[1] > 0
bearFlip = dir > 0 and dir[1] < 0
plotshape(showSignals and bullFlip and barstate.isconfirmed, title="Bull Flip",
style=shape.labelup, location=location.belowbar, size=size.small,
color=bullCol, text="UP", textcolor=color.black)
plotshape(showSignals and bearFlip and barstate.isconfirmed, title="Bear Flip",
style=shape.labeldown, location=location.abovebar, size=size.small,
color=bearCol, text="DN", textcolor=color.white)
alertcondition(bullFlip, "RQ KC SD ST Bull Flip", "Keltner SD SuperTrend turned UP {{exchange}}:{{ticker}}")
alertcondition(bearFlip, "RQ KC SD ST Bear Flip", "Keltner SD SuperTrend turned DOWN {{exchange}}:{{ticker}}")