def mrTest(filename): stocks = ['C', 'FDX', 'KO', 'MSFT', 'SBUX', 'NFLX', 'LUV'] for s in stocks: csvFile = open(s+filename, 'a') f = csv.writer(csvFile) table = {} f.writerow([s]) f.writerow(['t', 'R^2', 'AIC']) path = createPath(s + '/' + s + 'mrValidation.csv') models = loadModels(path, "mr") remainingStocks = copy.deepcopy(stocks) remainingStocks.remove(s) for line in models: for rs in remainingStocks: i = line['timewindow'] featurePath = createPath(rs + "/") features = loadFeatures(featurePath, i) prices = [] ema = [] rsi = [] macd = [] for row in features: prices.append(row['price']) ema.append(row['ema']) rsi.append(row['rsi']) macd.append(row['macd']) y = np.array(prices) x = np.vstack([ema, rsi, macd]).T validationStats = anova.ols(y, x, [line['constant'], line['ema'], line['rsi'], line['macd']]) r2 = validationStats.R2 aic = validationStats.ll()[1] # average out the results over k folds stats = [r2, aic] stats = [val/float(len(remainingStocks)) for val in stats] if i in table: table[i] = [sum(item) for item in izip(table[i], stats)] else: table[i] = stats print i print stats # write the averaged results for i in range(2, 202, 2): table[i].insert(0, i) print table[i] f.writerow(table[i]) csvFile.flush()
def emaTest(filename): stocks = ['C', 'FDX', 'KO', 'MSFT', 'SBUX', 'NFLX', 'LUV'] for s in stocks: csvFile = open(s + filename, 'a') f = csv.writer(csvFile) table = {} f.writerow([s]) f.writerow(['t', 'R^2', 'AIC']) path = createPath(s + '/' + s + 'emaValidation.csv') models = loadModels(path, "emaOnly") remainingStocks = copy.deepcopy(stocks) remainingStocks.remove(s) for line in models: for rs in remainingStocks: i = line['timewindow'] featurePath = createPath(rs + "/") features = loadFeatures(featurePath, i) prices = [] ema = [] for row in features: prices.append(row['price']) ema.append(row['ema']) y = np.array(prices) x = np.vstack([ema]).T validationStats = anova.ols(y, x, [line['constant'], line['ema']]) r2 = validationStats.R2 aic = validationStats.ll()[1] # average out the results over k folds stats = [r2, aic] stats = [val / float(len(remainingStocks)) for val in stats] if i in table: table[i] = [sum(item) for item in izip(table[i], stats)] else: table[i] = stats print i print stats # write the averaged results for i in range(2, 202, 2): table[i].insert(0, i) print table[i] f.writerow(table[i]) csvFile.flush()
''' Created on Apr 11, 2011 @author: bash125 ''' from tools.loadData import loadStock, createPath import os from tools import technicalFeatures as tf stocks = ['C', 'FDX', 'MSFT', 'SBUX', 'NFLX', 'LUV'] def ensure_dir(f): d = os.path.dirname(f) if not os.path.exists(d): os.makedirs(d) for s in stocks: timeseries = loadStock(s + '/' + s) path = createPath(s + '/' + 'Timewindow Features') ensure_dir(path) path += '/CSV/' ensure_dir(path) twf = tf.TechnicalFeatures(timeseries) for i in range(2, 202, 2): twf.setTimewindow(i) csvFile = path + 'timewindowFeatures' + str(i) + '.csv' twf.writeCsv(csvFile, twf.prices, twf.ema, twf.rsi, twf.macd)