Indicator · Pine Script v6 · Own pane

Ehlers Linear Predictive Filter (LPF)

by mihakralj

Calculates John Ehlers' Linear Predictive Filter based on the Griffiths Least Mean Squares adaptive predictor algorithm.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code128
Settings4inputs
LicenceApache-2.0
Last update4 weeks ago27 Sep 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

128 lines of Pine Script v6 from lib/cycles/lpf/lpf.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

lpf.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Ehlers Linear Predictive Filter (LPF)","LPF",overlay=false)
//@function Griffiths linear predictive filter dominant cycle estimator
//@param source Price input series
//@param lowerBound Lower bandpass boundary (minimum period)
//@param upperBound Upper bandpass boundary (maximum period)
//@param dataLength Data length for Griffiths predictor
//@returns [dominantCycle, signal, predict] Dominant cycle period, normalized signal, predicted value
//@optimized Uses native PineScript historical operator; roofing filter + AGC + Griffiths LMS inline
//@validation wolfram:"Griffiths LMS algorithm","Wiener-Hopf equation" external:"TASC 2025.01 Linear Predictive Filters","Ehlers Linear Prediction PDF"
lpf(series float source,simple int lowerBound,simple int upperBound,simple int dataLength)=>
    if lowerBound<8
        runtime.error("Lower bound must be at least 8")
    if upperBound<=lowerBound
        runtime.error("Upper bound must be greater than lower bound")
    if dataLength<4
        runtime.error("Data length must be at least 4")
    var array<float> xx=array.new_float(0)
    var array<float> coef=array.new_float(0)
    var array<float> pwr=array.new_float(0)
    var int storedLen=0
    var int storedUpper=0
    var bool configured=false
    var float hp=0.0
    var float lp=0.0
    var float peak=0.1
    var float signal=0.0
    var float dom=0.0
    var float prevDom=0.0
    if not configured or storedLen!=dataLength or storedUpper!=upperBound
        xx:=array.new_float(dataLength+1,0.0)
        coef:=array.new_float(dataLength+1,0.0)
        pwr:=array.new_float(upperBound+2,0.0)
        storedLen:=dataLength
        storedUpper:=upperBound
        configured:=true
        hp:=0.0
        lp:=0.0
        peak:=0.1
        signal:=0.0
        dom:=(lowerBound+upperBound)*0.5
        prevDom:=dom
    float price=nz(source)
    // Stage 1: Roofing filter — Highpass (Butterworth 2nd order)
    float alphaHP=(math.cos(0.707*2.0*math.pi/float(upperBound))+math.sin(0.707*2.0*math.pi/float(upperBound))-1.0)/math.cos(0.707*2.0*math.pi/float(upperBound))
    hp:=math.pow(1.0-alphaHP/2.0,2.0)*(price-2.0*nz(price[1])+nz(price[2]))+2.0*(1.0-alphaHP)*nz(hp[1])-math.pow(1.0-alphaHP,2.0)*nz(hp[2])
    // Stage 1b: SuperSmoother lowpass
    float a1=math.exp(-math.sqrt(2.0)*math.pi/float(lowerBound))
    float b1=2.0*a1*math.cos(math.sqrt(2.0)*math.pi/float(lowerBound))
    float ssC2=b1
    float ssC3=-(a1*a1)
    float ssC1=1.0-ssC2-ssC3
    lp:=ssC1*(hp+nz(hp[1]))*0.5+ssC2*nz(lp[1])+ssC3*nz(lp[2])
    // Stage 2: AGC normalization
    peak:=0.991*peak
    if math.abs(lp)>peak
        peak:=math.abs(lp)
    signal:=peak>0.0?lp/peak:0.0
    // Stage 3: Griffiths adaptive predictor
    // Shift data buffer
    for count=dataLength to 1
        array.set(xx,count,count>1?array.get(xx,count-1):0.0)
    array.set(xx,0,signal)
    // Compute signal power
    float sigPower=0.0
    for count=0 to dataLength-1
        float xVal=array.get(xx,count)
        sigPower+=xVal*xVal
    sigPower/=float(dataLength)
    // Convergence factor
    float mu=sigPower>0.0?0.25/(sigPower*float(dataLength)):0.0
    // Predict
    float xBar=0.0
    for count=1 to dataLength
        xBar+=array.get(coef,count)*array.get(xx,count)
    // Error + coefficient update
    float err=array.get(xx,0)-xBar
    for count=1 to dataLength
        float c=array.get(coef,count)+mu*err*array.get(xx,count)
        array.set(coef,count,c)
    // Stage 4: Spectrum from coefficients
    float maxPwrLocal=0.0
    for period=lowerBound to upperBound
        float realPart=0.0
        float imagPart=0.0
        for count=1 to dataLength
            float angle=2.0*math.pi*float(count)/float(period)
            realPart+=array.get(coef,count)*math.cos(angle)
            imagPart+=array.get(coef,count)*math.sin(angle)
        float p=realPart*realPart+imagPart*imagPart
        array.set(pwr,period,p)
        if p>maxPwrLocal
            maxPwrLocal:=p
    // Stage 5: Dominant cycle via center of gravity
    float spx=0.0
    float sp=0.0
    for period=lowerBound to upperBound
        float p=maxPwrLocal>0.0?array.get(pwr,period)/maxPwrLocal:0.0
        if p>=0.5
            spx+=float(period)*p
            sp+=p
    float rawDom=sp>0.0?spx/sp:dom
    // Constrain change to ±2 bars per update
    float maxDelta=2.0
    if rawDom-prevDom>maxDelta
        rawDom:=prevDom+maxDelta
    if prevDom-rawDom>maxDelta
        rawDom:=prevDom-maxDelta
    dom:=math.max(float(lowerBound),math.min(float(upperBound),rawDom))
    prevDom:=dom
    // Two-bar prediction for trigger
    float xPred=0.0
    if dataLength>2
        for count=1 to dataLength-2
            xPred+=array.get(coef,count)*array.get(xx,count)
    [dom,signal,xPred]

