-
Notifications
You must be signed in to change notification settings - Fork 2
/
technicalfilters.py
197 lines (151 loc) · 5.71 KB
/
technicalfilters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import numpy as np
from pyalgotrade import technical
from pyalgotrade import dataseries
from scipy import signal
class EMAEventWindow(technical.EventWindow):
def __init__(self, flen, alpha):
technical.EventWindow.__init__(self, flen)
self.__alpha = alpha
def EMAfilter(self, data, flen):
bcoeffs = []
for i in xrange(flen):
bcoeffs.append((1 - self.__alpha) ** i)
b = np.asarray(bcoeffs).astype(np.float64)
a = np.asarray([sum(bcoeffs)]).astype(np.float64)
out = signal.lfilter(b, a, data)
return out
def getValue(self):
ret = None
if self.windowFull():
these_vals = self.getValues()
filt = self.EMAfilter(these_vals, self.getWindowSize())
ret = filt[-1]
return ret
class EMA(technical.EventBasedFilter):
"""Exponential Moving Average filter.
"""
def __init__(self, dataSeries, flen, alpha, maxLen=dataseries.DEFAULT_MAX_LEN):
technical.EventBasedFilter.__init__(
self, dataSeries, EMAEventWindow(flen, alpha), maxLen)
class TRIXEventWindow(technical.EventWindow):
def __init__(self, flen, alphas):
technical.EventWindow.__init__(self, flen * 3)
self.__alpha1 = alphas[0]
self.__alpha2 = alphas[1]
self.__alpha3 = alphas[2]
self.__flen = flen
def EMAfilter(self, data, flen, alpha):
bcoeffs = []
for i in xrange(flen):
bcoeffs.append((1 - alpha) ** i)
b = np.asarray(bcoeffs).astype(np.float64)
a = np.asarray([sum(bcoeffs)]).astype(np.float64)
out = signal.lfilter(b, a, data)
return out
def getValue(self):
ret = None
if self.windowFull():
these_vals = self.getValues()
windowsize = self.getWindowSize()
filta = self.EMAfilter(these_vals, self.__flen, self.__alpha1)
filtb = self.EMAfilter(
filta, self.__flen, self.__alpha2)
filtc = self.EMAfilter(
filtb, self.__flen, self.__alpha3)
# print windowsize
# print these_vals
# print filta
# print filtb
# print filtc
ret = filtc[-1]
return ret
class TRIX(technical.EventBasedFilter):
"""Exponential Moving Average filter.
"""
def __init__(self, dataSeries, flen, alphas, maxLen=dataseries.DEFAULT_MAX_LEN):
technical.EventBasedFilter.__init__(
self, dataSeries, TRIXEventWindow(flen, alphas), maxLen)
class DerivativeEventWindow(technical.EventWindow):
def __init__(self):
technical.EventWindow.__init__(self, 2)
def Derivativefilter(self, data):
bcoeffs = [1, -1]
b = np.asarray(bcoeffs).astype(np.float64)
a = np.asarray([1]).astype(np.float64)
out = signal.lfilter(b, a, data)
return out
def getValue(self):
ret = None
if self.windowFull():
these_vals = self.getValues()
filt = self.Derivativefilter(these_vals)
ret = filt[-1]
return ret
class Derivative(technical.EventBasedFilter):
"""Exponential Moving Average filter.
"""
def __init__(self, dataSeries, maxLen=dataseries.DEFAULT_MAX_LEN):
technical.EventBasedFilter.__init__(
self, dataSeries, DerivativeEventWindow(), maxLen)
class ZeroSeriesEventWindow(technical.EventWindow):
def __init__(self):
technical.EventWindow.__init__(self, 2)
def ZeroSeriesfilter(self, data):
bcoeffs = [0]
b = np.asarray(bcoeffs).astype(np.float64)
a = np.asarray([1]).astype(np.float64)
out = signal.lfilter(b, a, data)
return out
def onNewValue(self, dateTime, value):
technical.EventWindow.onNewValue(self, dateTime, value)
def getValue(self):
ret = None
if self.windowFull():
these_vals = self.getValues()
filt = self.ZeroSeriesfilter(these_vals)
ret = filt[-1]
return ret
class ZeroSeries(technical.EventBasedFilter):
"""Exponential Moving Average filter.
"""
def __init__(self, dataSeries, maxLen=dataseries.DEFAULT_MAX_LEN):
technical.EventBasedFilter.__init__(
self, dataSeries, ZeroSeriesEventWindow(), maxLen)
class HullEventWindow(technical.EventWindow):
def __init__(self, flen, alphas):
technical.EventWindow.__init__(self, flen * 5)
self.__alpha1 = alphas[0]
self.__alpha2 = alphas[1]
self.__alpha3 = alphas[2]
self.__flen = flen
def EMAfilter(self, data, flen, alpha):
bcoeffs = []
for i in xrange(flen):
bcoeffs.append((1 - alpha) ** i)
b = np.asarray(bcoeffs).astype(np.float64)
a = np.asarray([sum(bcoeffs)]).astype(np.float64)
out = signal.lfilter(b, a, data)
return out
def getValue(self):
ret = None
if self.windowFull():
these_vals = self.getValues()
windowsize = self.getWindowSize()
filta = self.EMAfilter(these_vals, self.__flen, self.__alpha1)
filtb = self.EMAfilter(
filta, self.__flen, self.__alpha2)
filtc = self.EMAfilter(
filtb, self.__flen, self.__alpha3)
# print windowsize
# print these_vals
# print filta
# print filtb
# print filtc
ret = filtc[-1]
return ret
class Hull(technical.EventBasedFilter):
"""Exponential Moving Average filter.
"""
def __init__(self, dataSeries, flen, alphas, maxLen=dataseries.DEFAULT_MAX_LEN):
technical.EventBasedFilter.__init__(
self, dataSeries, HullEventWindow(flen, alphas), maxLen)