#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())
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())
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())
# 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())