def testPolicy(self, symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 1, 31), sv=100000): df_prices = get_data([symbol], pd.date_range(sd, ed)).ffill().bfill() df_prices_symbol = df_prices[[symbol]] momentum = get_momentum(df_prices_symbol) sma_r = get_sma_r(df_prices_symbol) bbp = get_bbp(df_prices_symbol) df_trades = df_prices_symbol.copy() df_trades[symbol] = 0 impact = 0.005 commission = 9.95 current_holding = 0 for i in range(df_prices_symbol.shape[0] - 1): price = df_prices_symbol.iloc[i, 0] sign = 0 if (bbp.iloc[i, 0] <= 0.6 and sma_r.iloc[i, 0] < 1) or \ (sma_r.iloc[i, 0] < 1 and momentum.iloc[i, 0] > 0.1) or \ (bbp.iloc[i, 0] <= 0.6 and momentum.iloc[i, 0] > 0.1): sign = 1 price = price * (1 + impact * sign) if (bbp.iloc[i, 0] >= 0.8 and sma_r.iloc[i, 0] > 1) or \ (sma_r.iloc[i, 0] > 1 and momentum.iloc[i, 0] < 0) or \ (bbp.iloc[i, 0] >= 0.8 and momentum.iloc[i, 0] < 0): sign = -1 price = price * (1 + impact * sign) if sign != 0: if current_holding == 0: sv -= commission sv = sv - price * 1000 * sign current_holding = 1000 * sign df_trades.iloc[i, 0] = 1000 * sign elif current_holding == -1000 * sign: sv -= commission sv = sv - price * 2000 * sign current_holding = 1000 * sign df_trades.iloc[i, 0] = 2000 * sign else: pass df_trades.index.names = ['Date'] df_trades.columns = ['Shares'] df_trades["Symbol"] = "JPM" df_trades["Order"] = [ "BUY" if x > 0 else "SELL" if x < 0 else "NA" for x in df_trades['Shares'] ] df_trades.drop(df_trades[df_trades['Shares'] == 0].index, inplace=True) df_trades['Shares'] = df_trades['Shares'].abs() df_trades = df_trades.reindex(columns=['Symbol', 'Order', 'Shares']) return df_trades
def testPolicy(self, symbol="JPM", sd=dt.datetime(2010, 1, 1), ed=dt.datetime(2011, 12, 31), sv=100000): df_prices = ut.get_data([symbol], pd.date_range(sd, ed)).ffill().bfill() df_prices = df_prices[[symbol]] # Make indicators df indicator_details = df_prices.copy() indicator_details.drop([symbol], axis=1, inplace=True) indicator_details[self.momentum] = idc.get_momentum(df_prices) indicator_details[self.sma_ratio] = idc.get_sma_r(df_prices) indicator_details[self.bollinger_percent] = idc.get_bbp(df_prices) df_trades = df_prices.copy() df_trades[symbol] = 0 current_holding = 0 indicators = indicator_details.iloc[self.window - 1] s = self.serialize(indicators) a = self.learner.querysetstate(s) prices_len = df_prices.shape[0] for i in range(self.window, prices_len - 1): s = self.serialize(indicator_details.iloc[i]) if a == 1: pass elif a == 0 and current_holding == 0: current_holding = -1000 df_trades.iloc[i, 0] = current_holding elif a == 0 and current_holding == 1000: current_holding = -1000 df_trades.iloc[i, 0] = current_holding * 2 elif a == 2 and current_holding == 0: current_holding = 1000 df_trades.iloc[i, 0] = current_holding elif a == 2 and current_holding == -1000: current_holding = 1000 df_trades.iloc[i, 0] = current_holding * 2 a = self.learner.querysetstate(s) return df_trades
def addEvidence(self, symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000): df_prices = ut.get_data([symbol], pd.date_range(sd, ed)).ffill().bfill() df_prices = df_prices[[symbol]] indicator_details = df_prices.copy() indicator_details.drop([symbol], axis=1, inplace=True) indicator_details[self.momentum] = idc.get_momentum(df_prices) indicator_details[self.sma_ratio] = idc.get_sma_r(df_prices) indicator_details[self.bollinger_percent] = idc.get_bbp(df_prices) self.add_indicator_details(indicator_details) indicators = indicator_details.iloc[self.window - 1] s = self.serialize(indicators) a = self.learner.querysetstate(s) df_prices_len = df_prices.shape[0] ith_run = 0 run_results = [] while ith_run < self.max_runs: if len(run_results) > 10 and round(run_results[-1], 4) == round( run_results[-2], 4): break ith_run = ith_run + 1 holding = 0 df_trades = df_prices.copy() df_trades[symbol] = 0 for i in range(self.window, df_prices_len - 1): reward = 0 s = self.serialize(indicator_details.iloc[i]) current_price = df_prices.iloc[i, 0] next_price = df_prices.iloc[i + 1, 0] if rand.uniform(0.0, 1.0) <= 0: a = rand.randint(0, 2) if a == 1: pass elif a == 2 and holding == 0: holding = 1000 df_trades.iloc[i, 0] = holding reward = -self.impact * (current_price + next_price) * holding elif a == 2 and holding == -1000: holding = 1000 df_trades.iloc[i, 0] = 2 * holding reward = -self.impact * (current_price + next_price) * 2 * holding elif a == 0 and holding == 0: holding = -1000 df_trades.iloc[i, 0] = holding reward = -self.impact * (current_price + next_price) * 1000 elif a == 0 and holding == 1000: holding = -1000 df_trades.iloc[i, 0] = 2 * holding reward = -self.impact * (current_price + next_price) * 2000 reward += (next_price - current_price) * holding a = self.learner.query(s, reward) df_trades.index.names = ['Date'] df_trades.columns = ['Shares'] df_trades["Symbol"] = "JPM" df_trades["Order"] = [ "BUY" if x > 0 else "SELL" if x < 0 else "NA" for x in df_trades['Shares'] ] df_trades.drop(df_trades[df_trades['Shares'] == 0].index, inplace=True) df_trades['Shares'] = df_trades['Shares'].abs() df_trades = df_trades.reindex( columns=['Symbol', 'Order', 'Shares']) df_trades.to_csv('orders.csv') port_values = ms.compute_portvals('./orders.csv', start_val=sv, commission=0.0, impact=self.impact) cumulative_return = port_values[-1] / port_values[0] - 1 run_results.append(cumulative_return)
def testPolicy(self, symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 1, 31), sv=100000): # get data dates = pd.date_range(sd, ed) prices_all = get_data([symbol], dates) # automatically adds SPY prices_all.fillna(method='ffill', inplace=True) prices_all.fillna(method='bfill', inplace=True) prices = prices_all[[symbol]] prices_SPY = prices_all['SPY'] lookback = 7 momentum = get_momentum(prices, lookback=lookback) sma_r = get_sma_r(prices, lookback=lookback) bbp = get_bbp(prices, lookback=lookback) commission = 9.95 impact = 0.005 holding = 0 cash = sv #print(bbp) df_trades = prices.copy() df_trades[symbol] = 0 for i in range(lookback - 1, prices.shape[0] - 1): price = prices.iloc[i, 0] # BUY if (bbp.iloc[i,0] <= 0.6 and sma_r.iloc[i,0] < 1) or \ (sma_r.iloc[i,0] < 1 and momentum.iloc[i,0] > 0.1) or \ (bbp.iloc[i,0] <= 0.6 and momentum.iloc[i,0] > 0.1): price = price * (1 + impact) if holding == 0: cash = cash - price * 1000 - commission holding = 1000 df_trades.iloc[i, 0] = 1000 elif holding == -1000: cash = cash - price * 2000 - commission holding = 1000 df_trades.iloc[i, 0] = 2000 else: pass # SELL elif (bbp.iloc[i,0] >= 0.8 and sma_r.iloc[i,0] > 1) or \ (sma_r.iloc[i,0] > 1 and momentum.iloc[i,0] < 0) or \ (bbp.iloc[i,0] >= 0.8 and momentum.iloc[i,0] < 0): price = price * (1 - impact) if holding == 0: cash = cash + price * 1000 - commission holding = -1000 df_trades.iloc[i, 0] = -1000 elif holding == 1000: cash = cash + price * 2000 - commission holding = -1000 df_trades.iloc[i, 0] = -2000 else: pass else: pass return df_trades
def addEvidence(self, symbol = "JPM", \ sd=dt.datetime(2008,1,1), \ ed=dt.datetime(2009,12,31), \ sv = 100000): rand.seed(5) LOOKBACK = 10 randomrate = 0.0 # Getting data 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.fillna(method='ffill', inplace=True) prices.fillna(method='bfill', inplace=True) prices_SPY = prices_all['SPY'] # only SPY, for comparison later # Make indicators df X = prices.copy() X.drop([symbol], axis=1, inplace=True) X['mmt'] = idc.get_momentum(prices) X['smar'] = idc.get_sma_r(prices) X['bbp'] = idc.get_bbp(prices) # Get thresholds for 3 indicators # Momentum sorted_vals = X['mmt'].sort_values() sorted_vals = sorted_vals.dropna() stepsize = math.floor(sorted_vals.shape[0] / 10) for i in range(0, 10): self.threshold_mmt.append(sorted_vals[(i + 1) * stepsize]) # Simple moving average ratio sorted_vals = X['smar'].sort_values() sorted_vals = sorted_vals.dropna() stepsize = math.floor(sorted_vals.shape[0] / 10) for i in range(0, 10): self.threshold_smar.append(sorted_vals[(i + 1) * stepsize]) # Bollinger band percentage sorted_vals = X['bbp'].sort_values() sorted_vals = sorted_vals.dropna() stepsize = math.floor(sorted_vals.shape[0] / 10) for i in range(0, 10): self.threshold_bbp.append(sorted_vals[(i + 1) * stepsize]) # Get initial state indicators = X.iloc[LOOKBACK - 1] s = self.serialize(indicators) a = self.learner.querysetstate(s) df_len = prices.shape[0] run_i = 0 run_results = [] MAX_RUNS = 50 while run_i < MAX_RUNS: if len(run_results) > 10: # Check for convergence if (round(run_results[-1], 4) == round(run_results[-2], 4)): break run_i += 1 holding = 0 df_trades = prices.copy() df_trades[symbol] = 0 for i in range(LOOKBACK, df_len - 1): s = self.serialize(X.iloc[i]) price = prices.iloc[i, 0] next_price = prices.iloc[i + 1, 0] if rand.uniform(0.0, 1.0) <= randomrate: # going rogue a = rand.randint(0, 2) # choose the random direction # Calculate reward r = 0 if a == 2: # LONG if holding == 0: holding = 1000 df_trades.iloc[i, 0] = 1000 r = -self.impact * (price + next_price) * 1000 elif holding == -1000: holding = 1000 df_trades.iloc[i, 0] = 2000 r = -self.impact * (price + next_price) * 2000 else: pass elif a == 1: # do nothing pass elif a == 0: # SHORT if holding == 0: holding = -1000 df_trades.iloc[i, 0] = -1000 r = -self.impact * (price + next_price) * 1000 elif holding == 1000: holding = -1000 df_trades.iloc[i, 0] = -2000 r = -self.impact * (price + next_price) * 2000 else: pass else: raise Exception("ERROR: INVALID ACTION") r += (next_price - price) * holding a = self.learner.query(s, r) por_vals = ms.compute_portvals(df_trades, start_val=sv, commission=0.0, impact=self.impact) cum_ret = por_vals[-1] / por_vals[0] - 1 run_results.append(cum_ret)
def testPolicy(self, symbol = "JPM", \ sd=dt.datetime(2010,1,1), \ ed=dt.datetime(2011,12,31), \ sv = 100000): rand.seed(5) LOOKBACK = 10 # 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.fillna(method='ffill', inplace=True) prices.fillna(method='bfill', inplace=True) prices_SPY = prices_all['SPY'] # only SPY, for comparison later if self.verbose: print(prices) # Make indicators df X = prices.copy() X.drop([symbol], axis=1, inplace=True) X['mmt'] = idc.get_momentum(prices) X['smar'] = idc.get_sma_r(prices) X['bbp'] = idc.get_bbp(prices) df_trades = prices.copy() df_trades[symbol] = 0 holding = 0 # Get initial state indicators = X.iloc[LOOKBACK - 1] s = self.serialize(indicators) a = self.learner.querysetstate(s) df_len = prices.shape[0] for i in range(LOOKBACK, df_len - 1): s = self.serialize(X.iloc[i]) if a == 2: # LONG if holding == 0: holding = 1000 df_trades.iloc[i, 0] = 1000 elif holding == -1000: holding = 1000 df_trades.iloc[i, 0] = 2000 elif a == 1: pass elif a == 0: # SHORT if holding == 0: holding = -1000 df_trades.iloc[i, 0] = -1000 elif holding == 1000: holding = -1000 df_trades.iloc[i, 0] = -2000 else: raise Exception("ERROR: INVALID ACTION") a = self.learner.querysetstate(s) # por_vals = ms.compute_portvals(df_trades, start_val = sv, commission=0.0, impact=self.impact) # cum_ret = por_vals[-1]/por_vals[0] - 1 return df_trades if self.verbose: print(type(trades)) # it better be a DataFrame! if self.verbose: print(trades) if self.verbose: print(prices_all)