Price Filter [AstrideUnicorn]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AstrideUnicorn

//@version=5
indicator("Price Filter [AstrideUnicorn]")

// Advanced fast price filter
// Adapted from "High-Frequency Trading with MSCI World ETF" by Markus Schmidberger (2018)

// Define inputs
length = input.int(200, minval=1, tooltip = "Length for long-term average")
threshold = input.float(0.5, minval=0, maxval=1, tooltip = "Treshold value to detect trend direction")

use_treshold = input.bool(false, title="Use treshold", tooltip = "Use treshold for the confirmation")

// Calculate price filter
pf = (close - ta.sma(close, length)) / ta.stdev(close, length)

pf_color = pf > pf[1] ? color.green : color.red

// Initialize plot
f_zero = plot(0)
f_filter = plot(pf, linewidth=2, color = pf_color)
plot(threshold, style = plot.style_circles )
plot(-threshold, style = plot.style_circles )

color_fill = pf > 0 ? color.green : color.red
if use_treshold
    color_fill := pf > threshold ? color.green : pf < -threshold ? color.red : color.blue
fill(f_zero, f_filter, color_fill)