def findSol(self, timewindow): pyevolve.logEnable() genome = G1DList.G1DList(4) # range is constrained by the solutions found by MR genome.setParams(rangemin=-12.0, rangemax=2.0) # Change the initializator to Real Values genome.initializator.set(Initializators.G1DListInitializatorReal) # Change the mutator to Gaussian genome.mutator.set(Mutators.G1DListMutatorRealGaussian) # The evaluator function (objective function) genome.evaluator.set(self.eval_func(self.features)) genome.crossover.set(Crossovers.G1DListCrossoverTwoPoint) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) # Set the Roulette Wheel selector method, the number of generations and # the termination criteria ga.selector.set(Selectors.GRouletteWheel) # set default parameters for the engine ga.setGenerations(100) #ga.setPopulationSize(80) #ga.setMutationRate(0.2) #ga.setCrossoverRate(0.8) ga.setMinimax(Consts.minimaxType["minimize"]) ga.setElitism(True) ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria) # Sets the DB Adapter, the resetDB flag will make the Adapter recreate # the database and erase all data every run, you should use this flag # just in the first time, after the pyevolve.db was created, you can # omit it. #sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True) dbPath = createPath('pyevolve.db') sqlite_adapter = DBAdapters.DBSQLite(dbname=dbPath, identify="timewindow" + str(timewindow), resetIdentify=True, resetDB=False) ga.setDBAdapter(sqlite_adapter) # Do the evolution, with stats dump frequency of 20 generations ga.evolve(freq_stats=20) # Best individual best = ga.bestIndividual() stats = { 'constant': best[0], 'EMA': best[1], 'RSI': best[2], 'MACD': best[3] } return stats
def findSol(self, timewindow): pyevolve.logEnable() genome = G1DList.G1DList(4) # range is constrained by the solutions found by MR genome.setParams(rangemin=-12.0, rangemax=2.0) # Change the initializator to Real Values genome.initializator.set(Initializators.G1DListInitializatorReal) # Change the mutator to Gaussian genome.mutator.set(Mutators.G1DListMutatorRealGaussian) # The evaluator function (objective function) genome.evaluator.set(self.eval_func(self.features)) genome.crossover.set(Crossovers.G1DListCrossoverTwoPoint) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) # Set the Roulette Wheel selector method, the number of generations and # the termination criteria ga.selector.set(Selectors.GRouletteWheel) # set default parameters for the engine ga.setGenerations(100) #ga.setPopulationSize(80) #ga.setMutationRate(0.2) #ga.setCrossoverRate(0.8) ga.setMinimax(Consts.minimaxType["minimize"]) ga.setElitism(True) ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria) # Sets the DB Adapter, the resetDB flag will make the Adapter recreate # the database and erase all data every run, you should use this flag # just in the first time, after the pyevolve.db was created, you can # omit it. #sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True) dbPath = createPath('pyevolve.db') sqlite_adapter = DBAdapters.DBSQLite(dbname=dbPath, identify="timewindow" + str(timewindow), resetIdentify=True, resetDB=False) ga.setDBAdapter(sqlite_adapter) # Do the evolution, with stats dump frequency of 20 generations ga.evolve(freq_stats=20) # Best individual best = ga.bestIndividual() stats = {'constant': best[0], 'EMA': best[1], 'RSI': best[2], 'MACD': best[3] } return stats
def loadFile(filename, alpha): f = csv.reader(open(filename)) w = csv.writer(open('hTests.csv', 'w')) count = 0 w.writerow(['t', 'mr-EMA z', 'GA-MR z', 'criticalZ']) for line in f: if count > 0: t,formula,e1R2,emaR2,mrR2,e1AIC, emaAIC,mrAIC,e2R2, e2AIC = line path = createPath('Timewindow Features', 'timewindowFeatures' + str(t) + '.csv') n = file_len(path) z1 = ft.fisherTransformation(math.sqrt(float(mrR2)), math.sqrt(float(emaR2)), n, n) z1, criticalZ, hTest = ft.normHTest(z1, alpha) z2 = ft.fisherTransformation(math.sqrt(float(e1R2)), math.sqrt(float(mrR2)), n, n) z2, criticalZ, hTest = ft.normHTest(z2, alpha) w.writerow([t, z1, z2, criticalZ]) count += 1
def findSol(self, windowSize): y = np.array(self.prices) x = np.vstack([self.ema, self.rsi, self.macd]).T mymodel = ols.ols(y, x, 'price', ['EMA', 'RSI', 'MACD']) labels = ['constant', 'EMA', 'RSI', 'MACD'] equation = "price(t+1) = " stats = {} for i in range(len(mymodel.b)): l = labels[i] c = mymodel.b[i] stats[l] = c if l == 'constant': equation += str(c) + " + " else: equation += str(c) + "*" + l + "(t) + " equation = equation[:-2] return stats stocks = ['C', 'FDX', 'KO', 'MSFT', 'SBUX', 'NFLX', 'LUV'] for s in stocks: path = createPath(s + '/' + s + 'mrValidation.csv') featuresPath = createPath(s + '/') cv.crossValidationFeatures(featuresPath, path, MRFeatures())
#ga.setMutationRate(0.2) #ga.setCrossoverRate(0.8) ga.setMinimax(Consts.minimaxType["minimize"]) ga.setElitism(True) ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria) # Sets the DB Adapter, the resetDB flag will make the Adapter recreate # the database and erase all data every run, you should use this flag # just in the first time, after the pyevolve.db was created, you can # omit it. #sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True) dbPath = createPath('pyevolve.db') sqlite_adapter = DBAdapters.DBSQLite(dbname=dbPath, identify="timewindow" + str(timewindow), resetIdentify=True, resetDB=False) ga.setDBAdapter(sqlite_adapter) # Do the evolution, with stats dump frequency of 20 generations ga.evolve(freq_stats=20) # Best individual best = ga.bestIndividual() stats = {'constant': best[0], 'EMA': best[1], 'RSI': best[2], 'MACD': best[3] } return stats filename = createPath('gaValidation.csv') timeseries = loadStock('KO') cv.crossValidationFeatures(filename, GAForecaster())
prices, ema, rsi, macd = self.tf.getTimewindow(windowSize) y = np.array(prices) x = np.vstack([ema, rsi, macd]).T mymodel = ols.ols(y,x,'price',['EMA','RSI','MACD']) labels = ['constant', 'EMA','RSI','MACD'] equation = "price(t+1) = " stats = {} for i in range(len(mymodel.b)): l = labels[i] c = mymodel.b[i] stats[l] = c if l == 'constant': equation += str(c) + " + " else: equation += str(c) + "*" + l + "(t) + " equation = equation[:-2] return stats stocks = ['NFLX', 'LUV'] for s in stocks: timeseries = loadStock(s + '/' + s) path = createPath(s + '/' + s + 'mrValidation.csv') featuresPath = createPath(s + '/') cv.crossValidationFeatures(featuresPath, path, timeseries, MultipleRegression())
def findSol(self, windowSize): prices, ema, rsi, macd = self.tf.getTimewindow(windowSize) y = np.array(prices) x = np.vstack([ema]).T mymodel = ols.ols(y,x,'price',['EMA']) labels = ['constant', 'EMA'] equation = "price(t+1) = " stats = {} for i in range(len(mymodel.b)): l = labels[i] c = mymodel.b[i] stats[l] = c if l == 'constant': equation += str(c) + " + " else: equation += str(c) + "*" + l + "(t) + " equation = equation[:-2] return stats stocks = ['NFLX', 'LUV'] for s in stocks: timeseries = loadStock(s + '/' + s) path = createPath(s + '/' + s + 'emaValidation.csv') cv.simpleCrossValidation(path, timeseries, SimpleRegression())
prices, ema, rsi, macd = self.tf.getTimewindow(windowSize) y = np.array(prices) x = np.vstack([ema]).T mymodel = ols.ols(y, x, 'price', ['EMA']) labels = ['constant', 'EMA'] equation = "price(t+1) = " stats = {} for i in range(len(mymodel.b)): l = labels[i] c = mymodel.b[i] stats[l] = c if l == 'constant': equation += str(c) + " + " else: equation += str(c) + "*" + l + "(t) + " equation = equation[:-2] return stats stocks = ['NFLX', 'LUV'] for s in stocks: timeseries = loadStock(s + '/' + s) path = createPath(s + '/' + s + 'emaValidation.csv') cv.simpleCrossValidation(path, timeseries, SimpleRegression())
with open(fname) as f: for i, l in enumerate(f): pass return i + 1 def loadFile(filename, alpha): f = csv.reader(open(filename)) w = csv.writer(open('hTests.csv', 'w')) count = 0 w.writerow(['t', 'mr-EMA z', 'GA-MR z', 'criticalZ']) for line in f: if count > 0: t,formula,e1R2,emaR2,mrR2,e1AIC, emaAIC,mrAIC,e2R2, e2AIC = line path = createPath('Timewindow Features', 'timewindowFeatures' + str(t) + '.csv') n = file_len(path) z1 = ft.fisherTransformation(math.sqrt(float(mrR2)), math.sqrt(float(emaR2)), n, n) z1, criticalZ, hTest = ft.normHTest(z1, alpha) z2 = ft.fisherTransformation(math.sqrt(float(e1R2)), math.sqrt(float(mrR2)), n, n) z2, criticalZ, hTest = ft.normHTest(z2, alpha) w.writerow([t, z1, z2, criticalZ]) count += 1 path = createPath('modelsComparison.csv') loadFile(path, 0.05)
# Sets the DB Adapter, the resetDB flag will make the Adapter recreate # the database and erase all data every run, you should use this flag # just in the first time, after the pyevolve.db was created, you can # omit it. #sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True) dbPath = createPath('pyevolve.db') sqlite_adapter = DBAdapters.DBSQLite(dbname=dbPath, identify="timewindow" + str(timewindow), resetIdentify=True, resetDB=False) ga.setDBAdapter(sqlite_adapter) # Do the evolution, with stats dump frequency of 20 generations ga.evolve(freq_stats=20) # Best individual best = ga.bestIndividual() stats = { 'constant': best[0], 'EMA': best[1], 'RSI': best[2], 'MACD': best[3] } return stats filename = createPath('gaValidation.csv') timeseries = loadStock('KO') cv.crossValidationFeatures(filename, GAForecaster())