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 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 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')
Example #4
0
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 experiment2(sd=dt.datetime(2008, 1, 1),
                ed=dt.datetime(2009, 12, 31),
                symbol="JPM",
                sv=100000,
                impact=0.000,
                commission=0,
                graphnum=1):

    manual_trades = ms.testPolicy(symbol=symbol, sd=sd, ed=ed)
    # manual_count = manual_trades.astype(bool).sum(axis=0)
    # print "Number of trades in the Manual Strategy is:", manual_count

    learner = sl.StrategyLearner(verbose=False, impact=impact)
    learner.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=sv)
    sl_trades = learner.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=sv)
    strategy_count = sl_trades.astype(bool).sum(axis=0)
    print "Number of trades in the Strategy Learner is:", strategy_count

    manual_portval = mksim.compute_portvals(manual_trades,
                                            start_val=sv,
                                            commission=commission,
                                            impact=impact)
    # m_cr, m_adr, m_ssdr, m_sr = mksim.compute_portfolio_stats(manual_portval)
    # print "Cumulative Return of the Manual Strategy is:", m_cr

    sl_portval = mksim.compute_portvals(sl_trades,
                                        start_val=sv,
                                        commission=commission,
                                        impact=impact)
    sl_cr, sl_adr, sl_ssdr, sl_sr = mksim.compute_portfolio_stats(sl_portval)
    print "Cumulative Return of the Strategy Learner is:", sl_cr

    benchmark_trades = manual_trades.copy()
    benchmark_trades[:] = 0
    benchmark_trades.ix[0, 0] = 1000
    benchmark_trades.ix[-1, 0] = -1000
    benchmark_portvals = mksim.compute_portvals(benchmark_trades,
                                                start_val=sv,
                                                commission=commission,
                                                impact=impact)

    plot_df = manual_trades.copy()
    plot_df['Manual Strategy'] = manual_portval[0:] / manual_portval.ix[0]
    plot_df['Strategy Learner'] = sl_portval[0:] / sl_portval.ix[0]
    plot_df['Benchmark'] = benchmark_portvals[0:] / benchmark_portvals.ix[0]
    plot_df = plot_df.drop(plot_df.columns[0], axis=1)

    ax = plot_df.plot(title="Stock Prices", color=['b', 'k', 'r'])
    ax.set_xlabel("Date")
    ax.set_ylabel("Normalized Portfolio Return for In-Sample Data")
    plt.savefig("exp2_graph_{}".format(str(graphnum)))
    plt.clf()
Example #6
0
def test_code():
    # Test in sample performance
    prices = get_prices(symbol=SYMBOL,
                        sd=IN_SAMPLE_DATES[0],
                        ed=IN_SAMPLE_DATES[1])
    # Strategy Learner
    st_learner = sl.StrategyLearner(verbose=False, impact=IMPACT)
    st_learner.addEvidence(symbol=SYMBOL,
                           sd=IN_SAMPLE_DATES[0],
                           ed=IN_SAMPLE_DATES[1],
                           sv=START_VAL)
    # Test in sample performance
    st_trades = st_learner.testPolicy(symbol=SYMBOL,
                                      sd=IN_SAMPLE_DATES[0],
                                      ed=IN_SAMPLE_DATES[1],
                                      sv=START_VAL)
    st_orders = st_learner.convert_trades_to_order(st_trades)
    st_portval = sim.compute_portvals(st_orders,
                                      start_val=START_VAL,
                                      commission=TRANSACTION_COMMISSION,
                                      impact=IMPACT,
                                      start_date=IN_SAMPLE_DATES[0],
                                      end_date=IN_SAMPLE_DATES[1])
    st_portval = st_learner.normalize_df(st_portval)
    st_cr, st_adr, st_sddr, st_sr = sim.get_portfolio_stats(st_portval)

    print_performance("Strategy Learner", st_cr, st_adr, st_sddr, st_sr,
                      st_portval)

    # Benchmark
    ben_portvals, ben_cr, ben_adr, ben_sddr, ben_sr = ms.benchmark(
        SYMBOL, IN_SAMPLE_DATES[0], IN_SAMPLE_DATES[1], START_VAL)
    print_performance("Benchmark", ben_cr, ben_adr, ben_sddr, ben_sr,
                      ben_portvals)

    # Manual strategy
    ms_trades = ms.testPolicy(SYMBOL, IN_SAMPLE_DATES[0], IN_SAMPLE_DATES[1],
                              START_VAL)
    ms_orders = ms.convert_trades_to_order(ms_trades)
    ms_portvals = ms.compute_portvals(df_orders=ms_orders,
                                      start_val=START_VAL,
                                      commission=TRANSACTION_COMMISSION,
                                      impact=IMPACT,
                                      start_date=IN_SAMPLE_DATES[0],
                                      end_date=IN_SAMPLE_DATES[1])
    ms_portvals = ms.normalize(ms_portvals)
    ms_cr, ms_adr, ms_sddr, ms_sr = ms.get_portfolio_stats(ms_portvals)
    print_performance("Manual Strategy", ms_cr, ms_adr, ms_sddr, ms_sr,
                      ms_portvals)

    plot_compare(ben_portvals, ms_portvals, st_portval)
def ms_testStrategy(symbol, sd, ed, sv=100000):
    # Set the symbol and the time period
    # Get the orders
    #############################################################
    sd = sd
    ed = ed

    # Get the orders
    df_trades = ms.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=9.95, impact=0.05, 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.05, 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.05, 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)
Example #8
0
def main():
    
    #Parameters
    sd_train = dt.datetime(2008,1,1)
    ed_train = dt.datetime(2009,12,31)
    sd_test = dt.datetime(2010,1,1)
    ed_test = dt.datetime(2011,12,31)
    sym = 'JPM'
    capital = 100000

    #Train strategy learner
    learner = sl.StrategyLearner(verbose = False, impact = 0.0)
    learner.addEvidence(symbol = sym, sd=sd_train, ed=ed_train, sv=capital)

    #Test strategy learner
    #sl_trades = learner.testPolicy(symbol = sym, sd=sd_train, ed=ed_train, sv=capital) #In sample
    sl_trades = learner.testPolicy(symbol = sym, sd=sd_test, ed=ed_test, sv=capital) #Out of sample
    sl_portvals = msc.compute_portvals(sl_trades, start_val=capital, commission=0.0, impact=0.0)

    #Test manual strategy
    #ms_trades = ms.testPolicy(symbol = sym, sd = sd_train, ed = ed_train, sv = capital) #In sample
    ms_trades = ms.testPolicy(symbol = sym, sd = sd_test, ed = ed_test, sv = capital) #Out of sample
    ms_portvals = msc.compute_portvals(ms_trades, start_val=capital, commission=0.0, impact=0.0)

    #Benchmark: Buying 1000 shares of JPM and holding throughout period
    bench_trades = pd.DataFrame(0.0, index=ms_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=0.0)

    #Calculate portfolio statistics for sl, ms, bench
    sl_cr, sl_adr, sl_sdr, sl_sr = compute_portfolio_stats(sl_portvals)
    ms_cr, ms_adr, ms_sdr, ms_sr = compute_portfolio_stats(ms_portvals)
    bench_cr, bench_adr, bench_sdr, bench_sr = compute_portfolio_stats(bench_portvals)

    #Plot performances
    sl_portvals_norm = sl_portvals/sl_portvals.iloc[0]
    ms_portvals_norm = ms_portvals/ms_portvals.iloc[0]
    bench_portvals_norm = bench_portvals/bench_portvals.iloc[0]
    df_compare = pd.DataFrame({'Strategy Learner': sl_portvals_norm.values, 'Manual Strategy': ms_portvals_norm.values, \
                               'Benchmark': bench_portvals_norm.values}, index = sl_portvals_norm.index)


    #ax = df_compare.plot(title = 'Comparing Strategies - In Sample Period', fontsize=12)
    ax = df_compare.plot(title = 'Comparing Strategies - Out-of-Sample Period', fontsize=12)
    ax.set_xlabel('Date')
    ax.set_ylabel('Value')
    plt.show()
