Indicator · Pine Script v6 · Own pane

Ehlers Trendflex (TRENDFLEX)

by mihakralj

This indicator calculates John Ehlers Trendflex in a separate pane.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code69
Settings2inputs
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

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

trendflex.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
// Indicator algorithm (C) 2013 John F. Ehlers
indicator("Ehlers Trendflex (TRENDFLEX)", "TRENDFLEX", overlay=false)

//@function Calculates Ehlers Trendflex using SuperSmoother pre-filtering and cumulative slope with RMS normalization
//@param source Series to calculate Trendflex from
//@param period Lookback period for trend measurement (>= 1)
//@returns Normalized Trendflex value centered around zero
//@optimized Uses O(1) running sum for cumulative slope instead of O(N) loop, with RMS normalization
trendflex(series float source, simple int period) =>
    if period <= 0
        runtime.error("Period must be positive")

    float src = nz(source)

    // SuperSmoother (2-pole Butterworth lowpass) coefficients
    float halfPeriod = period * 0.5
    float a1 = math.exp(-1.414 * math.pi / halfPeriod)
    float b1 = 2.0 * a1 * math.cos(1.414 * math.pi / halfPeriod)
    float c2 = b1
    float c3 = -(a1 * a1)
    float c1 = 1.0 - c2 - c3

    // SuperSmoother filter state
    var float filt = 0.0
    var float filt1 = 0.0
    float new_filt = bar_index < 2 ? src : c1 * (src + nz(src[1])) * 0.5 + c2 * filt + c3 * filt1
    filt1 := filt
    filt := new_filt

    // O(1) cumulative slope via circular buffer and running sum
    // Sum = Σ(Filt - Filt[i]) for i=1..N = N × Filt - Σ(Filt[i])
    var array<float> buf = array.new_float(period, 0.0)
    var int head = 0
    var float running_sum = 0.0
    var int count = 0

    int n = math.min(count, period)
    float slope_sum = n > 0 ? (n * new_filt - running_sum) / period : 0.0

    float oldest = array.get(buf, head)
    running_sum -= oldest
    running_sum += new_filt
    array.set(buf, head, new_filt)
    head := (head + 1) % period
    if count < period
        count += 1

    // RMS normalization via exponential mean-square
    var float ms = 0.0
    ms := 0.04 * slope_sum * slope_sum + 0.96 * ms

    float result = ms > 0 ? slope_sum / math.sqrt(ms) : 0.0
    na(source) ? na : result

// ---------- Main loop ----------

// Inputs
i_period = input.int(20, "Period", minval=1, tooltip="Lookback period for trend measurement")
i_source = input.source(close, "Source")

// Calculation
trendflex_value = trendflex(i_source, i_period)

// Plot
plot(trendflex_value, "TRENDFLEX", color=color.yellow, linewidth=2)
hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)

Overview

Ehlers Trendflex (TRENDFLEX) pre-filters the chosen source with a two-pole SuperSmoother, sums the difference between the current filtered value and recent filtered values as a cumulative slope, then divides that slope by an exponential root-mean-square so the result is centered near zero. Period sets the lookback and source selects the input series. It plots the oscillator and a dotted zero line and does not generate entries, exits, or stops.

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
Periodint20i_period
Sourcesourceclosei_source
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