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 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 evaluateInSample(): ts = ManualStrategy() trades_df = ts.testPolicy(CONSTANTS.DEFAULT_SYMBOL, CONSTANTS.IN_SAMPLE_START, CONSTANTS.IN_SAMPLE_END, CONSTANTS.START_CASH) port_vals = marketsim.compute_portvals(trades_df, CONSTANTS.DEFAULT_SYMBOL, CONSTANTS.START_CASH) port_vals_benchmark = marketsim.compute_portvals( benchmark(CONSTANTS.DEFAULT_SYMBOL, CONSTANTS.IN_SAMPLE_START, CONSTANTS.IN_SAMPLE_END), CONSTANTS.DEFAULT_SYMBOL, CONSTANTS.START_CASH) 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 = plot_df / plot_df.ix[plot_df.index.values[0]] plot_df.plot(color=['red', 'green'], linewidth=1) date_vals = trades_df.index.values previous_holdings = 0 for index, current_date in enumerate(date_vals): current_holdings = previous_holdings + trades_df.ix[current_date, CONSTANTS.TRADES] if previous_holdings != current_holdings: if current_holdings > 0: plt.axvline(current_date, color='blue') elif current_holdings < 0: plt.axvline(current_date, color='black') previous_holdings = current_holdings plt.savefig('Manual-InSample.png') print "In Sample" portfolio_values_final = port_vals.sum(axis=1) portfolio_values_benchmark_final = port_vals_benchmark.sum(axis=1) daily_rets = get_daily_returns(portfolio_values_final) daily_rets_benchmark = get_daily_returns(portfolio_values_benchmark_final) print "Cumulative returns: ", get_cumm_returns( portfolio_values_benchmark_final), get_cumm_returns( portfolio_values_final) print "Stddev: ", get_std_daily_returns( daily_rets_benchmark), get_std_daily_returns(daily_rets) print "Mean of daily returns: ", get_avg_daily_returns( daily_rets_benchmark), get_avg_daily_returns(daily_rets)
return daily_returns.mean() def get_std_daily_returns(daily_returns): return daily_returns.std() if __name__ == "__main__": ts = TheoreticallyOptimalStrategy() trades_df = ts.testPolicy(CONSTANTS.DEFAULT_SYMBOL, CONSTANTS.IN_SAMPLE_START, CONSTANTS.IN_SAMPLE_END, CONSTANTS.START_CASH) port_vals = marketsim.compute_portvals(trades_df, CONSTANTS.DEFAULT_SYMBOL, CONSTANTS.START_CASH, 0, 0) port_vals_benchmark = marketsim.compute_portvals( benchmark(CONSTANTS.DEFAULT_SYMBOL, CONSTANTS.IN_SAMPLE_START, CONSTANTS.IN_SAMPLE_END), CONSTANTS.DEFAULT_SYMBOL, CONSTANTS.START_CASH) plot_df = pd.DataFrame(index=port_vals.index) plot_df["Optimal Portfolio"] = port_vals[CONSTANTS.TOTAL] plot_df["Benchmark"] = port_vals_benchmark[CONSTANTS.TOTAL] plot_df = plot_df / plot_df.ix[plot_df.index.values[0]] plot_df.plot(colors=['red', 'green']) plt.savefig('TOP.png') portfolio_values_final = port_vals.sum(axis=1) portfolio_values_benchmark_final = port_vals_benchmark.sum(axis=1) daily_rets = get_daily_returns(portfolio_values_final) daily_rets_benchmark = get_daily_returns(portfolio_values_benchmark_final) print "Cumulative returns: ", get_cumm_returns(