def experiment1(sd=dt.datetime(2008, 1, 1),
                ed=dt.datetime(2009, 12, 31),
                symbol="JPM",
                sv=100000,
                impact=0,
                commission=0):

    manual_trades = ms.testPolicy(symbol=symbol, sd=sd, ed=ed)

    learner = sl.StrategyLearner(verbose=False, impact=impact)
    learner.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=sv)
    sl_trades = learner.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=sv)

    manual_portval = mksim.compute_portvals(manual_trades,
                                            start_val=sv,
                                            commission=commission,
                                            impact=impact)
    sl_portval = mksim.compute_portvals(sl_trades,
                                        start_val=sv,
                                        commission=commission,
                                        impact=impact)

    benchmark_trades = manual_trades.copy()
    benchmark_trades[:] = 0
    benchmark_trades.ix[0, 0] = 1000
    benchmark_trades.ix[-1, 0] = -1000
    benchmark_portvals = mksim.compute_portvals(benchmark_trades,
                                                start_val=sv,
                                                commission=commission,
                                                impact=impact)

    plot_df = manual_trades.copy()
    plot_df['Manual Strategy'] = manual_portval[0:] / manual_portval.ix[0]
    plot_df['Strategy Learner'] = sl_portval[0:] / sl_portval.ix[0]
    plot_df['Benchmark'] = benchmark_portvals[0:] / benchmark_portvals.ix[0]
    plot_df = plot_df.drop(plot_df.columns[0], axis=1)

    # print plot_df

    ax = plot_df.plot(title="Stock Prices", color=['b', 'k', 'r'])
    ax.set_xlabel("Date")
    ax.set_ylabel("Normalized Portfolio Return for In-Sample Data")
    plt.savefig("exp1_graph1")
    plt.clf()
Example #10
0
'''
benchmark
'''
date = pd.date_range(sd, ed)
stockvals = ut.get_data([symbol], date)
stockvals = stockvals.loc[:, [symbol]]
Benchmark = pd.DataFrame(0, index = stockvals.index[[0,-1]], columns = ['Order'])
Benchmark['Order'].iloc[0] = 1000
Benchmark['Order'].iloc[-1] = -1000
daily_value_benchmark = msc.compute_portvals(order = Benchmark, symbol = 'JPM', start_val = sv, commission=0, impact=0.000)/sv


'''
manuel strategy
'''
order = ms.testPolicy(symbol = 'JPM', sd = sd, ed = ed, sv = sv)
daily_value_manual = msc.compute_portvals(order = order, symbol = 'JPM', start_val = sv, commission=0, impact=0.000)/sv

cr_benchmark, adr_benchmark, sddr_benchmark = ms.portstats(daily_value_benchmark)
cr_manual, adr_manual, sddr_manual = ms.portstats(daily_value_manual)
cr_strategy, adr_strategy, sddr_strategy = ms.portstats(daily_value_strategy)




buy_m = order[order['Order'] > 0]
sell_m = order[order['Order'] < 0]

plt.figure(figsize=(20,5))
plt.plot(daily_value_benchmark.index, daily_value_benchmark, label = "Benchmark", color = "Blue")
plt.plot(daily_value_manual.index,daily_value_manual, label = "Manual Strategy", color = "Black")
Example #11
0
    return ('mtong31')


if __name__ == "__main__":
    print('Project 8 Experiment1: mtong31')
    sym = 'JPM'
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)
    sd2 = dt.datetime(2010, 1, 1)
    ed2 = dt.datetime(2011, 12, 31)
    sv = 100000
    learner = sl.StrategyLearner(verbose=False, impact=0.00)
    learner.addEvidence(sym, sd, ed, 100000)

    strategy = learner.testPolicy(sym, sd, ed)
    manual = ms.testPolicy(sym, sd, ed)
    benchmark = pd.DataFrame(index=strategy.index)
    benchmark[sym] = 0
    benchmark.iloc[0, 0] = 1000

    values = marketsimcode.compute_portvals(strategy)
    values_bench = marketsimcode.compute_portvals(benchmark)
    values_manual = marketsimcode.compute_portvals(manual)

    # below normalizes gains to the sv
    values /= sv
    values_bench /= sv
    values_manual /= sv

    fig = plt.figure(figsize=(10, 5), dpi=80)
    plt.plot(values, color='b', label='Strategy')
Example #12
0
np.random.seed(1481090000)
import matplotlib.pyplot as plt

## Pranshav Thakkar
## pthakkar7

def author():
    return 'pthakkar7'

if __name__ == '__main__':
    symbol = 'JPM'
    startval = 100000
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)

    manual_trades = ms.testPolicy(symbol, sd, ed, startval)

    manPortvals, manCR, manMean, manSTD = compute_portvals(manual_trades, startval, commission=0.0, impact=0.0)

    learner = sl.StrategyLearner(verbose=False, impact=0.0)
    learner.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=startval)
    learner_trades = learner.testPolicy(symbol, sd, ed, startval)

    learnPortvals, learnCR, learnMean, learnSTD = compute_portvals(learner_trades, startval, commission=0.0, impact=0.0)

    normmanual = manPortvals / manPortvals.iloc[0]
    normlearner = learnPortvals / learnPortvals.iloc[0]

    plt.title("Manual Strategy vs. Strategy Learner")
    plt.xlabel("Dates")
    plt.ylabel("Normalized Value of Portfolio")
Example #13
0
register_matplotlib_converters()

# Akshay Karthik
# akarthik3


def author():
    return 'akarthik3'


if __name__ == '__main__':
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)

    # Calculate normalized portfolio value of manual strategy's policy and strategy learner's learned policy, then plot them
    manualPortvals = compute_portvals(ms.testPolicy(sd=sd, ed=ed),
                                      commission=0.0,
                                      impact=0.0)[0]

    learner = sl.StrategyLearner()
    learner.addEvidence('JPM', sd, ed)
    learnerPortvals = compute_portvals(learner.testPolicy('JPM', sd, ed),
                                       commission=0.0,
                                       impact=0.0)[0]

    plt.title("Manual Strategy vs. Strategy Learner")
    plt.xlabel("Dates")
    plt.ylabel("Normalized Portfolio Value")

    plt.plot(manualPortvals / manualPortvals.iloc[0], label="Manual")
    plt.plot(learnerPortvals / learnerPortvals.iloc[0], label="Learner")
strategy_order_list.append([end_date_train.date(), syms, 'SELL', 0])

strategy_order_n = pd.DataFrame(np.array(strategy_order_list), columns=['Date', 'Symbol', 'Order', 'Shares'])
strategy_order_n.set_index('Date', inplace=True)

strategy_portvals = mk.compute_portvals(strategy_order_n, start_val=100000, commission=0, impact=0)
strategy_portvals.fillna(method='ffill', inplace=True)
strategy_portvals.fillna(method='bfill', inplace=True)
print "strategy portvals"
print strategy_portvals


print strategy_order_n

order_list_manual = mn.testPolicy(symbol = "JPM", \
        sd=dt.datetime(2008,1,1), \
        ed=dt.datetime(2009,12,31), \
        sv = 10000)
print order_list_manual


price = get_data([syms], pd.date_range(start_date_train, end_date_train))
benchmark_frame = price
benchmark_frame['Benchmark'] = np.nan
benchmark_frame['Benchmark'].ix[0] = 100000

for i in range(0, benchmark_frame.shape[0]):
    benchmark_frame['Benchmark'].ix[i] = 100000 - 1000 * price['JPM'].ix[0] + \
                                          1000 * price['JPM'].ix[i]

