本页源码来自 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/
// © Zh23531
//@version=6
// Laguerre Filter (Ehlers) — TradingView port of the NinjaTrader amaLaguerreFilter setup
// Original NT template: Period 20, overlay on price, DeepSkyBlue line, width 5.
indicator("Laguerre Filter", shorttitle = "LagFilt", overlay = true)
// ── Inputs ────────────────────────────────────────────────────────────────
src = input.source(close, "Source")
period = input.int(20, "Period", minval = 5)
useGamma = input.bool(false, "Override with manual gamma", group = "Advanced")
gammaManual = input.float(0.8, "Gamma (damping, 0–0.99)", minval = 0.0, maxval = 0.99, step = 0.01, group = "Advanced")
colorTrend = input.bool(false, "Color by slope (up/down)", group = "Style")
upCol = input.color(color.new(#00BFFF, 0), "Line / Up color", group = "Style")
dnCol = input.color(color.new(#FF4500, 0), "Down color", group = "Style")
lineW = input.int(5, "Line width", minval = 1, maxval = 10, group = "Style")
// ── Gamma from Period ─────────────────────────────────────────────────────
// Lag of the Laguerre filter = (2.5*g + 1.5) / (1 - g). Setting that equal to the lag
// of an SMA of the same period, (N - 1) / 2, gives g = (N - 4) / (N + 4).
float g = useGamma ? gammaManual : math.max(0.0, (period - 4.0) / (period + 4.0))
// ── Laguerre filter ───────────────────────────────────────────────────────
var float L0 = na
var float L1 = na
var float L2 = na
var float L3 = na
float pL0 = nz(L0, src)
float pL1 = nz(L1, src)
float pL2 = nz(L2, src)
float pL3 = nz(L3, src)
L0 := (1 - g) * src + g * pL0
L1 := -g * L0 + pL0 + g * pL1
L2 := -g * L1 + pL1 + g * pL2
L3 := -g * L2 + pL2 + g * pL3
float filt = (L0 + 2 * L1 + 2 * L2 + L3) / 6
// ── Plot ──────────────────────────────────────────────────────────────────
color c = colorTrend ? (filt >= nz(filt[1], filt) ? upCol : dnCol) : upCol
plot(filt, "Laguerre Filter", color = c, linewidth = lineW)
// Alerts on slope change
bool turnUp = filt > filt[1] and filt[1] <= filt[2]
bool turnDown = filt < filt[1] and filt[1] >= filt[2]
alertcondition(turnUp, "Laguerre turns up", "Laguerre Filter turned up")
alertcondition(turnDown, "Laguerre turns down", "Laguerre Filter turned down")