num_shares) # Train and test a StrategyLearner stl = StrategyLearner(num_shares=num_shares, impact=impact, commission=commission, verbose=True, num_states=3000, num_actions=3) stl.addEvidence(symbol=symbol, sd=sd, ed=ed,sv=sv) df_trades = stl.test_policy(symbol=symbol, sd=sd, ed=ed) # Retrieve performance stats via a market simulator print ("Performances during training period for {}".format(symbol)) print ("Date Range: {} to {}".format(sd, ed)) market_simulator(df_trades, df_benchmark_trades, symbol=symbol, start_val=sv, commission=commission, impact=impact) # Out-of-sample or testing period: Perform similiar steps as above, # except that we don't train the data (i.e. run add_evidence again) sd = dt.datetime(2010, 1, 1) ed = dt.datetime(2011, 12, 31) df_benchmark_trades = create_df_benchmark(symbol, sd, ed, num_shares) df_trades = stl.test_policy(symbol=symbol, sd=sd, ed=ed) print ("\nPerformances during testing period for {}".format(symbol)) print ("Date Range: {} to {}".format(sd, ed)) market_simulator(df_trades, df_benchmark_trades, symbol=symbol, start_val=sv, commission=commission, impact=impact)
def training(): # **** Training *** # Getting session variables start_d = str(session.get('start_d', None)) start_val = int(session.get('start_val', None)) symbol = session.get('symbol', None) num_shares = int(session.get('num_shares', None)) commission = float(session.get('commission', None)) impact = float(session.get('impact', None)) # Create a dataframe from csv file #df = get_data(symbol) # Import data from Alpha Vantage dates = pd.date_range(start_d, dt.date.today()) df = get_data_av(symbol, dates, del_cols=True) # Rename column Adj Close df.rename(columns={'Close': symbol}, inplace=True) if not isinstance(df, pd.DataFrame): return render_template( # name of template "error.html", # now we pass in our variables into the template form='showvalues', error="Data source can't be reached at this moment. Try again.") # We'll get 80% data to train split_percentage = 0.8 split = int(split_percentage * len(df)) # Train data set df_training = df[:split] # Test data set df_testing = df[split:] # Training dates start_date_training = df_training.index[0] end_date_training = df_training.index[-1] # Testing dates start_date_testing = df_testing.index[0] end_date_testing = df_testing.index[-1] # Get a dataframe of benchmark data. Benchmark is a portfolio starting with # $100,000, investing in 1000 shares of symbol and holding that position df_benchmark_trades = create_df_benchmark(df_training, num_shares) # **** Training **** # Train a StrategyLearner stl = strategyLearner(num_shares=num_shares, impact=impact, commission=commission, verbose=True, num_states=3000, num_actions=3, dyna=200) epochs, cum_returns, trades, portvals, df_features, thresholds = stl.add_evidence( df_training, symbol, start_val=start_val, start_date=start_date_training, end_date=end_date_training) df_trades = stl.test_policy(df_training, symbol, start_date=start_date_training, end_date=end_date_training) # cum_returns = map(lambda cum_returns: cum_returns * 100, cum_returns) plot_cum = plot_cum_return(epochs, cum_returns) # Retrieve performance stats via a market simulator print("Performances during training period for {}".format(symbol)) print("Date Range: {} to {}".format(start_date_training, end_date_training)) orders_count, sharpe_ratio, cum_ret, std_daily_ret, avg_daily_ret, final_value, cum_ret_bm, avg_daily_ret_bm, std_daily_ret_bm, sharpe_ratio_bm, final_value_bm, portvals, portvals_bm, df_orders = \ market_simulator(df_training, df_trades, df_benchmark_trades, symbol=symbol, start_val=start_val, commission=commission, impact=impact) plot_norm_data = plot_norm_data_vertical_lines( df_orders, portvals, portvals_bm, vert_lines=False, title="Training Portfolio Value", xtitle="Dates", ytitle="Value ($)") #TODO Check test results # **** Testing **** # Out-of-sample or testing period: Perform similiar steps as above, # except that we don't train the data (i.e. run add_evidence again) df_benchmark_trades = create_df_benchmark(df_testing, num_shares) df_trades = stl.test_policy(df_testing, symbol, start_date=start_date_testing, end_date=end_date_testing) print("\nPerformances during testing period for {}".format(symbol)) print("Date Range: {} to {}".format(start_date_testing, end_date_testing)) # Retrieve performance stats via a market simulator test_orders_count, test_sharpe_ratio, test_cum_ret, test_std_daily_ret, test_avg_daily_ret, test_final_value, test_cum_ret_bm, test_avg_daily_ret_bm, test_std_daily_ret_bm, test_sharpe_ratio_bm, test_final_value_bm, test_portvals, test_portvals_bm, test_df_orders = \ market_simulator(df_testing, df_trades, df_benchmark_trades, symbol=symbol, start_val=start_val, commission=commission, impact=impact) plot_norm_data_test = plot_norm_data_vertical_lines( test_df_orders, test_portvals, test_portvals_bm, vert_lines=True, title="Testing Portfolio Value", xtitle="Dates", ytitle="Value ($)") return render_template( # name of template "training.html", # Training data start_date=start_date_training, end_date=end_date_training, symbol=symbol, div_placeholder_plot_cum=Markup(plot_cum), div_placeholder_plot_norm_data=Markup(plot_norm_data), orders_count_p=orders_count, sharpe_ratio_p=round(sharpe_ratio, 3), cum_ret_p=round(cum_ret, 3), std_daily_ret_p=round(std_daily_ret, 3), avg_daily_ret_p=round(avg_daily_ret, 3), final_value_p=round(final_value, 3), sharpe_ratio_b=round(sharpe_ratio_bm, 3), cum_ret_b=round(cum_ret_bm, 3), std_daily_ret_b=round(std_daily_ret_bm, 3), avg_daily_ret_b=round(avg_daily_ret_bm, 3), final_value_b=round(final_value_bm, 3), # Testing datasets start_date_testing=start_date_testing, end_date_testing=end_date_testing, div_placeholder_plot_norm_data_testing=Markup(plot_norm_data_test), orders_count_p_testing=test_orders_count, sharpe_ratio_p_testing=round(test_sharpe_ratio, 3), cum_ret_p_testing=round(test_cum_ret, 3), std_daily_ret_p_testing=round(test_std_daily_ret, 3), avg_daily_ret_p_testing=round(test_avg_daily_ret, 3), final_value_p_testing=round(test_final_value, 3), sharpe_ratio_b_testing=round(test_sharpe_ratio_bm, 3), cum_ret_b_testing=round(test_cum_ret_bm, 3), std_daily_ret_b_testing=round(test_std_daily_ret_bm, 3), avg_daily_ret_b_testing=round(test_avg_daily_ret_bm, 3), final_value_b_testing=round(test_final_value_bm, 3))
# Create benchmark trades: buy 1000 shares of symbol, hold them till the last date df_benchmark_trades = pd.DataFrame( data=[(benchmark_prices.index.min(), symbol, "BUY", 1000), (benchmark_prices.index.max(), symbol, "SELL", 1000)], columns=["Date", "Symbol", "Order", "Shares"]) df_benchmark_trades.set_index("Date", inplace=True) # Create an instance of RuleBasedStrategy rule_based = RuleBasedStrategy() # Get df_trades df_trades = rule_based.test_policy(symbol=symbol, start_date=start_d, end_date=end_d) # Retrieve performance stats via a market simulator print ("Performances during training period for {}".format(symbol)) print ("Date Range: {} to {}".format(start_d, end_d)) market_simulator(df_trades, df_benchmark_trades, start_val=start_val) # Out-of-sample or testing period start_d = dt.datetime(2010, 1, 1) end_d = dt.datetime(2011, 12, 31) # Get benchmark data benchmark_prices = get_data([symbol], pd.date_range(start_d, end_d), addSPY=False).dropna() # Create benchmark trades: buy 1000 shares of symbol, hold them till the last date df_benchmark_trades = pd.DataFrame( data=[(benchmark_prices.index.min(), symbol, "BUY", 1000), (benchmark_prices.index.max(), symbol, "SELL", 1000)], columns=["Date", "Symbol", "Order", "Shares"])
params = False #gen_alg = ga.GeneticAlgorithm(symbol=symbol, dates=dates, start_val=start_val, verbose=True) #params, sharpe_ratio = gen_alg.start_ga() #pdb.set_trace() manual_strat = ManualStrategyTicktoTick() trades_df = manual_strat.testPolicy(symbol, dates=dates, start_val=start_val, ga_train=params) # Retrieve performance stats print ("Performances during training period (in-sample) for {}".format(symbol)) print ("Date Range: {} to {}".format(dates[0], dates[1])) print (" ") #pdb.set_trace() market_simulator(trades_df, benchmark_trades_df, start_val=start_val, insample=True) # Out-of-sample period dates = [dt.datetime(2012,1,1), dt.datetime(2012, 12, 31)] benchmark_df = util.get_data([symbol], pd.date_range(dates[0], dates[1]), addSPY=False).dropna() benchmark_trades_df = pd.DataFrame(data=[(benchmark_df.index.min(), symbol, "BUY", 1000), (benchmark_df.index.max(), symbol, "SELL", 1000)], columns=['Date', 'Symbol', 'Order', 'Shares']) benchmark_trades_df.set_index('Date', inplace=True) manual_strat = ManualStrategyTicktoTick() trades_df = manual_strat.testPolicy(symbol, dates=dates, start_val=start_val, ga_train=params) # Retrieve performance stats print ("Performances during testing period (out-of-sample) for {}".format(symbol)) print ("Date Range: {} to {}".format(dates[0], dates[1]))
if closing_trade != None: trades.append(closing_trade) # Check performance vs benchmark # Benchmark portfolio benchmark_trades_df = pd.DataFrame( data=[(split_date, symbol, "BUY", 1000), (prices_df.index.max(), symbol, "SELL", 1000)], columns=['Date', 'Symbol', 'Order', 'Shares']) benchmark_trades_df.set_index('Date', inplace=True) # Learned portfolio strategy_trades_df = pd.DataFrame( trades, columns=['Date', 'Symbol', 'Order', 'Shares']) strategy_trades_df.set_index('Date', inplace=True) # Retrieve performance stats print("Performances during training period (in-sample) for {}".format( symbol)) print("Date Range: {} to {}".format(dates[0], dates[1])) print(" ") #strategy.get_signal(prices_df, ohlc) #pdb.set_trace() market_simulator(strategy_trades_df, benchmark_trades_df, start_val=start_val, insample=True) pdb.set_trace()