def testPolicy(self, symbol = "JPM", \ sd=dt.datetime(2009,1,1), \ ed=dt.datetime(2010,1,1), \ sv = 10000): indicators = compute_indicators(symbols=['JPM'], sd=sd, ed=ed, lookback=14, gen_plot=False) self.discreticize(indicators) # here we build a fake set of trades # your code should return the same sort of data dates = pd.date_range(sd, ed) prices_all = ut.get_data([symbol], dates) # automatically adds SPY trades = prices_all[[ symbol, ]] # only portfolio symbols trades_SPY = prices_all['SPY'] # only SPY, for comparison later trades.values[:, :] = 0 # set them all to nothing trades.values[0, :] = 1000 # add a BUY at the start trades.values[40, :] = -1000 # add a SELL trades.values[41, :] = 1000 # add a BUY trades.values[60, :] = -2000 # go short from long trades.values[61, :] = 2000 # go long from short trades.values[-1, :] = -1000 #exit on the last day if self.verbose: print type(trades) # it better be a DataFrame! if self.verbose: print trades if self.verbose: print prices_all return trades
def addEvidence(self, symbol = "IBM", \ sd=dt.datetime(2008,1,1), \ ed=dt.datetime(2009,1,1), \ sv = 10000): rolling_days = 30 sd_original = sd sd = sd - dt.timedelta(2 * rolling_days) prices_normalized, prices_sma_ratio, momentum, bb_percent, volume = ind.compute_indicators( sd, ed, syms=[symbol], rolling_days=rolling_days) indicators = pd.concat([prices_sma_ratio, momentum, bb_percent['BBP']], axis=1) indicators = indicators.loc[sd_original:] prices_normalized = prices_normalized.loc[sd_original:] daily_price_change = self.get_daily_returns(prices_normalized) indicators = self.discretize(indicators) self.ql.querysetstate(int(float(indicators.iloc[0]['state']))) orders = pd.DataFrame(0, index=prices_normalized.index, columns=['Shares']) buy_sell = pd.DataFrame('BUY', index=prices_normalized.index, columns=['Order']) symbol_df = pd.DataFrame(symbol, index=prices_normalized.index, columns=['Symbol']) df_trades, df_trades.columns = pd.concat( [symbol_df, buy_sell, orders], axis=1), ['Symbol', 'Order', 'Shares'] df_trades_copy = df_trades.copy() i = 0 while i < 500: i += 1 total_holdings = 0 if (i > 20) and (df_trades.equals(df_trades_copy)): break df_trades_copy = df_trades.copy() for index, row in prices_normalized.iterrows(): a = self.ql.query( int(float(indicators.loc[index]['state'])), (total_holdings * daily_price_change.loc[index] * (1 - self.impact))) if (a == 1) and (total_holdings < 1000): buy_sell.loc[index]['Order'] = 'BUY' if total_holdings == 0: orders.loc[index]['Shares'] = 1000 total_holdings += 1000 else: orders.loc[index]['Shares'] = 2000 total_holdings += 2000 elif (a == 2) and (total_holdings > -1000): buy_sell.loc[index]['Order'] = 'SELL' if total_holdings == 0: orders.loc[index]['Shares'] = -1000 total_holdings = total_holdings - 1000 else: orders.loc[index]['Shares'] = -2000 total_holdings = total_holdings - 2000 df_trades = pd.concat([symbol_df, buy_sell, orders], axis=1) df_trades.columns = ['Symbol', 'Order', 'Shares']
def addEvidence(self, symbol = "JPM", \ sd=dt.datetime(2008,1,1), \ ed=dt.datetime(2009,1,1), \ sv = 10000): # add your code to do learning here indicators = compute_indicators(symbols=['JPM'], sd=sd, ed=ed, lookback=14, gen_plot=False) states = self.discreticize(indicators) states = states.astype(int) print "max_state", pd.to_numeric(states, downcast='integer') num_states = states.max() + 1 learner = ql.QLearner(num_states=num_states,\ num_actions = 3, \ alpha = 0.2, \ gamma = 0.9, \ rar = 0.98, \ radr = 0.999, \ dyna = 0, \ verbose=False) action = learner.querysetstate( states[0]) #set state and get first action # set initial state on first day print action # Calculate daily returns prices = ut.get_data([symbol], pd.date_range(sd, ed)) daily_returns = ((prices / prices.shift(1)) - 1) * 100 # print daily_returns[symbol] # print state for index, state in states[1:].iteritems(): reward = daily_returns.loc[index, symbol] action = learner.query(state, reward) print reward, action # example usage of the old backward compatible util function syms = [symbol] dates = pd.date_range(sd, ed) prices_all = ut.get_data(syms, dates) # automatically adds SPY prices = prices_all[syms] # only portfolio symbols prices_SPY = prices_all['SPY'] # only SPY, for comparison later if self.verbose: print prices # example use with new colname volume_all = ut.get_data(syms, dates, colname="Volume") # automatically adds SPY volume = volume_all[syms] # only portfolio symbols volume_SPY = volume_all['SPY'] # only SPY, for comparison later if self.verbose: print volume
def get_indicator_data(data, symbol, window, train=True): indicators = compute_indicators(data, symbol, window=window) indicators.fillna(0, inplace=True) indicator_data = indicators.copy() if train: indicator_data = indicator_data[:-3] indicator_data.drop('Upper_BB', axis=1, inplace=True) indicator_data.drop('Down_BB', axis=1, inplace=True) #indicator_data.drop('Volatility', axis=1, inplace=True) #indicator_data.drop('Momentum', axis=1, inplace=True) #indicator_data.drop('BB_val', axis=1, inplace=True) #indicator_data.drop('MACD', axis=1, inplace=True) indicator_data = indicator_data.values return indicator_data
def testPolicy(self, symbol = "IBM", \ sd=dt.datetime(2009,1,1), \ ed=dt.datetime(2010,1,1), \ sv = 10000): rolling_days = 30 sd_original = sd sd = sd - dt.timedelta(2 * rolling_days) prices_normalized, prices_sma_ratio, momentum, bb_percent, volume = ind.compute_indicators( sd, ed, syms=[symbol], rolling_days=rolling_days) indicators = pd.concat([prices_sma_ratio, momentum, bb_percent['BBP']], axis=1) indicators = indicators.loc[sd_original:] prices_normalized = prices_normalized.loc[sd_original:] indicators = self.discretize(indicators) orders = pd.DataFrame(0, index=prices_normalized.index, columns=['Shares']) buy_sell = pd.DataFrame('BUY', index=prices_normalized.index, columns=['Order']) symbol_df = pd.DataFrame(symbol, index=prices_normalized.index, columns=['Symbol']) total_holdings = 0 self.ql.querysetstate(int(float(indicators.iloc[0]['state']))) for index, row in prices_normalized.iterrows(): a = self.ql.querysetstate( int(float(indicators.loc[index]['state']))) if (a == 1) and (total_holdings < 1000): buy_sell.loc[index]['Order'] = 'BUY' if total_holdings == 0: orders.loc[index]['Shares'] = 1000 total_holdings += 1000 else: orders.loc[index]['Shares'] = 2000 total_holdings += 2000 elif (a == 2) and (total_holdings > -1000): buy_sell.loc[index]['Order'] = 'SELL' if total_holdings == 0: orders.loc[index]['Shares'] = -1000 total_holdings = total_holdings - 1000 else: orders.loc[index]['Shares'] = -2000 total_holdings = total_holdings - 2000 df_trades, df_trades.columns = pd.concat( [symbol_df, buy_sell, orders], axis=1), ['Symbol', 'Order', 'Shares'] df_trades = df_trades.drop('Symbol', axis=1).drop('Order', axis=1) return df_trades
def TA_screener(lookback_range): TA_results_global = {} count = 0 col_name = ['symbol'] wishlist_df = pd.read_csv('wishlist.csv', names=col_name) wishlist = wishlist_df.symbol.tolist() for element in wishlist: print(element) TA_results = {} df = pd.read_csv("stock_data/" + element + "_" + str(start_date) + "_" + str(end_date) + ".csv") # print(df) open = df['Open'] high = df['High'] low = df['Low'] close = df['Close'] volume = df['Volume'] date = df['Date'] count = count + 1 trends_dict = find_trends(df) recognised_patterns = pattern_recogniser(open, high, low, close, volume, date) # print(recognised_patterns) volumes = volume_screener(volume, date) SandR_levels = SandR_calc(df) sup_res_dict = sup_res_calculator(close, date) indicators = compute_indicators(open, high, low, close, volume, date) TA_results = globalDictForSingleStock(recognised_patterns, volumes, sup_res_dict, SandR_levels, indicators, trends_dict, lookback_range) # print(TA_results) df = df.set_index("Date") trades = checklist(TA_results, df, lookback_range) TA_results_global[element] = trades # if count == 10: # break # break # TA_results_sorted = dict(sorted(TA_results_global.items(), key=lambda item : item['Score'][1])) # TA_results_global = TA_results_sorted TA_results_global = sort_trades_dict(TA_results_global) # print(TA_results_global) writeJsonFile("TA_screener_output.json", TA_results_global) return TA_results_global
def get_indicator_data(data, symbol, window, train=True): # Compute indicators, which are features indicators = compute_indicators(data, symbol, window=window) indicators.fillna(0, inplace=True) indicator_data = indicators.copy() if train: indicator_data = indicator_data[:-3] # Uncomment below to try different set of features indicator_data.drop('Upper_BB', axis=1, inplace=True) indicator_data.drop('Down_BB', axis=1, inplace=True) #indicator_data.drop('Volatility', axis=1, inplace=True) #indicator_data.drop('Momentum', axis=1, inplace=True) #indicator_data.drop('BB_val', axis=1, inplace=True) #indicator_data.drop('MACD', axis=1, inplace=True) indicator_data = indicator_data.values # to numpy array return indicator_data
def testPolicy(symbol="AAPL", sd=dt.datetime(2010, 1, 1), ed=dt.datetime(2011, 12, 31), sv=100000): symbols = [symbol] prices, sma, sma_over, bb_std, top_band, bottom_band, bb, momentum = ind.compute_indicators( start_date=sd, end_date=ed, symbols=symbols, lookback=14) prices = prices.ix[:, ['JPM']] sma = sma.ix[:, ['JPM']] sma_over = sma_over.ix[:, ['JPM']] bb_std = bb_std.ix[:, ['JPM']] top_band = top_band.ix[:, ['JPM']] bottom_band = bottom_band.ix[:, ['JPM']] bb = bb.ix[:, ['JPM']] momentum = momentum.ix[:, ['JPM']] orders = prices.copy() orders.ix[:, :] = np.NaN sma_cross = pd.DataFrame(0, index=sma_over.index, columns=sma_over.columns) sma_cross[sma_over >= 1] = 1 sma_cross[1:] = sma_cross.diff() sma_cross.ix[0] = 0 orders[(sma_over < 0.85)] = 1 orders[(sma_over > 1.25)] = -1 orders[(bb < -0.4)] = 1 orders[(bb > 1.4)] = -1 orders[(momentum < -0.03)] = 1 orders[(momentum > 0.08)] = -1 orders[(sma_cross != 0)] = 0 orders.ffill(inplace=True) orders.fillna(0, inplace=True) orders[1:] = orders.diff() orders.ix[0] = 0 order_list = pd.DataFrame(data='JPM', index=orders.index, columns=['Symbol', 'Order', 'Shares']) holds = 0 length = len(prices.index) for i in range(0, length - 1): if (orders.ix[i, 'JPM'] > 0) and (holds == 0): order_list.ix[i, "Order"] = 'BUY' order_list.ix[i, "Shares"] = 1000 holds = holds + order_list.ix[i, "Shares"] elif (orders.ix[i, 'JPM'] > 0) and (holds == 1000): order_list.ix[i, "Order"] = 'HOLD' order_list.ix[i, "Shares"] = 0 holds = holds + order_list.ix[i, "Shares"] elif (orders.ix[i, 'JPM'] > 0) and (holds == -1000): order_list.ix[i, "Order"] = 'BUY' order_list.ix[i, "Shares"] = 2000 holds = holds + order_list.ix[i, "Shares"] elif (orders.ix[i, 'JPM'] < 0) and (holds == 0): order_list.ix[i, "Order"] = 'SELL' order_list.ix[i, "Shares"] = 1000 holds = holds - order_list.ix[i, "Shares"] elif (orders.ix[i, 'JPM'] < 0) and (holds == 1000): order_list.ix[i, "Order"] = 'SELL' order_list.ix[i, "Shares"] = 2000 holds = holds - order_list.ix[i, "Shares"] elif (orders.ix[i, 'JPM'] < 0) and (holds == -1000): order_list.ix[i, "Order"] = 'HOLD' order_list.ix[i, "Shares"] = 0 holds = holds - order_list.ix[i, "Shares"] else: order_list.ix[i, "Order"] = 'HOLD' order_list = order_list[order_list.Order != 'HOLD'] order_list = order_list[order_list.Order != 'JPM'] return order_list
def addEvidence(self, symbol = "IBM", \ sd=dt.datetime(2008,1,1), \ ed=dt.datetime(2009,1,1), \ sv = 10000): N = 30 K = 2 * N sd_original = sd # add your code to do learning here sd = sd - dt.timedelta(K) # example usage of the old backward compatible util function syms = [symbol] dates = pd.date_range(sd, ed) prices_all = ut.get_data(syms, dates) # automatically adds SPY prices = prices_all[syms] # only portfolio symbols prices_SPY = prices_all['SPY'] # only SPY, for comparison later prices_normalized, prices_sma_ratio, momentum, bb_percent = ind.compute_indicators( sd, ed, syms=[symbol], rolling_days=N) indicators = pd.concat([prices_sma_ratio, momentum, bb_percent['BBP']], axis=1) indicators = indicators.loc[sd_original:] prices_normalized = prices_normalized.loc[sd_original:] daily_price_change = self.get_daily_returns(prices_normalized) indicators = self.discretize(indicators) initial_state = indicators.iloc[0]['state'] self.ql.querysetstate(int(float(initial_state))) orders = pd.DataFrame(0, index=prices_normalized.index, columns=['Shares']) buy_sell = pd.DataFrame('BUY', index=prices_normalized.index, columns=['Order']) symbol_df = pd.DataFrame(symbol, index=prices_normalized.index, columns=['Symbol']) df_trades = pd.concat([symbol_df, buy_sell, orders], axis=1) df_trades.columns = ['Symbol', 'Order', 'Shares'] df_trades_copy = df_trades.copy() i = 0 while i < 500: i += 1 reward = 0 total_holdings = 0 if (i > 20) and (df_trades.equals(df_trades_copy)): #print(i) break df_trades_copy = df_trades.copy() for index, row in prices_normalized.iterrows(): reward = total_holdings * daily_price_change.loc[index] * ( 1 - self.impact) a = self.ql.query(int(float(indicators.loc[index]['state'])), reward) if (a == 1) and (total_holdings < 1000): buy_sell.loc[index]['Order'] = 'BUY' if total_holdings == 0: orders.loc[index]['Shares'] = 1000 total_holdings += 1000 else: orders.loc[index]['Shares'] = 2000 total_holdings += 2000 elif (a == 2) and (total_holdings > -1000): buy_sell.loc[index]['Order'] = 'SELL' if total_holdings == 0: orders.loc[index]['Shares'] = -1000 total_holdings = total_holdings - 1000 else: orders.loc[index]['Shares'] = -2000 total_holdings = total_holdings - 2000 df_trades = pd.concat([symbol_df, buy_sell, orders], axis=1) df_trades.columns = ['Symbol', 'Order', 'Shares']