del benchmark_frame['SPY']
del benchmark_frame['JPM']
Example #15
0
    trade_rf, trade_dt = learner.testPolicy(sym, sd, ed, sv)
    print "\nRandom Forest\n"
    portval1 = compute_portvals(sym,
                                trade_rf,
                                commission=0,
                                impact=0.001,
                                start_val=sv)
    print "\nDecision Trees\n"
    portval_dt = compute_portvals(sym,
                                  trade_dt,
                                  commission=0,
                                  impact=0.001,
                                  start_val=sv)

    ##Manual Strategy
    trade2 = ms.testPolicy(sym, sd, ed, sv)
    print "\nManual Strategy\n"
    portval2 = compute_portvals(sym,
                                trade2,
                                commission=0,
                                impact=0.00,
                                start_val=sv)

    ##CNN
    original = sys.stdout  #temporarily redirect output
    sys.stdout = open('CNN_debug.log', 'w')
    trade_cnn = cnn.cnn(sym, sd, ed)
    sys.stdout = original
    print "\nCNN\n"
    portval_cnn = compute_portvals(sym,
                                   trade_cnn,
Example #16
0
def test_code():
        # Manual Strategy plots for JPM in sample and benchmark
        df_trades = man.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))
        result = result[result != 0];
        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

        # benchmark for BestStrategy
        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.ix[-1, "Order"] = 'SELL';
        orders.ix[-1, "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,:]

        # plot for BestStrategy
        df_temp = pd.concat([normed_pors, normed_bench], keys=['portvals', 'benchmark'], axis=1)
        ax = df_temp.plot(color = ['black', 'blue'], title = "Best Possible Portfolio vs Benchamark for JPM")


        # Strategy Learner plots for JPM in sample and benchmark
        learner = sl.StrategyLearner(verbose=False,impact=0)
        learner.addEvidence(symbol = "JPM", sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), sv = 100000)
        frame = learner.testPolicy(symbol = "JPM", sd=dt.datetime(2008,1,1), ed=dt.datetime(2009,12,31), sv = 100000)

        # change the format of the result of the strategy learner to make as input for marketsim
        Qframe = pd.DataFrame(index = frame.index, columns = ['Symbol', 'Order', 'Shares']);
        Qframe.ix[0:, 'Symbol'] = "JPM"
        for i in range (0, len(frame)):
                if frame.ix[i, 'value'] < 0:
                        Qframe.ix[i, 'Order'] = 'SELL'
                        Qframe.ix[i, 'Shares'] = np.absolute(frame.ix[i, 'value'])
                else:
                        Qframe.ix[i, 'Order'] = 'BUY'
                        Qframe.ix[i, 'Shares'] = np.absolute(frame.ix[i, 'value'])
        Qframe = Qframe[Qframe.Shares != 0]

        # use marketsim to calculate portfolio and compare to plot with benchmark
        Qresult = ms.compute_portvals(orders = Qframe, start_val = 100000, commission = 0, impact = 0, symbols = "JPM", start_date=dt.datetime(2008,1,1), end_date=dt.datetime(2009,12,31))
        Qresult = Qresult[Qresult != 0];
        cr = Qresult[-1] / Qresult[0] - 1
        daily_rets = (Qresult / Qresult.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

        # plot for StrategyLeaner
        normed_Q = Qresult / Qresult.ix[0,:]
        df_temp2 = pd.concat([normed_Q, normed_bench], keys=['portvals', 'benchmark'], axis=1)
        ax = df_temp2.plot(color = ['black', 'blue'], title = "StrategyLearner Portfolio vs Benchamark for JPM")

        plt.show()
Example #17
0
def experiment1():

    sl = StrategyLearner()
    ms = ManualStrategy()
    start_date = dt.datetime(2008, 1, 1)
    end_date = dt.datetime(2009, 12, 31)

    symbol = 'JPM'
    starting_value = 100000
    commission = 0
    impact = 0

    sl.addEvidence(symbol=symbol,
                   sd=dt.datetime(2008, 1, 1),
                   ed=dt.datetime(2009, 12, 31),
                   sv=starting_value)
    qlearn_trades = sl.testPolicy(symbol="JPM",
                                  sd=dt.datetime(2008, 1, 1),
                                  ed=dt.datetime(2009, 12, 31),
                                  sv=starting_value)
    manual_trades = ms.testPolicy(symbol="JPM",
                                  sd=dt.datetime(2008, 1, 1),
                                  ed=dt.datetime(2009, 12, 31),
                                  sv=starting_value)
    benchmark_trades = ms.get_benchmark_order(symbol="JPM",
                                              sd=dt.datetime(2008, 1, 1),
                                              ed=dt.datetime(2009, 12, 31),
                                              sv=starting_value)

    qlearn_portfolio = (mm.compute_portvals(
        qlearn_trades, starting_value, commission, impact)) / starting_value
    manual_potfolio = (mm.compute_portvals(
        manual_trades, starting_value, commission, impact)) / starting_value
    benchmark_portfolio = (mm.compute_portvals(
        benchmark_trades, starting_value, commission, impact)) / starting_value

    plt.clf()
    plt.figure(figsize=(15, 5))
    plt.xlim(start_date, end_date)

    ax = qlearn_portfolio.plot(
        title=
        "Figure 1. QLearning Strategy vs. Manual Rule-based vs. Benchmark",
        fontsize=12,
        color="green",
        label="QLearner-based")
    benchmark_portfolio.plot(ax=ax, color="black", label="Benchmark")
    manual_potfolio.plot(ax=ax, color="blue", label="Manual Rule-based")
    ax.set_ylabel('Normalized Value')
    ax.set_xlabel('Dates')

    plt.legend(['QLearner', 'Benchmark', "Manual Rule"], loc="lower right")
    plt.savefig("In-Sample.png")
    #plt.show()

    q_cr, q_adr, q_sddr, q_sr = get_portfolio_stats(qlearn_portfolio)
    m_cr, m_adr, m_sddr, m_sr = get_portfolio_stats(manual_potfolio)
    b_cr, b_adr, b_sddr, b_sr = get_portfolio_stats(benchmark_portfolio)

    print "Cumulative Return of {}: {}".format("QLearner", q_cr)
    print "Cumulative Return of {}: {}".format("ManualStrategy", m_cr)
    print "Cumulative Return of {}: {}".format("Benchmark", b_cr)
    print "Mean Daily Return of {}: {}".format("QLearner", q_adr)
    print "Mean Daily Return of {}: {}".format("ManualStrategy", m_adr)
    print "Mean Daily Return of {}: {}".format("Benchmark", b_adr)
    print "Standard Deviation of daily return of {}: {}".format(
        "QLearner", q_sddr)
    print "Standard Deviation of daily return of {}: {}".format(
        "ManualStrategy", m_sddr)
    print "Standard Deviation of daily return of {}: {}".format(
        "Benchmark", b_sddr)
Example #18
0
def test_code():
    # this is a helper function you can use to test your code
    # note that during autograding his function will not be called.
    # Define input parameters
    sv = 100000
    commission = 0
    impact = 0.0
    commission_manual = 9.95
    impact_manual = 0.005

    sd_in = dt.datetime(2008, 1, 1)
    ed_in = dt.datetime(2009, 12, 31)

    optimal_df_trades = tos.testPolicy(symbol="JPM", sd=sd_in, ed=ed_in)
    optimal_portvals = compute_portvals(optimal_df_trades,
                                        start_val=sv,
                                        commission=commission,
                                        impact=impact)

    manual_df_trades = ms.testPolicy(symbol="JPM", sd=sd_in, ed=ed_in)
    manual_portvals = compute_portvals(manual_df_trades,
                                       start_val=sv,
                                       commission=commission_manual,
                                       impact=impact_manual)

    benchmark_df_trades = optimal_df_trades.copy()
    benchmark_df_trades[:] = 0
    benchmark_df_trades.ix[0, 0] = 1000
    benchmark_df_trades.ix[-1, 0] = -1000
    benchmark_portvals = compute_portvals(benchmark_df_trades,
                                          start_val=sv,
                                          commission=commission,
                                          impact=impact)

    plot_df = optimal_df_trades.copy()
    plot_df['Benchmark'] = benchmark_portvals[0:] / benchmark_portvals.ix[0]
    plot_df['Optimal Strategy'] = optimal_portvals[0:] / optimal_portvals.ix[0]
    plot_df = plot_df.drop(plot_df.columns[0], axis=1)

    ax = plot_df.plot(title="Stock Prices", color=['b', 'k'])
    ax.set_xlabel("Date")
    ax.set_ylabel("Normalized Portfolio Return")
    plt.savefig("graph")
    plt.clf()

    manual_plot_df = plot_df.copy()
    manual_plot_df[
        'Manual Strategy'] = manual_portvals[0:] / manual_portvals.ix[0]
    manual_plot_df = manual_plot_df.drop('Optimal Strategy', axis=1)

    ax = manual_plot_df.plot(title="Stock Prices", color=['b', 'k'])
    for trade in manual_df_trades.index[manual_df_trades['JPM'] < 0]:
        ax.axvline(x=trade, color='red')
    for trade in manual_df_trades.index[manual_df_trades['JPM'] > 0]:
        ax.axvline(x=trade, color='green')
    ax.set_xlabel("Date")
    ax.set_ylabel("Normalized Portfolio Return")
    plt.savefig("graph2")
    plt.clf()

    ###########################################################################################################################
    sd_out = dt.datetime(2010, 1, 1)
    ed_out = dt.datetime(2011, 12, 31)

    manual_df_trades_out = ms.testPolicy(symbol="JPM", sd=sd_out, ed=ed_out)
    manual_portvals_out = compute_portvals(manual_df_trades_out,
                                           start_val=sv,
                                           commission=commission_manual,
                                           impact=impact_manual)

    benchmark_df_trades_out = manual_df_trades_out.copy()
    benchmark_df_trades_out[:] = 0
    benchmark_df_trades_out.ix[0, 0] = 1000
    benchmark_df_trades_out.ix[-1, 0] = -1000
    benchmark_portvals_manual_out = compute_portvals(
        benchmark_df_trades_out,
        start_val=sv,
        commission=commission_manual,
        impact=impact_manual)

    manual_plot_df_out = manual_df_trades_out.copy()
    manual_plot_df_out['Benchmark'] = benchmark_portvals_manual_out[
        0:] / benchmark_portvals_manual_out.ix[0]
    manual_plot_df_out[
        'Manual Strategy (Out of Sample)'] = manual_portvals_out[
            0:] / manual_portvals_out.ix[0]
    manual_plot_df_out = manual_plot_df_out.drop(manual_plot_df_out.columns[0],
                                                 axis=1)

    ax = manual_plot_df_out.plot(title="Stock Prices", color=['b', 'k'])
    for trade in manual_df_trades_out.index[manual_df_trades_out['JPM'] < 0]:
        ax.axvline(x=trade, color='red')
    for trade in manual_df_trades_out.index[manual_df_trades_out['JPM'] > 0]:
        ax.axvline(x=trade, color='green')
    ax.set_xlabel("Date")
    ax.set_ylabel("Normalized Portfolio Return")
    plt.savefig("graph3")
    plt.clf()

    # if isinstance(portvals, pd.DataFrame):
    #     portvals = portvals[portvals.columns[0]] # just get the first column
    # else:
    #     "warning, code did not return a DataFrame"

    # Get portfolio stats
    # Here we just fake the data. you should use your code from previous assignments.
    cr, adr, ssdr, sr = compute_portfolio_stats(optimal_portvals)
    cr_manual, adr_manual, ssdr_manual, sr_manual = compute_portfolio_stats(
        manual_portvals)
    cr_bm, adr_bm, ssdr_bm, sr_bm = compute_portfolio_stats(benchmark_portvals)
    cr_m_out, adr_m_out, ssdr_m_out, sr_m_out = compute_portfolio_stats(
        manual_portvals_out)

    start_date = dt.datetime(2008, 1, 1)
    end_date = dt.datetime(2009, 12, 31)

    # cum_ret_man, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = [0.2,0.01,0.02,1.5]

    # Compare portfolio against $SPX
    print "Date Range: {} to {}".format(start_date, end_date)
    print
    print "Sharpe Ratio of Optimal Strategy Fund (In Sample): {}".format(sr)
    print "Sharpe Ratio of Manual Strategy Fund (In Sample) : {}".format(
        sr_manual)
    print "Sharpe Ratio of Benchmark Strategy Fund (In Sample) : {}".format(
        sr_bm)
    print "Sharpe Ratio of Manual Strategy Fund (Out Sample) : {}".format(
        sr_m_out)

    print
    print "Cumulative Return of Optimal Strategy Fund (In Sample): {}".format(
        cr)
    print "Cumulative Return of Manual Strategy Fund (In Sample) : {}".format(
        cr_manual)
    print "Cumulative Return of Benchmark Strategy Fund (In Sample) : {}".format(
        cr_bm)
    print "Cumulative Return of Manual Strategy Fund (Out Sample) : {}".format(
        cr_m_out)
    print
    print "Standard Deviation of Optimal Strategy Fund (In Sample): {}".format(
        ssdr)
    print "Standard Deviation of Manual Strategy Fund (In Sample) : {}".format(
        ssdr_manual)
    print "Standard Deviation of Benchmark Strategy Fund (In Sample) : {}".format(
        ssdr_bm)
    print "Standard Deviation of Manual Strategy Fund (Out Sample) : {}".format(
        ssdr_m_out)
    print
    print "Average Daily Return of Optimal Fund (In Sample): {}".format(adr)
    print "Average Daily Return of Manual Strategy Fund (In Sample) : {}".format(
        adr_manual)
    print "Average Daily Return of Benchmark Strategy Fund (In Sample) : {}".format(
        adr_bm)
    print "Average Daily Return of Manual Strategy Fund (Out Sample) : {}".format(
        adr_m_out)
    print
    print "Final Optimal Strategy Fund Portfolio Value (In Sample): {}".format(
        optimal_portvals[-1])
    print "Final Manual Strategy Fund  Portfolio Value (In Sample): {}".format(
        manual_portvals[-1])
    print "Final Benchmark Strategy Fund  Portfolio Value (In Sample): {}".format(
        benchmark_portvals[-1])
    print "Final Manual Strategy Fund  Portfolio Value (Out Sample): {}".format(
        manual_portvals_out[-1])


# if __name__ == "__main__":
# test_code()
import marketsimcode
from util import get_data, plot_data
import pandas as pd

# Specifying training time periods
train_sd = dt.datetime(2008, 1, 1)
train_ed = dt.datetime(2009, 12, 31)
# Specifying symbol
symbol = "JPM"
# Specifying Start value
sv = 100000
impact = 0.005

########################### Manual Strategy ###################################
orders_manual = ManualStrategy.testPolicy(symbol=symbol,
                                          sd=train_sd,
                                          ed=train_ed,
                                          sv=sv)
portvals_manual = marketsimcode.compute_portvals(orders_manual,
                                                 start_val=sv,
                                                 commission=0,
                                                 impact=impact)

############################ Strategy Learner #################################
st = StrategyLearner.StrategyLearner(impact=impact)
st.addEvidence(symbol = symbol, \
        sd=train_sd, \
        ed=train_ed, \
        sv = sv)
orders_strategy = st.testPolicy(symbol=symbol, sd=train_sd, ed=train_ed, sv=sv)

# converting strategy learner orders to correct format
Example #20
0
def test_code():
    sym = 'JPM'
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)

    learner = sl.StrategyLearner(verbose=False, impact=0.005)  # constructor
    learner.addEvidence(symbol=sym, sd=sd, ed=ed, sv=100000)  # training phase

    orders = learner.testPolicy(symbol=sym, sd=sd, ed=ed,
                                sv=100000)  # testing phase
    slDF = mrk.tranformOrders(orders)
    slDF = mrk.compute_portvals(slDF, 100000, commission=0.0, impact=0.005)
    norm_slDF = slDF / slDF.iloc[0]

    orders = ms.testPolicy(symbol=sym, sd=sd, ed=ed, sv=100000)
    msDF = mrk.tranformOrders(orders)
    msDF = mrk.compute_portvals(msDF, 100000, commission=0.0, impact=0.005)
    norm_msDF = msDF / msDF.iloc[0]

    orders = ms.testPolicyOpt(symbol=sym, sd=sd, ed=ed, sv=100000)
    optDF = mrk.tranformOrders(orders)
    optDF = mrk.compute_portvals(optDF, 100000, commission=0.0, impact=0.005)
    norm_optDF = optDF / optDF.iloc[0]

    bench_DF = mrk.benchmark("JPM", sd=sd, ed=ed, sv=100000)
    norm_bench_DF = bench_DF / bench_DF.iloc[0]

    plt.figure(figsize=(14, 6))
    plt.title("Experiment 1: Manual vs. Q-Learner")
    plt.xlabel("Date")
    plt.ylabel("Portfolio Value")
    plt.plot(norm_optDF, 'green', label="Optimal")
    plt.plot(norm_slDF, 'red', label="Q-Learner")
    plt.plot(norm_msDF, 'black', label="Manual")
    plt.plot(norm_bench_DF, 'blue', label="Benchmark")
    plt.tick_params(axis='x', labelrotation=25)
    plt.legend()
    plt.grid()
    plt.savefig("Experiment1.png", dpi=300)

    opt_cr, opt_adr, opt_sddr, opt_sr = mrk.calc_stats(optDF['SUM'])
    q_cr, q_adr, q_sddr, q_sr = mrk.calc_stats(slDF['SUM'])
    man_cr, man_adr, man_sddr, man_sr = mrk.calc_stats(msDF['SUM'])
    bench_cr, bench_adr, bench_sddr, bench_sr = mrk.calc_stats(bench_DF['SUM'])
    # Compare portfolio against $SPX
    # print "Date Range: {} to {}".format(start_date, end_date)
    print
    print "Sharpe Ratio of Optimal: {}".format(opt_sr)
    print "Sharpe Ratio of Learner: {}".format(q_sr)
    print "Sharpe Ratio of Manual: {}".format(man_sr)
    print "Sharpe Ratio of Benchmark: {}".format(bench_sr)
    print
    print "Cumulative Return of Optimal: {}".format(opt_cr)
    print "Cumulative Return of Learner: {}".format(q_cr)
    print "Cumulative Return of Manual: {}".format(man_cr)
    print "Cumulative Return of Benchmark: {}".format(bench_cr)
    print
    print "Standard Deviation of Optimal: {}".format(opt_sddr)
    print "Standard Deviation of Learner: {}".format(q_sddr)
    print "Standard Deviation of Manual: {}".format(man_sddr)
    print "Standard Deviation of Benchmark: {}".format(bench_sddr)
    print
    print "Average Daily Return of Optimal: {}".format(opt_adr)
    print "Average Daily Return of Learner: {}".format(q_adr)
    print "Average Daily Return of Manual: {}".format(man_adr)
    print "Average Daily Return of Benchmark: {}".format(bench_adr)
