add ehlers super smoother

This commit is contained in:
Janne Sinivirta 2018-02-13 17:47:26 +02:00
parent cf013140a6
commit 178d1ed423
1 changed files with 18 additions and 0 deletions

View File

@ -1,3 +1,5 @@
from math import exp, pi, sqrt, cos
from pandas import Series
@ -7,3 +9,19 @@ def went_up(series: Series) -> Series:
def went_down(series: Series) -> Series:
return series < series.shift(1)
def ehlers_super_smoother(series: Series, smoothing: float = 6):
magic = pi * sqrt(2) / smoothing
a1 = exp(-magic)
coeff2 = 2 * a1 * cos(magic)
coeff3 = -a1 * a1
coeff1 = (1 - coeff2 - coeff3) / 2
filtered = series.copy()
for i in range(2, len(series)):
filtered.iloc[i] = coeff1 * (series.iloc[i] + series.iloc[i-1]) + \
coeff2 * filtered.iloc[i-1] + coeff3 * filtered.iloc[i-2]
return filtered