2018-02-13 15:47:26 +00:00
|
|
|
from math import exp, pi, sqrt, cos
|
|
|
|
|
2018-03-17 21:16:03 +00:00
|
|
|
import numpy as np
|
2018-02-14 08:17:43 +00:00
|
|
|
import talib as ta
|
2018-02-13 09:37:59 +00:00
|
|
|
from pandas import Series
|
|
|
|
|
|
|
|
|
2018-03-17 21:16:03 +00:00
|
|
|
def went_up(series: Series) -> bool:
|
2018-02-13 09:37:59 +00:00
|
|
|
return series > series.shift(1)
|
|
|
|
|
|
|
|
|
2018-03-17 21:16:03 +00:00
|
|
|
def went_down(series: Series) -> bool:
|
2018-02-13 09:37:59 +00:00
|
|
|
return series < series.shift(1)
|
2018-02-13 15:47:26 +00:00
|
|
|
|
|
|
|
|
2018-03-17 21:16:03 +00:00
|
|
|
def ehlers_super_smoother(series: Series, smoothing: float = 6) -> type(Series):
|
2018-02-13 15:47:26 +00:00
|
|
|
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
|
2018-02-14 08:17:43 +00:00
|
|
|
|
|
|
|
|
2018-03-17 21:16:03 +00:00
|
|
|
def fishers_inverse(series: Series, smoothing: float = 0) -> np.ndarray:
|
2018-02-14 08:17:43 +00:00
|
|
|
""" Does a smoothed fishers inverse transformation.
|
|
|
|
Can be used with any oscillator that goes from 0 to 100 like RSI or MFI """
|
|
|
|
v1 = 0.1 * (series - 50)
|
|
|
|
if smoothing > 0:
|
|
|
|
v2 = ta.WMA(v1.values, timeperiod=smoothing)
|
|
|
|
else:
|
|
|
|
v2 = v1
|
2018-03-17 21:16:03 +00:00
|
|
|
return (np.exp(2 * v2)-1) / (np.exp(2 * v2) + 1)
|