Example #21
0
    rtl.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=100000)
    #testing
    trades_rtl = rtl.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=100000)
    vals_rtl = marketsim(trades_rtl,
                         prices)  #default impact and commission are 0

    #ML_3indicators portvals table
    #    rtl3 = sl1.StrategyLearner(verbose = False, impact = 0)
    #train learner
    #    rtl3.addEvidence(symbol = symbol, sd = sd, ed = ed, sv = 100000)
    #testing
    #    trades_rtl3 = rtl3.testPolicy(symbol = symbol,sd = sd, ed = ed, sv = 100000)
    #    vals_rtl3 = marketsim(trades_rtl3, prices) #default impact and commission are 0

    #manual_strategy portvals table
    trades_manual = manu.testPolicy(symbol=symbol, sd=sd, ed=ed)
    vals_manual = marketsim(trades_manual,
                            prices)  #default impact and commission are 0

    #normalization of the portvals
    vals_benchmark = vals_benchmark / vals_benchmark.ix[0]
    vals_rtl = vals_rtl / vals_rtl.ix[0]
    #    vals_rtl3 = vals_rtl3 / vals_rtl3.ix[0]
    vals_manual = vals_manual / vals_manual.ix[0]

    #plots
    benchmark, = plt.plot(vals_benchmark, "b", label="Benchmark")
    rtl, = plt.plot(vals_rtl, "r", label="RTLearner_Strategy")
    #    rtl3, = plt.plot(vals_rtl3, "g", label = "RTLearner_Strategy_3int")
    manual_strategy, = plt.plot(vals_manual, "k", label="Manual_Strategy")
    plt.gcf().subplots_adjust(bottom=0.2)
