def run_strategy(start_date, end_date): print('') #ms = manualStrategy(symbol = 'JPM', sd=start_date, ed=end_date, sv = 100000) np.random.seed(106348) symbols = ['AAPL', 'XOM', 'GOOG', 'IBM'] for sym in symbols: print sym learner = StrategyLearner() learner.addEvidence(symbol=sym, sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000) df_trades = learner.testPolicy(symbol=sym, sd=start_date, ed=end_date, sv=100000) df_orders = learner.createOrdersDataFrame(df_trades) ms = manual.manualStrategy(symbol=sym, sd=start_date, ed=end_date, sv=100000) df_trades_ms = ms.testPolicy() df_orders_ms = ms.createOrdersDataFrame(df_trades_ms) benchmark = learner.createBenchMarkDataFrame(df_trades, sd=start_date) portvals_strategy = msim.compute_portvals(orders_file=df_orders, start_val=100000, commission=0, 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) portvals_manual = msim.compute_portvals(orders_file=df_orders_ms, start_val=100000, commission=0, impact=0.005, end_date=end_date, start_date=start_date) print('Statistics for Random Forest Strategy') msim.computeStatistics(portvals_strategy) print('') print('Statistics for Manual Strategy') msim.computeStatistics(portvals_manual) print('') print('Statistics for Benchmark Strategy') msim.computeStatistics(portvals_benchmark) generatePerformanceChart(df_trades, portvals_strategy, portvals_manual, portvals_benchmark)
def run_strategy(start_date, end_date): print('') #ms = manualStrategy(symbol = 'JPM', sd=start_date, ed=end_date, sv = 100000) np.random.seed(106348) learner = StrategyLearner() learner.addEvidence(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000) df_trades = learner.testPolicy(symbol="JPM", sd=start_date, ed=end_date, sv=100000) df_orders = learner.createOrdersDataFrame(df_trades) ms = manual.manualStrategy(symbol='JPM', sd=start_date, ed=end_date, sv=100000) df_trades_ms = ms.testPolicy() df_orders_ms = ms.createOrdersDataFrame(df_trades_ms) benchmark = learner.createBenchMarkDataFrame(df_trades, sd=start_date) portvals_strategy = msim.compute_portvals(orders_file=df_orders, start_val=100000, commission=0, 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) portvals_manual = msim.compute_portvals(orders_file=df_orders_ms, start_val=100000, commission=0, impact=0.005, end_date=end_date, start_date=start_date) print('Statistics for Random Forest Strategy') msim.computeStatistics(portvals_strategy) print('') print('Statistics for Manual Strategy') msim.computeStatistics(portvals_manual) print('') print('Statistics for Benchmark Strategy') msim.computeStatistics(portvals_benchmark) generatePerformanceChart(df_trades, portvals_strategy, portvals_manual, portvals_benchmark) tot_cum_ret = 0 tot_sharpe_ratio = 0 tot_std_daily_ret = 0 tot_avg_daily_ret = 0 N = 10 i = 0 print '' print 'Lets run the Random Forest 10 times and average results for performance' while i < N: np.random.seed(np.random.randint(0, 100000)) learner = StrategyLearner() learner.addEvidence(symbol="JPM", sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), sv=100000) df_trades = learner.testPolicy(symbol="JPM", sd=start_date, ed=end_date, sv=100000) df_orders = learner.createOrdersDataFrame(df_trades) portvals_strategy = msim.compute_portvals(orders_file=df_orders, start_val=100000, commission=0, impact=0.005, end_date=end_date, start_date=start_date) cum_ret, sharpe_ratio, std_daily_ret, avg_daily_ret = msim.computeStatisticsReturnValues( portvals_strategy) tot_cum_ret = tot_cum_ret + cum_ret tot_sharpe_ratio = tot_sharpe_ratio + sharpe_ratio tot_std_daily_ret = tot_std_daily_ret + std_daily_ret i = i + 1 print 'The average cumulative return is: ' + str(tot_cum_ret / N) print 'The average sharpe ratio is: ' + str(tot_sharpe_ratio / N) print 'The average standard dev of daily returns is: ' + str( tot_std_daily_ret / N)