Indicator · Pine Script v6 · On the chart

Bandpass Filter (BPF)

by mihakralj

This indicator calculates a bandpass series by cascading a highpass infinite impulse response filter with a lowpass filter.

View on GitHub

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

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

bpf.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Bandpass Filter (BPF)", "BPF", overlay=true)

//@function Optimized Bandpass Filter combining highpass and lowpass filters
//@param source Series to calculate bandpass from
//@param lp Lower cutoff period for highpass filter
//@param up Upper cutoff period for lowpass filter
//@returns Optimized bandpass filtered series
//@optimized Uses combined HP and LP IIR filters with O(1) complexity per bar
bpf(series float src, simple int lp, simple int up) =>
    var float SQRT2_PI = math.sqrt(2.0) * math.pi
    var float hp = na
    var float hp_c1 = 0.0
    var float hp_c2 = 0.0
    var float hp_c3 = 0.0
    var int prev_hp_length = 0
    var float bpf_val = na
    var float lp_c1 = 0.0
    var float lp_c2 = 0.0
    var float lp_c3 = 0.0
    var int prev_lp_length = 0
    if prev_hp_length != lp
        float hp_arg = SQRT2_PI / float(lp)
        float hp_exp_arg = math.exp(-hp_arg)
        hp_c2 := 2.0 * hp_exp_arg * math.cos(hp_arg)
        hp_c3 := -hp_exp_arg * hp_exp_arg
        hp_c1 := (1.0 + hp_c2 - hp_c3) / 4.0
        prev_hp_length := lp
    if prev_lp_length != up
        float lp_arg = SQRT2_PI / float(up)
        float lp_exp_arg = math.exp(-lp_arg)
        lp_c2 := 2.0 * lp_exp_arg * math.cos(lp_arg)
        lp_c3 := -lp_exp_arg * lp_exp_arg
        lp_c1 := 1.0 - lp_c2 - lp_c3
        prev_lp_length := up
    float ssrc = nz(src, src[1])
    float src1 = nz(src[1], ssrc)
    float src2 = nz(src[2], src1)
    float hp1 = nz(hp[1], 0.0)
    float hp2 = nz(hp[2], 0.0)
    hp := hp_c1 * (ssrc - 2.0 * src1 + src2) + hp_c2 * hp1 + hp_c3 * hp2
    float bp1 = nz(bpf_val[1], hp)
    float bp2 = nz(bpf_val[2], bp1)
    bpf_val := lp_c1 * hp + lp_c2 * bp1 + lp_c3 * bp2
    bpf_val

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

// Inputs
i_lower = input.int(10, "Lower Cutoff", minval=1)
i_upper = input.int(20, "Higher Cutoff", minval=1)
i_source = input.source(close, "Source")

// Calculation
filt = bpf(i_source, i_lower, i_upper)

// Plot
plot(i_source - filt, "Bandpass", color=color.yellow, linewidth=2)

Overview

Lower and higher cutoff periods set the pass band, and the source input selects the series being filtered. It plots the source minus the filtered value as a yellow line on the price chart. The calculation uses current and prior bars with constant work per bar. It does not generate entries, exits, or risk rules.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

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
Lower Cutoffint10i_lower
Higher Cutoffint20i_upper
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

948 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
575 stars

The indicator plots ICT displacement candles on the price chart and can switch to retracement scanning. Displacement requires a move larger than a set share of the 14-bar average high-low range, lasting at least a chosen number of bars, with optional close, pr

Indicator v6 SMC / ICT / price actionCandlestick patterns
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Displacement Candles Indicator
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

1833 Tracker

by Allen1212
303 stars

1833 Tracker is an overlay indicator that visualizes historical buy and sell transactions executed by ARK Invest ETFs for the stock on the main price chart. It displays trade dates using green buy and red sell labels positioned relative to price bars, indicati

Indicator v4
Updated 2 years ago · None stated GitHub repository of 1833 Tracker

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