def compareAlgorithmsWithData(testCases): global algosToTest, data, headers, testOutputFilename allResults = {} for key in algosToTest.keys(): allResults[key] = [] for companyName in sorted(testCases.keys()): print('Testing ' + companyName) data, headers = para.readFile(display.nameToFile(companyName)) for key in sorted(algosToTest.keys()): #print(' algo ' + key) for target in testCases[companyName]: algo = algosToTest[key] result = testAlgo(algo, target) if result != None: allResults[key].append(result) #printResult(key, result) averageResults = {} f = open(testOutputFilename, 'w+') for key in allResults.keys(): averageResult = computeAverageResult(allResults[key]) s = formatResult(key, averageResult) f.write(s+'\n') f.close()
def verify(fileName, todayIndex): tomorrowIndex = todayIndex + 1 global groupSize, predictSize, config if outputCharts == True: chart = chartprinter.new(fileName, tomorrowIndex, config.algo, groupSize, predictSize) else: chart = None data, headers = para.readFile(fileName) if tomorrowIndex - groupSize < 0: return None if tomorrowIndex + predictSize > len(data['Close']): return None getKnownData = getKnownFun(tomorrowIndex) getFutureData = getFutureFun(tomorrowIndex, predictSize) knownData = {} futureData = {} for key in data: knownData[key] = getKnownData(data[key]) futureData[key] = getFutureData(data[key]) defaultEarnings = getDefaultEarnings(futureData) strategy = decideStrategy(knownData, groupSize, predictSize, chart) if strategy == None: return (1, False, defaultEarnings) #dontTrade result, traded = applyStrategy(strategy, futureData) return (result, traded, defaultEarnings)
def testCubicFit(): import matplotlib.pyplot as plt data, headers = para.readFile('data_/3_D_SYSTEMS_CORP_DEL.csv') dates = data['Date'] close = data['Close'] start = 390 end = 450 dataList = para.averageLast(5)([close], len(close)) #dataList = close linf = linearInterpolate(dataList[start:end]) cubf = cubicSpline(dataList[start:end]) x = list(map(lambda x : x/10, range(0,9*(end-start)))) linear = list(map(linf, x)) cubic = list(map(cubf, x)) coeffs = fitPoly(3)(cubic, 0, len(cubic)) p = polynomialFunction(coeffs) estimate = list(map(p,x)) plt.plot(x, linear) plt.plot(x, cubic) plt.plot(x, estimate) plt.show()
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)
def findDoubleTops(): dataFiles = util.listDataFiles() for f in dataFiles: data, headers = para.readFile(f) intervals = dataselect.findDoubleTops(data) if (len(intervals) > 0): print(util.getNameOnly(f)) print(intervals)
def main(): dataFiles = util.listDataFiles() results = [] for dataFile in dataFiles: print('Reading ' + dataFile) data, headers = para.readFile(dataFile) datalist = data['Close'] results += computeResults(datalist) import statistics print('Results: ' + str(len(results))) print(statistics.mean(results)) print(statistics.stdev(results))
def compareAlgorithms(fileName): global data, headers data, headers = para.readFile(fileName) if (len(data['Date']) == 0): print('Empty File') return global algosToTest for key in algosToTest.keys(): print('Testing ' + key) algo = algosToTest[key] result = testAlgo(algo, 404) printResult(key, result)
def runVisualise(): fileName = 'data_/AMERICAN_MANAGEMENT_SYSTEMS_INC.csv' #fileName = 'table.csv' targetGroup = 89 #global data, headers data, headers = para.readFile(fileName) dates = data['Date'] close = data['Close'] groups = grouping.groupUp(data, data['Close']) target = targetGroup targetNext = target+const.ma results = testalgos.compareAllGroupsBefore(groups, target) results2 = testalgos.compareAllGroupsBefore(groups, targetNext) #results2 = compareAllGroupsTo(groups, targetNext) results.reverse() results.sort(key=lambda x : x[2]) results2.sort(key=lambda x : x[2]) print('Target ' + str(target)) display.plotgroup(data, groups[target]) plt.show() for v in results[0:10]: print(v) totalRank = 0 ranks = [] for v in results[0:10]: rank = testalgos.getRank(results2, v[0]+const.ma) totalRank += rank ranks.append(rank) print('Total Rank = ' + str(totalRank)) print(ranks) display.plotAverageHighLow(groups, results[0:10], groups[targetNext][2]) return for v in results[0:10]: print() print('Chosen group:') print('Number ' + str(v[0]) + ', SimilarityScore = ' + str(v[2])) print('Matching Group:') print('Number ' + str(v[0]+const.ma) + ', Rank = ' + str(testalgos.getRank(results2, v[0]+const.ma))) print('Plotting Target Next Group (blue)') display.plotnormalizedWith(data, groups[targetNext], [similarity.byFirst]) print('Plotting Predicted Next Group (green)') display.plotnormalizedWith(data, groups[v[0]+const.ma], [similarity.byFirst]) plt.show()
def randomTestOnFile(fileName): import random global predictSize data, headers = para.readFile(fileName) length = len(data['Close']) - predictSize - 1 cases = filter(lambda v : random.random() < 0.05, range(0,length)) results = [] for i in cases: result = verify(fileName, i) if result != None: results.append(result) return results
def viewGraph(): fileName = 'data_/F_5_NETWORKS_INC.csv' start, end = 200,1000 data, headers = para.readFile(fileName) dates = data['Date'] #lines = ['High', 'Low', 'Close', 'Open'] lines = ['Running', 'DiffClose', 'DiffCloseSign'] for line in lines: #plt.plot(data[line][start:end]) plt.plot(similarity.byMean(data[line][start:end])) plt.show()
def runDataSelectOn(fileName): data, headers = para.readFile(fileName) dates = data['Date'] if (len(dates) == 0): #print('Empty File') return groups = grouping.groupUp(data, data['Close']) matches = dataselect.findMatches(data, groups) #print('Found ' + str(len(matches)) + 'matches') if (len(matches) <= 0): return #print only when there is at least one match. print(util.getNameOnly(fileName)) for group in matches: display.printgroupattrs(group, dates)
def predict(): global groupSize, predictSize, config fileName = 'chinasp.csv' todayIndex = 6230 chart = chartprinter.new(fileName, todayIndex, config.algo, predictSize) data, headers = para.readFile(fileName) getKnownData = getKnownFun(todayIndex) knownData = {} for key in data: knownData[key] = getKnownData(data[key]) strategy = decideStrategy(knownData, groupsize, predictSize, chart) if strategy == None: print("don't trade")
def testAlgorithmsForAverageScore(testCases, weightDict): allResults = [] weightDataFun = weightedData(weightDict) for companyName in testCases.keys(): #print('Testing ' + companyName) data, headers = para.readFile(display.nameToFile(companyName)) for key in algosToTest.keys(): #print(' algo ' + key) algo = algosToTest[key] for target in testCases[companyName]: result = testAlgoWeighted(algo, target, weightDataFun, data) if result != None: allResults.append(result) averageResult = testalgos.computeAverageResult(allResults) return averageResult[2] # Maximise the amount of return alone.
def selectiveTestOnFile(fileName): import random global predictSize data, headers = para.readFile(fileName) length = len(data['Close']) - predictSize - 1 cases = list(filter(lambda v : v < length, getDataselectPoints(data))) #cases = filter(lambda v : random.random() < 0.05, range(0,length)) #print('Cases: ' + str(cases)) results = [] for i in cases: result = verify(fileName, i) if result != None: results.append(result) return results
def bestPolyFit(): import matplotlib.pyplot as plt data, headers = para.readFile('data_/3_D_SYSTEMS_CORP_DEL.csv') dates = data['Date'] close = data['Close'] #groups = grouping.groupUp(data, close) start = 230 end = 379 #dataList = para.averageLast(5)([close], len(close)) dataList = close #group = groups[23] #coeffs = fitPoly(3)(dataList, group[0], group[1]) plt.plot(dataList[start:end]) bestRatio = 0 bestDegree = -1 lastValue = -1 savedPlot = None for i in range(2,20): coeffs = fitPoly(i)(dataList, start, end) p = polynomialFunction(coeffs) estimate = list(map(p, range(0,end-start))) score = similarity.lpNorms(2)(estimate, dataList[start:end]) print('Degree ' + str(i) + ' score = ' + str(score)) #plt.plot(estimate) if lastValue == -1: lastValue = score else: ratio = lastValue/score lastValue = score if ratio > bestRatio: savedPlot = estimate bestRatio = ratio bestDegree = i print('Best degree = ' + str(bestDegree) + ' with score = ' + str(bestRatio)) plt.plot(savedPlot) plt.show()
def testPolyFit(): import matplotlib.pyplot as plt data, headers = para.readFile('data_/A_C_I_WORLDWIDE_INC.csv') dates = data['Date'] close = data['Close'] #groups = grouping.groupUp(data, close) start = 80 end = 200 #dataList = para.averageLast(5)([close], len(close)) dataList = close #group = groups[23] #coeffs = fitPoly(3)(dataList, group[0], group[1]) plt.plot(dataList[start:end]) coeffs = fitPoly(4)(dataList, start, end) p = polynomialFunction(coeffs) estimate = list(map(p, range(0,end-start))) plt.plot(estimate) plt.show()
def verify(fileName, predictSize): data, headers = para.readFile(fileName) return computeResults(data['Close'], predictSize)
def translateDate(fileName, dayIndex): para.caching = True data, headers = para.readFile(fileName) return data['Date'][int(dayIndex)]