Indicator · Pine Script v6 · Own pane

SAK: Swiss Army Knife Indicator

by mihakralj· code credited to QuanTAlib

This indicator implements John Ehlers' Swiss Army Knife as a second-order recursive filter and plots the result in a separate pane.

View on GitHub

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

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

sak.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("SAK: Swiss Army Knife Indicator", shorttitle="SAK", overlay=false)

// @function Calculates the Ehlers Swiss Army Knife indicator.
//   A unified second-order IIR filter framework where coefficient selection
//   determines the filter type: EMA, SMA, Gaussian, Butterworth, Smoother,
//   HighPass, 2-Pole HighPass, BandPass, or BandStop.
//   Filt = c0 * (b0*Price + b1*Price[1] + b2*Price[2]) + a1*Filt[1] + a2*Filt[2]
//   Source: John F. Ehlers, "Swiss Army Knife Indicator," TASC January 2006.
// @param src Series to filter.
// @param filterType Filter mode string: "EMA","SMA","Gauss","Butter","Smooth","HP","2PHP","BP","BS"
// @param period Cycle period for coefficient computation. Must be >= 2.
// @param n SMA lookback length (used only for SMA mode). Must be >= 1.
// @param delta Bandwidth parameter for BandPass/BandStop modes.
// @returns The filtered value.
sak(series float src, simple string filterType, simple int period, simple int n, simple float delta) =>
    // ── Compute alpha, beta, gamma from Period and filterType ──
    float alpha = 0.0
    float beta1 = 0.0
    float gamma1 = 0.0

    if filterType == "BP" or filterType == "BS"
        gamma1 := 1.0 / math.cos(2.0 * math.pi * delta / period)
        beta1  := math.cos(2.0 * math.pi / period)
        alpha  := gamma1 - math.sqrt(gamma1 * gamma1 - 1.0)
    else if filterType == "Gauss" or filterType == "Butter" or filterType == "2PHP"
        beta1 := 2.415 * (1.0 - math.cos(2.0 * math.pi / period))
        alpha := -beta1 + math.sqrt(beta1 * beta1 + 2.0 * beta1)
    else  // EMA, HP, SMA, Smooth
        alpha := (math.cos(2.0 * math.pi / period) + math.sin(2.0 * math.pi / period) - 1.0) / math.cos(2.0 * math.pi / period)

    // ── Assign unified coefficients c0, b0, b1, b2, a1, a2 ──
    float c0 = 1.0
    float c1 = 0.0
    float b0 = 1.0
    float b1 = 0.0
    float b2 = 0.0
    float a1 = 0.0
    float a2 = 0.0

    if filterType == "EMA"
        c0 := 1.0
        b0 := alpha
        b1 := 0.0
        b2 := 0.0
        a1 := 1.0 - alpha
        a2 := 0.0
    else if filterType == "SMA"
        c0 := 1.0
        c1 := 1.0 / n
        b0 := 1.0 / n
        b1 := 0.0
        b2 := 0.0
        a1 := 1.0
        a2 := 0.0
    else if filterType == "Gauss"
        c0 := alpha * alpha
        b0 := 1.0
        b1 := 0.0
        b2 := 0.0
        a1 := 2.0 * (1.0 - alpha)
        a2 := -(1.0 - alpha) * (1.0 - alpha)
    else if filterType == "Butter"
        c0 := alpha * alpha / 4.0
        b0 := 1.0
        b1 := 2.0
        b2 := 1.0
        a1 := 2.0 * (1.0 - alpha)
        a2 := -(1.0 - alpha) * (1.0 - alpha)
    else if filterType == "Smooth"
        c0 := alpha * alpha / 4.0
        b0 := 1.0
        b1 := 2.0
        b2 := 1.0
        a1 := 0.0
        a2 := 0.0
    else if filterType == "HP"
        c0 := 1.0 - alpha / 2.0
        b0 := 1.0
        b1 := -1.0
        b2 := 0.0
        a1 := 1.0 - alpha
        a2 := 0.0
    else if filterType == "2PHP"
        c0 := (1.0 - alpha / 2.0) * (1.0 - alpha / 2.0)
        b0 := 1.0
        b1 := -2.0
        b2 := 1.0
        a1 := 2.0 * (1.0 - alpha)
        a2 := -(1.0 - alpha) * (1.0 - alpha)
    else if filterType == "BP"
        c0 := (1.0 - alpha) / 2.0
        b0 := 1.0
        b1 := 0.0
        b2 := -1.0
        a1 := beta1 * (1.0 + alpha)
        a2 := -alpha
    else if filterType == "BS"
        c0 := (1.0 + alpha) / 2.0
        b0 := 1.0
        b1 := -2.0 * beta1
        b2 := 1.0
        a1 := beta1 * (1.0 + alpha)
        a2 := -alpha

    // ── SMA special path: uses running sum via c1 ──
    float result = 0.0
    if filterType == "SMA"
        result := c1 * src + a1 * nz(result[1]) - c1 * nz(src[n])
    else
        result := c0 * (b0 * src + b1 * nz(src[1]) + b2 * nz(src[2])) + a1 * nz(result[1]) + a2 * nz(result[2])
    result

