Ehlers 3-Pole Super Smoother Filter (SSF3)

mihakralj · study · 50 行 · 点赞 11 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
// Indicator algorithm (C) 2004-2024 John F. Ehlers
indicator("Ehlers 3-Pole Super Smoother Filter (SSF3)", "SSF3", overlay=true)
 
//@function Calculates 3-Pole Super Smoother Filter
//@param source Series to calculate SSF3 from
//@param length Number of bars used in the calculation
//@returns SSF3 value with optimized 3-pole smoothing
//@optimized Uses 3-pole IIR filter with O(1) complexity per bar
ssf3(series float src, simple int length) =>
    var float SQRT3_PI = math.sqrt(3.0) * math.pi
    var float ssf3_internal = 0.0
    var float coef1 = 0.0
    var float coef2 = 0.0
    var float coef3 = 0.0
    var float coef4 = 0.0
    var int prev_length = 0
    if prev_length != length
        int p = math.max(1, length)
        float a1 = math.exp(-math.pi / p)
        float b1 = 2.0 * a1 * math.cos(SQRT3_PI / p)
        float c1 = a1 * a1
        coef2 := b1 + c1
        coef3 := -(c1 + b1 * c1)
        coef4 := c1 * c1
        coef1 := 1.0 - coef2 - coef3 - coef4
        prev_length := p
    float ssrc = nz(src, src[1])
    float src1 = nz(src[1], ssrc)
    float src2 = nz(src[2], src1)
    float src3 = nz(src[3], src2)
    float filt1 = nz(ssf3_internal[1], src1)
    float filt2 = nz(ssf3_internal[2], src2)
    float filt3 = nz(ssf3_internal[3], src3)
    ssf3_internal := coef1 * ssrc + coef2 * filt1 + coef3 * filt2 + coef4 * filt3
    ssf3_internal
 
// ---------- Main loop ----------
 
// Inputs
i_length = input.int(20, "Length", minval=1)
i_source = input.source(close, "Source")
 
// Calculation
ssf3_val = ssf3(i_source, i_length)
 
// Plot
plot(ssf3_val, "SSF3", color=color.yellow, linewidth=2)

← 返回列表