def test_code(): # we only use the JPM data and the same manual strategy # we test the in-sample data for the calculation symbol = 'JPM' sd = dt.datetime(2008, 1, 1) ed = dt.datetime(2009, 12, 31) index = pd.date_range(sd, ed) in_sample_data = pd.DataFrame(index) df_data = get_data(['JPM'], index) trading_days = df_data.index sv = 100000 start_share = 1000 # normalization for the data df_data = df_data / df_data.iloc[0, :] # we only analyze JPM data, select the JPM data file here # print(df_JPM, '...') JPM_prices = df_data[['JPM']] df_trade = indicator_frame(df_data[['SPY']]) # Benchmark performance for the calculation benchmark_val = pd.DataFrame([start_share] + [0] * (len(trading_days) - 1), columns=[symbol], index=trading_days) benchmark_val = compute_portvals(benchmark_val, start_val=sv, commission=0, impact=0.005) benchmark_val /= benchmark_val.iloc[0, :] # Test: case ms = ManualStrategy() df_trades_hard = ms.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=sv, bbr_top=0.91, bbr_bottom=-0.91, SMA_ratio_up=1.02, SMA_ratio_low=0.99) port_vals_hard = compute_portvals(df_trades_hard, start_val=sv, commission=0, impact=0.005) port_vals_hard /= port_vals_hard.iloc[0, :] # Test: case for strategy_learn learner = sl.StrategyLearner(verbose=False) learner.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=sv) df_trades_strategy = learner.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=sv) port_vals_strategy = compute_portvals(df_trades_strategy, start_val=sv, commission=0, impact=0.005) port_vals_strategy /= port_vals_strategy.iloc[0, :] # Plot the data fig, ax = plt.subplots() benchmark_val.plot(ax=ax, color='green') port_vals_hard.plot(ax=ax, color='red') port_vals_strategy.plot(ax=ax, color='black') plt.legend(["Benchmark", "Manual Strategy", "Strategy Learner"]) plt.title("Comparison of different method for in sample data") plt.xlabel("Dates") plt.ylabel("Normalizaed value") ax = plt.gca() ax.xaxis.grid(True, which='Major', linestyle='--') ax.yaxis.grid(True, which='Major', linestyle='--') plt.show()
def testStrategy(symbol, sd, ed, sv=100000): # Set the symbol and the time period sd = sd ed = ed # Get the orders ############################################################# sleanrer = sl.StrategyLearner() sleanrer.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=sv) order_index = sleanrer.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=sv) order_index = order_index[order_index.iloc[:, 0] != 0] order_index['Order'] = '' order_index.loc[order_index['Trade'] > 0, 'Order'] = 'BUY' order_index.loc[order_index['Trade'] < 0, 'Order'] = 'SELL' order_index['Shares'] = order_index.loc[:, 'Trade'].abs() order_index['Symbol'] = symbol order_index.reset_index(inplace=True) order_index = order_index.iloc[:, [0, 4, 2, 3]] order_index.columns = ['Date', 'Symbol', 'Order', 'Shares'] df_trades=order_index ###################################################################### # Get the portfolio value and the benchmark value matrix_v_port = mktsim.compute_portvals(df_trades, start_val=sv, commission=9.95, impact=0.005, sd=sd, ed=ed)[0] data_bench = df_trades.iloc[:1] data_bench.iloc[0, 2] = 'BUY' matrix_v_bench = mktsim.compute_portvals(data_bench, start_val=sv, commission=9.95, impact=0.005, sd=sd, ed=ed)[ 0] # Get the orders data for vertical lines orders = mktsim.compute_portvals(df_trades, start_val=sv, commission=9.95, impact=0.005, sd=sd, ed=ed)[1] # Get the statistics for the portfolio daily_ret = matrix_v_port.pct_change() adr = matrix_v_port.pct_change().mean() sddr = matrix_v_port.pct_change().std() cr = matrix_v_port.iloc[-1] / matrix_v_port.iloc[0] - 1 # Get the statistics for the benchmark daily_ret_bench = matrix_v_bench.pct_change() adr_bench = matrix_v_bench.pct_change().mean() sddr_bench = matrix_v_bench.pct_change().std() cr_bench = matrix_v_bench.iloc[-1] / matrix_v_port.iloc[0] - 1 # Get the normalized data for the two portfolios port_norm = matrix_v_port / matrix_v_port.iloc[0] bench_norm = matrix_v_bench / matrix_v_bench.iloc[0] print 'Cumulative return of the benchmark is ', cr_bench print 'Cumulative return of the portfolio is ', cr print 'Stdev of the daily return of the benchmark is ', sddr_bench print 'Stdev of the daily return of the portfolio is ', sddr print 'Mean of the daily return of the benchmark is ', adr_bench print 'Mean of the daily return of the portfolio is ', adr print 'The trading strategy is \n', df_trades pl_data = pd.concat([port_norm, bench_norm], axis=1) pl_data.columns = ['Manual Strategy', 'Benchmark'] ms.plot_data(pl_data, orders=orders)
def evaluate(symbol, sd, ed, startVal): ts = ManualStrategy.ManualStrategy() trades_df = ts.testPolicy(symbol, sd, ed, startVal) port_vals = compute_portvals(trades_df, symbol, startVal) indices = port_vals.index.values print "Cumulative returns manual strategy is: ", \ port_vals.ix[indices[-1], CONSTANTS.TOTAL]/port_vals.ix[indices[0], CONSTANTS.TOTAL] port_vals_benchmark = compute_portvals(benchmark(symbol, sd, ed), symbol, startVal) print "Cumulative returns benchmark is: ", \ port_vals_benchmark.ix[indices[-1], CONSTANTS.TOTAL]/port_vals_benchmark.ix[indices[0], CONSTANTS.TOTAL] ts = StrategyLearner.StrategyLearner(impact=0) ts.addEvidence(symbol, sd, ed, startVal) trades_df = ts.testPolicy(symbol, sd, ed, startVal) port_vals_strategy = compute_portvals(trades_df, symbol, startVal) print "Cumulative returns strategy learner is: ", \ port_vals_strategy.ix[indices[-1], CONSTANTS.TOTAL]/port_vals_strategy.ix[indices[0], CONSTANTS.TOTAL] plot_df = pd.DataFrame(index=port_vals.index) plot_df["Manual Portfolio"] = port_vals[CONSTANTS.TOTAL] plot_df["Benchmark"] = port_vals_benchmark[CONSTANTS.TOTAL] plot_df["Strategy Learner"] = port_vals_strategy[CONSTANTS.TOTAL] plot_df = plot_df / plot_df.ix[plot_df.index.values[0]] plot_df.plot(color=['red', 'green', 'blue'], linewidth=1) plt.savefig('experiment1.png')
def main(): sd = dt.date(2008,1,1) ed = dt.date(2009,12,31) sv = 100000 symbol = ['JPM'] dates = dates = pd.date_range(sd, ed) prices_all = ut.get_data(symbol, dates) str_learn = sl.StrategyLearner(verbose= False, impact=0.0) str_learn.addEvidence('JPM', sd, ed, sv) df_str = str_learn.testPolicy('JPM', sd, ed, sv) df_trades,df_benchmark = ms.testPolicy('JPM', sd, ed, sv) portfolio_stand = compute_portvals(df_str,sv,0.0,0.0) compute(portfolio_stand) port_ms = compute_portvals(df_trades,sv,0.0,0.0) compute(port_ms) port_bench = compute_portvals(df_benchmark,sv,0.0,0.0) compute(port_bench) chart = pd.concat([portfolio_stand, port_ms,port_bench], axis=1) chart.columns = ['Portfolio Strategy Learner','Portfolio Manual Strategy', 'Portfolio Benchmark'] chart.plot(grid=True, title='Comparison of Portfolio Values', use_index=True, color=['Red', 'Blue','Black']) plt.savefig("Comp") plt.show()
def addEvidence(self, symbol = "IBM", \ sd=dt.datetime(2008,1,1), \ ed=dt.datetime(2009,12,31), \ sv = 10000): # add your code to do learning here # example usage of the old backward compatible util function # syms=[symbol] # dates = pd.date_range(sd, ed) # prices_all = util.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 = util.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 data4train = manu.training_data(symbol, sd, ed, self.impact) #convert to np array data4train = data4train.as_matrix() trainX = data4train[:, 0:-1] trainY = data4train[:, -1] # create a learner and train it; API for BagLearner #self.learner = bl.BagLearner(rtl.RTLearner, kwargs={"leaf_size":7}, bags=10, boost=False, verbose=False) # a Random Forest learner self.learner.addEvidence(trainX, trainY) # train it # evaluate in sample predY = self.learner.query(trainX) # get the predictions
def ms_testStrategy(symbol, sd, ed, sv=100000): # Set the symbol and the time period # Get the orders ############################################################# sd = sd ed = ed # Get the orders df_trades = ms.testPolicy(symbol=symbol, sd=sd, ed=ed) ###################################################################### # Get the portfolio value and the benchmark value matrix_v_port = mktsim.compute_portvals(df_trades, start_val=sv, commission=9.95, impact=0.05, sd=sd, ed=ed)[0] data_bench = df_trades.iloc[:1] data_bench.iloc[0, 2] = 'BUY' matrix_v_bench = mktsim.compute_portvals(data_bench, start_val=sv, commission=9.95, impact=0.05, sd=sd, ed=ed)[ 0] # Get the orders data for vertical lines orders = mktsim.compute_portvals(df_trades, start_val=sv, commission=9.95, impact=0.05, sd=sd, ed=ed)[1] # Get the statistics for the portfolio daily_ret = matrix_v_port.pct_change() adr = matrix_v_port.pct_change().mean() sddr = matrix_v_port.pct_change().std() cr = matrix_v_port.iloc[-1] / matrix_v_port.iloc[0] - 1 # Get the statistics for the benchmark daily_ret_bench = matrix_v_bench.pct_change() adr_bench = matrix_v_bench.pct_change().mean() sddr_bench = matrix_v_bench.pct_change().std() cr_bench = matrix_v_bench.iloc[-1] / matrix_v_port.iloc[0] - 1 # Get the normalized data for the two portfolios port_norm = matrix_v_port / matrix_v_port.iloc[0] bench_norm = matrix_v_bench / matrix_v_bench.iloc[0] print 'Cumulative return of the benchmark is ', cr_bench print 'Cumulative return of the portfolio is ', cr print 'Stdev of the daily return of the benchmark is ', sddr_bench print 'Stdev of the daily return of the portfolio is ', sddr print 'Mean of the daily return of the benchmark is ', adr_bench print 'Mean of the daily return of the portfolio is ', adr print 'The trading strategy is \n', df_trades pl_data = pd.concat([port_norm, bench_norm], axis=1) pl_data.columns = ['Manual Strategy', 'Benchmark'] ms.plot_data(pl_data, orders=orders)
def experiment1_run(): # Simulation settings symbol = "JPM" sd = dt.datetime(2008, 1, 1) ed = dt.datetime(2009, 12, 31) sv = 100000 impact = 0.005 commission = 9.95 # Evaluate strategy learner learner = sl.StrategyLearner(verbose=False, impact=impact, commission=commission) # Constructor learner.add_evidence(symbol=symbol, sd=sd, ed=ed, sv=sv) # Training phase df_trades_learner = learner.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=sv) # Testing phase df_trades_learner['Symbol'] = symbol # Add symbol column df_trades_learner['Shares'] = df_trades_learner['Trade'] sl_portvals = ms.compute_portvals(df_trades_learner, sv, commission, impact) # Compute portvals # Evaluate manual strategy manual = mst.ManualStrategy(verbose=False) df_trades_manual = manual.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=sv) df_trades_manual['Symbol'] = symbol df_trades_manual['Shares'] = df_trades_manual['Trade'] ms_portvals = ms.compute_portvals(df_trades_manual, sv, commission, impact) # Compute portfolio values of benchmark dates = pd.date_range(sd, ed) prices_all = ut.get_data([symbol], dates) # automatically adds SPY # Create comparison dataframe compare_df = pd.DataFrame(index=df_trades_manual.index) compare_df['Learner Portfolio'] = sl_portvals.values compare_df['Manual Portfolio'] = ms_portvals.values compare_df['Benchmark Portfolio'] = prices_all[symbol].values compare_df['Learner Portfolio'] = compare_df[ 'Learner Portfolio'] / compare_df['Learner Portfolio'].iloc[0] compare_df['Manual Portfolio'] = compare_df[ 'Manual Portfolio'] / compare_df['Manual Portfolio'].iloc[0] compare_df['Benchmark Portfolio'] = compare_df[ 'Benchmark Portfolio'] / compare_df['Benchmark Portfolio'].iloc[0] # Plot learner vs. manual portfolio fig, ax = plt.subplots() ax = compare_df[[ 'Learner Portfolio', 'Manual Portfolio', 'Benchmark Portfolio' ]].plot(title='Learner vs. Manual vs. Benchmark Performance', fontsize=12, ax=ax, grid=True) ax.set_xlabel("Date") ax.set_ylabel("Normalized Returns") ax.legend() fig.savefig('learner_vs_manual_portolfio.png', dpi=300)
def compare(): #manual trades manual_trades = ms.testPolicy() df_manual = msc.compute_portvals(df_orders=manual_trades) df_manual['portfolio'] = df_manual['portval'] / df_manual['portval'][0] #print(df_manual) # q learner trades random.seed(100) learner = sl.StrategyLearner() learner.addEvidence(symbol='JPM', sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000) learner_trades = learner.testPolicy(symbol='JPM', sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000) df_learner = msc.compute_portvals(df_orders=learner_trades) df_learner['portfolio'] = df_learner['portval'] / df_learner['portval'][0] print(df_learner['portfolio'][-1]) #print(df_learner['portfolio'][-1]-df_learner['portfolio'][0]) # benchmark sd = dt.datetime(2008, 1, 1) ed = dt.datetime(2009, 12, 31) df_benm = get_data(['JPM'], pd.date_range(sd, ed)) df_benm.drop(['SPY'], axis=1, inplace=True) commission = 9.95 impact = 0.005 sv = 100000 cash_hold = sv - df_benm.iloc[ 0, 0] * 1000 - commission - impact * 1000 * df_benm.iloc[0, 0] df_benm['portval'] = cash_hold + df_benm['JPM'] * 1000 df_benm['portfolio'] = df_benm['portval'] / df_benm.iloc[0, 1] # plot chart plt.figure(3, figsize=(8, 5)) plt.title('Portfolios of Trade Strategies') plt.plot(df_manual.index, df_manual['portfolio'], label='Manual Strategy', color='blue') plt.plot(df_learner.index, df_learner['portfolio'], label='Q learner Strategy', color='red') plt.plot(df_benm.index, df_benm['portfolio'], label='Benchmark', color='black') plt.xlabel('TIME') plt.ylabel('Portfolio') plt.legend() #plt.show() plt.savefig('Experiment1')
def plotStuff(): slr = sl.StrategyLearner(verbose=False, impact=0.05) slr.addEvidence(symbol="JPM", ) df_trades_sl = slr.testPolicy(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 1, 1), sv=100000) df_trades_sl['Symbol'] = 'JPM' df_trades_sl['Order'] = 'BUY' df_trades_sl.loc[df_trades_sl.Shares < 0, 'Order'] = 'SELL' df_trades_sl = df_trades_sl[df_trades_sl.Shares != 0] df_trades_sl = df_trades_sl[['Symbol', 'Order', 'Shares']] portvals_sl = ms.compute_portvals(df_trades_sl, start_val=100000, impact=0.05) df_trades = mst.testPolicy(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 1, 1), sv=100000) portvals = ms.compute_portvals(df_trades, start_val=100000, impact=0.05) syms = ['SPY'] dates = pd.date_range(dt.datetime(2008, 1, 1), dt.datetime(2009, 1, 1)) prices_all = ut.get_data(syms, dates) # automatically adds SPY prices_SPY = prices_all['SPY'] # only SPY, for comparison later prices_SPY_normalized = normalize_stocks(prices_SPY) prices_portval_normalized = normalize_stocks(portvals) prices_sl_normalized = normalize_stocks(portvals_sl) chart_df = pd.concat([ prices_portval_normalized, prices_SPY_normalized, prices_sl_normalized ], axis=1) chart_df.columns = ['Manual Strategy', 'Benchmark', 'Strategy Learner'] chart_df.plot(grid=True, title='Comparing Manual strategy with Strategy Learner', use_index=True, color=['Black', 'Blue', 'Red']) cum_ret_sl, avg_daily_ret_sl, std_daily_ret_sl, sharpe_ratio_sl = ms.compute_portfolio_stats( prices_sl_normalized) cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = ms.compute_portfolio_stats( prices_portval_normalized) cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = ms.compute_portfolio_stats( prices_SPY_normalized) print('Sharpe Ratio Strategy Learner:{}'.format(sharpe_ratio_sl)) print('Sharpe Ratio Manual Strategy:{}'.format(sharpe_ratio)) print('Sharpe Ratio Benchmark:{}'.format(sharpe_ratio_SPY)) print('Cum Return Strategy Learner:{}'.format(cum_ret_sl)) print('Cum Return Manual Strategy:{}'.format(cum_ret)) print('Cum Return Benchmark:{}'.format(cum_ret_SPY)) plt.show()
def testManual(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000): manual = ms.ManualStrategy() manual_trades = manual.testPolicy(symbol, sd, ed, sv) manual_portvals = mkt.compute_portvals(manual_trades, sv, commission=9.95, impact=0.005) return manual_portvals
def experiment2(sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), symbol="JPM", sv=100000, impact=0.000, commission=0, graphnum=1): manual_trades = ms.testPolicy(symbol=symbol, sd=sd, ed=ed) # manual_count = manual_trades.astype(bool).sum(axis=0) # print "Number of trades in the Manual Strategy is:", manual_count learner = sl.StrategyLearner(verbose=False, impact=impact) learner.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=sv) sl_trades = learner.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=sv) strategy_count = sl_trades.astype(bool).sum(axis=0) print "Number of trades in the Strategy Learner is:", strategy_count manual_portval = mksim.compute_portvals(manual_trades, start_val=sv, commission=commission, impact=impact) # m_cr, m_adr, m_ssdr, m_sr = mksim.compute_portfolio_stats(manual_portval) # print "Cumulative Return of the Manual Strategy is:", m_cr sl_portval = mksim.compute_portvals(sl_trades, start_val=sv, commission=commission, impact=impact) sl_cr, sl_adr, sl_ssdr, sl_sr = mksim.compute_portfolio_stats(sl_portval) print "Cumulative Return of the Strategy Learner is:", sl_cr benchmark_trades = manual_trades.copy() benchmark_trades[:] = 0 benchmark_trades.ix[0, 0] = 1000 benchmark_trades.ix[-1, 0] = -1000 benchmark_portvals = mksim.compute_portvals(benchmark_trades, start_val=sv, commission=commission, impact=impact) plot_df = manual_trades.copy() plot_df['Manual Strategy'] = manual_portval[0:] / manual_portval.ix[0] plot_df['Strategy Learner'] = sl_portval[0:] / sl_portval.ix[0] plot_df['Benchmark'] = benchmark_portvals[0:] / benchmark_portvals.ix[0] plot_df = plot_df.drop(plot_df.columns[0], axis=1) ax = plot_df.plot(title="Stock Prices", color=['b', 'k', 'r']) ax.set_xlabel("Date") ax.set_ylabel("Normalized Portfolio Return for In-Sample Data") plt.savefig("exp2_graph_{}".format(str(graphnum))) plt.clf()
def get_port_val_sl(impact, symbol, start_date, end_date, sv, commission=9.95): sl = StrategyLearner.StrategyLearner(impact=impact, commission=commission) sl.addEvidence(symbol=symbol, sd=start_date, ed=end_date, sv=100000, n_bins=5) df_trades_sl = sl.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) df_orders_sl, _ = ManualStrategy.generate_orders(df_trades_sl, symbol) port_vals_sl = ManualStrategy.compute_portvals(df_orders_sl, start_val=100000, sd=start_date, ed=end_date, commission=commission, impact=impact) normed_port_sl = port_vals_sl / port_vals_sl.ix[0] return normed_port_sl
def run(self): sd = dt.datetime(2008,1,1) ed = dt.datetime(2009,12,31) sv = 100000 sym = 'JPM' s_learner = sl.StrategyLearner(verbose=False, impact=0.000) s_learner.addEvidence(symbol=sym, sd=sd, ed=ed, sv=sv) sl_trades = s_learner.testPolicy(symbol=sym, sd=sd, ed=ed, sv=sv) prices = s_learner.prices m_learner = ml.ManualStrategy(title = "Manual Strategy vs. Stratey Learner vs. Benchmark") ml_trades, bench_trades = m_learner.testPolicy(symbol=sym, sd=sd, ed=ed, sv=sv) m_learner.plot(prices, bench_trades, ml_trades, sl_trades)
def testPolicy(self, symbol = "IBM", \ sd=dt.datetime(2009,1,1), \ ed=dt.datetime(2010,1,1), \ sv = 10000): current_holding = 0 dates = pd.date_range(sd, ed) prices_all = ut.get_data([symbol], dates) trades = prices_all[[ symbol, ]].copy(deep=True) trades_SPY = prices_all['SPY'] window_size = self.window_size idt_size = self.idt_size #check indicators #SMA,BB,MM = idt.calculate_indicators(symbols=[symbol],sd=sd,ed=ed,window_size=self.window_size,plot_fig=False) #check indicators prices = ut.get_data([symbol], pd.date_range(sd, ed)) prices = prices[symbol] SMA = manu.simple_moving_average(prices, window_size=20) # sma_int = prices[symbol]/SMA['SMA']-1 # BB = manu.bollinger_band(prices,window_size=20) # MM = manu.momentum(prices,window_size = 20) trades.values[:, :] = 0 Xtest = [] # prices = prices_all[symbol] for i in range(window_size + idt_size + 1, len(prices) - 1): data = np.array(SMA[i - idt_size:i]) #data = np.array(SMA[i-idt_size:i]) Xtest.append(data) result = self.learner.query(Xtest) for i, r in enumerate(result): if r > 0: trades.values[i + window_size + idt_size + 1, :] = 1000 - current_holding current_holding = 1000 elif r < 0: trades.values[i + window_size + idt_size + 1, :] = -1000 - current_holding current_holding = -1000 # if self.verbose: print type(trades) # if self.verbose: print trades # if self.verbose: print prices_all return trades
def experiment1(): ms = mas.MannualStrategy(symbol="JPM", sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), sv=100000) portval_ms = ms.manual_portfolio(symbol="JPM", sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), sv=100000, commission=0.0, impact=0.0) portval_benchmark = ms.benchmark_portfolio(symbol="JPM", sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), sv=100000, commission=0.0, impact=0.0) stl = sl.StrategyLearner() portval_stl, trades = stl.strategy_learner_result(symbol="JPM", sv=100000, sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), commission=0.0, impact=0.00) ax = portval_stl.plot(title="Experiment 1. Manual Strategy vs Benchmark vs Q Learning Strategy", fontsize=12, color="red", label="Q Learning Strategy") portval_benchmark.plot(ax=ax, color="blue", label="Benchmark") portval_ms.plot(ax=ax, color="black", label="Manual Strategy") ax.set_ylabel('Normalized Value') ax.set_xlabel('Dates') plt.grid(True) plt.legend(loc=0) plt.savefig("Experiment1.png") plt.show()
def main(): #Parameters sd_train = dt.datetime(2008,1,1) ed_train = dt.datetime(2009,12,31) sd_test = dt.datetime(2010,1,1) ed_test = dt.datetime(2011,12,31) sym = 'JPM' capital = 100000 #Train strategy learner learner = sl.StrategyLearner(verbose = False, impact = 0.0) learner.addEvidence(symbol = sym, sd=sd_train, ed=ed_train, sv=capital) #Test strategy learner #sl_trades = learner.testPolicy(symbol = sym, sd=sd_train, ed=ed_train, sv=capital) #In sample sl_trades = learner.testPolicy(symbol = sym, sd=sd_test, ed=ed_test, sv=capital) #Out of sample sl_portvals = msc.compute_portvals(sl_trades, start_val=capital, commission=0.0, impact=0.0) #Test manual strategy #ms_trades = ms.testPolicy(symbol = sym, sd = sd_train, ed = ed_train, sv = capital) #In sample ms_trades = ms.testPolicy(symbol = sym, sd = sd_test, ed = ed_test, sv = capital) #Out of sample ms_portvals = msc.compute_portvals(ms_trades, start_val=capital, commission=0.0, impact=0.0) #Benchmark: Buying 1000 shares of JPM and holding throughout period bench_trades = pd.DataFrame(0.0, index=ms_trades.index, columns = [sym]) bench_trades[sym].iloc[0] = 1000.0 bench_portvals = msc.compute_portvals(bench_trades, start_val=capital, commission=0.0, impact=0.0) #Calculate portfolio statistics for sl, ms, bench sl_cr, sl_adr, sl_sdr, sl_sr = compute_portfolio_stats(sl_portvals) ms_cr, ms_adr, ms_sdr, ms_sr = compute_portfolio_stats(ms_portvals) bench_cr, bench_adr, bench_sdr, bench_sr = compute_portfolio_stats(bench_portvals) #Plot performances sl_portvals_norm = sl_portvals/sl_portvals.iloc[0] ms_portvals_norm = ms_portvals/ms_portvals.iloc[0] bench_portvals_norm = bench_portvals/bench_portvals.iloc[0] df_compare = pd.DataFrame({'Strategy Learner': sl_portvals_norm.values, 'Manual Strategy': ms_portvals_norm.values, \ 'Benchmark': bench_portvals_norm.values}, index = sl_portvals_norm.index) #ax = df_compare.plot(title = 'Comparing Strategies - In Sample Period', fontsize=12) ax = df_compare.plot(title = 'Comparing Strategies - Out-of-Sample Period', fontsize=12) ax.set_xlabel('Date') ax.set_ylabel('Value') plt.show()
def experiment1(): mbs = ms.MannualStrategy(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000) portval_mbs = mbs.get_manualrule_portfolio(symbol="JPM", sv=100000, sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), commission=0.0, impact=0.00) portval_benchmark = mbs.get_benchmark_portfolio(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime( 2009, 12, 31), sv=100000, commission=0.0, impact=0.00) sbl = sl.StrategyLearner(verbose=False, impact=0.0) trades_sbl, portval_sbl = sbl.get_strategy_learner_portfolio( symbol="JPM", sv=100000, sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), commission=0.0, impact=0.00) ax = portval_sbl.plot( title= "Figure 1. Benchmark vs. QLearning Strategy vs. Manual Rule-based", fontsize=12, color="black", label="QLearner-based") portval_benchmark.plot(ax=ax, color="blue", label="Benchmark") portval_mbs.plot(ax=ax, color="red", label="Manual Rule-based") ax.set_ylabel('Normalized Value') ax.set_xlabel('Dates') plt.grid(True) plt.legend(loc=0) plt.savefig("Fig1.png") plt.show()
def experiment1(sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), symbol="JPM", sv=100000, impact=0, commission=0): manual_trades = ms.testPolicy(symbol=symbol, sd=sd, ed=ed) learner = sl.StrategyLearner(verbose=False, impact=impact) learner.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=sv) sl_trades = learner.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=sv) manual_portval = mksim.compute_portvals(manual_trades, start_val=sv, commission=commission, impact=impact) sl_portval = mksim.compute_portvals(sl_trades, start_val=sv, commission=commission, impact=impact) benchmark_trades = manual_trades.copy() benchmark_trades[:] = 0 benchmark_trades.ix[0, 0] = 1000 benchmark_trades.ix[-1, 0] = -1000 benchmark_portvals = mksim.compute_portvals(benchmark_trades, start_val=sv, commission=commission, impact=impact) plot_df = manual_trades.copy() plot_df['Manual Strategy'] = manual_portval[0:] / manual_portval.ix[0] plot_df['Strategy Learner'] = sl_portval[0:] / sl_portval.ix[0] plot_df['Benchmark'] = benchmark_portvals[0:] / benchmark_portvals.ix[0] plot_df = plot_df.drop(plot_df.columns[0], axis=1) # print plot_df ax = plot_df.plot(title="Stock Prices", color=['b', 'k', 'r']) ax.set_xlabel("Date") ax.set_ylabel("Normalized Portfolio Return for In-Sample Data") plt.savefig("exp1_graph1") plt.clf()
def test_code(): # Test in sample performance prices = get_prices(symbol=SYMBOL, sd=IN_SAMPLE_DATES[0], ed=IN_SAMPLE_DATES[1]) # Strategy Learner st_learner = sl.StrategyLearner(verbose=False, impact=IMPACT) st_learner.addEvidence(symbol=SYMBOL, sd=IN_SAMPLE_DATES[0], ed=IN_SAMPLE_DATES[1], sv=START_VAL) # Test in sample performance st_trades = st_learner.testPolicy(symbol=SYMBOL, sd=IN_SAMPLE_DATES[0], ed=IN_SAMPLE_DATES[1], sv=START_VAL) st_orders = st_learner.convert_trades_to_order(st_trades) st_portval = sim.compute_portvals(st_orders, start_val=START_VAL, commission=TRANSACTION_COMMISSION, impact=IMPACT, start_date=IN_SAMPLE_DATES[0], end_date=IN_SAMPLE_DATES[1]) st_portval = st_learner.normalize_df(st_portval) st_cr, st_adr, st_sddr, st_sr = sim.get_portfolio_stats(st_portval) print_performance("Strategy Learner", st_cr, st_adr, st_sddr, st_sr, st_portval) # Benchmark ben_portvals, ben_cr, ben_adr, ben_sddr, ben_sr = ms.benchmark( SYMBOL, IN_SAMPLE_DATES[0], IN_SAMPLE_DATES[1], START_VAL) print_performance("Benchmark", ben_cr, ben_adr, ben_sddr, ben_sr, ben_portvals) # Manual strategy ms_trades = ms.testPolicy(SYMBOL, IN_SAMPLE_DATES[0], IN_SAMPLE_DATES[1], START_VAL) ms_orders = ms.convert_trades_to_order(ms_trades) ms_portvals = ms.compute_portvals(df_orders=ms_orders, start_val=START_VAL, commission=TRANSACTION_COMMISSION, impact=IMPACT, start_date=IN_SAMPLE_DATES[0], end_date=IN_SAMPLE_DATES[1]) ms_portvals = ms.normalize(ms_portvals) ms_cr, ms_adr, ms_sddr, ms_sr = ms.get_portfolio_stats(ms_portvals) print_performance("Manual Strategy", ms_cr, ms_adr, ms_sddr, ms_sr, ms_portvals) plot_compare(ben_portvals, ms_portvals, st_portval)
def testPolicy(self, symbol = "IBM", \ sd=dt.datetime(2009,1,1), \ ed=dt.datetime(2010,1,1), \ sv = 10000): dates = pd.date_range(sd, ed) prices_all = util.get_data([symbol], dates) # automatically adds SPY price = prices_all[symbol] # only portfolio symbols # print price data4testX = manu.testing_data(symbol, sd, ed) #convert to np array data4testX = data4testX.as_matrix() testX = data4testX # query our learner, what to do in these days predPolicy = self.learner.query(testX) # get the predictions predPolicy = pd.Series(data=predPolicy, index=price.index) #convert to pdSeries # trades df based on predPolicy trades_df = price.copy() trades_df[:] = 0 # # Strategy based on predPolicy, fill in trades_df trades_df[predPolicy == 1] = 1000 trades_df[predPolicy == -1] = -1000 #trades_df.plot(title = "trades", label="trades" ) trades_df = trades_df.to_frame() #Convert to trades_delta_df trades_delta_df = trades_df.copy() for i in range(0, len(trades_df)): if i == 0: trades_delta_df.iloc[i] = trades_df.iloc[i] else: trades_delta_df.iloc[i] = trades_df.iloc[i] - trades_df.iloc[ i - 1] #print type(trades_delta_df) # will be passed into marketSim #return trades_df # used in my old marketsimcode return trades_delta_df # used in auto-grader
def experiment1(): sd = dt.datetime(2008, 1, 1) ed = dt.datetime(2009, 12, 31) sv = 100000 symbol = 'JPM' price = get_data([symbol], pd.date_range(sd, ed), False) price = price.dropna() # Manual Strategy manual_strategy = ms.ManualStrategy() manual_orders = manual_strategy.testPolicy([symbol], sd, ed, sv) manual_orders = manual_orders.reset_index() manual_portvals = compute_portvals(manual_orders, price, sd, ed, sv, 9.95, 0.005) norm_manual = manual_portvals / manual_portvals[0] benchmark = manual_strategy.get_benchmark(symbol, price, sd, ed, sv) norm_benchmark = benchmark / benchmark[0] # Strategy Learner strategy_learner = sl.StrategyLearner() strategy_learner.addEvidence(symbol, sd, ed, sv) strategy_orders = strategy_learner.testPolicy(symbol, sd, ed, sv) strategy_orders = strategy_orders.reset_index() strategy_portvals = compute_portvals(strategy_orders, price, sd, ed, sv, 9.95, 0.005) norm_strategy = strategy_portvals / strategy_portvals[0] # Plotting fig1, ax = plt.subplots() norm_manual.plot(label='Manual Strategy', color='#d63729') norm_benchmark.plot(label='Benchmark', color='#1594CC') norm_strategy.plot(label='Strategy Learner', color='#26D279') plt.title('JPM Stock In Sample') plt.ylabel('Normalized Portfolio Value') plt.xlabel('Date') plt.legend() plt.grid() fig1.savefig("experiment1.png") plt.close(fig1)
def addEvidence(self, symbol = "IBM", \ sd=dt.datetime(2008,1,1), \ ed=dt.datetime(2009,1,1), \ sv = 10000): window_size = self.window_size idt_size = self.idt_size #Check for N day return N = self.N threshold = max(0.03, 2 * self.impact) #check indicators prices = ut.get_data([symbol], pd.date_range(sd, ed)) prices = prices[symbol] SMA = manu.simple_moving_average(prices, window_size=20) # sma_int = prices[symbol]/SMA['SMA']-1 # BB = manu.bollinger_band(prices,window_size=20) # MM = manu.momentum(prices,window_size = 20) # MM = idt.calculate_indicators(symbols=[symbol],sd=sd,ed=ed,window_size=self.window_size,plot_fig=False) #turn regression model to Classification X = [] Y = [] for i in range(window_size + idt_size + 1, len(prices) - N): X.append(np.array(SMA[i - idt_size:i])) # X.append(np.array(SMA[i-idt_size:i])) gain = (prices.values[i + N] - prices.values[i]) / prices.values[i] if gain > threshold: Y.append(1) elif gain < -threshold: Y.append(-1) else: Y.append(0) X = np.array(X) Y = np.array(Y) self.learner.addEvidence(X, Y)
def test_code(): #in sample start_date = dt.datetime(2008,1,1) end_date = dt.datetime(2009,12,31) #out-of-sample dates # start_date = dt.datetime(2010,1,1) # end_date = dt.datetime(2011, 12, 31) #orders_df = bps.testPolicy(sd=start_date, ed=end_date) plt.figure() #I'm going to try to cheat orders_df = ms.testPolicy(sd=start_date, ed=end_date) benchmark_policy = ind.benchmark_policy(sd=start_date, ed=end_date) benchmark_policy.to_csv('benchmark.csv') orders_df.to_csv('orders.csv') #portvals_raw = compute_portvals_abridged(orders_df, commission=0, impact=0) portvals_raw = compute_portvals_abridged(orders_df, commission=9.95, impact=0.005) benchmark_portvals_raw = compute_portvals_abridged(benchmark_policy, commission=0, impact=0) #clean up portvals = extract_portvals_only(portvals_raw) benchmark_portvals = extract_portvals_only(benchmark_portvals_raw) cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = compute_portval_stats(portvals, rfr=0.0, sf=252, sv=100000) bench_cum_ret, bench_avg_daily_ret, bench_std_daily_ret, bench_sharpe_ratio = compute_portval_stats(benchmark_portvals, rfr=0.0, sf=252, sv=100000) #Get SPY data symbols = ['SPY'] allocations = [1] start_val = 1000000 risk_free_rate = 0.0 sample_freq = 252 # Assess the portfolio of SPY cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY, ev = assess_portfolio(sd = start_date, ed = end_date,syms = symbols, allocs = allocations,sv = 100000, gen_plot = False) # Compare portfolio against $SPX print "Date Range: {} to {}".format(start_date, end_date) print print "Sharpe Ratio of Fund: {}".format(sharpe_ratio) print "Sharpe Ratio of Benchmark: {}".format(bench_sharpe_ratio) print "Sharpe Ratio of SPY : {}".format(sharpe_ratio_SPY) print print "Cumulative Return of Fund: {}".format(cum_ret) print "Cumulative Return of Benchmark: {}".format(bench_cum_ret) print "Cumulative Return of SPY : {}".format(cum_ret_SPY) print print "Standard Deviation of Fund: {}".format(std_daily_ret) print "Standard Deviation of Benchmark: {}".format(bench_std_daily_ret) print "Standard Deviation of SPY : {}".format(std_daily_ret_SPY) print print "Average Daily Return of Fund: {}".format(avg_daily_ret) print "Average Daily Return of Benchmark: {}".format(bench_avg_daily_ret) print "Average Daily Return of SPY : {}".format(avg_daily_ret_SPY) print print "Final Portfolio Value: {}".format(portvals[-1]) print "Final Benchmark Value: {}".format(benchmark_portvals[-1]) print "Final SPY Value: {}".format(ev) benchmark_normalized = normalize_data(benchmark_portvals_raw) benchmark_normalized = extract_portvals_only(benchmark_normalized) best_portfolio_normalized = normalize_data(portvals_raw) best_portfolio_normalized = extract_portvals_only(best_portfolio_normalized) stock_library = make_df_to_match_trading_days(colnames=['Date', 'Value'], symbol='JPM', sd=start_date, ed=end_date) benchmark_line = plt.plot(stock_library.index, benchmark_normalized.values, label="Benchmark") plt.setp(benchmark_line, linestyle='-', color='b', linewidth=1.0) best_portfolio_line = plt.plot(stock_library.index, best_portfolio_normalized.values, label="Manual Strategy") plt.setp(best_portfolio_line, linestyle='-', color='k', linewidth=1.0) legend = plt.legend(loc='best', shadow=True) plt.title("Normalized chart for Portfolios") plt.ylabel("Normalized Value") plt.grid() plt.show()
def test_code(): # this is a helper function you can use to test your code # note that during autograding his function will not be called. # Define input parameters sv = 100000 commission = 0 impact = 0.0 commission_manual = 9.95 impact_manual = 0.005 sd_in = dt.datetime(2008, 1, 1) ed_in = dt.datetime(2009, 12, 31) optimal_df_trades = tos.testPolicy(symbol="JPM", sd=sd_in, ed=ed_in) optimal_portvals = compute_portvals(optimal_df_trades, start_val=sv, commission=commission, impact=impact) manual_df_trades = ms.testPolicy(symbol="JPM", sd=sd_in, ed=ed_in) manual_portvals = compute_portvals(manual_df_trades, start_val=sv, commission=commission_manual, impact=impact_manual) benchmark_df_trades = optimal_df_trades.copy() benchmark_df_trades[:] = 0 benchmark_df_trades.ix[0, 0] = 1000 benchmark_df_trades.ix[-1, 0] = -1000 benchmark_portvals = compute_portvals(benchmark_df_trades, start_val=sv, commission=commission, impact=impact) plot_df = optimal_df_trades.copy() plot_df['Benchmark'] = benchmark_portvals[0:] / benchmark_portvals.ix[0] plot_df['Optimal Strategy'] = optimal_portvals[0:] / optimal_portvals.ix[0] plot_df = plot_df.drop(plot_df.columns[0], axis=1) ax = plot_df.plot(title="Stock Prices", color=['b', 'k']) ax.set_xlabel("Date") ax.set_ylabel("Normalized Portfolio Return") plt.savefig("graph") plt.clf() manual_plot_df = plot_df.copy() manual_plot_df[ 'Manual Strategy'] = manual_portvals[0:] / manual_portvals.ix[0] manual_plot_df = manual_plot_df.drop('Optimal Strategy', axis=1) ax = manual_plot_df.plot(title="Stock Prices", color=['b', 'k']) for trade in manual_df_trades.index[manual_df_trades['JPM'] < 0]: ax.axvline(x=trade, color='red') for trade in manual_df_trades.index[manual_df_trades['JPM'] > 0]: ax.axvline(x=trade, color='green') ax.set_xlabel("Date") ax.set_ylabel("Normalized Portfolio Return") plt.savefig("graph2") plt.clf() ########################################################################################################################### sd_out = dt.datetime(2010, 1, 1) ed_out = dt.datetime(2011, 12, 31) manual_df_trades_out = ms.testPolicy(symbol="JPM", sd=sd_out, ed=ed_out) manual_portvals_out = compute_portvals(manual_df_trades_out, start_val=sv, commission=commission_manual, impact=impact_manual) benchmark_df_trades_out = manual_df_trades_out.copy() benchmark_df_trades_out[:] = 0 benchmark_df_trades_out.ix[0, 0] = 1000 benchmark_df_trades_out.ix[-1, 0] = -1000 benchmark_portvals_manual_out = compute_portvals( benchmark_df_trades_out, start_val=sv, commission=commission_manual, impact=impact_manual) manual_plot_df_out = manual_df_trades_out.copy() manual_plot_df_out['Benchmark'] = benchmark_portvals_manual_out[ 0:] / benchmark_portvals_manual_out.ix[0] manual_plot_df_out[ 'Manual Strategy (Out of Sample)'] = manual_portvals_out[ 0:] / manual_portvals_out.ix[0] manual_plot_df_out = manual_plot_df_out.drop(manual_plot_df_out.columns[0], axis=1) ax = manual_plot_df_out.plot(title="Stock Prices", color=['b', 'k']) for trade in manual_df_trades_out.index[manual_df_trades_out['JPM'] < 0]: ax.axvline(x=trade, color='red') for trade in manual_df_trades_out.index[manual_df_trades_out['JPM'] > 0]: ax.axvline(x=trade, color='green') ax.set_xlabel("Date") ax.set_ylabel("Normalized Portfolio Return") plt.savefig("graph3") plt.clf() # if isinstance(portvals, pd.DataFrame): # portvals = portvals[portvals.columns[0]] # just get the first column # else: # "warning, code did not return a DataFrame" # Get portfolio stats # Here we just fake the data. you should use your code from previous assignments. cr, adr, ssdr, sr = compute_portfolio_stats(optimal_portvals) cr_manual, adr_manual, ssdr_manual, sr_manual = compute_portfolio_stats( manual_portvals) cr_bm, adr_bm, ssdr_bm, sr_bm = compute_portfolio_stats(benchmark_portvals) cr_m_out, adr_m_out, ssdr_m_out, sr_m_out = compute_portfolio_stats( manual_portvals_out) start_date = dt.datetime(2008, 1, 1) end_date = dt.datetime(2009, 12, 31) # cum_ret_man, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = [0.2,0.01,0.02,1.5] # Compare portfolio against $SPX print "Date Range: {} to {}".format(start_date, end_date) print print "Sharpe Ratio of Optimal Strategy Fund (In Sample): {}".format(sr) print "Sharpe Ratio of Manual Strategy Fund (In Sample) : {}".format( sr_manual) print "Sharpe Ratio of Benchmark Strategy Fund (In Sample) : {}".format( sr_bm) print "Sharpe Ratio of Manual Strategy Fund (Out Sample) : {}".format( sr_m_out) print print "Cumulative Return of Optimal Strategy Fund (In Sample): {}".format( cr) print "Cumulative Return of Manual Strategy Fund (In Sample) : {}".format( cr_manual) print "Cumulative Return of Benchmark Strategy Fund (In Sample) : {}".format( cr_bm) print "Cumulative Return of Manual Strategy Fund (Out Sample) : {}".format( cr_m_out) print print "Standard Deviation of Optimal Strategy Fund (In Sample): {}".format( ssdr) print "Standard Deviation of Manual Strategy Fund (In Sample) : {}".format( ssdr_manual) print "Standard Deviation of Benchmark Strategy Fund (In Sample) : {}".format( ssdr_bm) print "Standard Deviation of Manual Strategy Fund (Out Sample) : {}".format( ssdr_m_out) print print "Average Daily Return of Optimal Fund (In Sample): {}".format(adr) print "Average Daily Return of Manual Strategy Fund (In Sample) : {}".format( adr_manual) print "Average Daily Return of Benchmark Strategy Fund (In Sample) : {}".format( adr_bm) print "Average Daily Return of Manual Strategy Fund (Out Sample) : {}".format( adr_m_out) print print "Final Optimal Strategy Fund Portfolio Value (In Sample): {}".format( optimal_portvals[-1]) print "Final Manual Strategy Fund Portfolio Value (In Sample): {}".format( manual_portvals[-1]) print "Final Benchmark Strategy Fund Portfolio Value (In Sample): {}".format( benchmark_portvals[-1]) print "Final Manual Strategy Fund Portfolio Value (Out Sample): {}".format( manual_portvals_out[-1]) # if __name__ == "__main__": # test_code()
import indicators from util import get_data import pandas as pd import datetime as dt import matplotlib.pyplot as plt import ManualStrategy import marketismcode 'manual' ms = ManualStrategy.ManualStrategy() def run(sd, ed, vert_lines): manual_orders = ms.testPolicy('JPM', sd, ed, 1000000) bench_orders = ms.benchmark('JPM', sd, ed, 1000000) manual_sim = marketismcode.compute_portvals(manual_orders) bench_sim = marketismcode.compute_portvals(bench_orders) manual_sim['value'] = manual_sim['value'] / manual_sim['value'][0] bench_sim['value'] = bench_sim['value'] / bench_sim['value'][0] long_positions = manual_sim['value'].where( manual_orders['JPM'] > 0).dropna(how="all") short_positions = manual_sim['value'].where( manual_orders['JPM'] < 0).dropna(how="all") daily_returns = (manual_sim['value'][1:] / manual_sim['value'][:-1].values) - 1 dr_mean = daily_returns.mean()
def experiment2(): sl = StrategyLearner() ms = ManualStrategy() start_date = dt.datetime(2008, 1, 1) end_date = dt.datetime(2009, 12, 31) symbol = 'JPM' starting_value = 100000 commission = 0 impact = 0.002 sl.addEvidence(symbol=symbol, sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=starting_value) qlearn_trades = sl.testPolicy(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=starting_value) manual_trades = ms.testPolicy(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=starting_value) benchmark_trades = ms.get_benchmark_order(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=starting_value) qlearn_portfolio = (mm.compute_portvals( qlearn_trades, starting_value, commission, impact)) / starting_value manual_potfolio = (mm.compute_portvals( manual_trades, starting_value, commission, impact)) / starting_value benchmark_portfolio = (mm.compute_portvals( benchmark_trades, starting_value, commission, impact)) / starting_value plt.clf() plt.figure(figsize=(15, 5)) plt.xlim(start_date, end_date) plt.ylim(0.5, 2.0) ax = qlearn_portfolio.plot( title= "Figure 1. QLearning Strategy vs. Manual Rule-based vs. Benchmark with 0.002 Impact", fontsize=12, color="green", label="QLearner-based") benchmark_portfolio.plot(ax=ax, color="black", label="Benchmark") manual_potfolio.plot(ax=ax, color="blue", label="Manual Rule-based") ax.set_ylabel('Normalized Value') ax.set_xlabel('Dates') plt.legend(['QLearner', 'Benchmark', "Manual Rule"], loc="lower right") plt.savefig("Impact1.png") #plt.show() #====================================================================================================================================================== impact_2 = 0.007 sl_2 = StrategyLearner() ms_2 = ManualStrategy() sl_2.addEvidence(symbol=symbol, sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=starting_value) qlearn_trades_2 = sl_2.testPolicy(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=starting_value) manual_trades_2 = ms.testPolicy(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=starting_value) benchmark_trades_2 = ms.get_benchmark_order(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=starting_value) qlearn_portfolio_2 = (mm.compute_portvals(qlearn_trades_2, starting_value, commission, impact_2)) / starting_value manual_potfolio_2 = (mm.compute_portvals(manual_trades_2, starting_value, commission, impact_2)) / starting_value benchmark_portfolio_2 = (mm.compute_portvals(benchmark_trades_2, starting_value, commission, impact_2)) / starting_value plt.clf() plt.figure(figsize=(15, 5)) plt.xlim(start_date, end_date) plt.ylim(0.5, 2.0) ax = qlearn_portfolio_2.plot( title= "Figure 1. QLearning Strategy vs. Manual Rule-based vs. Benchmark with 0.007 Impact", fontsize=12, color="green", label="QLearner-based") benchmark_portfolio_2.plot(ax=ax, color="black", label="Benchmark") manual_potfolio_2.plot(ax=ax, color="blue", label="Manual Rule-based") ax.set_ylabel('Normalized Value') ax.set_xlabel('Dates') plt.legend(['QLearner', 'Benchmark', "Manual Rule"], loc="lower right") plt.savefig("Impact2.png")
def author(): return 'agizatulina3' if __name__ == '__main__': random.seed(300) np.random.seed(300) start_value = 100000 start = '01-01-2008' end = '12-31-2009' symbol = 'JPM' manual_str = mst.ManualStrategy() learner_str = stg.StrategyLearner() learner_str.addEvidence(symbol, start, end, start_value) manual_trades = manual_str.testPolicy(symbol, start, end, start_value) learner_trades = learner_str.testPolicy(symbol, start, end, start_value) # Modify to make it work with compute_portvals learner_trades['Date'] = learner_trades.index learner_trades['Symbol'] = symbol learner_trades['Order'] = 'BUY' learner_trades['Shares'] = learner_trades[symbol] learner_trades.reset_index(drop=True, inplace=True) learner_df = learner_trades.copy()
sv = 100000 impact_list = [0, 0.005, 0.01, 0.03, 0.05, 0.07, 0.1] summary = pd.DataFrame(0, index = impact_list, columns = ['Trade#'] + ['Cumulative Return'] + ['Daily Standard Deviation'] + ['Average Daily Return']) ''' strategy learner ''' i = 0 plt.figure(figsize=(20,5)) random.seed(12) for impact in impact_list: random.seed(12) learner = sl.StrategyLearner(verbose = False, impact = impact) # constructor learner.addEvidence(symbol = symbol, sd=sd, ed=ed, sv = sv) # training phase df_trades = learner.testPolicy(symbol = symbol, sd=sd, ed=ed, sv = sv) # testing phase daily_value_strategy = msc.compute_portvals(order = df_trades, symbol = symbol, start_val = sv, commission=0, impact=impact)/sv num_trade = df_trades.loc[df_trades['Order'] != 0].shape[0] cr_strategy, adr_strategy, sddr_strategy = ms.portstats(daily_value_strategy) summary.loc[impact_list[i],'Trade#'] = num_trade summary.loc[impact_list[i],'Cumulative Return'] = cr_strategy summary.loc[impact_list[i],'Daily Standard Deviation'] = sddr_strategy summary.loc[impact_list[i],'Average Daily Return'] = adr_strategy i += 1 plt.plot(daily_value_strategy.index,daily_value_strategy, label = "Impact = " + str(impact)) plt.title("Learner behaviour with respect to the change of impact facotr") plt.legend() plt.xlabel("Date") plt.show()
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
marketsim.compute_portvals(data=df_trades, symbol=outsample_args['symbol'], commission=0.0, impact=0.0, sd=dt.datetime(2010, 1, 1), ed=dt.datetime(2011, 12, 31))[0] port_val_insample = \ marketsim.compute_portvals(data=df_trades_insample, symbol=insample_args['symbol'], commission=0.0, impact=0.0, sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31))[0] benchmark_outsample = learner.benchmark(**outsample_args) benchmark_insample = learner.benchmark(**insample_args) port_val_normal_insample = port_val_insample / port_val_insample.iloc[0] port_val_normal_outsample = port_val_outsample / port_val_outsample.iloc[0] benchmark_normal_outsample = benchmark_outsample / benchmark_outsample[0] benchmark_normal_insample = benchmark_insample / benchmark_insample[0] val_plot(1, port_val_normal_insample, benchmark_normal_insample, df_trades_insample, 'Strategy Learner: In Sample') val_plot(2, port_val_normal_outsample, benchmark_normal_outsample, df_trades, 'Strategy Learner: Out Sample') # ===============Manual Strategy============ tos = ms.ManualStrategy() df_both_in = tos.testPolicy(**insample_args) benchmark = tos.benchmark(**insample_args) benchmark_out = tos.benchmark(**outsample_args) df_both_out = tos.testPolicy(**outsample_args) port_val_both_in = \ marketsim.compute_portvals(data=df_both_in, symbol=insample_args['symbol'], commission=0.0, impact=0.0, sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31))[0] port_val_both_out = marketsim.compute_portvals(data = df_both_out ,symbol=outsample_args['symbol'], commission=0.0, impact=0.0, sd=dt.datetime(2010, 1, 1), ed=dt.datetime(2011, 12, 31))[0] port_val_both_normal = port_val_both_in / port_val_both_in.iloc[0] port_val_both_out = port_val_both_out / port_val_both_out.iloc[0]