Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Trading with the Laguerre Filter: A Powerful Tool for Trend Following – Amibroker AFL Code

3 min read

The financial markets are like a vast ocean, with prices constantly ebbing and flowing. For traders navigating these waters, having the right tools can mean the difference between riding the waves successfully or getting caught in the undertow. Enter the Laguerre Filter—a sophisticated yet practical indicator that helps traders identify trends with remarkable clarity.

Understanding the Laguerre Filter

Developed by John F. Ehlers, the Laguerre Filter represents a significant advancement in technical analysis. At its core, it’s a smoothing indicator that reduces market noise while maintaining responsiveness to price changes. Think of it as a pair of high-tech goggles that filter out the market’s static, allowing you to see the underlying trend more clearly.

What sets the Laguerre Filter apart is its use of advanced mathematical concepts—specifically, Laguerre polynomials—to achieve superior smoothing without the typical lag associated with traditional moving averages. The indicator consists of two components: the filter itself (which can be overlaid on price charts) and the Laguerre Oscillator, which provides additional insight into market momentum.

// TASC Issue: July 2025
// Article: A Tool For Trend Trading - Laguerre Filters
// Article By: John F. Ehlers
// Language: Amibroker AFL

_SECTION_BEGIN("Laguerre Filters");

// Enable X-Axis with Dates as per Rule No 2
SetChartOptions(0, chartShowArrows | chartShowDates);

// Input Parameters
sourceInput = ParamField("Source", 3); // Close price
usInput = Param("Period", 30, 3, 100, 1); // Critical period of UltimateSmoother
gammaInput = Param("Gamma", 0.5, 0.0, 0.999, 0.01); // Gamma value for Laguerre calculations
rmsInput = Param("RMS Length", 100, 1, 500, 1); // Number of bars in RMS calculation

// Function: UltimateSmoother
// Since AFL doesn't support user-defined functions with multiple return values,
// we'll implement this inline
function UltimateSmoother(srcArray, period)
{
    local a1, c2, c3, c1;
    local us, i;
    
    a1 = exp(-1.414 * 3.14159 / period);
    c2 = 2.0 * a1 * cos(1.414 * 3.14159 / period);
    c3 = -a1 * a1;
    c1 = (1.0 + c2 - c3) / 4.0;
    
    us = srcArray;
    
    for(i = 4; i < BarCount; i++)
    {
        us[i] = (1.0 - c1) * srcArray[i] + 
                (2.0 * c1 - c2) * srcArray[i-1] - 
                (c1 + c3) * srcArray[i-2] + 
                c2 * us[i-1] + c3 * us[i-2];
    }
    
    return us;
}

// Calculate UltimateSmoother
us = UltimateSmoother(sourceInput, usInput);

// Laguerre Filter implementation
// Using arrays and loops since AFL doesn't support self-referencing
l0 = us;
l1 = sourceInput; // Initialize with source
l2 = sourceInput;
l3 = sourceInput;
l4 = sourceInput;
l5 = sourceInput;

g1 = 1.0 - gammaInput;

// Calculate Laguerre filter components using for loop
for(i = 1; i < BarCount; i++)
{
    l1[i] = g1 * l0[i-1] + gammaInput * l1[i-1];
    l2[i] = g1 * l1[i-1] + gammaInput * l2[i-1];
    l3[i] = g1 * l2[i-1] + gammaInput * l3[i-1];
    l4[i] = g1 * l3[i-1] + gammaInput * l4[i-1];
    l5[i] = g1 * l4[i-1] + gammaInput * l5[i-1];
}

// Calculate Laguerre Filter
laguerreFilter = (l0 + 4.0 * l1 + 6.0 * l2 + 4.0 * l3 + l5) / 16;

// Laguerre Oscillator implementation
l0_osc = us;
l1_osc = sourceInput; // Initialize

// Calculate l1 for oscillator using for loop
for(i = 1; i < BarCount; i++)
{
    l1_osc[i] = l0_osc[i-1] + gammaInput * (l1_osc[i-1] - l0_osc[i]);
}

// Calculate difference
diff = l0_osc - l1_osc;

// Calculate RMS (Root Mean Square)
squaredDiff = diff * diff;
rms = sqrt(MA(squaredDiff, rmsInput));

// Calculate Laguerre Oscillator with zero division protection
laguerreOscillator = IIf(rms != 0, diff / rms, 0);


// Plot Laguerre Filter on main chart
//Plot(laguerreFilter, "Laguerre Filter", colorBlue, styleLine | styleThick);


_SECTION_END();

// Separate section for Laguerre Oscillator (non-overlay indicator)
_SECTION_BEGIN("Laguerre Oscillator");

// Plot Laguerre Oscillator in separate pane
Plot(laguerreOscillator, "Laguerre Oscillator", colorRed, styleLine | styleThick | styleOwnScale);
Plot(0, "Zero Line", colorGrey40, styleLine);

