mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 05:28:05 +00:00
pine files
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// The MIT License (MIT)
|
||||
// © mihakralj
|
||||
//@version=6
|
||||
indicator("Bilateral Filter (BILATERAL)", "BILATERAL", overlay=true)
|
||||
|
||||
//@function Calculates Bilateral Filter
|
||||
//@doc https://github.com/mihakralj/pinescript/blob/main/indicators/filters/bilateral.md
|
||||
//@param src Series to calculate Bilateral Filter from
|
||||
//@param length Number of bars used in the calculation (spatial domain)
|
||||
//@param sigma_s_ratio Ratio to determine spatial standard deviation
|
||||
//@param sigma_r_mult Multiplier for range standard deviation
|
||||
//@returns Bilateral Filter value
|
||||
//@optimized Uses edge-preserving bilateral smoothing with O(n) complexity per bar
|
||||
bilateral(series float src, simple int length, simple float sigma_s_ratio, simple float sigma_r_mult) =>
|
||||
float sigma_s = math.max(length * sigma_s_ratio, 1e-10)
|
||||
float sigma_r = math.max(ta.stdev(src, length) * sigma_r_mult, 1e-10)
|
||||
float sum_weights = 0.0
|
||||
float sum_weighted_src = 0.0
|
||||
float center_val = nz(src[0], src[1])
|
||||
for i = 0 to length - 1
|
||||
float val = nz(src[i], center_val)
|
||||
float diff_spatial = float(i)
|
||||
float diff_range = center_val - val
|
||||
float weight_spatial = math.exp(-(diff_spatial * diff_spatial) / (2.0 * sigma_s * sigma_s))
|
||||
float weight_range = math.exp(-(diff_range * diff_range) / (2.0 * sigma_r * sigma_r))
|
||||
float weight = weight_spatial * weight_range
|
||||
sum_weights += weight
|
||||
sum_weighted_src += weight * val
|
||||
float result = sum_weights == 0.0 ? center_val : sum_weighted_src / sum_weights
|
||||
result
|
||||
|
||||
// ---------- Main loop ----------
|
||||
|
||||
// Inputs
|
||||
i_length = input.int(20, "Length", minval=2)
|
||||
i_sigma_s_ratio = input.float(0.5, "Spatial Sigma Ratio", minval=0.01, step=0.05)
|
||||
i_sigma_r_mult = input.float(1.0, "Range Sigma Multiplier", minval=0.01, step=0.1)
|
||||
i_source = input.source(close, "Source")
|
||||
|
||||
// Calculation
|
||||
filtered_value = bilateral(i_source, i_length, i_sigma_s_ratio, i_sigma_r_mult)
|
||||
|
||||
// Plot
|
||||
plot(filtered_value, "Bilateral", color=color.yellow, linewidth=2)
|
||||
@@ -0,0 +1,47 @@
|
||||
// The MIT License (MIT)
|
||||
// © mihakralj
|
||||
//@version=6
|
||||
indicator("Butterworth 2nd Order Filter (BUTTER)", "BUTTER", overlay=true)
|
||||
|
||||
//@function Calculates 2nd Order Butterworth Lowpass Filter
|
||||
//@doc https://github.com/mihakralj/pinescript/blob/main/indicators/filters/butter.md
|
||||
//@param src Series to calculate Butterworth filter from
|
||||
//@param length Cutoff period (related to -3dB frequency)
|
||||
//@returns Butterworth filter value
|
||||
//@optimized Uses IIR 2nd order Butterworth filter with O(1) complexity per bar
|
||||
butter(series float src, simple int length) =>
|
||||
float pi = math.pi
|
||||
int safe_length = math.max(length, 2)
|
||||
float omega = 2.0 * pi / safe_length
|
||||
float sin_omega = math.sin(omega)
|
||||
float cos_omega = math.cos(omega)
|
||||
float alpha = sin_omega / math.sqrt(2.0)
|
||||
float a0 = 1.0 + alpha
|
||||
float a1 = -2.0 * cos_omega
|
||||
float a2 = 1.0 - alpha
|
||||
float b0 = (1.0 - cos_omega) / 2.0
|
||||
float b1 = 1.0 - cos_omega
|
||||
float b2 = (1.0 - cos_omega) / 2.0
|
||||
var float filt = na
|
||||
if bar_index < 2
|
||||
filt := nz(src, 0.0)
|
||||
else
|
||||
float ssrc = nz(src, src[1])
|
||||
float src1 = nz(src[1], ssrc)
|
||||
float src2 = nz(src[2], src1)
|
||||
float filt1 = nz(filt[1], ssrc)
|
||||
float filt2 = nz(filt[2], filt1)
|
||||
filt := (b0 * ssrc + b1 * src1 + b2 * src2 - a1 * filt1 - a2 * filt2) / a0
|
||||
filt
|
||||
|
||||
// ---------- Main loop ----------
|
||||
|
||||
// Inputs
|
||||
i_length = input.int(20, "Length", minval=2)
|
||||
i_source = input.source(close, "Source")
|
||||
|
||||
// Calculation
|
||||
butter_val = butter(i_source, i_length)
|
||||
|
||||
// Plot
|
||||
plot(butter_val, "Butterworth", color=color.yellow, linewidth=2)
|
||||
@@ -0,0 +1,43 @@
|
||||
// The MIT License (MIT)
|
||||
// © mihakralj
|
||||
// Indicator algorithm (C) 2004-2024 John F. Ehlers
|
||||
//@version=6
|
||||
indicator("Supersmooth Filter (SSF)", "SSF", overlay=true)
|
||||
|
||||
//@function Calculates Supersmooth Lowpass Filter
|
||||
//@doc https://github.com/mihakralj/pinescript/blob/main/indicators/filters/ssf.md
|
||||
//@param source Series to calculate SSF from
|
||||
//@param length Number of bars used in the calculation
|
||||
//@returns SSF value with optimized smoothing
|
||||
//@optimized Uses 2-pole IIR Butterworth-style filter with O(1) complexity per bar
|
||||
ssf(series float src, simple int length) =>
|
||||
var float SQRT2_PI = math.sqrt(2.0) * math.pi
|
||||
var float ssf_internal = 0.0
|
||||
var float c1 = 0.0
|
||||
var float c2 = 0.0
|
||||
var float c3 = 0.0
|
||||
var int prev_length = 0
|
||||
if prev_length != length
|
||||
float arg = SQRT2_PI / float(length)
|
||||
float exp_arg = math.exp(-arg)
|
||||
c2 := 2.0 * exp_arg * math.cos(arg)
|
||||
c3 := -exp_arg * exp_arg
|
||||
c1 := 1.0 - c2 - c3
|
||||
prev_length := length
|
||||
float ssrc = nz(src, src[1])
|
||||
float src1 = nz(src[1], ssrc)
|
||||
float src2 = nz(src[2], src1)
|
||||
ssf_internal := c1 * ssrc + c2 * nz(ssf_internal[1], src1) + c3 * nz(ssf_internal[2], src2)
|
||||
ssf_internal
|
||||
|
||||
// ---------- Main loop ----------
|
||||
|
||||
// Inputs
|
||||
i_length = input.int(20, "Length", minval=1)
|
||||
i_source = input.source(close, "Source")
|
||||
|
||||
// Calculation
|
||||
ssf_val = ssf(i_source, i_length)
|
||||
|
||||
// Plot
|
||||
plot(ssf_val, "SSF", color=color.yellow, linewidth=2)
|
||||
@@ -0,0 +1,44 @@
|
||||
// The MIT License (MIT)
|
||||
// © mihakralj
|
||||
//@version=6
|
||||
indicator("Ultrasmooth Filter (USF)", "USF", overlay=true)
|
||||
|
||||
//@function Calculates Ultrasmooth Filter
|
||||
//@doc https://github.com/mihakralj/pinescript/blob/main/indicators/filters/usf.md
|
||||
//@param src Series to calculate USF from
|
||||
//@param length Number of bars used in the calculation
|
||||
//@returns USF value with optimized smoothing
|
||||
//@optimized Uses 2-pole IIR filter with momentum enhancement, O(1) complexity per bar
|
||||
usf(series float src, simple int length) =>
|
||||
var float SQRT2_PI = math.sqrt(2.0) * math.pi
|
||||
var float usf_val = na
|
||||
var float c1 = 0.0
|
||||
var float c2 = 0.0
|
||||
var float c3 = 0.0
|
||||
var int prev_length = 0
|
||||
if prev_length != length
|
||||
float arg = SQRT2_PI / float(length)
|
||||
float exp_arg = math.exp(-arg)
|
||||
c2 := 2.0 * exp_arg * math.cos(arg)
|
||||
c3 := -exp_arg * exp_arg
|
||||
c1 := (1.0 + c2 - c3) / 4.0
|
||||
prev_length := length
|
||||
float ssrc = nz(src, src[1])
|
||||
float src1 = nz(src[1], ssrc)
|
||||
float src2 = nz(src[2], src1)
|
||||
float us1 = nz(usf_val[1], src1)
|
||||
float us2 = nz(usf_val[2], src2)
|
||||
usf_val := (1.0 - c1) * ssrc + (2.0 * c1 - c2) * src1 - (c1 + c3) * src2 + c2 * us1 + c3 * us2
|
||||
usf_val
|
||||
|
||||
// ---------- Main loop ----------
|
||||
|
||||
// Inputs
|
||||
i_length = input.int(20, "Length", minval=1)
|
||||
i_source = input.source(close, "Source")
|
||||
|
||||
// Calculation
|
||||
filt = usf(i_source, i_length)
|
||||
|
||||
// Plot
|
||||
plot(filt, "UltraSmooth", color=color.yellow, linewidth=2)
|
||||
Reference in New Issue
Block a user