def plot_data(df_trades_man, df_trades_strat, df_trades_base, trader_name, symbol="JPM"): portvals_man = msc.compute_portvals(df_trades_man, symbol) portvals_strat = msc.compute_portvals(df_trades_strat, symbol) portvals_base = msc.compute_portvals(df_trades_base, symbol) plt.plot(portvals_man / portvals_man[0], color="red") plt.plot(portvals_strat / portvals_strat[0], color="orange") plt.plot(portvals_base / portvals_base[0], color="green") # for index, val in df_trades.iterrows(): # # print(index) # # print(val['Trades']) # if val['Trades'] > 0: # plt.axvline(index,color='blue') # if val['Trades'] < 0: # plt.axvline(index,color='black') plt.xticks(rotation=30) plt_name = "Experiment 1 - Normalized Returns " + trader_name plt.legend(["Manual", "Strategy", "Benchmark"], loc="bottom left") plt.xlabel("Date") plt.ylabel("Normalized Returns") plt.title(plt_name) plt.savefig(plt_name + '.png', bbox_inches="tight") plt.close()
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 theoretically_optimal_strategy(sd=dt(2008, 1, 1), ed=dt(2009, 12, 31), symbols=['JPM'], commission=0.00, impact=0.00): df_trades = testPolicy(symbols=symbols, sd=sd, ed=ed) portvals = ms.compute_portvals(df_trades, sd=sd, ed=ed, commission=commission, impact=impact) portvals_benchmark = ms.portfolio_vals_benchmark(sd=sd, ed=ed, symbols=symbols) final_portvals_benchmark = ms.compute_portvals(portvals_benchmark, sd=sd, ed=ed, commission=commission, impact=impact) ms.compute_portfolio_stats(portvals) ms.compute_portfolio_stats(final_portvals_benchmark) adjusted_portvals = portvals / portvals.ix[0, :] adjusted_portvals_benchmark = final_portvals_benchmark / final_portvals_benchmark.ix[ 0, :] plot_theoretically_optimal_strategy(symbols, adjusted_portvals, adjusted_portvals_benchmark)
def main(): start_date = '2008-1-1' end_date = '2009-12-31' symbols = 'JPM' bps = BestPossibleStrategy() df_trades = bps.testPolicy(symbols, start_date, end_date, 100000) port_vals = compute_portvals(orders=df_trades, start_val=100000, commission=0.00, impact=0.00) df_bchm = bps.benchMark(symbols, start_date, end_date, 100000) port_vals_bchm = compute_portvals(orders=df_bchm, start_val=100000, commission=0.00, impact=0.00) #print port_vals, port_vals_bchm cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = compute_portfolio_stats( port_vals) cum_ret_bchm, avg_daily_ret_bchm, std_daily_ret_bchm, sharpe_ratio_bchm = compute_portfolio_stats( port_vals_bchm) # Compare portfolio against $SPX print "Date Range: {} to {}".format(start_date, end_date) print print "Cumulative Return of Portfolio: {}".format(cum_ret) print "Cumulative Return of Benchmark: {}".format(cum_ret_bchm) print print "Standard Deviation of Portfolio: {}".format(std_daily_ret) print "Standard Deviation of Benchmark: {}".format(std_daily_ret_bchm) print print "Average Daily Return of Portfolio: {}".format(avg_daily_ret) print "Average Daily Return of Benchmark: {}".format(avg_daily_ret_bchm) print print "Sharpe Ratio of Portfolio: {}".format(sharpe_ratio) print "Sharpe Ratio of Benchmark: {}".format(sharpe_ratio_bchm) print print "Final Portfolio Value: {}".format(port_vals[-1]) print "Final Benchmark Value: {}".format(port_vals_bchm[-1]) port_vals_bchm_norm = port_vals_bchm / port_vals_bchm.ix[0, ] port_vals_norm = port_vals / port_vals.ix[0, ] port_vals_bchm_norm = port_vals_bchm_norm.to_frame() port_vals_norm = port_vals_norm.to_frame() f1 = plt.figure(1) re = port_vals_bchm_norm.join(port_vals_norm, lsuffix='_benchmark', rsuffix='_portfolio') re.columns = ['Benchmark', 'Value of the best possible portfolio'] ax = re.plot( title="Normalized Benchmark and Value of The Best Possible Portfolio", fontsize=12, color=["blue", "black"]) ax.set_xlabel("Date") ax.set_ylabel("Portfolio") f1.show() plt.show()
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 plot_data(df_trades, df_trades_base, trader_name, symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31)): portvals = msc.compute_portvals(df_trades, symbol) base_port_val = msc.compute_portvals(df_trades_base, symbol) plt.plot(portvals / portvals[0], color="red") plt.plot(base_port_val / base_port_val[0], color="green") for index, val in df_trades.iterrows(): # print(index) # print(val['Trades']) if val['Trades'] > 0: plt.axvline(index, color='blue') if val['Trades'] < 0: plt.axvline(index, color='black') plt.xticks(rotation=30) plt_name = trader_name + " - Normalized " + symbol + " Returns" plt.legend([trader_name, "Benchmark"], loc="bottom left") plt.xlabel("Date") plt.ylabel("Normalized Returns") plt.title(plt_name) plt.savefig(plt_name + '.png', bbox_inches="tight") plt.close()
def plot(self, benchmark, manual_trades): manual_port = ms.compute_portvals(manual_trades, commission=9.95, impact=0.005) bench_port = ms.compute_portvals(benchmark, commission=9.95, impact=0.005) manual_port = manual_port/manual_port.iloc[0] bench_port = bench_port/bench_port.iloc[0] ms.get_port_stats(manual_port, self.title + " Normalized Manual Strategy") ms.get_port_stats(bench_port, self.title + " Normalized Benchmark") plt.figure() plt.plot(bench_port, label = "Benchmark", color ="green") plt.plot(manual_port, label = "Manual Strategy", color ="red") longs = manual_trades.index[manual_trades[self.symbol] > 0] for long in longs: plt.axvline(x = long, color = "blue") shorts = manual_trades.index[manual_trades[self.symbol] < 0] for short in shorts: plt.axvline(x = short, color = "black") plt.title(self.title) plt.legend() plt.savefig(self.title + ".png")
def manual_strategy(sd=dt(2008, 1, 1), ed=dt(2009, 12, 31), symbols=['JPM'], master='in_sample'): df_trades = testPolicy(symbols=symbols, master=master, sd=sd, ed=ed) portvals = ms.compute_portvals(df_trades, sd=sd, ed=ed) portvals_benchmark = ms.portfolio_vals_benchmark(sd=sd, ed=ed, symbols=symbols) final_portvals_benchmark = ms.compute_portvals(portvals_benchmark, sd=sd, ed=ed) ms.compute_portfolio_stats(portvals) ms.compute_portfolio_stats(final_portvals_benchmark) adjusted_portvals = portvals / portvals.ix[0, :] adjusted_portvals_benchmark = final_portvals_benchmark / final_portvals_benchmark.ix[ 0, :] plot_in_out_sample_comparative_benchmark(symbols, adjusted_portvals, adjusted_portvals_benchmark, df_trades, master=master)
def run_strategy(start_date, end_date): print('') ms = manualStrategy(symbol='JPM', sd=start_date, ed=end_date, sv=100000) df_trades = ms.testPolicy() df_orders = ms.createOrdersDataFrame(df_trades) benchmark = ms.createBenchMarkDataFrame(df_trades, sd=start_date) portvals = msim.compute_portvals(orders_file=df_orders, start_val=100000, commission=9.95, impact=0.005, end_date=end_date, start_date=start_date) portvals_benchmark = msim.compute_portvals(orders_file=benchmark, start_val=100000, commission=0, impact=0, end_date=end_date, start_date=start_date) print('Statistics for Manual Strategy') msim.computeStatistics(portvals) print('') print('Statistics for Benchmark Strategy') msim.computeStatistics(portvals_benchmark) generatePerformanceChart(df_trades, portvals, portvals_benchmark)
def test(symbol, sd, ed, sv, plot_file, sl, ms, commission, impact): # get trades trades_sl = sl.testPolicy(symbol, sd, ed, sv) trades_ms = ms.testPolicy(symbol, sd, ed, sv) # build order book orders_sl = get_orders_df(trades_sl, symbol) orders_ms = get_orders_df(trades_ms, symbol) orders_bench = get_benchmark_trades(sd, ed, symbol) # execute orders portfolio_ms = compute_portvals(orders_ms, sv, commission, impact, sd, ed) portfolio_sl = compute_portvals(orders_sl, sv, commission, impact, sd, ed) portfolio_bench = compute_portvals(orders_bench, 100000, commission, 0, sd, ed) # normalize portfolio values portfolio_ms = portfolio_ms / portfolio_ms.iloc[0] portfolio_bench = portfolio_bench / portfolio_bench.iloc[0] portfolio_sl = portfolio_sl / portfolio_sl.iloc[0] x = portfolio_bench.index plot_cmp(x, portfolio_ms['value'], portfolio_bench['value'], portfolio_sl['value'], None, False, plot_file) print_stats([portfolio_sl, portfolio_ms, portfolio_bench])
def testStrategy(symbol, sd, ed, sv=100000): df_trades = 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=0.0, impact=0.0)[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=0.0, impact=0.0)[0] # 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 = ['Best Possible Strategy', 'Benchmark'] plot_data(pl_data)
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 plot(self, prices, benchmark, manual_trades, strategy_trades): manual_port = ms.compute_portvals(prices, manual_trades, commission=9.95, impact=0.005) bench_port = ms.compute_portvals(prices, benchmark, commission=9.95, impact=0.005) strat_port = ms.compute_portvals(prices, strategy_trades, commission=9.95, impact=0.005) manual_port = manual_port/manual_port.iloc[0] bench_port = bench_port/bench_port.iloc[0] strat_port = strat_port/strat_port.iloc[0] ms.get_port_stats(manual_port, self.title + " Normalized Manual Strategy") ms.get_port_stats(bench_port, self.title + " Normalized Benchmark") ms.get_port_stats(strat_port, self.title + " Normalized Strategy Learner") plt.figure() plt.plot(bench_port, label = "Benchmark", color ="green") plt.plot(manual_port, label = "Manual Strategy", color ="red") plt.plot(strat_port, label = "Strategy Learner", color = 'blue') plt.ylabel('Cumulative Return') plt.xlabel('Date') # longs = manual_trades.index[manual_trades[self.symbol] > 0] # for long in longs: # plt.axvline(x = long, color = "blue") # # shorts = manual_trades.index[manual_trades[self.symbol] < 0] # for short in shorts: # plt.axvline(x = short, color = "black") plt.title(self.title) plt.legend() plt.savefig(self.title + ".png")
def main(): # Parameters sd_train = dt.datetime(2008, 1, 1) ed_train = dt.datetime(2009, 12, 31) sym = 'JPM' capital = 100000 impacts = np.linspace(0.0, 0.5, 51) num_trades = list() bench_cum_ret = list() sl_cum_ret = list() # Test learner for different market impacts for imp in impacts: # Train strategy learner learner = sl.StrategyLearner(verbose=False, impact=imp) learner.addEvidence(symbol=sym, sd=sd_train, ed=ed_train, sv=capital) learner.addEvidence() # Test strategy learner sl_trades = learner.testPolicy(symbol=sym, sd=sd_train, ed=ed_train, sv=capital) sl_portvals = msc.compute_portvals(sl_trades, start_val=capital, commission=0.0, impact=imp) # Benchmark: Buying 1000 shares of JPM and holding throughout period bench_trades = pd.DataFrame(0.0, index=sl_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=imp) # Compute cumulative returns bench_cum_ret.append(bench_portvals.iloc[-1] / bench_portvals.iloc[0] - 1) sl_cum_ret.append(sl_portvals.iloc[-1] / sl_portvals.iloc[0] - 1) # Calculate number of trades made by strategy learner num_trades.append(len(sl_trades[sl_trades[sym] != 0.0])) # Plots plt.plot(impacts, num_trades) plt.xlabel('Market Impact') plt.ylabel('Num. Trades') plt.title('Number of Trades vs Market Impact') plt.show() # plt.plot(impacts, bench_cum_ret, label='Benchmark') plt.plot(impacts, sl_cum_ret, label='Strategy Learner') plt.legend() plt.xlabel('Market Impact') plt.ylabel('Cumulative Returns') plt.title('Cumulative Return vs Market Impact') plt.show()
def code(): sd = dt.datetime(2003, 1, 1) ed = dt.datetime(2004, 12, 31) sv = 100000 df_trades = testPolicy(symbol="NFLX", sd=sd, ed=ed, sv=sv) portvals = ms.compute_portvals(df_trades, start_val=sv, commission=9.95, impact=0.005) prices_portval_normalized = ind.normalize_stocks(portvals) prices_SPY_normalized = pd.read_csv('first.csv', names=['First', 'Second']) prices_SPY_normalized.set_index(prices_SPY_normalized['First'], inplace=True) del prices_SPY_normalized['First'] chart_df = pd.concat([prices_portval_normalized, prices_SPY_normalized], axis=1) chart_df.columns = ['Portfolio', 'Benchmark'] chart_df.plot(grid=True, title='Manual Strategy vs. Benchmark Index (In-Sample)', use_index=True, color=['Black', 'Blue']) num = 0 for index, row in df_trades.iterrows(): if df_trades.loc[index]['Order'] == 'BUY': num += df_trades.loc[index]['Shares'] if num == 0: plt.axvline(x=index, color='k', linestyle='-') else: plt.axvline(x=index, color='g', linestyle='-') elif df_trades.loc[index]['Order'] == 'SELL': num -= df_trades.loc[index]['Shares'] if num == 0: plt.axvline(x=index, color='k', linestyle='-') else: plt.axvline(x=index, color='r', linestyle='-') plt.savefig('insample.png') plt.savefig('insample.png') print('Out of Sample Stats:') sd = dt.datetime(2005, 1, 1) ed = dt.datetime(2006, 12, 31) sv = 100000 df_trades = testPolicy(symbol="NFLX", sd=sd, ed=ed, sv=sv) portvals = ms.compute_portvals(df_trades, start_val=sv) syms = ['NFLX'] dates = pd.date_range(sd, ed) prices_all = get_data(syms, dates) prices_portval_normalized = ind.normalize_stocks(portvals) prices_SPY_normalized = pd.read_csv('second.csv', names=['First', 'Second']) prices_SPY_normalized.set_index(prices_SPY_normalized['First'], inplace=True) del prices_SPY_normalized['First'] chart_df = pd.concat([prices_portval_normalized, prices_SPY_normalized], axis=1) chart_df.columns = ['Portfolio', 'Benchmark'] chart_df.plot(grid=True, title='Manual Strategy vs. Benchmark Index (Out-Of-Sample)', use_index=True, color=['Black', 'Blue'])
def test_manual(symbol, sd, ed, opening, title=''): syb = symbol start = sd end = ed openings = opening temp = get_data(['JPM'], pd.date_range(start, end), addSPY=False) manuals = testPolicy('JPM', start, end) benchmark = manuals.copy() ###TODO### #print benchmark ###TODO### benchmark.iloc[:, :] = 0.0 benchmark = transform(benchmark) manuals = transform(manuals) benchmark.loc[temp['JPM'].first_valid_index(), 'Shares'] = 1000 benchmark.loc[temp['JPM'].first_valid_index(), 'Order'] = 1 #NOTE:making an temporary transformation to accomadate marketsim result = sim.compute_portvals(manuals, start_val=openings, commission=9.95, impact=0.005) ###############NOTE:DEBUG NOTE################# #print manuals[manuals['Order']<0] #print result #exit() ###############NOTE:DEBUG NOTE################# #change the column name for optimal result = result.to_frame() result.columns = ['manual'] result['benchmark'] = sim.compute_portvals(benchmark, start_val=openings, commission=9.95, impact=0.005) result = result.div(result.iloc[0], axis=1) result.fillna(method='ffill', inplace=True) result.fillna(method='bfill', inplace=True) ax = result.plot(title="Manual Strategy and Benchmark", style=['r', 'g']) longs = result.index[manuals['Order'] == 1].tolist() shorts = result.index[manuals['Order'] == -1].tolist() for xl in longs: ax.axvline(x=xl, color='b', linestyle='-') for xs in shorts: ax.axvline(x=xs, color='k', linestyle='-') plt.savefig(title + "manual_benchmark.png") plt.clf() dr_op = result['manual'][1:] / result['manual'][:-1].values - 1 dr_op[0] = 0 dr_base = result['benchmark'][1:] / result['benchmark'][:-1].values - 1 dr_base[0] = 0 adr_op = dr_op.mean() adr_base = dr_base.mean() sddr_op = dr_op.std() sddr_base = dr_base.std() cr_op = result['manual'][-1] - 1 cr_base = result['benchmark'][-1] - 1 print "cumulative return, mean,std of daily returns of Manual is" print cr_op, adr_op, sddr_op print "cumulative return, mean,std of daily returns of Benchmark is" print cr_base, adr_base, sddr_base
def test_code(): df_trades = testPolicy(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000) result = ms.compute_portvals(orders=df_trades, start_val=100000, commission=0, impact=0, symbols="JPM", start_date=dt.datetime(2008, 1, 1), end_date=dt.datetime(2009, 12, 31)) cr = result[-1] / result[0] - 1 daily_rets = (result / result.shift(1)) - 1 daily_rets = daily_rets[1:] avg_daily_ret = daily_rets.mean() std_daily_ret = daily_rets.std() sharpe_ratio = np.sqrt(252) * daily_rets.mean() / std_daily_ret print "Cumulative Return:", cr print "Average Daily Return:", avg_daily_ret print "Standard Deviation Daily Return:", std_daily_ret symbols = ['JPM'] prices = get_data( symbols, pd.date_range(dt.datetime(2008, 1, 1), dt.datetime(2009, 12, 31))) orders = pd.DataFrame(data='JPM', index=prices.index, columns=['Symbol', 'Order', 'Shares']) orders.ix[0, "Order"] = 'BUY' orders.ix[0, "Shares"] = 1000 orders = orders[orders.Order != 'HOLD'] orders = orders[orders.Order != 'JPM'] benchmark = ms.compute_portvals(orders=orders, start_val=100000, commission=0, impact=0, symbols="JPM", start_date=dt.datetime(2008, 1, 1), end_date=dt.datetime(2009, 12, 31)) cr = benchmark[-1] / benchmark[0] - 1 daily_rets = (benchmark / benchmark.shift(1)) - 1 daily_rets = daily_rets[1:] avg_daily_ret = daily_rets.mean() std_daily_ret = daily_rets.std() sharpe_ratio = np.sqrt(252) * daily_rets.mean() / std_daily_ret print "Cumulative Return:", cr print "Average Daily Return:", avg_daily_ret print "Standard Deviation Daily Return:", std_daily_ret normed_pors = result / result.ix[0, :] normed_bench = benchmark / benchmark.ix[0, :] df_temp = pd.concat([normed_pors, normed_bench], keys=['portvals', 'benchmark'], axis=1) df_temp.plot(color=['black', 'blue'], title="Best Possible Portfolio vs Benchamark") 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 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 optimize_stoch(df, k, d): """Crude iterative optimization method for stochastic window days""" trades = testPolicy(df, k, d) port_val = compute_portvals(trades)[-1] best = port_val for k1 in range(k - 6, k + 6): for d1 in range(d - 2, d + 5): if compute_portvals(testPolicy(df, k1, d1))[-1] > best: best = compute_portvals(testPolicy(df, k1, d1))[-1] print(k1, d1)
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 optimize_macd(df, ema1, ema2, macds): """Crude iterative optimization method for MACD EMA days and signal days""" trades = testPolicy(df, ema1, ema2, macds) port_val = compute_portvals(trades)[-1] best = port_val for e1 in range(ema1 - 3, ema1 + 3): for e2 in range(ema2 - 3, ema2 + 3): for s in range(macds - 3, macds + 3): if compute_portvals(testPolicy(df, e1, e2, s))[-1] > best: best = compute_portvals(testPolicy(df, e1, e2, s))[-1] print(e1, e2, s)
def evaluate(symbol, sd, ed, startVal): port_vals_benchmark = compute_portvals(benchmark(symbol, sd, ed), symbol, startVal) indices = port_vals_benchmark.index.values impact = 0 ts = StrategyLearner.StrategyLearner(impact=0) ts.addEvidence(symbol, sd, ed, startVal) trades_df = ts.testPolicy(symbol, sd, ed, startVal) print "Number of trades with impact: ", impact, " is ", getNumTrades( trades_df) port_vals_strategy0 = compute_portvals(trades_df, symbol, startVal, impact=0) print "Cumulative returns with impact: ", impact, "are ", \ port_vals_strategy0.ix[indices[-1], CONSTANTS.TOTAL]/port_vals_strategy0.ix[indices[0], CONSTANTS.TOTAL] impact = 0.010 ts = StrategyLearner.StrategyLearner(impact=impact) ts.addEvidence(symbol, sd, ed, startVal) trades_df = ts.testPolicy(symbol, sd, ed, startVal) print "Number of trades with impact: ", impact, " is ", getNumTrades( trades_df) port_vals_strategy5 = compute_portvals(trades_df, symbol, startVal, impact=impact) print "Cumulative returns with impact: ", impact, "are ", \ port_vals_strategy5.ix[indices[-1], CONSTANTS.TOTAL] / port_vals_strategy5.ix[indices[0], CONSTANTS.TOTAL] impact = 0.015 ts = StrategyLearner.StrategyLearner(impact=impact) ts.addEvidence(symbol, sd, ed, startVal) trades_df = ts.testPolicy(symbol, sd, ed, startVal) print "Number of trades with impact: ", impact, " is ", getNumTrades( trades_df) port_vals_strategy15 = compute_portvals(trades_df, symbol, startVal, impact=impact) print "Cumulative returns with impact: ", impact, "are ", \ port_vals_strategy15.ix[indices[-1], CONSTANTS.TOTAL] / port_vals_strategy15.ix[indices[0], CONSTANTS.TOTAL] plot_df = pd.DataFrame(index=port_vals_benchmark.index) plot_df["Benchmark"] = port_vals_benchmark[CONSTANTS.TOTAL] plot_df["Impact 0"] = port_vals_strategy0[CONSTANTS.TOTAL] plot_df["Impact 10"] = port_vals_strategy5[CONSTANTS.TOTAL] plot_df["Impact 15"] = port_vals_strategy15[CONSTANTS.TOTAL] plot_df = plot_df / plot_df.ix[plot_df.index.values[0]] plot_df.plot(color=['red', 'green', 'blue', 'black'], linewidth=1) plt.savefig('experiment2.png')
def testManualStrategy_Out(symbol='JPM', sd='2010-01-01', ed='2011-12-31', sv=100000, commission=9.95, impact=0.005): ms = ManualStrategy() df_trades = ms.testPolicy(symbol, sd, ed, sv) # Prices for reference dates = pd.date_range(sd, ed) data = util.get_data([symbol], dates, addSPY=False, colname='Adj Close') data.dropna(axis=0, how='all', inplace=True) # Benchmark: buy once and hold benchmark_trades = pd.DataFrame(data=0.0, index=data.index, columns=[symbol]) benchmark_trades.iloc[0] = 1000 benchmark_portvals = mkt.compute_portvals(benchmark_trades, sv, commission, impact) benchmark_portvals_normed = benchmark_portvals / benchmark_portvals.ix[ 0, :] # Manual trades manual_portvals = mkt.compute_portvals(df_trades, sv, commission, impact) manual_portvals_normed = manual_portvals / manual_portvals.ix[0, :] # Plot comparison and trades fig, ax = plt.subplots() ax.set_title('Out-sample performance of Benchmark and Manual') ax.plot(benchmark_portvals_normed, color='g', label='Benchmark') ax.plot(manual_portvals_normed, label='Manual', color='r') for buy_point in df_trades.index[df_trades[symbol] >= 1000].tolist(): ax.axvline(buy_point, color='b') for sell_point in df_trades.index[df_trades[symbol] <= -1000].tolist(): ax.axvline(sell_point, color='k') ax.legend() plt.savefig('ManualStrategy_out.png') # Calculate stats cr_benchmark, adr_benchmark, sddr_benchmark, sr_benchmark = mkt.calculate_stats( benchmark_portvals) cr_manual, adr_manual, sddr_manual, sr_manual = mkt.calculate_stats( manual_portvals) # print("----------------------------") print("---- Out sample ----") print("----------------------------") print(f"Cumulative return of benchmark: {cr_benchmark}") print(f"Average Daily Return of benchmark: {adr_benchmark}") print(f"Standard deviation of Daily return of benchmark: {sddr_benchmark}") print(f"Sharpe ratio of benchmark: {sr_benchmark}") print("----------------------------") print(f"Cumulative return of Manual: {cr_manual}") print(f"Average Daily Return of Manual: {adr_manual}") print(f"Standard deviation of Daily return of Manual: {sddr_manual}") print(f"Sharpe ratio of Manual: {sr_manual}") print("----------------------------")
def test_code(): df_trades = testPolicy() portvals = ms.compute_portvals(df_trades, start_val=100000, commission=0, impact=0) if isinstance(portvals, pd.DataFrame): portvals = portvals[ portvals.columns[0]] # just get the first column else: "warning, code did not return a DataFrame" # Calculate stats of Portfolio cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = ms.compute_portfolio_stats(portvals) # Benchmark Portfolio df_benchmark = pd.DataFrame(index=df_trades.index, columns=["JPM"]) df_benchmark.loc[df_trades.index] = 0 df_benchmark.loc[df_trades.index[0]] = 1000 # Buying 1000 shares of JPM portvals_benchmark = ms.compute_portvals(df_benchmark, start_val=100000, commission=0, impact=0) # Calculate stats of Benchmark Portfolio cum_ret_bench, avg_daily_ret_bench, std_daily_ret_bench, sharpe_ratio_bench = \ ms.compute_portfolio_stats(portvals_benchmark[portvals_benchmark.columns[0]]) # Normalize Portfolio and Benchmark Portfolio portvals_norm = portvals / portvals.iloc[0] portvals_benchmark = portvals_benchmark / portvals_benchmark.iloc[0] # Generate Plot figure, axis = plt.subplots() portvals_norm.plot(ax=axis, color='r') portvals_benchmark.plot(ax=axis, color='g') plt.title("Comparison of Theoretically Optimal Portfolio vs Benchmark") plt.legend(["Theoretically Optimal Strategy", "Benchmark"]) plt.xlabel("Date") plt.ylabel("Normalized Portfolio Value") plt.savefig("OptimalStrategy.png") # plt.show() # Display Portfolio Stats start_date = portvals.index[0] end_date = portvals.index[-1] print(f"Date Range: {start_date} to {end_date}") print() print(f"Sharpe Ratio of Fund: {sharpe_ratio}") print(f"Sharpe Ratio of Benchmark: {sharpe_ratio_bench}") print() print(f"Cumulative Return of Fund: {cum_ret}") print(f"Cumulative Return of Benchmark: {cum_ret_bench}") print() print(f"Standard Deviation of Fund: {std_daily_ret}") print(f"Standard Deviation of Benchmark: {std_daily_ret_bench}") print() print(f"Average Daily Return of Fund: {avg_daily_ret}") print(f"Average Daily Return of Benchmark: {avg_daily_ret_bench}") print() print(f"Final Portfolio Value: {portvals[-1]}")
def compareTwoStrategy(strategy_orders, bm_orders): # call marketsimcode.py to compute portvalue strategy_port = mc.compute_portvals(strategy_orders, start_date, end_date, sv, com, imp) bm_port = mc.compute_portvals(bm_orders, start_date, end_date, sv, com, imp) # print bm_port # print strategy_port # plot portvale for benchmark and symbol stock plotStrategy(strategy_port, bm_port, sv, True)
def test_code(): start_date = dt.datetime(2008, 1, 1) end_date = dt.datetime(2009, 12, 31) start_value = 100000 symbol = ["JPM"] commission = 9.95 impact = 0.005 ## Manual strategy df_trades, start = testPolicy(symbol, start_date, end_date, start_value) manual_port_val = ms.compute_portvals(df_trades, start_date, end_date, start_value, commission, impact) print "Rule Based Portfolio" port_status(manual_port_val) ## Benchmark portfolio benchmark = [] benchmark.append([start, symbol[0], 'BUY', 1000]) benchmark_trades = pd.DataFrame( np.array(benchmark), columns=['Date', 'Symbol', 'Order', 'Shares']) ##print benchmark_trades benchmark_port_val = ms.compute_portvals(benchmark_trades, start_date, end_date, start_value, commission, impact) print "\nBenchmark Portfolio" port_status(benchmark_port_val) ## Plot the chart of Benchmark and Best Portfolio value = manual_port_val.copy() value = value.rename(columns={'Value': 'Rule Based Portfolio'}) value['Benchmark Portfolio'] = benchmark_port_val['Value'] value = value / value.ix[0, :] plt.figure(figsize=(6, 4)) plt.plot(value) plt.gca().get_lines()[0].set_color("black") plt.gca().get_lines()[1].set_color("blue") plt.legend(value.columns.values, loc=0, prop={'size': 9}) plt.title("Rule Based Strategy vs. Benchmark") plt.ylabel('Normalized Value') plt.grid(False) plt.setp(plt.gca().get_xticklabels(), rotation=30) xcoords = df_trades.loc[df_trades['Order'] == 'SELL']['Date'] for xc in xcoords: plt.axvline(x=xc, color='red', linestyle='dashed', linewidth=1) xcoords = df_trades.loc[df_trades['Order'] == 'BUY']['Date'] for xc in xcoords: plt.axvline(x=xc, color='green', linestyle='dashed', linewidth=1) plt.show()
def testPolicy(self, symbol = "AAPL", sd =dt.datetime(2010,1,1), ed =dt.datetime(2011,12,31), sv = 100000): stockDF = get_data([symbol], pd.date_range(sd, ed)) stockDF = stockDF[symbol] stockDF = stockDF - stockDF.shift(-1) df_trades = pd.DataFrame(np.nan, columns=['Date','Symbol', 'Order', 'Shares'], index=stockDF.index) df_trades['Date'] = stockDF.index df_trades['Symbol'] = symbol df_trades['Shares'] = 0 df_trades['Order'] = "HOLD" currholdings = 0 for i in stockDF.index.values: date = pd.to_datetime(str(i)) date = date.strftime('%Y-%m-%d') if stockDF[i] > 0: if currholdings == 1000: df_trades.loc[date, 'Order'] = 'SELL' df_trades.loc[date, "Shares"] = 2000 if currholdings == 0: df_trades.loc[date, 'Order'] = 'SELL' df_trades.loc[date, "Shares"] = 1000 currholdings = -1000 if stockDF[i] < 0: if currholdings == -1000: df_trades.loc[date, 'Order'] = 'BUY' df_trades.loc[date, "Shares"] = 2000 if currholdings == 0: df_trades.loc[date, 'Order'] = 'BUY' df_trades.loc[date, "Shares"] = 1000 currholdings = 1000 df_trades2 = df_trades.copy() df_trades2['Order'] = "HOLD" df_trades2.iloc[0,2] = "BUY" df_trades2.iloc[0,3] = 1000 benchmark = compute_portvals(df_trades2, start_val = 100000, impact = 0, commission =0) port_val = compute_portvals(df_trades, start_val = 100000, impact = 0, commission =0) port_val_stats(port_val) port_val_stats(benchmark) port_val = port_val/port_val.iloc[0] benchmark = benchmark/benchmark.iloc[0] port_val = port_val.rename("Best Strategy") benchmark = benchmark.rename("Benchmark") pv = port_val.plot(legend = True, y = "Cumulative Return", x = "Dates", color="black") benchmark.plot(ax=pv, title = "Daily Returns of " + str(symbol), legend = True, color="blue") plt.ylabel("Cumulative Return") plt.xlabel("Dates") plt.show() return df_trades
def optimize_BB(df, w, th): """Crude iterative optimization method for BB window and threshold""" trades = testPolicy(df, w, th) port_val = compute_portvals(trades)[-1] best = port_val new_th = None new_w = None for i in np.linspace(0.1, th, 50): for j in range(17, 22): if compute_portvals(testPolicy(df, j, i))[-1] > best: best = compute_portvals(testPolicy(df, j, i))[-1] new_th = i new_w = j print(new_w, new_th, end='\n\n')