// Fill area between oscillator and zero line
PlotOHLC(0, 0, laguerreOscillator, laguerreOscillator, "", 
         IIf(laguerreOscillator > 0, colorLightBlue, colorLightOrange), 
         styleCloud | styleOwnScale);

_SECTION_END();

The Power of Gamma

The secret sauce of the Laguerre Filter lies in its gamma parameter. This single value, ranging from 0 to 0.999, controls the indicator’s behavior:

  • Lower gamma values (0.1-0.3): The filter becomes more responsive to price changes, catching trends early but potentially generating more false signals
  • Medium gamma values (0.4-0.6): A balanced approach suitable for most trading scenarios
  • Higher gamma values (0.7-0.9): The filter becomes smoother, filtering out more noise but potentially missing early trend changes

Reading the Laguerre Oscillator

Looking at the Nifty daily chart you’ve provided, the Laguerre Oscillator in the lower panel tells a compelling story. The oscillator fluctuates between positive (blue) and negative (orange) territories, with the zero line serving as the equilibrium point.

Key Trading Signals:

  1. Trend Identification: When the oscillator is above zero and rising, it suggests bullish momentum. Conversely, readings below zero indicate bearish pressure.
  2. Momentum Shifts: The peaks and troughs in the oscillator often precede actual price reversals. Notice how the oscillator topped out in early April before the Nifty began its correction.
  3. Divergences: One of the most powerful signals occurs when price makes a new high/low but the oscillator fails to confirm. This divergence often signals an impending reversal.

Practical Trading Strategies

Strategy 1: Trend Following

The simplest approach is to trade in the direction of the Laguerre Oscillator:

  • Enter long when the oscillator crosses above zero
  • Exit or go short when it crosses below zero
  • Use a slightly higher gamma (0.6-0.7) to filter out whipsaws

Strategy 2: Momentum Extremes

More aggressive traders can look for extreme readings:

  • When the oscillator reaches unusually high levels (above 0.8), consider taking profits on long positions
  • When it drops to extreme lows (below -0.8), look for potential bounce opportunities

Strategy 3: Divergence Trading

This requires more patience but can yield excellent results:

  • Watch for price making new highs while the oscillator makes lower highs (bearish divergence)
  • Look for price making new lows while the oscillator makes higher lows (bullish divergence)

Risk Management Considerations

Like any technical indicator, the Laguerre Filter isn’t infallible. Here’s how to use it responsibly:

  1. Confirm with Price Action: Don’t rely solely on the oscillator. Confirm signals with support/resistance levels, candlestick patterns, or volume.
  2. Adjust for Market Conditions: In trending markets, use a higher gamma to stay with the trend longer. In choppy markets, lower the gamma for quicker signals.
  3. Set Clear Stop Losses: Use the oscillator to identify entry points, but always define your risk with predetermined stop losses.

Real-World Application

Looking at your Nifty chart from September 2024 to July 2025, we can see how the Laguerre Oscillator captured several significant moves:

  • The October 2024 selloff was preceded by the oscillator turning negative
  • The strong rally from March to April 2025 coincided with sustained positive readings
  • The recent consolidation phase shows the oscillator hovering around zero, suggesting indecision

The Laguerre Filter offers traders a sophisticated tool for trend identification and momentum analysis. By understanding its mechanics and applying it judiciously, traders can enhance their market timing and improve their overall trading performance. Remember, no indicator is perfect—the key is to use the Laguerre Filter as part of a comprehensive trading strategy that includes proper risk management and multiple forms of confirmation.

As you incorporate this tool into your trading arsenal, start with paper trading to get a feel for how it behaves in different market conditions. With practice, you’ll develop an intuitive understanding of when the Laguerre Filter is giving you high-probability signals and when to exercise caution.

The markets will always be challenging, but with tools like the Laguerre Filter, you’re better equipped to navigate their complexities and capitalize on the opportunities they present.

Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

One Reply to “Trading with the Laguerre Filter: A Powerful Tool for…”

  1. You commented
    // Since AFL doesn’t support user-defined functions with multiple return values …

    AmiBroker has an IIR function which simplifies your ultimatesmoother code to

    PI = 3.14159265358979;
    length = Param( “Length”, 20, 1, 50, 1 );

    function UltimateSmoother( src, period )
    {
    // using: // https://www.traders.com/Documentation/FEEDbk_docs/2024/05/TradersTips.html
    a1 = exp( ( -sqrt( 2 ) * PI ) / period );
    c2 = 2 * a1 * cos( ( sqrt( 2 ) * PI ) / period );
    c3 = -a1 * a1;
    c1 = ( 1 + c2 – c3 ) / 4;

    // input for IIR
    b0 = ( 1 – c1 );
    b1 = ( 2 * c1 – c2 );
    b2 = -( c1 + c3 );
    a1 = c2;
    a2 = c3;

    us = IIR( src, b0, a1, b1, a2, b2 );

    return us;
    }

    This code is beyond my coding skills but I found this on the web

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More