Example #22
0
    print "In-sample"
    print
    print "Date Range: {} to {}".format(sd, ed)
    print
    print "Cumulative Return of Portfolio(Q): {}".format(cum_ret)
    print "Cumulative Return of Benchmark : {}".format(cum_ret_b)
    print
    print "Standard Deviation of Portfolio(Q): {}".format(std_daily_ret)
    print "Standard Deviation of Benchmark : {}".format(std_daily_ret_b)
    print
    print "Average Daily Return of Portfolio(Q): {}".format(avg_daily_ret)
    print "Average Daily Return of Benchmark : {}".format(avg_daily_ret_b)
    print

    ms.testPolicy(symbol="JPM",
                  sd=dt.datetime(2008, 1, 1),
                  ed=dt.datetime(2009, 12, 31),
                  sv=100000)

    sd = dt.datetime(2010, 1, 1)
    ed = dt.datetime(2011, 12, 31)
    sv = 100000
    syms = [symbol]
    dates = pd.date_range(sd, ed)
    df = get_data(syms, dates)
    prices = df[symbol]
    sp = prices.iloc[0]
    ep = prices.iloc[-1]
    bm = prices * 1000 + (sv - sp * 1000)

    learner = sl.StrategyLearner(verbose=False, impact=0.005)  # constructor
    learner.addEvidence(symbol="JPM",
Example #23
0
    strategy_learner = sl.StrategyLearner(verbose=False, impact=impact)
    strategy_learner.addEvidence(symbol="JPM",
                                 sd=start_date,
                                 ed=end_date,
                                 sv=sv)
    trades_df = strategy_learner.testPolicyWithAdditionalStats(symbol="JPM",
                                                               sd=start_date,
                                                               ed=end_date,
                                                               sv=sv)

    portvals_sl = msc.compute_portvals(trades_df,
                                       start_val=sv,
                                       commission=commission,
                                       impact=impact)
    trades_ms_df = ms.testPolicy(symbol="JPM",
                                 sd=start_date,
                                 ed=end_date,
                                 sv=sv)
    portvals_ms = msc.compute_portvals(trades_ms_df,
                                       start_val=sv,
                                       commission=commission,
                                       impact=impact)

    benchmark = ms.testPolicyBenchmark(symbol="JPM",
                                       sd=start_date,
                                       ed=end_date,
                                       sv=sv)
    port_values_benchmark = msc.compute_portvals(benchmark,
                                                 start_val=sv,
                                                 commission=commission,
                                                 impact=impact)
Example #24
0
def test_code():
    # this is a helper function you can use to test your code
    # note that during autograding his function will not be called.
    # Define input parameters
    plotgraph('BestPossibleStrategy',
              'Best Strategy',
              policy=bp.testPolicy,
              sd=dt.datetime(2008, 1, 1),
              ed=dt.datetime(2009, 12, 31))
    plotgraph('In Sample ManualStrategy',
              'Manual Strategy',
              sd=dt.datetime(2008, 1, 1),
              ed=dt.datetime(2009, 12, 31))
    plotgraph('Out of Sample ManualStrategy',
              'Manual Strategy',
              sd=dt.datetime(2010, 1, 1),
              ed=dt.datetime(2011, 12, 31))
    mstrade = ms.testPolicy(symbol="JPM",
                            sd=dt.datetime(2010, 1, 1),
                            ed=dt.datetime(2011, 12, 31),
                            sv=100000)

    portvals = compute_portvals(mstrade)
    if isinstance(portvals, pd.DataFrame):
        portvals = portvals[portvals.columns[0]]
    # Get portfolio stats
    # Here we just fake the data. you should use your code from previous assignments.
    start_date = portvals.index[0]
    end_date = portvals.index[-1]
    daily_return = portvals.copy()
    daily_return[1:] = (portvals.ix[1:] / portvals.ix[:-1].values) - 1
    daily_return.ix[0] = 0.
    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = [
        portvals.ix[-1] / portvals.ix[0] - 1, daily_return[1:].mean(),
        daily_return[1:].std(),
        252**0.5 * ((daily_return[1:] - 0).mean()) / daily_return[1:].std()
    ]
    bm = pd.DataFrame()
    bm['Date'] = [dt.datetime(2010, 1, 4), dt.datetime(2011, 12, 30)]
    bm['JPM'] = [1000, -1000]

    bm.set_index('Date', inplace=True)
    bmportvals = compute_portvals(bm)
    if isinstance(bmportvals, pd.DataFrame):
        bmportvals = bmportvals[bmportvals.columns[0]]
    bmdaily_return = bmportvals.copy()
    bmdaily_return[1:] = (bmportvals.ix[1:] / bmportvals.ix[:-1].values) - 1
    bmdaily_return.ix[0] = 0.
    cum_ret_bm, avg_daily_ret_bm, std_daily_ret_bm, sharpe_ratio_bm = [
        bmportvals.ix[-1] / bmportvals.ix[0] - 1, bmdaily_return[1:].mean(),
        bmdaily_return[1:].std(), 252**0.5 *
        ((bmdaily_return[1:] - 0).mean()) / bmdaily_return[1:].std()
    ]

    #
    # bptesttrade =bp.testPolicy(symbol = "JPM", sd=dt.datetime(2010,1,1), ed=dt.datetime(2011,12,31), sv = 100000)
    #
    # portvals =compute_portvals(bptesttrade)
    # bm.set_index ([[dt.datetime(2010,1,1), dt.datetime(2011,12,31)]],inplace=True)
    #
    # bmportvals =  compute_portvals(bm)
    # bmportvals = bmportvals[bmportvals.columns[0]]

    # Compare portfolio against $SPX
    print "Date Range: {} to {}".format(start_date, end_date)
    print
    print
    print
    print "Sharpe Ratio of Fund: {}".format(sharpe_ratio)
    print "Sharpe Ratio of BM : {}".format(sharpe_ratio_bm)
    print
    print "Cumulative Return of Fund: {}".format(cum_ret)
    print "Cumulative Return of BM : {}".format(cum_ret_bm)
    print
    print "Standard Deviation of Fund: {}".format(std_daily_ret)
    print "Standard Deviation of BM : {}".format(std_daily_ret_bm)
    print
    print "Average Daily Return of Fund: {}".format(avg_daily_ret)
    print "Average Daily Return of BM : {}".format(avg_daily_ret_bm)
    print
    print "Final Portfolio Value: {}".format(portvals[-1])
    print "Final Benchmark Portfolio Value: {}".format(bmportvals[-1])
Example #25
0
    prices_all = ut.get_data([symbol], dates)  # automatically adds SPY


    prices = ut.get_data([symbol], pd.date_range(sd, ed))
    prices = prices[symbol]

    benchmark_trades = prices_all[[symbol,]].copy(deep=True)  # only portfolio symbols
    benchmark_trades.values[:, :] = 0
    benchmark_trades.values[0, :] = 1000
    vals_bench = marketsim(benchmark_trades, prices)

    learner = sl.StrategyLearner(verbose = False, impact = 0) # constructor
    learner.addEvidence(symbol = symbol, sd=sd, ed=ed, sv = 100000) # training phase
    df_trades = learner.testPolicy(symbol = symbol, sd=sd, ed=ed, sv = 100000) # testing phase
    vals_ml = marketsim(df_trades, prices, impact = 0)

    manual_trades = ms.testPolicy(symbol=symbol, sd=sd, ed=ed)
    vals_manual = marketsim(manual_trades, prices, impact = 0, commission = 0)

    # portvals = portvals / portvals.ix[0]
    vals_bench = vals_bench / vals_bench.ix[0]
    vals_ml = vals_ml / vals_ml.ix[0]
    vals_manual = vals_manual / vals_manual.ix[0]

    benchmark, = plt.plot(vals_bench, 'b', label = 'Benchmark')
    mystrategy, = plt.plot(vals_manual, 'k', label = 'Manual Strategy')
    mlstrategy, = plt.plot(vals_ml, 'r', label = 'ML strategy')

    plt.legend(handles=[benchmark, mystrategy, mlstrategy], loc=2)
    plt.show()
Example #26
0
    #Mean
    mean_port = daily_ret_port.mean()
    print("Mean of Daily Returns of " + strategy_name + ":" + str(mean_port))


if __name__ == "__main__":
    register_matplotlib_converters()
    np.random.seed(903511279)
    symbol = ['JPM']

    #In sample
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)

    #Manual Strategy
    trades_df = ms.testPolicy(symbol, sd, ed, 100000.0)
    portvals = msc.compute_portvals(trades_df, 100000, 0.0, 0.005, sd, ed)

    #Benchmark: Starting cash: $100,000, investing in 1000 shares of JPM and holding that position.
    market_dates = pd.date_range(sd, ed)
    prices_JPM = get_data(symbol, market_dates)
    prices_JPM = prices_JPM['JPM']
    normed_prices = prices_JPM / prices_JPM.iloc[0]
    normed_prices = normed_prices.to_frame()
    bench_trades = pd.DataFrame(index=normed_prices.index)
    #initialzing bench_trades values
    bench_trades['Symbol'] = 'JPM'
    bench_trades['Order'] = np.NaN
    bench_trades['Shares'] = 1000.0
    #BUY 1000 JPM on day1
    bench_trades.iloc[0, 1] = 'BUY'
