/
base_utils_bak.py
184 lines (139 loc) · 3.89 KB
/
base_utils_bak.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
import numpy as np
import validations
import math_func_utils as utils
class RollingList(object):
"""A List that has a maximum number of indexes.
Newest additions are added to the end of the list until list_limit value
is reached. At that point, the zero-index is removed as new values continue
to be added to the end.
"""
def __init__(self, input_data=None):
self._rollinglist = []
self._list_limit = 10
# Default setting only allows for numerical values.
self._only_numerical_values = True
if input_data:
self.Add(input_data)
def __len__(self):
return len(self._rollinglist)
@property
def get(self):
if len(self)>0:
return self._rollinglist[len(self)-1]
return None
@property
def limit(self):
return self._list_limit
@property
def list(self):
return self._rollinglist
def AllowAllValues(self):
self._only_numerical_values = False
def AllowOnlyNumbers(self, cleanse = False):
self._only_numerical_values = True
if cleanse:
for i, val in enumerate(self._rollinglist):
if not validations.isNumeric(val):
self._rollinglist.pop(i)
def SetLimit(self, val):
if validations.isNumeric(val):
self._list_limit = val
while len(self) > val:
self._rollinglist.pop(0)
def Delete(self):
self._rollinglist.pop(0)
delete = Delete
def Add(self, val):
if validations.isNumeric(val) and self._only_numerical_values:
if validations.isList(val):
for x in val:
self.Add(x)
else:
self._rollinglist.append(val)
if len(self) > self.limit:
self.Delete()
elif not self._only_numerical_values:
self._rollinglist.append(val)
add = Add
def Copy(self):
copy = []
for val in self._rollinglist:
copy.append(val)
return copy
copy = Copy
def Reset(self):
self.__init__()
class MovingAverage(object):
"""Moving Averages Constructor class"""
_DEFAULT_SCOPES = {
'9-period MA':9,
'20-period MA':20
}
def __init__(self):
self.scopes = self._DEFAULT_SCOPES
# Incoming raw Data
self.data = RollingList()
self.data.SetLimit(self.maxscope)
# Output
self.vals = RollingList()
self.vals._only_numerical_values = False
self.scopevalues = sorted([val for val in self.scopes.itervalues()])
def __len__(self):
return len(self.vals)
@property
def maxscope(self):
max_val = 0
for k, v in self.scopes.iteritems():
if v > max_val:
max_val = v
return max_val
@property
def get(self):
if len(self)>0:
return self.vals.list[len(self)-1]
return None
@property
def list(self):
return self.vals.list
def Reset(self, default_scopes=False):
if default_scopes:
self.__init__()
else:
# Incoming raw Data
self.data = RollingList()
self.data.SetLimit(self.maxscope)
# Output
self.vals = RollingList()
self.vals.data_validation_switch = False
def AddScope(self, label=None, value=None):
if label and validations.isNumeric(value):
self.scopes[label] = value
else:
pass # Error exception handling here
def DelScope(self, val):
if isinstance(val, str):
self.scopes.pop(val, None)
if validations.isNumeric(val):
for k, v in self.scopes.iteritems():
if val == v:
self.scopes.pop(k, None)
def Add(self, val):
if validations.isNumeric(val):
if validations.isList(val):
for x in val:
self.Add(x)
else:
self.data.add(val)
self._calcAverages()
add = Add
def _calcAverages(self):
temp_list = self.data.copy()
temp_list.reverse()
averages = []
for scope, val in self.scopes.iteritems():
if len(temp_list) >= val:
avg = np.mean(temp_list[:val])
else:
avg = 'NA'
averages.append((scope, val, avg))
self.vals.add(tuple(averages))