def test_stocks(params): results = pd.DataFrame() tick_dict = params[-1] columns = params[:-1] name = str(columns) tick_list = [tick_dict[x]['adj_close'] for x in columns] data = pd.DataFrame(columns=columns) len_tick_list = [len(x) for x in tick_list] if all(x == len_tick_list[0] for x in len_tick_list): results.loc[name, 'Start Date'] = tick_list[0].index[0] results.loc[name, 'End Date'] = tick_list[0].index[-1] else: minimum = min(len_tick_list) minimum = minimum * -1 for x, tick in enumerate(tick_list): tick_list[x] = tick[minimum:] results.loc[name, 'Start Date'] = tick_list[0].index[0] results.loc[name, 'End Date'] = tick_list[0].index[-1] for x, col in enumerate(columns): data[col] = tick_list[x] for col in columns: index = np.argwhere(np.isnan(data[col])) data[col][index] = data[col][index - 1] sec_list = SecurityList() sec_list.importData(data) eig = sec_list.genHedgeRatio() results.loc[name, 'Eigenvector'] = str(eig) res = np.dot(data, eig) cadf = ts.adfuller(res) results.loc[name, 'ADF Test'] = cadf[0] results.loc[name, 'ADF 1% Critical Value'] = cadf[4]['1%'] results.loc[name, 'Hurst'] = hurst(res) return results
def main(): tickers = scrape_list( 'http://en.wikipedia.org/wiki/List_of_S%26P_500_companies') lticks = list() for l in tickers.values(): for tick in l: lticks.append(tick) sl = SecurityList(lticks) start = datetime.datetime(1994, 9, 29) end = datetime.datetime(2017, 4, 5) sl.downloadQuandl(start, end) ts = sl.genTimeSeries() plt.plot(np.arange(len(ts)), ts) plt.show()
def initialize(context): tickers = [ 'AYI', 'APA', 'AMZN', 'LNT', 'CTL', 'ALB', 'ABBV', 'AMT', 'ADM', 'AON', 'ORCL' ] context.tickers = [symbol(x) for x in tickers] schedule_function(place_orders, date_rules.every_day()) context.days = int() context.return_thresh = 0.02 context.hasOrdered = False start = datetime.datetime(2013, 1, 3) end = datetime.datetime(2017, 8, 1) sec_list = SecurityList(tickers=tickers) sec_list.downloadQuandl(start, end) context.hedgeRatio = sec_list.genHedgeRatio() context.predicted = list()
def place_orders(context, data): def orderPortfolio(order): for i, tick in enumerate(context.tickers): if order == 'long': order_target_percent(tick, context.hedgeRatio[i]) if order == 'short': order_target_percent(tick, -context.hedgeRatio[i]) if order == 'exit': order_target_percent(tick, 0) if context.hasOrdered: context.position_len += 1 if context.position_len == context.exit_day: orderPortfolio(order='exit') context.hasOrdered = False else: close = list() for tick in context.tickers: close.append(np.array([data.current(tick, 'price')])) sec_list = SecurityList(data=close) matrix = sec_list.genMatrix() current_price = np.dot(matrix, context.hedgeRatio) rolling_max = max(context.predicted) rolling_min = min(context.predicted) rolling_max_i = np.where(context.predicted == rolling_max) rolling_min_i = np.where(context.predicted == rolling_min) long_returns = (rolling_max - current_price) / current_price short_returns = (current_price - rolling_min) / current_price if (long_returns - short_returns) > context.return_thresh: orderPortfolio(order='long') context.hasOrdered = True context.exit_day = rolling_max_i context.position_len = int() elif (short_returns - long_returns) > context.return_thresh: orderPortfolio(order='short') context.hasOrdered = True context.exit_day = rolling_min_i context.position_len = int()
def before_trading_start(context, data): if context.hasOrdered: pass else: close = list() for tick in context.tickers: close.append(data.history(tick, 'close', 252 + context.days, '1d')) sec_list = SecurityList(data=close) ts, hedgeRatio, matrix = sec_list.genTimeSeries() plt.plot(ts) plt.show() history = [x for x in ts] for t in range(252): model = ARMA(history, order=(5, 0)) model_fit = model.fit(disp=0) output = model_fit.forecast() context.predicted.append(output[0]) history.append(output[0]) plt.plot(context.predicted) plt.show() context.days += 1
def initialize(context): set_slippage(VolumeShareSlippage(volume_limit=0.025,price_impact=0.1)) tickers = ['AYI','APA','AMZN','LNT','CTL','ALB','ABBV','AMT','ADM','AON','ORCL'] context.tick_list = tickers context.tickers = [ symbol(x) for x in tickers ] context.long= False context.short= False start = datetime.datetime(2013,1,3) end = datetime.datetime(2017,8,1) sec_list = SecurityList(tickers=tickers) sec_list.downloadQuandl(start,end) ts = sec_list.genTimeSeries() context.adjDiv = sec_list.adjDividends() context.adjHedge = sec_list.adjSplits() context.divFactors = sec_list.getAdjFactors() context.splits = sec_list.getSplits() context.avg = ts.mean() context.std = ts.std() context.leverage = 1 context.share_num = 0 context.diff_thresh = 100 schedule_function(adjust_splits_dividends, date_rules.every_day(), time_rules.market_open()) schedule_function(place_orders, date_rules.every_day(), time_rules.market_open(hours=1, minutes=30))
def initialize(context): set_slippage(VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)) tickers = [ 'AYI', 'APA', 'AMZN', 'LNT', 'CTL', 'ALB', 'ABBV', 'AMT', 'ADM', 'AON', 'ORCL' ] context.tick_list = tickers context.tickers = [symbol(x) for x in tickers] context.long = False context.short = False start = datetime.datetime(2013, 1, 3) end = datetime.datetime(2017, 8, 1) sec_list = SecurityList(tickers=tickers) sec_list.downloadQuandl(start, end) ts, context.hedgeRatio, context.df_close = sec_list.genTimeSeries() context.adjustedHedge = np.copy(context.hedgeRatio) context.adjustedHedge[3] = context.adjustedHedge[3] / 2 context.avg = ts.mean() context.std = ts.std() context.volume = sec_list.getVolume() context.share_num = 0 context.diff_thresh = 100 schedule_function(place_orders, date_rules.every_day())
import pandas as pd import numpy as np import matplotlib.pyplot as plt from statsmodels.tsa.arima_model import ARMA from securityList import SecurityList from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error start = datetime.datetime(2013, 1, 3) end = datetime.datetime(2017, 8, 1) ticks = [ 'AYI', 'APA', 'AMZN', 'LNT', 'CTL', 'ALB', 'ABBV', 'AMT', 'ADM', 'AON', 'ORCL' ] sec_list = SecurityList(ticks) sec_list.downloadQuandl(start, end) ts = sec_list.genTimeSeries() X_train, X_test, y_train, y_test = train_test_split(np.arange(1, len(ts) + 1), ts, shuffle=False) model = ARMA(y_train, order=(5, 0)) model_fit = model.fit(disp=0) residuals = pd.DataFrame(model_fit.resid) residuals.plot() plt.show() residuals.plot(kind='kde') plt.show() history = [x for x in y_train] predictions = list()
import matplotlib.pyplot as plt from securityList import SecurityList from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split np.set_printoptions(threshold=np.nan) start = datetime.datetime(2013, 1, 3) end = datetime.datetime(2017, 8, 1) ticks = [ 'AYI', 'APA', 'AMZN', 'LNT', 'CTL', 'ALB', 'ABBV', 'AMT', 'ADM', 'AON', 'ORCL' ] sec_list = SecurityList(ticks) sec_list.downloadQuandl(start, end) ts = sec_list.runJohansen() plt.figure() plt.plot(np.linspace(0, 1, len(ts)), ts) plt.show() X_train, X_test, y_train, y_test = train_test_split(np.linspace(0, 1, len(ts)), ts, random_state=0, shuffle=False) poly = PolynomialFeatures(6) X_poly_train = poly.fit_transform(X_train.reshape(len(X_train), 1)) X_poly_test = poly.fit_transform(X_test.reshape(len(X_test), 1)) linreg = LinearRegression().fit(X_poly_train, y_train) train = linreg.predict(X_poly_train) test = linreg.predict(X_poly_test)