def experiment2():

    sl = StrategyLearner()
    ms = ManualStrategy()
    start_date = dt.datetime(2008, 1, 1)
    end_date = dt.datetime(2009, 12, 31)

    symbol = 'JPM'
    starting_value = 100000
    commission = 0
    impact = 0.002

    sl.addEvidence(symbol=symbol,
                   sd=dt.datetime(2008, 1, 1),
                   ed=dt.datetime(2009, 12, 31),
                   sv=starting_value)
    qlearn_trades = sl.testPolicy(symbol="JPM",
                                  sd=dt.datetime(2008, 1, 1),
                                  ed=dt.datetime(2009, 12, 31),
                                  sv=starting_value)
    manual_trades = ms.testPolicy(symbol="JPM",
                                  sd=dt.datetime(2008, 1, 1),
                                  ed=dt.datetime(2009, 12, 31),
                                  sv=starting_value)
    benchmark_trades = ms.get_benchmark_order(symbol="JPM",
                                              sd=dt.datetime(2008, 1, 1),
                                              ed=dt.datetime(2009, 12, 31),
                                              sv=starting_value)

    qlearn_portfolio = (mm.compute_portvals(
        qlearn_trades, starting_value, commission, impact)) / starting_value
    manual_potfolio = (mm.compute_portvals(
        manual_trades, starting_value, commission, impact)) / starting_value
    benchmark_portfolio = (mm.compute_portvals(
        benchmark_trades, starting_value, commission, impact)) / starting_value

    plt.clf()
    plt.figure(figsize=(15, 5))
    plt.xlim(start_date, end_date)
    plt.ylim(0.5, 2.0)

    ax = qlearn_portfolio.plot(
        title=
        "Figure 1. QLearning Strategy vs. Manual Rule-based vs. Benchmark with 0.002 Impact",
        fontsize=12,
        color="green",
        label="QLearner-based")
    benchmark_portfolio.plot(ax=ax, color="black", label="Benchmark")
    manual_potfolio.plot(ax=ax, color="blue", label="Manual Rule-based")
    ax.set_ylabel('Normalized Value')
    ax.set_xlabel('Dates')

    plt.legend(['QLearner', 'Benchmark', "Manual Rule"], loc="lower right")
    plt.savefig("Impact1.png")
    #plt.show()

    #======================================================================================================================================================

    impact_2 = 0.007

    sl_2 = StrategyLearner()
    ms_2 = ManualStrategy()
    sl_2.addEvidence(symbol=symbol,
                     sd=dt.datetime(2008, 1, 1),
                     ed=dt.datetime(2009, 12, 31),
                     sv=starting_value)
    qlearn_trades_2 = sl_2.testPolicy(symbol="JPM",
                                      sd=dt.datetime(2008, 1, 1),
                                      ed=dt.datetime(2009, 12, 31),
                                      sv=starting_value)
    manual_trades_2 = ms.testPolicy(symbol="JPM",
                                    sd=dt.datetime(2008, 1, 1),
                                    ed=dt.datetime(2009, 12, 31),
                                    sv=starting_value)
    benchmark_trades_2 = ms.get_benchmark_order(symbol="JPM",
                                                sd=dt.datetime(2008, 1, 1),
                                                ed=dt.datetime(2009, 12, 31),
                                                sv=starting_value)

    qlearn_portfolio_2 = (mm.compute_portvals(qlearn_trades_2, starting_value,
                                              commission,
                                              impact_2)) / starting_value
    manual_potfolio_2 = (mm.compute_portvals(manual_trades_2, starting_value,
                                             commission,
                                             impact_2)) / starting_value
    benchmark_portfolio_2 = (mm.compute_portvals(benchmark_trades_2,
                                                 starting_value, commission,
                                                 impact_2)) / starting_value

    plt.clf()
    plt.figure(figsize=(15, 5))
    plt.xlim(start_date, end_date)
    plt.ylim(0.5, 2.0)

    ax = qlearn_portfolio_2.plot(
        title=
        "Figure 1. QLearning Strategy vs. Manual Rule-based vs. Benchmark with 0.007 Impact",
        fontsize=12,
        color="green",
        label="QLearner-based")
    benchmark_portfolio_2.plot(ax=ax, color="black", label="Benchmark")
    manual_potfolio_2.plot(ax=ax, color="blue", label="Manual Rule-based")
    ax.set_ylabel('Normalized Value')
    ax.set_xlabel('Dates')

    plt.legend(['QLearner', 'Benchmark', "Manual Rule"], loc="lower right")
    plt.savefig("Impact2.png")
