Turning Point Oscillator
def create_tposcillator(s, peaks, troughs):
tps = sorted(peaks + troughs)
TPOscillator = []
index = []
for i in range(len(s)):
if i < min(min(peaks), min(troughs)) or i > max(max(peaks), max(troughs)):
TPOsc = 999 # TPOsc undefined
elif i in troughs:
TPOsc = 0.
elif i in peaks:
TPOsc = 1.
else:
idx_left = max([idx for idx in tps if idx < i])
idx_right = min([idx for idx in tps if idx > i])
if idx_left in peaks:
Pt = s[idx_left]
Tt = s[idx_right]
else :
Tt = s[idx_left]
Pt = s[idx_right]
TPOsc = (s[i] - Tt) / (Pt - Tt)
if TPOsc < 0. or TPOsc > 1.:
print (s.index[i],'TPOsc=',TPOsc,'s[i]=',s[i],'idx_left=',idx_left, 'idx_right=',idx_right, 'Pt=',Pt, 'Tt=',Tt)
if TPOsc != 999:
#print (i, TPOsc)
TPOscillator.append(TPOsc)
index.append(s.index[i])
return pd.Series(TPOscillator, index=index)
Definition 4: Turning point oscillator(TPOscillator) Let $X$ be a price sequence and let $A(X)$ be its alternating pivots sequence(that is, $A(x)$ is the list of turning point times in $X$). The TPOscillator is a mapping $Γ: N → [0, 1]$, such that: $Γ(t)= 0$ if $t$ is a trough, $1$ if it is a peak and $x_t − P_t / P_t − T_t$ otherwise, where $P_t$ and $T_t$ are the values of the timeseries at the nearest peak and trough located on opposite sides of time $t$.