// ── Inputs ──────────────────────────────────────────────
filterType = input.string("BP", "Filter Type", options=["EMA","SMA","Gauss","Butter","Smooth","HP","2PHP","BP","BS"])
p          = input.int(20, "Period", minval=2)
n          = input.int(10, "SMA Length (N)", minval=1, tooltip="Only used for SMA mode")
delta      = input.float(0.1, "Delta (BW)", minval=0.01, step=0.01, tooltip="Bandwidth for BP/BS modes")

// ── Calculation ─────────────────────────────────────────
result = sak(close, filterType, p, n, delta)

// ── Plot ────────────────────────────────────────────────
isOverlay = filterType == "EMA" or filterType == "SMA" or filterType == "Gauss" or filterType == "Butter" or filterType == "Smooth"
plot(result, "SAK", color=color.yellow, linewidth=2)
hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)

Overview

A mode string selects EMA, SMA, Gaussian, Butterworth, smoother, high-pass, two-pole high-pass, band-pass, or band-stop coefficients applied to the source series. Period sets the cycle used in the coefficients, SMA length is used only in SMA mode, and delta sets bandwidth for band-pass and band-stop. It does not generate trades, stops, or exits.

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

Moving averages, 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
Filter TypestringBPfilterType
Periodint20p
SMA Length (N)int10n
Delta (BW)float0.1delta
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

948 stars

The Derivative Oscillator calculates a double-exponentially smoothed Relative Strength Index (RSI) and subtracts its Simple Moving Average (SMA) to form the main oscillator line. A secondary signal line is plotted using an SMA of the main oscillator values. Th

Indicator v4 Oscillators & momentumMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Derivative Oscillator

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

MACD

by Alorse
330 stars

This indicator calculates and plots the standard Moving Average Convergence Divergence oscillator in a separate pane. It computes the difference between a fast and slow moving average, derives a signal line through smoothing, and plots a histogram representing

Indicator v4 Oscillators & momentumMoving averages
Updated 2 months ago · None stated GitHub repository of MACD

MACD-V

by harryguiacorn
219 stars

MACD-V is a volatility-normalized momentum indicator based on research by Alex Spiroglou. It calculates the standard MACD histogram and normalizes it by dividing by the Average True Range (ATR). Plotted in a separate pane as a colored histogram, it includes us

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of MACD-V
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

KamaFama

by Mastaaa1987
141 stars

KamaFama is an indicator that translates a Freqtrade trading strategy into Pine Script for visualization. It combines Kaufman Adaptive Moving Average (KAMA), MAMA/FAMA calculations derived via Hilbert Transform, Williams %R, and correlation trend filters to ev

Indicator v6 Moving averagesOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of KamaFama

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