def main(symbol="JPM", start_date=dt.datetime(2008, 1, 1), end_date=dt.datetime(2009, 12, 31)): np.random.seed(3**9) random.seed(3**9) print "Random seed is: ", 3**9, "\n\n" # Manual Strategy (df_trades, indicators_manual) = testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) #df_trades.to_csv("manual.csv") Manual_trade_dates = df_trades[symbol].nonzero() #print SL_trade_dates[0][0] print "\nFirst trading date of Manual Strategy: ", df_trades.index.values[ Manual_trade_dates[0][0]] print "Trading times of Manual Strategy: ", len( Manual_trade_dates[0]), "\n" Manual_portval = compute_portvals(df_trades, start_val=100000, commission=0.0, impact=0.0, symbol=symbol) Manual_portval_stats = compute_portvals_stats(Manual_portval) # Learning Strategy strategy_learner = sl.StrategyLearner(verbose=False, impact=0.0) strategy_learner.addEvidence(symbol=symbol, sd=start_date, ed=end_date, sv=100000) df_SL = strategy_learner.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) SL_trade_dates = df_SL[symbol].nonzero() #print SL_trade_dates[0][0] print "\nFirst trading date of Strategy Learner: ", df_SL.index.values[ SL_trade_dates[0][0]] print "Trading times of Strategy Learner: ", len(SL_trade_dates[0]), "\n" SL_portval = compute_portvals(df_SL, start_val=100000, commission=0.0, impact=0.0, symbol=symbol) SL_portval_stats = compute_portvals_stats(SL_portval) # Benchmark df_benchmark = Benchmark(symbol=symbol, sd=start_date, ed=end_date, sv=100000) BM_trade_dates = df_benchmark[symbol].nonzero() #print df_benchmark print "\nFirst trading date of Benchmark: ", df_benchmark.index.values[ BM_trade_dates[0][0]] print "Trading times of Benchmark: ", len(BM_trade_dates[0]), "\n" Benchmark_portval = compute_portvals(df_benchmark, start_val=100000, commission=0.0, impact=0.0, symbol=symbol) Benchmark_portval_stats = compute_portvals_stats(Benchmark_portval) ############################ # # Print Stats # ############################ print "Date Range: {} to {}".format(start_date, end_date) print print "Sharpe Ratio of Manual: {}".format(Manual_portval_stats[0]) print "Sharpe Ratio of SL: {}".format(SL_portval_stats[0]) print "Sharpe Ratio of Benchmark: {}".format(Benchmark_portval_stats[0]) print print "Cumulative Return of Manual: {}".format(Manual_portval_stats[1]) print "Cumulative Return of SL: {}".format(SL_portval_stats[1]) print "Cumulative Return of Benchmark: {}".format( Benchmark_portval_stats[1]) print print "Average Daily Return of Manual: {}".format(Manual_portval_stats[2]) print "Average Daily Return of SL: {}".format(SL_portval_stats[2]) print "Average Daily Return of Benchmark: {}".format( Benchmark_portval_stats[2]) print print "Standard Deviation of Manual: {}".format(Manual_portval_stats[3]) print "Standard Deviation of SL: {}".format(SL_portval_stats[3]) print "Standard Deviation of Benchmark: {}".format( Benchmark_portval_stats[3]) print print "Final Portfolio Value Manual: {}".format(Manual_portval[-1]) print "Final Portfolio Value SL: {}".format(SL_portval[-1]) print "Final Portfolio Value Benchmark: {}".format(Benchmark_portval[-1]) portfolio_df = pd.DataFrame( index=Benchmark_portval.index, columns=['Manual Strategy', 'Strategy Learner', 'Benchmark']) portfolio_df['Manual Strategy'] = Manual_portval / Manual_portval[0] portfolio_df['Strategy Learner'] = SL_portval / SL_portval[0] portfolio_df['Benchmark'] = Benchmark_portval / Benchmark_portval[0] #manual_buy = indicators_manual[indicators_manual['Final_Signal'] == 1] #print manual_buy.index #manual_sell = indicators_manual[indicators_manual['Final_Signal'] == -1] #print manual_sell.index ax = portfolio_df.plot(fontsize=16, color=["blue", "red", "black"]) ax.set_xlabel("Dates", fontsize=16) ax.set_ylabel("Normalized Portfolio Values", fontsize=16) #ymin, ymax = ax.get_ylim() #plt.vlines(manual_buy.index,ymin,ymax,color='g') #plt.vlines(manual_sell.index,ymin,ymax,color='r') #filename = "Figure2.png" #plt.savefig(filename) figure = plt.gcf() # get current figure figure.set_size_inches(8, 6) figure.suptitle("Manual Strategy Vs. Strategy Learner Vs. Benchmark", fontsize=16) # when saving, specify the DPI figure.savefig("Figure1.png")
def main(symbol="JPM", start_date=dt.datetime(2008,1,1), end_date=dt.datetime(2009,12,31)): np.random.seed(3 ** 9) random.seed(3 ** 9) print "Random seed is: ", 3**9, "\n\n" # Learning Strategy: (impact = 0.0) strategy_learner1 = sl.StrategyLearner(verbose = False, impact=0.0) strategy_learner1.addEvidence(symbol=symbol, sd=start_date, ed=end_date, sv=100000) df_SL1 = strategy_learner1.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) SL1_portval = compute_portvals(df_SL1, start_val = 100000, commission=0.0, impact=0.0, symbol=symbol) SL1_portval_stats = compute_portvals_stats(SL1_portval) SL_trade_dates = df_SL1[symbol].nonzero() #print SL_trade_dates[0][0] if len(SL_trade_dates[0]): print "\nFirst trading date of Strategy Learner (impact=0.0): ", df_SL1.index.values[SL_trade_dates[0][0]] print "Trading times of Strategy Learner (impact=0.0): ", len(SL_trade_dates[0]), "\n" else: print "\nFirst trading date of Strategy Learner (impact=0.0): ", 0 print "Trading times of Strategy Learner (impact=0.0): ", "No trading" # Learning Strategy: (impact = 0.005) strategy_learner2 = sl.StrategyLearner(verbose = False, impact=0.005) strategy_learner2.addEvidence(symbol=symbol, sd=start_date, ed=end_date, sv=100000) df_SL2 = strategy_learner2.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) SL2_portval = compute_portvals(df_SL2, start_val = 100000, commission=0.0, impact=0.005, symbol=symbol) SL2_portval_stats = compute_portvals_stats(SL2_portval) SL_trade_dates = df_SL2[symbol].nonzero() #print SL_trade_dates[0][0] #print len(SL_trade_dates[0]) if len(SL_trade_dates[0]): print "\nFirst trading date of Strategy Learner (impact=0.005): ", df_SL2.index.values[SL_trade_dates[0][0]] print "Trading times of Strategy Learner (impact=0.005): ", len(SL_trade_dates[0]), "\n" else: print "\nFirst trading date of Strategy Learner (impact=0.005): ", 0 print "Trading times of Strategy Learner (impact=0.005): ", "No trading" # Learning Strategy: (impact = 0.05) strategy_learner3 = sl.StrategyLearner(verbose = False, impact=0.05) strategy_learner3.addEvidence(symbol=symbol, sd=start_date, ed=end_date, sv=100000) df_SL3 = strategy_learner3.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) SL3_portval = compute_portvals(df_SL3, start_val = 100000, commission=0.0, impact=0.05, symbol=symbol) SL3_portval_stats = compute_portvals_stats(SL3_portval) SL_trade_dates = df_SL3[symbol].nonzero() if len(SL_trade_dates[0]): print "\nFirst trading date of Strategy Learner (impact=0.05): ", df_SL3.index.values[SL_trade_dates[0][0]] print "Trading times of Strategy Learner (impact=0.05): ", len(SL_trade_dates[0]), "\n" else: print "\nFirst trading date of Strategy Learner (impact=0.05): ", 0 print "Trading times of Strategy Learner (impact=0.05): ", "No trading" # Learning Strategy: (impact = 0.5) strategy_learner4 = sl.StrategyLearner(verbose = False, impact=0.5) strategy_learner4.addEvidence(symbol=symbol, sd=start_date, ed=end_date, sv=100000) df_SL4 = strategy_learner4.testPolicy(symbol=symbol, sd=start_date, ed=end_date, sv=100000) SL4_portval = compute_portvals(df_SL4, start_val = 100000, commission=0.0, impact=0.5, symbol=symbol) SL4_portval_stats = compute_portvals_stats(SL4_portval) SL_trade_dates = df_SL4[symbol].nonzero() #print SL_trade_dates[0][0] if len(SL_trade_dates[0]): print "\nFirst trading date of Strategy Learner (impact=0.5): ", df_SL4.index.values[SL_trade_dates[0][0]] print "Trading times of Strategy Learner (impact=0.5): ", len(SL_trade_dates[0]), "\n" else: print "\nFirst trading date of Strategy Learner (impact=0.5): ", 0 print "Trading times of Strategy Learner (impact=0.5): ", "No trading" # Benchmark df_benchmark = Benchmark(symbol=symbol, sd=start_date, ed=end_date, sv=100000) Benchmark_portval = compute_portvals(df_benchmark, start_val = 100000, commission=0.0, impact=0.0, symbol=symbol) Benchmark_portval_stats = compute_portvals_stats(Benchmark_portval) BM_trade_dates = df_benchmark[symbol].nonzero() #print df_benchmark print "\nFirst trading date of Benchmark: ", df_benchmark.index.values[BM_trade_dates[0][0]] print "Trading times of Benchmark: ", len(BM_trade_dates[0]), "\n" ############################ # # Print Stats # ############################ print "Date Range: {} to {}".format(start_date, end_date) print print "Sharpe Ratio of SL1: {}".format(SL1_portval_stats[0]) print "Sharpe Ratio of SL2: {}".format(SL2_portval_stats[0]) print "Sharpe Ratio of SL3: {}".format(SL3_portval_stats[0]) print "Sharpe Ratio of SL4: {}".format(SL4_portval_stats[0]) print "Sharpe Ratio of Benchmark: {}".format(Benchmark_portval_stats[0]) print print "Cumulative Return of SL1: {}".format(SL1_portval_stats[1]) print "Cumulative Return of SL2: {}".format(SL2_portval_stats[1]) print "Cumulative Return of SL3: {}".format(SL3_portval_stats[1]) print "Cumulative Return of SL4: {}".format(SL4_portval_stats[1]) print "Cumulative Return of Benchmark: {}".format(Benchmark_portval_stats[1]) print print "Average Daily Return of SL1: {}".format(SL1_portval_stats[2]) print "Average Daily Return of SL2: {}".format(SL2_portval_stats[2]) print "Average Daily Return of SL3: {}".format(SL3_portval_stats[2]) print "Average Daily Return of SL4: {}".format(SL4_portval_stats[2]) print "Average Daily Return of Benchmark: {}".format(Benchmark_portval_stats[2]) print print "Standard Deviation of SL1: {}".format(SL1_portval_stats[3]) print "Standard Deviation of SL2: {}".format(SL2_portval_stats[3]) print "Standard Deviation of SL3: {}".format(SL3_portval_stats[3]) print "Standard Deviation of SL4: {}".format(SL4_portval_stats[3]) print "Standard Deviation of Benchmark: {}".format(Benchmark_portval_stats[3]) print print "Final Portfolio Value SL1: {}".format(SL1_portval[-1]) print "Final Portfolio Value SL2: {}".format(SL2_portval[-1]) print "Final Portfolio Value SL3: {}".format(SL3_portval[-1]) print "Final Portfolio Value SL4: {}".format(SL4_portval[-1]) print "Final Portfolio Value Benchmark: {}".format(Benchmark_portval[-1]) portfolio_df = pd.DataFrame(index=Benchmark_portval.index, columns=['Strategy Learner (impact=0.0)', \ 'Strategy Learner (impact=0.005)', \ 'Strategy Learner (impact=0.05)', \ 'Strategy Learner (impact=0.5)', 'Benchmark']) portfolio_df['Strategy Learner (impact=0.0)'] = SL1_portval / SL1_portval[0] portfolio_df['Strategy Learner (impact=0.005)'] = SL2_portval / SL2_portval[0] portfolio_df['Strategy Learner (impact=0.05)'] = SL3_portval / SL3_portval[0] portfolio_df['Strategy Learner (impact=0.5)'] = SL4_portval / SL4_portval[0] portfolio_df['Benchmark'] = Benchmark_portval / Benchmark_portval[0] #manual_buy = indicators_manual[indicators_manual['Final_Signal'] == 1] #print manual_buy.index #manual_sell = indicators_manual[indicators_manual['Final_Signal'] == -1] #print manual_sell.index ax = portfolio_df.plot(fontsize=16, color = ["red", "orange", "green", "navy", "black"]) ax.set_xlabel("Dates", fontsize=16) ax.set_ylabel("Normalized Portfolio Values", fontsize=16) #ymin, ymax = ax.get_ylim() #plt.vlines(manual_buy.index,ymin,ymax,color='g') #plt.vlines(manual_sell.index,ymin,ymax,color='r') #filename = "Figure2.png" #plt.savefig(filename) figure = plt.gcf() # get current figure figure.set_size_inches(9, 6) figure.suptitle("Strategy Learner with various impact factors Vs. Benchmark", fontsize=16) # when saving, specify the DPI figure.savefig("Figure2.png")
def main(start_date=dt.datetime(2008, 1, 1), end_date=dt.datetime(2009, 12, 31)): (df_trades, indicators_manual) = testPolicy(symbol="JPM", sd=start_date, ed=end_date, sv=100000) #print df_trades.head(10) Manual_portval = compute_portvals(df_trades, start_val=100000, commission=9.95, impact=0.005, symbol="JPM") Manual_portval_stats = compute_portvals_stats(Manual_portval) df_benchmark = Benchmark(symbol="JPM", sd=start_date, ed=end_date, sv=100000) Benchmark_portval = compute_portvals(df_benchmark, start_val=100000, commission=9.95, impact=0.005, symbol="JPM") #print Benchmark_portval Benchmark_portval_stats = compute_portvals_stats(Benchmark_portval) #print df_benchmark ############################ # # Print Stats # ############################ print "Date Range: {} to {}".format(start_date, end_date) print print "Sharpe Ratio of Manual: {}".format(Manual_portval_stats[0]) print "Sharpe Ratio of Benchmark: {}".format(Benchmark_portval_stats[0]) print print "Cumulative Return of Manual: {}".format(Manual_portval_stats[1]) print "Cumulative Return of Benchmark: {}".format( Benchmark_portval_stats[1]) print print "Average Daily Return of Manual: {}".format(Manual_portval_stats[2]) print "Average Daily Return of Benchmark: {}".format( Benchmark_portval_stats[2]) print print "Standard Deviation of Manual: {}".format(Manual_portval_stats[3]) print "Standard Deviation of Benchmark: {}".format( Benchmark_portval_stats[3]) print print "Final Portfolio Value Manual: {}".format(Manual_portval[-1]) print "Final Portfolio Value Benchmark: {}".format(Benchmark_portval[-1]) portfolio_df = pd.DataFrame(index=Manual_portval.index, columns=['Manual Strategy', 'Benchmark']) portfolio_df['Manual Strategy'] = Manual_portval / Manual_portval[0] portfolio_df['Benchmark'] = Benchmark_portval / Benchmark_portval[0] manual_buy = indicators_manual[indicators_manual['Final_Signal'] == 1] #print manual_buy.index manual_sell = indicators_manual[indicators_manual['Final_Signal'] == -1] #print manual_sell.index plt.figure(figsize=(12, 8)) ax = portfolio_df.plot(title="Manual Strategy Vs. Benchmark", fontsize=12, color=["black", "blue"]) ax.set_xlabel("Dates") ax.set_ylabel("Normalized Portfolio Values") ymin, ymax = ax.get_ylim() plt.vlines(manual_buy.index, ymin, ymax, color='g') plt.vlines(manual_sell.index, ymin, ymax, color='r') filename = "Manual.Vs.Benchmark" + "_" + str(start_date) + "_" + str( end_date) + ".png" plt.savefig(filename)