Indicator · Pine Script v6 · Own pane

SAM: Smoothed Adaptive Momentum

by mihakralj· code credited to QuanTAlib

This indicator calculates John Ehlers' Smoothed Adaptive Momentum oscillator in a separate pane.

View on GitHub

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

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

sam.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0
// https://mozilla.org/MPL/2.0/
// © QuanTAlib

//@version=6
indicator("SAM: Smoothed Adaptive Momentum", shorttitle="SAM", overlay=false)

// @function Calculates the Ehlers Smoothed Adaptive Momentum.
//   Measures the Dominant Cycle period via Homodyne Discriminator,
//   then computes one-cycle momentum (close - close[DC]) and applies
//   a 2-pole Super Smoother filter for final output.
//   Source: John F. Ehlers, "Cybernetic Analysis for Stocks and Futures" (2004),
//   Chapter 12: "Adapting to the Trend," p.166.
// @param src Series to analyze.
// @param alpha Smoothing factor for cycle measurement. Default 0.07.
// @param cutoff Super Smoother cutoff period. Default 8.
// @returns The smoothed adaptive momentum oscillator value.
sam(series float src, simple float alpha, simple int cutoff) =>
    // ── 4-bar FIR smoother ──
    float smooth = (src + 2.0 * nz(src[1]) + 2.0 * nz(src[2]) + nz(src[3])) / 6.0

    // ── Hilbert Transform via Ehlers' detrender/quadrature ──
    float pi = math.pi
    float detrend = 0.0
    detrend := (0.0962 * smooth + 0.5769 * nz(smooth[2]) - 0.5769 * nz(smooth[4]) - 0.0962 * nz(smooth[6])) * (0.075 * nz(detrend[1]) + 0.54)

    // ── In-phase and Quadrature components ──
    float q1 = 0.0
    q1 := (0.0962 * detrend + 0.5769 * nz(detrend[2]) - 0.5769 * nz(detrend[4]) - 0.0962 * nz(detrend[6])) * (0.075 * nz(q1[1]) + 0.54)
    float i1 = nz(detrend[3])

    // ── Advance phase by 90 degrees ──
    float ji = (0.0962 * i1 + 0.5769 * nz(i1[2]) - 0.5769 * nz(i1[4]) - 0.0962 * nz(i1[6])) * (0.075 * nz(ji[1]) + 0.54)
    float jq = (0.0962 * q1 + 0.5769 * nz(q1[2]) - 0.5769 * nz(q1[4]) - 0.0962 * nz(q1[6])) * (0.075 * nz(jq[1]) + 0.54)

    // ── Phasor addition for Homodyne Discriminator ──
    float i2 = 0.0
    float q2 = 0.0
    i2 := i1 - jq
    q2 := q1 + ji
    i2 := alpha * i2 + (1.0 - alpha) * nz(i2[1])
    q2 := alpha * q2 + (1.0 - alpha) * nz(q2[1])

    // ── Homodyne Discriminator for period ──
    float re = 0.0
    float im = 0.0
    re := i2 * nz(i2[1]) + q2 * nz(q2[1])
    im := i2 * nz(q2[1]) - q2 * nz(i2[1])
    re := alpha * re + (1.0 - alpha) * nz(re[1])
    im := alpha * im + (1.0 - alpha) * nz(im[1])

    float period = 0.0
    if im != 0.0 and re != 0.0
        period := 2.0 * pi / math.atan(im / re)
    period := math.max(math.min(period, 50.0), 6.0)

    float instPeriod = 0.0
    instPeriod := 0.33 * period + 0.67 * nz(instPeriod[1])
    float dcPeriod = 0.0
    dcPeriod := 0.15 * instPeriod + 0.85 * nz(dcPeriod[1])

    // ── Adaptive Momentum: one dominant-cycle lookback ──
    int dcLen = math.max(int(dcPeriod), 1)
    float momentum = src - nz(src[dcLen])

    // ── 2-pole Super Smoother on momentum ──
    float a1 = math.exp(-math.sqrt(2.0) * pi / cutoff)
    float b1 = 2.0 * a1 * math.cos(math.sqrt(2.0) * pi / cutoff)
    float c2 = b1
    float c3 = -a1 * a1
    float c1 = 1.0 - c2 - c3
    float filt = 0.0
    filt := c1 * (momentum + nz(momentum[1])) / 2.0 + c2 * nz(filt[1]) + c3 * nz(filt[2])
    filt

// ── Inputs ──────────────────────────────────────────────
a = input.float(0.07, "Alpha", minval=0.01, maxval=1.0, step=0.01)
c = input.int(8, "Cutoff", minval=2)

// ── Calculation ─────────────────────────────────────────
result = sam(close, a, c)

// ── Plot ────────────────────────────────────────────────
plot(result, "SAM", color=color.yellow, linewidth=2)
hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)

Overview

SAM: Smoothed Adaptive Momentum identifies the dominant cycle period using a Hilbert Transform and Homodyne Discriminator, then measures price momentum across that dynamic cycle length. A two-pole Super Smoother filter smooths the resulting momentum line to reduce noise. The alpha input adjusts cycle measurement smoothing, while the cutoff setting controls the Super Smoother filter period. The script provides an oscillator line without generating automated trade signals, entry rules, or exit thresholds.

Details

Published by

mihakralj on GitHub

Code credit

QuanTAlib, as written in the script's header

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum

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
Alphafloat0.07a
Cutoffint8c
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

Asymmetrical RSI

by everget
948 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator

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
261 stars

This indicator demonstrates an experimental graph-style scatter visualization within a separate pane using Pine Script arrays. It tracks two features across recent bars: the single-bar price change and a scaled 14-period Relative Strength Index. Values are loa

Indicator v4 Oscillators & momentum
Updated 4 years ago · None stated GitHub repository of Example: Graph Style - 4th Dimension RSI

KD指標

by TaichiS
219 stars

This script calculates and plots a Stochastic Oscillator (KD indicator) in a separate chart pane. It determines the fast stochastic value over a 9-period lookback, then applies a 3-period Simple Moving Average (SMA) to create the %K line, and a second 3-period

Indicator v4 Oscillators & momentum
Updated 5 years ago · None stated GitHub repository of KD指標

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