def testPolicy(self, symbol = "IBM", \ sd=dt.datetime(2009,1,1), \ ed=dt.datetime(2010,1,1), \ sv = 10000): syms = [symbol] # here we build a fake set of trades # your code should return the same sort of data syms = [symbol] dates = pd.date_range(sd, ed) prices, prices_SPY = id.get_price(syms, dates) if self.verbose: print prices daily_returns = (prices / prices.shift(1)) - 1 daily_returns = daily_returns[1:] # get indicators and combine them into as a feature data_frame lookback = 14 _, PSR = id.get_SMA(prices, lookback) _, _, bb_indicator = id.get_BB(prices, lookback) momentum = id.get_momentum(prices, lookback) indices = prices.index holdings = pd.DataFrame(np.nan, index=indices, columns=['Holdings']) holdings.iloc[0] = 0 for i in range(1, daily_returns.shape[0]): state = self.indicators_to_state(PSR.iloc[i], bb_indicator.iloc[i], momentum.iloc[i]) # Get action by Query learner with current state and reward to get action action = self.learner.querysetstate(state) #print("SL 286 action is ", action) # Get holdings with the new action. holdings.iloc[i], _ = self.apply_action(holdings.iloc[i - 1][0], action, 0) holdings.ffill(inplace=True) holdings.fillna(0, inplace=True) trades = holdings.diff() trades.iloc[0] = 0 # buy and sell happens when the difference change direction df_trades = pd.DataFrame(data=trades.values, index=trades.index, columns=['Trades']) #print("293: ", df_trades) if self.verbose: print type(df_trades) # it better be a DataFrame! if self.verbose: print trades if self.verbose: print prices return df_trades
def testPolicy(self, symbol, sd, ed, sv=100000): # this policy is like this: buy when the price will go up the next day, sell when the price will do down the next day # get price data dates = pd.date_range(sd, ed) prices_all = get_data([symbol], dates, addSPY=True, colname='Adj Close') prices = prices_all[symbol] # only portfolio symbols # get indicators lookback = 14 _, PSR = id.get_SMA(prices, lookback) _, _, bb_indicator = id.get_BB(prices, lookback) momentum = id.get_momentum(prices, lookback) holdings = pd.DataFrame(np.nan, index=prices.index, columns=['Holdings']) # make sure when PSR (= price / SMA -1) >0.05 and bb_indicator > 1 and momentum > 0.05 SELL or hold -1000 # when PSR (= price / SMA -1) < -0.05 and bb_indicator < -1 and momentum < -0.05 Buy or hold -1000 #print("PSR: ", PSR) #print("momentum: ", momentum) #print("bb_indicator: ", bb_indicator) for t in range(prices.shape[0]): if PSR.iloc[t] < -0.02 and bb_indicator.iloc[ t] < -0.8 and momentum.iloc[t] < -0.03: holdings.iloc[t] = 1000 elif PSR.iloc[t] > 0.02 and bb_indicator.iloc[ t] > 0.8 and momentum.iloc[t] > 0.03: holdings.iloc[t] = -1000 # fill the NAN data holdings.ffill(inplace=True) holdings.fillna(0, inplace=True) trades = holdings.diff() trades.iloc[0] = 0 #trades.iloc[-1] = 0 #trades.columns = 'Trades' # buy and sell happens when the difference change direction df_trades = pd.DataFrame(data=trades.values, index=trades.index, columns=['Trades']) return df_trades
def experiment1(): ms = ManualStrategy.ManualStrategy() sl = StrategyLearner.StrategyLearner() commission = 9.95 impact = 0.005 # in sample start_date = dt.datetime(2008, 1, 1) end_date = dt.datetime(2009, 12, 31) # dates = pd.date_range(start_date, end_date) symbol = 'JPM' sl.addEvidence(symbol=symbol, sd=start_date, ed=end_date, sv=100000, n_bins=5) df_trades_ms = ms.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) df_trades_sl = sl.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) # generate orders based on trades df_orders_ms, benchmark_orders = ManualStrategy.generate_orders( df_trades_ms, symbol) df_orders_sl, _ = ManualStrategy.generate_orders(df_trades_sl, symbol) port_vals_ms = ManualStrategy.compute_portvals(df_orders_ms, start_val=100000, sd=start_date, ed=end_date, commission=commission, impact=impact) port_vals_sl = ManualStrategy.compute_portvals(df_orders_sl, start_val=100000, sd=start_date, ed=end_date, commission=commission, impact=impact) #benchmark_orders.loc[benchmark_orders.index[1], 'Shares'] = 0 benchmark_vals = ManualStrategy.compute_portvals(benchmark_orders, sd=start_date, ed=end_date, start_val=100000, commission=commission, impact=impact) normed_port_ms = port_vals_ms / port_vals_ms.ix[0] normed_port_sl = port_vals_sl / port_vals_sl.ix[0] normed_bench = benchmark_vals / benchmark_vals.ix[0] dates = pd.date_range(start_date, end_date) prices_all = get_data([symbol], dates, addSPY=True, colname='Adj Close') prices = prices_all[symbol] # only portfolio symbols # get indicators lookback = 14 _, PSR = id.get_SMA(prices, lookback) _, _, bb_indicator = id.get_BB(prices, lookback) momentum = id.get_momentum(prices, lookback) # figure 5. plt.figure(figsize=(12, 6.5)) top = plt.subplot2grid((5, 1), (0, 0), rowspan=3, colspan=1) bottom = plt.subplot2grid((5, 1), (3, 0), rowspan=2, colspan=1, sharex=top) # plot the Long or short action for index, marks in df_trades_sl.iterrows(): if marks['Trades'] > 0: plt.axvline(x=index, color='blue', linestyle='dashed', alpha=.9) elif marks['Trades'] < 0: plt.axvline(x=index, color='black', linestyle='dashed', alpha=.9) else: pass top.xaxis_date() top.grid(True) top.plot(normed_port_sl, lw=2, color='red', label='Q-Learning Strategy') top.plot(normed_port_ms, lw=1.5, color='black', label='Manual Strategy') top.plot(normed_bench, lw=1.2, color='green', label='Benchmark') top.set_title( 'Machine Learning Strategy (MLS), Manual Strategy (MS) - In Sample Analysis' ) top.set_ylabel('Normalized Value') for index, marks in df_trades_sl.iterrows(): if marks['Trades'] > 0: top.axvline(x=index, color='blue', linestyle='dashed', alpha=.9) elif marks['Trades'] < 0: top.axvline(x=index, color='black', linestyle='dashed', alpha=.9) else: pass bottom.plot(momentum, color='olive', lw=1, label="momentum") bottom.plot(PSR, color='purple', lw=1, label="PSR") #bottom.plot(bb_indicator, color='blue', lw=1, label="Bollinger") bottom.set_title('Indicators') bottom.axhline(y=-0.2, color='grey', linestyle='--', alpha=0.5) bottom.axhline(y=0, color='grey', linestyle='--', alpha=0.5) bottom.axhline(y=0.2, color='grey', linestyle='--', alpha=0.5) bottom.legend() top.legend() top.axes.get_xaxis().set_visible(False) plt.xlim(start_date, end_date) filename = '01_MLS_insample.png' plt.savefig(filename) plt.close() port_cr_sl, port_adr_sl, port_stddr_sl, port_sr_sl = ManualStrategy.get_portfolio_stats( port_vals_sl) port_cr_ms, port_adr_ms, port_stddr_ms, port_sr_ms = ManualStrategy.get_portfolio_stats( port_vals_ms) bench_cr, bench_adr, bench_stddr, bench_sr = ManualStrategy.get_portfolio_stats( benchmark_vals) # Compare portfolio against benchmark print "=== Machine Learning Strategy (MLS) V.S. Manual Strategy (MS) In Sample ===" print "Date Range: {} to {}".format(start_date, end_date) print print "Sharpe Ratio of MLS: {}".format(port_sr_sl) print "Sharpe Ratio of MS: {}".format(port_sr_ms) print "Sharpe Ratio of BenchMark : {}".format(bench_sr) print print "Cumulative Return of MLS: {}".format(port_cr_sl) print "Cumulative Return of MS: {}".format(port_cr_ms) print "Cumulative Return of Benchmark : {}".format(bench_cr) print print "Standard Deviation of MLS: {}".format(port_stddr_sl) print "Standard Deviation of MS: {}".format(port_stddr_ms) print "Standard Deviation of Benchmark : {}".format(bench_stddr) print print "Average Daily Return of MLS: {}".format(port_adr_sl) print "Average Daily Return of MS: {}".format(port_adr_ms) print "Average Daily Return of BenchMark : {}".format(bench_adr) print print "Final MLS Portfolio Value: {}".format(port_vals_sl[-1]) print "Final MS Portfolio Value: {}".format(port_vals_ms[-1]) print "Final Benchmark Portfolio Value: {}".format(benchmark_vals[-1]) print # ======================== # OUT OF SAMPLE Analysis # ======================== start_date = dt.datetime(2010, 1, 1) end_date = dt.datetime(2011, 12, 31) # dates = pd.date_range(start_date, end_date) symbol = 'JPM' df_trades_ms = ms.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) df_trades_sl = sl.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) # generate orders based on trades df_orders_ms, benchmark_orders = ManualStrategy.generate_orders( df_trades_ms, symbol) df_orders_sl, _ = ManualStrategy.generate_orders(df_trades_sl, symbol) port_vals_ms = ManualStrategy.compute_portvals(df_orders_ms, start_val=100000, sd=start_date, ed=end_date, commission=commission, impact=impact) port_vals_sl = ManualStrategy.compute_portvals(df_orders_sl, start_val=100000, sd=start_date, ed=end_date, commission=commission, impact=impact) #benchmark_orders.loc[benchmark_orders.index[1], 'Shares'] = 0 benchmark_vals = ManualStrategy.compute_portvals(benchmark_orders, sd=start_date, ed=end_date, start_val=100000, commission=commission, impact=impact) normed_port_ms = port_vals_ms / port_vals_ms.ix[0] normed_port_sl = port_vals_sl / port_vals_sl.ix[0] normed_bench = benchmark_vals / benchmark_vals.ix[0] dates = pd.date_range(start_date, end_date) prices_all = get_data([symbol], dates, addSPY=True, colname='Adj Close') prices = prices_all[symbol] # only portfolio symbols # get indicators lookback = 14 _, PSR = id.get_SMA(prices, lookback) _, _, bb_indicator = id.get_BB(prices, lookback) momentum = id.get_momentum(prices, lookback) # figure 5. plt.figure(figsize=(12, 6.5)) top = plt.subplot2grid((5, 1), (0, 0), rowspan=3, colspan=1) bottom = plt.subplot2grid((5, 1), (3, 0), rowspan=2, colspan=1, sharex=top) # plot the Long or short action for index, marks in df_trades_sl.iterrows(): if marks['Trades'] > 0: plt.axvline(x=index, color='blue', linestyle='dashed', alpha=.9) elif marks['Trades'] < 0: plt.axvline(x=index, color='black', linestyle='dashed', alpha=.9) else: pass top.xaxis_date() top.grid(True) top.plot(normed_port_sl, lw=2, color='red', label='Q-Learning Strategy') top.plot(normed_port_ms, lw=1.5, color='black', label='Manual Strategy') top.plot(normed_bench, lw=1.2, color='green', label='Benchmark') top.set_title( 'Machine Learning Strategy (MLS) V.S. Manual Strategy (MS) - Out Sample Analysis' ) top.set_ylabel('Normalized Value') for index, marks in df_trades_sl.iterrows(): if marks['Trades'] > 0: top.axvline(x=index, color='blue', linestyle='dashed', alpha=.9) elif marks['Trades'] < 0: top.axvline(x=index, color='black', linestyle='dashed', alpha=.9) else: pass bottom.plot(momentum, color='olive', lw=1, label="momentum") bottom.plot(PSR, color='purple', lw=1, label="PSR") #bottom.plot(bb_indicator, color='blue', lw=1, label="Bollinger") bottom.set_title('Indicators') bottom.axhline(y=-0.2, color='grey', linestyle='--', alpha=0.5) bottom.axhline(y=0, color='grey', linestyle='--', alpha=0.5) bottom.axhline(y=0.2, color='grey', linestyle='--', alpha=0.5) bottom.legend() top.legend() top.axes.get_xaxis().set_visible(False) plt.xlim(start_date, end_date) filename = '02_MLS_outsample.png' plt.savefig(filename) plt.close() port_cr_sl, port_adr_sl, port_stddr_sl, port_sr_sl = ManualStrategy.get_portfolio_stats( port_vals_sl) port_cr_ms, port_adr_ms, port_stddr_ms, port_sr_ms = ManualStrategy.get_portfolio_stats( port_vals_ms) bench_cr, bench_adr, bench_stddr, bench_sr = ManualStrategy.get_portfolio_stats( benchmark_vals) # Compare portfolio against benchmark print "=== Machine Learning Strategy (MLS) V.S. Manual Strategy (MS) OUT Sample ===" print "Date Range: {} to {}".format(start_date, end_date) print print "Sharpe Ratio of MLS: {}".format(port_sr_sl) print "Sharpe Ratio of MS: {}".format(port_sr_ms) print "Sharpe Ratio of BenchMark : {}".format(bench_sr) print print "Cumulative Return of MLS: {}".format(port_cr_sl) print "Cumulative Return of MS: {}".format(port_cr_ms) print "Cumulative Return of Benchmark : {}".format(bench_cr) print print "Standard Deviation of MLS: {}".format(port_stddr_sl) print "Standard Deviation of MS: {}".format(port_stddr_ms) print "Standard Deviation of Benchmark : {}".format(bench_stddr) print print "Average Daily Return of MLS: {}".format(port_adr_sl) print "Average Daily Return of MS: {}".format(port_adr_ms) print "Average Daily Return of BenchMark : {}".format(bench_adr) print print "Final MLS Portfolio Value: {}".format(port_vals_sl[-1]) print "Final MS Portfolio Value: {}".format(port_vals_ms[-1]) print "Final Benchmark Portfolio Value: {}".format(benchmark_vals[-1]) print
def addEvidence(self, symbol = "IBM", \ sd=dt.datetime(2008,1,1), \ ed=dt.datetime(2009,1,1), \ sv = 10000,n_bins=6): # this method should create a QLearner, and train it for trading syms = [symbol] dates = pd.date_range(sd, ed) prices, prices_SPY = id.get_price(syms, dates) if self.verbose: print prices daily_returns = (prices / prices.shift(1)) - 1 daily_returns = daily_returns[1:] # get indicators and combine them into as a feature data_frame lookback = 14 _, PSR = id.get_SMA(prices, lookback) _, _, bb_indicator = id.get_BB(prices, lookback) momentum = id.get_momentum(prices, lookback) _, self.pbins = pd.qcut(PSR, n_bins, labels=False, retbins=True) _, self.bbins = pd.qcut(bb_indicator, n_bins, labels=False, retbins=True) _, self.mbins = pd.qcut(momentum, n_bins, labels=False, retbins=True) self.pbins = self.pbins[1:-1] self.bbins = self.bbins[1:-1] self.mbins = self.mbins[1:-1] # start training converged = False df_trades = None count = 0 old_cum_ret = 0.0 converge_count = 0 converged_prev = False #print "Total number of states is:", total_states # Initialize QLearner, self.learner = ql.QLearner(num_states=100 * n_bins, num_actions=self.num_actions, alpha=0.5, gamma=0.9, rar=0.0, radr=0.0, dyna=0, verbose=self.verbose) while (not converged) and (count < 100): # Set first state to the first data point (first day) indices = daily_returns.index holdings = pd.DataFrame(np.nan, index=indices, columns=['Holdings']) #first_state = self.indicators_to_state(PSR.iloc[0], bb_indicator.iloc[0], momentum.iloc[0]) #print("SL 152: holdings.iloc[0] = ", holdings.iloc[0][0], "; daily_rets.iloc[1] = ", daily_returns.iloc[1][0]) holdings.iloc[0] = 0. #print("SL 153") #df_prices = prices.copy() #df_prices['Cash'] = pd.Series(1.0, index=indices) #df_trades = df_prices.copy() #df_trades[:] = 0.0 state = self.indicators_to_state(PSR.iloc[0], bb_indicator.iloc[0], momentum.iloc[0]) action = self.learner.querysetstate(state) holdings.iloc[0], reward = self.apply_action( holdings.iloc[0][0], action, daily_returns.iloc[1][0]) #print("SL 171: PSR.shape[0] = ",PSR.shape[0],"; daily_returns.shape[0] = ",daily_returns.shape[0]) # Cycle through dates for j in range(1, daily_returns.shape[0]): state = self.indicators_to_state(PSR.iloc[j], bb_indicator.iloc[j], momentum.iloc[j]) # Get action by Query learner with current state and reward to get action action = self.learner.query(state, reward) # update reward and holdings with the new action. holdings.iloc[j], reward = self.apply_action( holdings.iloc[j - 1][0], action, daily_returns.iloc[j][0]) #print("SL 183: holdings.iloc[j][0] = ",holdings.iloc[j][0]) # Implement action returned by learner and update portfolio #print("SL 206: one learning is done.") #print("SL 215, holdings.iloc[0]",holdings.iloc[0]) holdings.iloc[-1] = 0 holdings.ffill(inplace=True) holdings.fillna(0, inplace=True) #print("SL 216 holdings = ",holdings) trades = holdings.diff() trades.iloc[0] = 0 # buy and sell happens when the difference change direction df_trades = pd.DataFrame(data=trades.values, index=indices, columns=['Trades']) df_orders, _ = generate_orders(df_trades, symbol) port_vals = compute_portvals(df_orders, sd=sd, ed=ed, impact=self.impact, start_val=sv, commission=self.commission) cum_ret, _, _, _ = get_portfolio_stats(port_vals) count += 1 old_cum_ret,converged_prev,converge_count,converged = \ check_convergence(old_cum_ret,cum_ret,converged_prev,converge_count) # check if converge #if converged: #print("SL 212: converged at iteration # ",count, "cum_ret is: ", cum_ret) return df_trades # 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 SMA_test(): data = load_data(DATA_STOCK) SMA = get_SMA(data["close"].tolist(), 0, data["close"].shape[0]) print(SMA)
def testPolicy(self, symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000): ''' The input parameters are: symbol: the stock symbol to act on sd: A datetime object that represents the start date ed: A datetime object that represents the end date sv: Start value of the portfolio The output df_trades in form: Date | Symbol | Order | Shares ''' # window = 10 dates = pd.date_range(sd, ed) symbols = symbol prices = ut.get_data([symbol], dates) prices.fillna(method='ffill', inplace=True) prices.fillna(method='bfill', inplace=True) # print("prices[:20]") # print(prices[:20]) # print(len(prices)) # 1. compute the 3 indicators from indicator.py # note: first 10 days might be no tradings since they are within the window time frame and 3 indicators rerturns N/A SMA = ind.get_SMA(prices, symbols, self.window) price_SMA_ratio = ind.get_SMA_ratio(prices, symbols, self.window) momentum = ind.get_momentum(prices, self.window) bb_value = ind.get_bollinger_band(prices, symbols, self.window) # print('Price/SMA ratio print here:') # print(price_SMA_ratio[:20]) # print(len(price_SMA_ratio)) # print('================') # print('momentum print here:') # print(momentum[:20]) # print(len(momentum)) # print('================') # print('bb_value print here:') # print(bb_value[:20]) # print('================') #This is for creating the dataframe for df_trades symbol_list = [] date_range = [] price_of_day = [] order = [] share = [] current_shares = 0 net_hold = 0 #net_hold = 0 no share, net_hold = 1: long position, net_hold = -1 shorted position buy_date = [] sell_date = [] #for SMA_ratio: small should buy, large should sell #for momemtum: negative should sell, positive should buy #for bb_value: small should buy, large should sell for i in range(len(prices.index) - 1): # for i in 0 - 504 symbol_list.append(symbol) date_range.append(prices.index[i]) price_of_day.append(prices[symbol][i]) #when you should buy if price_SMA_ratio['Price/SMA ratio'][i] < 0.95 or momentum[symbol][ i] > 0.3 or bb_value[i] <= 0.6 and current_shares == 0: order.append("BUY") share.append(1000) current_shares += 1000 elif price_SMA_ratio['Price/SMA ratio'][i] < 0.95 or momentum[ symbol][ i] > 0.3 or bb_value[i] <= 0.6 and current_shares < 0: order.append("BUY") share.append(2000) current_shares += 2000 #when you should sell elif price_SMA_ratio['Price/SMA ratio'][i] > 1.0 or momentum[ symbol][i] < -0.15 or bb_value[ i] > 0.9 and current_shares == 0: order.append("SELL") share.append(-1000) current_shares += -1000 elif price_SMA_ratio['Price/SMA ratio'][i] > 1.0 or momentum[ symbol][ i] < -0.15 or bb_value[i] > 0.9 and current_shares > 0: order.append("SELL") share.append(-2000) current_shares += -2000 else: order.append("HOLD") share.append(0) current_shares += 0 #create the dataframe to store the result df_trades = pd.DataFrame( index=date_range, columns=['Symbol', 'Prices', 'Order', 'Shares']) df_trades['Symbol'] = symbol_list df_trades['Prices'] = price_of_day df_trades['Order'] = order df_trades['Shares'] = share # print("df_trades[:20]") # print(df_trades[:20]) # print(len(df_trades)) return df_trades