// ---------- Main loop ----------
i_source=input.source(close,"Source")
i_lower=input.int(18,"Lower Bound",minval=8,maxval=200)
i_upper=input.int(40,"Upper Bound",minval=10,maxval=500)
i_length=input.int(40,"Data Length",minval=4,maxval=200)
[dominantCycle,sig,pred]=lpf(i_source,i_lower,i_upper,i_length)
plot(dominantCycle,"Dominant Cycle",color=color.yellow,linewidth=2)
plot(sig*20+30,"Signal",color=color.lime,linewidth=1)
plot(pred*20+30,"Predict",color=color.red,linewidth=1)

Overview

Ehlers Linear Predictive Filter (LPF) processes the source price through a roofing filter combining a highpass filter and SuperSmoother lowpass filter, followed by automatic gain control normalization. The script then applies an adaptive linear prediction algorithm to estimate cycle components. It outputs the dominant cycle period, normalized signal, and predicted values in a separate indicator pane. User settings configure the upper and lower bandpass bounds alongside the predictive filter data length.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum, Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

algorithmic-tradingcsharpdotnetpinescriptpythonquantitative-financesimdta-libtechnical-analysistechnical-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Sourcesourceclosei_source
Lower Boundint18i_lower
Upper Boundint40i_upper
Data Lengthint40i_length
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Schaff Trend Cycle

by everget
948 stars

This indicator plots the Schaff Trend Cycle in a separate pane. It subtracts a slow exponential moving average from a fast one, applies a stochastic cycle twice, and smooths each result to form a 0–100 oscillator. Line color follows the slope, or the upper and

Indicator v4 Oscillators & momentumTrend
Updated 10 days ago · GPL-3.0 GitHub repository of Schaff Trend Cycle

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

[RS]Standardized Trend Map

by ricardosantos79
261 stars

Standardized Trend Map calculates series momentum relative to active price range across 40 distinct lookback periods ranging from 2 to 80 bars on an exponential moving average source. Each standardized calculation rounds to an integer values of -1, 0, or 1 to

Indicator v4 TrendOscillators & momentum
Updated 4 years ago · None stated GitHub repository of [RS]Standardized Trend Map

ADX Histogram

by harryguiacorn
219 stars

This indicator calculates and displays the Average Directional Index (ADX) as a column histogram in a separate pane. It evaluates market trend strength by calculating directional movement metrics based on specified smoothing and DI length inputs. When the ADX

Indicator v5 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of ADX Histogram
218 stars

Multi-Timeframe Analysis Suite calculates trend direction and momentum conditions across five user-selected timeframes using moving averages, RSI, and Stochastic oscillators. It evaluates moving average slope and oscillator levels to compute trend alignment, m

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe Analysis Suite

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers