-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataselect.py
436 lines (337 loc) · 13.5 KB
/
dataselect.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import parameters as para
#Format of group:
# (startindex, endindex, data, index) #bitmap deprecated
""" REGION: CONFIGURATION - START """
# Change the default configuration here. (choice of criteria from those available below in criteriaOptions)
defaultChoice = 2
# The different critera that can be used for dataselect are defined here.
def configureCriteriaOptions():
def configure(criteriaFun, criteriaType, singleTS): return (criteriaFun, criteriaType, singleTS)
global criteriaOptions
# criteriaFun: The function used to filter out groups that match a criteria
# criteriaType: The type of function used. Either "byPoints" or "byIndividual"
# singleTS: True if the function operates on data['Close'] only. False if it operates on everything in data.
criteriaOptions = {
# criteria 0: break five-years-high
0: configure (
criteriaFun = breakHigh(yearsToDays(1), yearsToDays(5)),
criteriaType = byPoints,
singleTS = True,
),
# criteria 1: Double Tops
1: configure (
criteriaFun = compose(getEndPoints, findDoubleTops),
criteriaType = byPoints,
singleTS = False,
),
# criteria 2: Double Tops Filtered
2: configure (
criteriaFun = compose(getEndPoints, findDoubleTopsFiltered),
criteriaType = byPoints,
singleTS = False,
),
# criteria 3: Random select
3: configure (
criteriaFun = randomMatch(0.003),
criteriaType = byIndividual,
singleTS = True,
),
}
""" REGION: CONFIGURATION - END """
""" REGION: MAIN CONTROLS - START """
# This function is the main function to call in dataselect.py
# choice is numeric (which configuration to use)
def findMatches(data, groups, choice = None):
if choice == None: choice = defaultChoice
criteriaFun, criteriaType, singleTS = criteriaOptions[choice]
if singleTS: data = data['Close']
return criteriaType(data, groups, criteriaFun)
# An alternative function to call in dataselect.py.
# Allows you to specify the criteriaType and criteriaFun manually instead of
# using one of the existing configurations.
def findMatchesWith(data, groups, criteriaType, criteriaFun):
return criteriaType(data, groups, criteriaFun)
# One of the criteria types.
# Used for individual criteria.
def byIndividual(fulldata, groups, criteria):
def fun(group):
return criteria(fulldata, group[0], group[1])
return list(filter(fun, groups))
# One of the criteria types.
# Used for points criteria.
# Assumes groups are sorted in chronological order (increasing)
def byPoints(fulldata, groups, criteria):
markedPoints = criteria(fulldata)
selectedGroups = []
for point in markedPoints:
group = findFirstGroupContainingPoint(groups, point)
if group != None: # rare case. point is outside of all groups.
selectedGroups.append(group)
return selectedGroups
""" REGION: MAIN CONTROLS - END """
""" REGION: INDIVIDUAL CRITERION - START """
# Format of an individual criteria (byIndividual) function:
# function(fulldata, startIndex, endIndex)
# where startIndex, endIndex refers to the start, end points of the group.
# Returns: True if the group is to be selected, False if the group is to be omitted.
def randomMatch(probability):
import random
def criteria(fulldata, startIndex, endIndex):
return random.random() < probability
return criteria
def containsPoints(markedPoints):
def fun(fulldata, startIndex, endIndex):
for point in markedPoints:
if startIndex <= point and point < endIndex:
return True
return False
return fun
""" REGION: INDIVIDUAL CRITERION - END """
""" REGION: POINTS CRITERION - START """
# Format of a points criteria (byPoints) function:
# function(fulldata)
# Returns: A set of points (by index) in the data which match the criteria.
# Later on, we use these points to select groups by identifying the first group that contains each point.
def randomPoints(frac):
import random
def criteria(fulldata):
return list(filter(lambda v : random.random() < frac, range(0,len(fulldata))))
return criteria
def breakHigh(minDays, maxDays):
def criteria(fulldata):
if len(fulldata) < maxDays:
return []
markedPoints = []
maxIndexList = generateMaxIndexList(fulldata, maxDays)
preMaxIndexList = generateMaxIndexList(fulldata, maxDays-1)
maxMinusMin = maxDays-minDays
for i in range(0,len(maxIndexList)):
point = i+maxDays-1
if maxIndexList[i] == point and preMaxIndexList[i] < i+maxMinusMin:
markedPoints.append(point)
return markedPoints
return criteria
""" REGION: POINTS CRITERION - END """
""" REGION: INTERVAL FILTERS - START """
def getEndPoints(intervals):
return list(map(lambda v : v[1], intervals))
def increasingAveragesFilter(dataList):
avg10 = para.averageLastList(dataList, 10)
avg30 = para.averageLastList(dataList, 30)
avg60 = para.averageLastList(dataList, 60)
limit = 60
def fun(interval):
point = interval[0]
if point < limit: return False
return avg10[point] > avg30[point] and avg30[point] > avg60[point]
return lambda intervals : filter(fun, intervals)
""" REGION: INTERVAL FILTERS - END """
""" REGION: UTILITY - START """
# Compose multiple functions into one function
def compose(*funs):
# reverse the list.
funs = funs[::-1]
def composed(x):
for fun in funs:
x = fun(x)
return x
return composed
# A function used to convert data -> data[dataType]. Useful to compose with other functions.
def using(dataType):
def fun(data):
return data[dataType]
return fun
def findFirstGroupContainingPoint(groups, point):
for group in groups: # group[0] = startIndex, group[1] = endIndex
if group[0] <= point and point < group[1]:
return group
return None
# An approximate conversion for the number of working days in a year.
def yearsToDays(years):
return years*250 #250 working days in a year.
# A utility function to efficiently compute the maximum values of a sliding window on an array.
# Given an array/list arr, and a window of size wSize,
# |------[-------]--------|
# '-max-'
# For all possible window positions, from 0 to len(data) - wSize,
# Return the index of the maximum value of arr[] in that window.
# currently assuming all values of arr are positive. (so that -1 works.)
# O(n) algorithm.
def generateMaxIndexList(arr, wSize):
resultList = []
maxIndexList = [-1]*len(arr)
unprocessedPoint = wSize
unprocessedMax = -1
maxIndexList[wSize-1] = wSize-1
nextMaxPoint = -1
for i in range(wSize-2, -1, -1):
if arr[i] >= arr[maxIndexList[i+1]]:
maxIndexList[i] = i
else:
maxIndexList[i] = maxIndexList[i+1]
resultList.append(maxIndexList[0])
for i in range(1,len(arr)-wSize+1):
if i > nextMaxPoint:
nextMaxPoint = maxIndexList[i]
unprocessedMax = max(unprocessedMax, arr[i+wSize-1])
if nextMaxPoint == -1 or arr[nextMaxPoint] < unprocessedMax:
maxIndexList[i+wSize-1] = i+wSize-1
for j in range(i+wSize-2, unprocessedPoint-1, -1):
if arr[j] >= arr[maxIndexList[j+1]]:
maxIndexList[j] = j
else:
maxIndexList[j] = maxIndexList[j+1]
nextMaxPoint = maxIndexList[unprocessedPoint]
unprocessedPoint = i+wSize
unprocessedMax = -1
resultList.append(nextMaxPoint)
return resultList
# Opposite of generateMaxIndexList above. Also O(n).
def generateMinIndexList(arr, wSize):
maxValue = max(arr)
negList = list(map(lambda x : maxValue - x, arr))
return generateMaxIndexList(negList, wSize)
# Helper function for finding Double Tops
# returns a function that can detects whether an index i is a peak value.
def peakIdentifierFun(dataList, peakGap):
windowMax = generateMaxIndexList(dataList, peakGap*2)
def isPeak(i):
return i >= peakGap and i + peakGap < len(dataList) and \
dataList[i] >= dataList[windowMax[i-peakGap]]
return isPeak
# Helper function for finding Double Tops (for drawing the peaks)
# converts a boolean function to a functoin that returns high for True, low for False.
def boolToIntFun(boolFun, low, high):
def intFun(x):
if boolFun(x):
return high
return low
return intFun
# Double Tops Filtered: Finds points which are double tops, which satisfy the "increasing averages" condition.
def findDoubleTopsFiltered(data):
intervals = findDoubleTops(data)
return increasingAveragesFilter(data['Close'])(intervals)
def plotDoubleTopsFiltered(data, plotGraphs = False, plotPeaks = False, start = None, end = None):
if start == None: start = 0
if end == None: end = len(data['High'])
intervals = findDoubleTops(data, start=start, end=end)
intervals = increasingAveragesFilter(data['Close'])(intervals)
boxlow = 0
boxhigh = 100
if plotGraphs:
import matplotlib.pyplot as plt
def plotBox(i,j):
record = [boxlow]*(end-start)
for k in range(i,j+1):
record[k-start] = boxhigh
plt.plot(record)
for interval in intervals:
plotBox(interval[0], interval[1])
findDoubleTops(data, plotGraphs, plotPeaks, start=start, end=end)
# Find Double Tops criteria function. Also can be used to plot the double tops by setting the optional arguments.
# start / end = None means default values.
def findDoubleTops(data, plotGraphs = False, plotPeaks = False, start = None, end = None):
dates = data['Date']
high, low, close = data['High'], data['Low'], data['Close']
if not plotGraphs: plotPeaks = False
if plotGraphs: import matplotlib.pyplot as plt
if start == None: start = 0
if end == None: end = len(high)
peakGap = 5
trenchValueTolerance = 0.85
toleranceDown = 0.95
toleranceUp = 1.05
if start+(peakGap*2) >= end: return []
graphBaseValue = min(high[start:end])
graphPeakValue = max(high[start:end])
# define functions
isPeak = peakIdentifierFun(high, peakGap)
if plotPeaks:
isPeakInt = boolToIntFun(isPeak, graphBaseValue, graphPeakValue)
intervals = []
def addInterval(a, b):
for interval in intervals:
if abs(interval[0] - a) <= 2 and abs(interval[1] - b) <= 2:
return
intervals.append((a,b))
if plotGraphs:
def plotBox(i,j, value):
record = [graphBaseValue]*(end-start)
for k in range(i,j+1):
record[k-start] = value
plt.plot(record)
peaks = list(filter(isPeak, range(start,end)))
for i in range(0,len(peaks)):
curr = peaks[i]
currValue = high[curr]
for j in range(i+1,len(peaks)):
next = peaks[j]
nextValue = high[next]
ratio = nextValue/currValue
if ratio >= toleranceUp:
break
if ratio <= toleranceDown:
continue
# toleranceDown < ratio < toleranceUp
if curr+1 >= next:
continue
if min(high[curr+1:next]) >= toleranceDown*min(currValue, nextValue):
continue
trenchValue = min(low[curr+1:next])
peakValue = max(currValue, nextValue)
leftTail = -1
rightTail = -1
# scan right.
for k in range(next+1,end):
if high[k] > peakValue:
break
if low[k] < trenchValueTolerance*trenchValue:
rightTail = k
break
if rightTail == -1:
continue
# scan left.
for k in range(curr-1,start-1,-1):
if high[k] > peakValue:
break
if low[k] < trenchValueTolerance*trenchValue:
leftTail = k
break
if leftTail == -1:
continue
if plotGraphs:
plotBox(curr,next, high[curr])
plotBox(curr,next, high[next])
plotBox(leftTail,rightTail-1, trenchValue)
addInterval(leftTail,rightTail)
if plotPeaks:
peaks = list(map(isPeakInt, range(start,end)))
plt.plot(peaks)
if plotGraphs:
plt.plot(high[start:end])
plt.show()
return intervals
""" REGION: UTILITY - END """
""" REGION: INITIALISATION - START """
configureCriteriaOptions()
""" REGION: INITIALISATION - END """
def main():
#data, headers = para.readFile('data_/AKAMAI_TECHNOLOGIES_INC.csv')
data, headers = para.readFile('data_334111/FUSION_I_O_INC.csv')
start = None
end = None
plotDoubleTopsFiltered(data, True, False, start=start, end=end)
""" REGION: CONDITION DEFINITIONS - START """
# These are conditions used by grouping.redefineGroupingConditions (in grouping.py)
conditionBreakHigh = (byPoints,
compose(breakHigh(yearsToDays(1), yearsToDays(5)), using('Close'))
)
conditionDoubleTops = (byPoints,
compose(getEndPoints, findDoubleTops)
)
conditionDoubleTopsFiltered = (byPoints,
compose(getEndPoints, findDoubleTopsFiltered),
)
""" REGION: CONDITION DEFINITIONS - START """
if __name__ == '__main__':
main()