Example #28
0
def plot_code(
        sd=dt.datetime(2008, 1, 1), ed=dt.datetime(2009, 12, 31), impact=0.0):
    # orders_df = bps.testPolicy(sd=start_date, ed=end_date)
    plt.figure()  # I'm going to try to cheat

    orders_df = ms.testPolicy(sd=sd, ed=ed, sv=100000)
    benchmark_policy = ind.benchmark_policy(sd=sd, ed=ed, sv=100000)

    #get strat learner orders
    strat_learner = sl.StrategyLearner(verbose=False, impact=impact)
    strat_learner.addEvidence(symbol="JPM", sd=sd, ed=ed, sv=100000)
    strat_orders_df = strat_learner.testPolicy(sd=sd, ed=ed, sv=100000)
    strat_orders_df = strat_learner.get_classic_policy_for_testing()

    benchmark_policy.to_csv('benchmark.csv')
    orders_df.to_csv('orders.csv')
    strat_orders_df.to_csv('strat_orders.csv')

    # portvals_raw = compute_portvals_abridged(orders_df, commission=0, impact=0)
    portvals_raw = market.compute_portvals_abridged(orders_df,
                                                    commission=0,
                                                    impact=impact)
    benchmark_portvals_raw = market.compute_portvals_abridged(benchmark_policy,
                                                              commission=0,
                                                              impact=impact)
    strat_portvals_raw = market.compute_portvals_abridged(strat_orders_df,
                                                          commission=0,
                                                          impact=impact)

    # clean up
    portvals = market.extract_portvals_only(portvals_raw)
    benchmark_portvals = market.extract_portvals_only(benchmark_portvals_raw)
    strat_portvals = market.extract_portvals_only(strat_portvals_raw)

    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = market.compute_portval_stats(
        portvals, rfr=0.0, sf=252, sv=100000)
    bench_cum_ret, bench_avg_daily_ret, bench_std_daily_ret, bench_sharpe_ratio = market.compute_portval_stats(
        benchmark_portvals, rfr=0.0, sf=252, sv=100000)
    learn_cum_ret, learn_avg_daily_ret, learn_std_daily_ret, learn_sharpe_ratio = market.compute_portval_stats(
        strat_portvals, rfr=0.0, sf=252, sv=100000)

    # # Get SPY data
    # symbols = ['SPY']
    # allocations = [1]
    # start_val = 1000000
    # risk_free_rate = 0.0
    # sample_freq = 252
    #
    # # Assess the portfolio of SPY
    # cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY, ev = market.assess_portfolio(sd=sd,
    #                                                                                            ed=ed,
    #                                                                                            syms=symbols,
    #                                                                                            allocs=allocations,
    #                                                                                            sv=100000,
    #                                                                                            gen_plot=False)

    # Compare portfolio against $SPX

    print "Impact is {}".format(impact)

    print "Date Range: {} to {}".format(sd, ed)
    print
    print "Sharpe Ratio of Manual Strategy: {}".format(sharpe_ratio)
    print "Sharpe Ratio of Benchmark: {}".format(bench_sharpe_ratio)
    print "Sharpe Ratio of Strategy Learner: {}".format(learn_sharpe_ratio)
    print
    print "Cumulative Return of Manual Strategy: {}".format(cum_ret)
    print "Cumulative Return of Benchmark: {}".format(bench_cum_ret)
    print "Cumulative Return of Strategy Learner : {}".format(learn_cum_ret)
    print
    print "Standard Deviation of Manual Strategy: {}".format(std_daily_ret)
    print "Standard Deviation of Benchmark: {}".format(bench_std_daily_ret)
    print "Standard Deviation of Strategy Learner : {}".format(
        learn_std_daily_ret)
    print
    print "Average Daily Return of Manual Strategy: {}".format(avg_daily_ret)
    print "Average Daily Return of Benchmark: {}".format(bench_avg_daily_ret)
    print "Average Daily Return of Strategy Learner : {}".format(
        learn_avg_daily_ret)
    print
    print "Final Manual Strategy Value: {}".format(portvals[-1])
    print "Final Benchmark Value: {}".format(benchmark_portvals[-1])
    print "Final Strategy Learner Value: {}".format(strat_portvals[-1])

    benchmark_normalized = market.normalize_data(benchmark_portvals_raw)
    benchmark_normalized = market.extract_portvals_only(benchmark_normalized)
    best_portfolio_normalized = market.normalize_data(portvals_raw)
    best_portfolio_normalized = market.extract_portvals_only(
        best_portfolio_normalized)
    strat_portfolio_normalized = market.normalize_data(strat_portvals_raw)
    strat_portfolio_normalized = market.extract_portvals_only(
        strat_portfolio_normalized)

    stock_library = market.make_df_to_match_trading_days(
        colnames=['Date', 'Value'], symbol='JPM', sd=sd, ed=ed)

    benchmark_line = plt.plot(stock_library.index,
                              benchmark_normalized.values,
                              label="Benchmark")
    plt.setp(benchmark_line, linestyle='-', color='b', linewidth=1.0)
    best_portfolio_line = plt.plot(stock_library.index,
                                   best_portfolio_normalized.values,
                                   label="Manual Strategy")
    plt.setp(best_portfolio_line, linestyle='-', color='k', linewidth=3.0)
    strat_portfolio_line = plt.plot(stock_library.index,
                                    strat_portfolio_normalized.values,
                                    label="Learner Strategy")
    plt.setp(strat_portfolio_line, linestyle='-', color='r', linewidth=1.0)
    legend = plt.legend(loc='best', shadow=True)
    plt.title("Normalized chart for Portfolios - impact={})".format(impact))
    plt.ylabel("Normalized Value")
    plt.grid()
    plt.savefig("chart_impact_{}.png".format(impact))

    return cum_ret, learn_cum_ret
Example #29
0
def test_code():

    #in sample
    start_date = dt.datetime(2008,1,1)
    end_date = dt.datetime(2009,12,31)

    #out-of-sample dates
    # start_date = dt.datetime(2010,1,1)
    # end_date = dt.datetime(2011, 12, 31)

    #orders_df = bps.testPolicy(sd=start_date, ed=end_date)
    plt.figure() #I'm going to try to cheat

    orders_df = ms.testPolicy(sd=start_date, ed=end_date)
    benchmark_policy = ind.benchmark_policy(sd=start_date, ed=end_date)

    benchmark_policy.to_csv('benchmark.csv')
    orders_df.to_csv('orders.csv')

    #portvals_raw = compute_portvals_abridged(orders_df, commission=0, impact=0)
    portvals_raw = compute_portvals_abridged(orders_df, commission=9.95, impact=0.005)
    benchmark_portvals_raw = compute_portvals_abridged(benchmark_policy, commission=0, impact=0)

    #clean up
    portvals = extract_portvals_only(portvals_raw)
    benchmark_portvals = extract_portvals_only(benchmark_portvals_raw)


    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = compute_portval_stats(portvals, rfr=0.0, sf=252, sv=100000)
    bench_cum_ret, bench_avg_daily_ret, bench_std_daily_ret, bench_sharpe_ratio = compute_portval_stats(benchmark_portvals, rfr=0.0, sf=252, sv=100000)


    #Get SPY data
    symbols = ['SPY']
    allocations = [1]
    start_val = 1000000
    risk_free_rate = 0.0
    sample_freq = 252

    # Assess the portfolio of SPY
    cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY, ev = assess_portfolio(sd = start_date, ed = end_date,syms = symbols,
                                             allocs = allocations,sv = 100000, gen_plot = False)

    # Compare portfolio against $SPX
    print "Date Range: {} to {}".format(start_date, end_date)
    print
    print "Sharpe Ratio of Fund: {}".format(sharpe_ratio)
    print "Sharpe Ratio of Benchmark: {}".format(bench_sharpe_ratio)
    print "Sharpe Ratio of SPY : {}".format(sharpe_ratio_SPY)
    print
    print "Cumulative Return of Fund: {}".format(cum_ret)
    print "Cumulative Return of Benchmark: {}".format(bench_cum_ret)
    print "Cumulative Return of SPY : {}".format(cum_ret_SPY)
    print
    print "Standard Deviation of Fund: {}".format(std_daily_ret)
    print "Standard Deviation of Benchmark: {}".format(bench_std_daily_ret)
    print "Standard Deviation of SPY : {}".format(std_daily_ret_SPY)
    print
    print "Average Daily Return of Fund: {}".format(avg_daily_ret)
    print "Average Daily Return of Benchmark: {}".format(bench_avg_daily_ret)
    print "Average Daily Return of SPY : {}".format(avg_daily_ret_SPY)
    print
    print "Final Portfolio Value: {}".format(portvals[-1])
    print "Final Benchmark Value: {}".format(benchmark_portvals[-1])
    print "Final SPY Value: {}".format(ev)

    benchmark_normalized = normalize_data(benchmark_portvals_raw)
    benchmark_normalized = extract_portvals_only(benchmark_normalized)
    best_portfolio_normalized = normalize_data(portvals_raw)
    best_portfolio_normalized = extract_portvals_only(best_portfolio_normalized)

    stock_library = make_df_to_match_trading_days(colnames=['Date', 'Value'], symbol='JPM',
                                  sd=start_date, ed=end_date)


    benchmark_line = plt.plot(stock_library.index, benchmark_normalized.values, label="Benchmark")
    plt.setp(benchmark_line, linestyle='-', color='b', linewidth=1.0)
    best_portfolio_line = plt.plot(stock_library.index, best_portfolio_normalized.values, label="Manual Strategy")
    plt.setp(best_portfolio_line, linestyle='-', color='k', linewidth=1.0)
    legend = plt.legend(loc='best', shadow=True)
    plt.title("Normalized chart for Portfolios")
    plt.ylabel("Normalized Value")
    plt.grid()
    plt.show()
Example #30
0
def test_code():
    # In-sample
    symbol = "JPM"
    sv = 100000
    commission = 0.0
    sd_in = dt.datetime(2008, 1, 1)
    ed_in = dt.datetime(2009, 12, 31)
    period = pd.date_range(sd_in, ed_in)
    trading_days = get_data(["SPY"], dates=period).index

    ############################
    #
    # Cumulative return
    #
    ############################

    df_cum_ret = pd.DataFrame(
        columns=["Benchmark", "Manual Strategy", "QLearning Strategy"],
        index=np.linspace(0.0, 0.01, num=10))
    for impact, _ in df_cum_ret.iterrows():
        print("Compare cumulative return against impact={}".format(impact))

        # Benchmark
        benchmark_trade = pd.DataFrame([1000] + [0] * (len(trading_days) - 1),
                                       columns=[symbol],
                                       index=trading_days)
        benchmark = compute_portvals(benchmark_trade,
                                     start_val=sv,
                                     commission=commission,
                                     impact=impact)
        benchmark /= benchmark.iloc[0, :]
        df_cum_ret.loc[impact, "Benchmark"] = cummu_return(benchmark)

        # Manual Strategy
        ms = ManualStrategy()
        df_trades_manual = ms.testPolicy(symbol=symbol,
                                         sd=sd_in,
                                         ed=ed_in,
                                         sv=sv,
                                         bbr_top=0.91,
                                         bbr_bottom=-0.91,
                                         SMA_ratio_up=1.01,
                                         SMA_ratio_low=0.99)
        portvals_manual = compute_portvals(df_trades_manual,
                                           start_val=sv,
                                           commission=commission,
                                           impact=impact)
        portvals_manual /= portvals_manual.iloc[0, :]
        df_cum_ret.loc[impact,
                       "Manual Strategy"] = cummu_return(portvals_manual)

        # QLearning Strategy
        learner = sl.StrategyLearner(verbose=False,
                                     impact=impact,
                                     commission=commission)
        learner.addEvidence(symbol=symbol, sd=sd_in, ed=ed_in, sv=sv)
        df_trades_qlearning = learner.testPolicy(symbol=symbol,
                                                 sd=sd_in,
                                                 ed=ed_in,
                                                 sv=sv)
        portvals_qlearning = compute_portvals(df_trades_qlearning,
                                              start_val=sv,
                                              commission=commission,
                                              impact=impact)
        portvals_qlearning /= portvals_qlearning.iloc[0, :]
        df_cum_ret.loc[impact,
                       "QLearning Strategy"] = cummu_return(portvals_qlearning)

    fig, ax = plt.subplots()
    df_cum_ret[["Benchmark"]].plot(ax=ax, color="g", marker="o")
    df_cum_ret[["Manual Strategy"]].plot(ax=ax, color="r", marker="o")
    df_cum_ret[["QLearning Strategy"]].plot(ax=ax, color="b", marker="o")
    plt.title(
        "Cumulative return against impact on {} stock over in-sample period".
        format(symbol))
    plt.xlabel("Impact")
    plt.ylabel("Cumulative return")
    plt.grid()
    plt.tight_layout()
    plt.show()
    # plt.savefig("figures/experiment2_{}_cr_in_sample.png".format(symbol))

    nb_orders = pd.DataFrame(
        columns=["Benchmark", "Manual Strategy", "QLearning Strategy"],
        index=[0.2, 0.35, 0.5, 0.65, 0.8])
    for impact, _ in nb_orders.iterrows():
        print("Compare number of orders against impact={}".format(impact))

        # Benchmark
        benchmark_trade = pd.DataFrame([1000] + [0] * (len(trading_days) - 1),
                                       columns=[symbol],
                                       index=trading_days)
        nb_orders.loc[impact, "Benchmark"] = (np.abs(benchmark_trade[symbol]) >
                                              0).sum()

        # Manual Strategy
        ms = ManualStrategy()
        df_trades_manual = ms.testPolicy(symbol=symbol,
                                         sd=sd_in,
                                         ed=ed_in,
                                         sv=sv,
                                         bbr_top=0.91,
                                         bbr_bottom=-0.91,
                                         SMA_ratio_up=1.01,
                                         SMA_ratio_low=0.99)
        nb_orders.loc[impact, "Manual Strategy"] = (np.abs(
            df_trades_manual[symbol]) > 0).sum()

        # QLearning Strategy
        learner = sl.StrategyLearner(verbose=False,
                                     impact=impact,
                                     commission=commission)
        learner.addEvidence(symbol=symbol, sd=sd_in, ed=ed_in, sv=sv)
        df_trades_qlearning = learner.testPolicy(symbol=symbol,
                                                 sd=sd_in,
                                                 ed=ed_in,
                                                 sv=sv)
        nb_orders.loc[impact, "QLearning Strategy"] = (np.abs(
            df_trades_qlearning[symbol]) > 0).sum()

    fig, ax = plt.subplots()
    nb_orders[["Benchmark"]].plot(ax=ax, color="g", marker="o")
    nb_orders[["Manual Strategy"]].plot(ax=ax, color="r", marker="o")
    nb_orders[["QLearning Strategy"]].plot(ax=ax, color="b", marker="o")
    plt.title(
        "Number of orders against impact on {} stock over in-sample period".
        format(symbol))
    plt.xlabel("Impact")
    plt.ylabel("Number of orders")
    ax = plt.gca()
    ax.xaxis.grid(True, which='Major', linestyle='--')
    ax.yaxis.grid(True, which='Major', linestyle='--')
    plt.grid()
    plt.tight_layout()
    plt.show()