Пример #1
0
def simulate_orders(symbols,orders_files = ["./orders_rule.csv"], start_val = 100000,
                    labels=["Manual rule based","benchmark"],strategy=["Manual rule based"],
                    start_date="2008-01-01",end_date="2009-12-31",benchmark=True,
                    benchmark_date="2008-01-02",bm_filename="benchmark_orders",two_strategy=False,
                    legend_loc="lower right",title="title",
                    entry_line=0,savefig="Fig.png"):
    
    origin_portvals=compute_portvals(orders_files[0] , start_val,start_date=start_date,end_date=end_date)
    #normalize portvals    
    portvals=origin_portvals/origin_portvals.ix[0]    
    start_date = dt.date.strftime(portvals.index[0],"%Y-%m-%d")
    end_date = dt.date.strftime(portvals.index[-1],"%Y-%m-%d")
    
    
    print "Date Range: {} to {}".format(start_date, end_date)

    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = compute_portfolio_stats(portvals)
    print "Cumulative Return of {}: {}".format(strategy[0],cum_ret.values[0])
    print "Standard Deviation of daily return of {}: {}".format(strategy[0],std_daily_ret.values[0])
    print "Average Daily Return of {}: {}".format(strategy[0],avg_daily_ret.values[0])
    
    ax1=portvals.plot(title=title)
    
    ax1.set_ylabel("Normalized Price")
    ax1.set_xlabel("Date")
    if benchmark:
        origin_benchmark_portvals=benchmark_portforlio(symbol=symbols[0],date=benchmark_date,start_date=start_date,end_date=end_date,filename=bm_filename)
        benchmark_portvals=origin_benchmark_portvals/origin_benchmark_portvals.ix[0]
        cum_ret_BM, avg_daily_ret_BM, std_daily_ret_BM, sharpe_ratio_BM = compute_portfolio_stats(benchmark_portvals)
    
        print "Cumulative Return of Benchmark : {}".format(cum_ret_BM.values[0])
        print "Standard Deviation of daily return of Benchmark : {}".format(std_daily_ret_BM.values[0])
        print "Average Daily Return of Benchmark : {}".format(avg_daily_ret_BM.values[0])
        benchmark_portvals.plot(ax=ax1,color="black")
    if two_strategy:
        origin_portvals2=compute_portvals(orders_files[1] , start_val,start_date=start_date,end_date=end_date)
        #normalize portvals    
        portvals2=origin_portvals2/origin_portvals2.ix[0]
        cum_ret2, avg_daily_ret2, std_daily_ret2, sharpe_ratio2 = compute_portfolio_stats(portvals2)
        print "Cumulative Return of {}: {}".format(strategy[1],cum_ret2.values[0])
        print "Standard Deviation of daily return of  {}: {}".format(strategy[1],std_daily_ret2.values[0])
        print "Average Daily Return of {}: {}".format(strategy[1],avg_daily_ret2.values[0])

        portvals2.plot(ax=ax1,color="g")
    if entry_line>=0:
        ymin, ymax = ax1.get_ylim()
        orders=pd.read_csv(orders_files[entry_line])
        for i in range(0,len(orders),2):
            if orders.ix[i,"Order"]=="SELL":
                ax1.axvline(pd.to_datetime(orders.ix[i,"Date"]),ymin=0,ymax=ymax,color="r")
            else:
                ax1.axvline(pd.to_datetime(orders.ix[i,"Date"]),ymin=0,ymax=ymax,color="g")             
        
    plt.legend(loc=legend_loc,prop={'size':8},labels=labels)
    plt.savefig(savefig)
    plt.close()
Пример #2
0
    def test_orders_short(self):

        tests = [{'filename': './orders/orders-short.csv', 'start_val':1000000, 'final_val':998035.0, 'cr':-0.001965, 'adr':-0.000178539446839, 'sdr':0.00634128215394, 'sr':-0.446948390642},
            {'filename': './orders/orders.csv', 'start_val':1000000, 'final_val':1133860.0, 'cr':0.13386, 'adr':0.000551651296638, 'sdr':0.00720514136323, 'sr':1.21540888742},
            {'filename': './orders/orders2.csv', 'start_val':1000000, 'final_val':1078752.6, 'cr':0.0787526, 'adr':0.000353426354584, 'sdr':0.00711102080156, 'sr':0.788982285751},
            {'filename': './orders/orders3.csv', 'start_val':1000000, 'final_val':1050160.0, 'cr':0.05016, 'adr':0.000365289198877, 'sdr':0.00560508094997, 'sr':1.03455887842},
            {'filename': './orders/orders-leverage-1.csv', 'start_val':1000000, 'final_val':1050160.0, 'cr':0.05016, 'adr':0.000487052265169, 'sdr':0.00647534272091, 'sr':1.19402406143},
            {'filename': './orders/orders-leverage-2.csv', 'start_val':1000000, 'final_val':1074650.0, 'cr':0.07465, 'adr':0.00202241842159, 'sdr':0.00651837064888, 'sr':4.92529481246},]

        for i, test in enumerate(tests):
            portvals = m.compute_portvals(orders_file = test['filename'], start_val = test['start_val'])
            if isinstance(portvals, pd.DataFrame):
                portvals = portvals[portvals.columns[0]] # just get the first column
            else:
                "warning, code did not return a DataFrame"

            print "portfolio_values", type(portvals)
            print portvals.shape
            cr, adr, sdr, sr = m.compute_portfolio_stats(portvals)
            np.testing.assert_almost_equal(portvals.ix[-1], test['final_val'], decimal=1)
            np.testing.assert_almost_equal(cr, test['cr'], decimal = 4)
            np.testing.assert_almost_equal(adr, test['adr'], decimal = 4)
            np.testing.assert_almost_equal(sdr, test['sdr'], decimal = 4)
            np.testing.assert_almost_equal(sr, test['sr'], decimal = 4)
            print "test", i, "passes"
Пример #3
0
    def check_no_leverage(self, name, filename, start_val, num_days,
                          last_day_portval, sharpe_ratio, avg_daily_ret):
        print name
        portvals = compute_portvals(orders_file=filename, start_val=start_val)
        portvals = portvals[portvals.columns[0]]
        days = len(portvals)
        cr, adr, sddr, sr = compute_portfolio_stats(portvals)

        np.testing.assert_allclose(
            [num_days, last_day_portval, sharpe_ratio, avg_daily_ret],
            [days, portvals[-1], sr, adr],
            rtol=0.001)
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()
Пример #5
0
def test_code():
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)
    sv = 100000
    opt_rd = optimize_in_sample_port(n_trials=20, period=1)
    rd = opt_rd

    df_trades = testPolicy(symbol="JPM", sd=sd, ed=ed, sv=sv, rolling_days=rd)
    portvals = ms.compute_portvals(df_trades, start_val=sv)

    syms = ['JPM']
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)
    prices_portval_normalized = normalize_stocks(portvals)
    prices = prices_all[syms]
    prices_JPM_normalized = normalize_stocks(prices)

    chart_df = pd.concat([prices_portval_normalized, prices_JPM_normalized],
                         axis=1)
    chart_df.columns = ['Portfolio', 'Benchmark']
    pl = chart_df.plot(title='BPS and Benchmark Comparison for In Sample',
                       use_index=True,
                       color=['Black', 'Blue'],
                       lw=2)

    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = ms.compute_portfolio_stats(
        portvals)
    cum_ret_Bench, avg_daily_ret_Bench, std_daily_ret_Bench, sharpe_ratio_Bench = ms.compute_portfolio_stats(
        prices)

    print('In Sample stats:')
    print "Optimal Rolling_Days Value: {}".format(rd)
    print "Cumulative Return of Portfolio: {}".format(cum_ret)
    print "Cumulative Return of Benchmark : {}".format(cum_ret_Bench)
    print "Final Portfolio Value: {}".format(portvals[-1])

    fig = pl.get_figure()
    fig.savefig("plots/BPS_InSample.png")
Пример #6
0
def performance_comparative_analysis():
    #summary lists of cr
    crs_ML_insample=[]
    crs_Rule_insample=[]
    crs_benchmark_insample=[]
    crs_ML_outsample=[]
    crs_Rule_outsample=[]
    crs_benchmark_outsample=[]
    #summary lists of sharpe ratio
    srs_ML_insample=[]
    srs_Rule_insample=[]
    srs_benchmark_insample=[]
    srs_ML_outsample=[]
    srs_Rule_outsample=[]
    srs_benchmark_outsample=[]
    
    #average performance from 20 times runs
    for i in range(20):
        print "start iteration",i+1
        ML_based(expected_up=0.7,expected_down=0.7,symbols=["AAPL"],plot=False,bags=200,leaf_size=5)
        portvals = compute_portvals(orders_file = "./orders_ml.csv", start_val = 100000)
        cr, adr, sddr, sr=compute_portfolio_stats(portvals)
        crs_ML_insample.append(cr.values[0])
        srs_ML_insample.append(sr.values[0])
        
        
        
        portvals_test = compute_portvals(orders_file = "./orders_ml_test.csv", start_val = 100000,start_date="2010-01-01",end_date="2011-12-31")
        cr_test, adr_test, sddr_test, sr_test=compute_portfolio_stats(portvals_test)
        crs_ML_outsample.append(cr_test.values[0])
        srs_ML_outsample.append(sr_test.values[0])        

        
        rule_based(symbols=["AAPL"],plot=False)
        rule_portvals=compute_portvals(orders_file="./orders_rule.csv",start_val=100000)
        cr_rule, adr_rule, sddr_rule, sr_rule=compute_portfolio_stats(rule_portvals)
        crs_Rule_insample.append(cr_rule.values[0])
        srs_Rule_insample.append(sr_rule.values[0])        

        rule_test_portvals=compute_portvals(orders_file="./orders_rule_test.csv",start_val=100000,start_date="2010-01-01",end_date="2011-12-31")
        cr_rule_test, adr_rule_test, sddr_rule_test, sr_rule_test=compute_portfolio_stats(rule_test_portvals)
        crs_Rule_outsample.append(cr_rule_test.values[0])
        srs_Rule_outsample.append(sr_rule_test.values[0])         

        
        bm_portforlio=benchmark_portforlio(symbol="AAPL",date="2008-01-02",shares=200,start_val = 100000,
                         start_date="2008-01-01",end_date="2009-12-31",filename="benchmark_orders")
        cr_bm, adr_bm, sddr_bm, sr_bm=compute_portfolio_stats(bm_portforlio)
        crs_benchmark_insample.append(cr_bm.values[0])
        srs_benchmark_insample.append(sr_bm.values[0])   

        
        bm_test_portforlio=benchmark_portforlio(symbol="AAPL",date="2010-01-04",shares=200,start_val = 100000,
                         start_date="2010-01-01",end_date="2011-12-31",filename="benchmark_orders_test")
        cr_bm_test, adr_bm_test, sddr_bm_test, sr_bm_test=compute_portfolio_stats(bm_test_portforlio)
        crs_benchmark_outsample.append(cr_bm_test.values[0])
        srs_benchmark_outsample.append(sr_bm_test.values[0])           
    
        
    type=["Benchmark in sample","Rule_based in sample","ML in sample","Benchmark out of sample","Rule_based out of sample","ML out of sample"]
    mean_cr=[np.mean(crs_benchmark_insample),np.mean(crs_Rule_insample),np.mean(crs_ML_insample),np.mean(crs_benchmark_outsample),np.mean(crs_Rule_outsample),np.mean(crs_ML_outsample)]
    std_cr=[np.std(crs_benchmark_insample),np.std(crs_Rule_insample),np.std(crs_ML_insample),np.std(crs_benchmark_outsample),np.std(crs_Rule_outsample),np.std(crs_ML_outsample)]
    tms_cr=np.column_stack((type,mean_cr,std_cr))    
    pfdf_cr=pd.DataFrame(tms_cr,columns=["type","mean of Cumulative Return","std of Cumulative Return"])
    print 
    print "Performance Table by Cumulative Return"
    print
    print pfdf_cr
    pfdf_cr.to_csv("inandoutsample_performance_cr.csv")
    
    mean_sr=[np.mean(srs_benchmark_insample),np.mean(srs_Rule_insample),np.mean(srs_ML_insample),np.mean(srs_benchmark_outsample),np.mean(srs_Rule_outsample),np.mean(srs_ML_outsample)]
    std_sr=[np.std(srs_benchmark_insample),np.std(srs_Rule_insample),np.std(srs_ML_insample),np.std(srs_benchmark_outsample),np.std(srs_Rule_outsample),np.std(srs_ML_outsample)]
    tms_sr=np.column_stack((type,mean_sr,std_sr))    
    pfdf_sr=pd.DataFrame(tms_sr,columns=["type","mean of Sharpe ratio","std of Sharpe ratio"])
    print 
    print "Performance Table by Sharpe Ratio"
    print
    print pfdf_sr
    pfdf_sr.to_csv("inandoutsample_performance_sr.csv")
Пример #7
0
def test_code():
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)
    sv = 100000
    rd = 10
    k = 2.0

    df_trades = testPolicy(symbol="JPM",
                           sd=sd,
                           ed=ed,
                           sv=sv,
                           rolling_days=rd,
                           k=k)
    portvals = ms.compute_portvals(df_trades, start_val=sv)

    syms = ['JPM']
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)
    prices_portval_normalized = normalize_stocks(portvals)
    prices = prices_all[syms]
    prices_JPM_normalized = normalize_stocks(prices)

    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = ms.compute_portfolio_stats(
        portvals)
    cum_ret_Bench, avg_daily_ret_Bench, std_daily_ret_Bench, sharpe_ratio_Bench = ms.compute_portfolio_stats(
        prices)

    print('In Sample stats:')
    print "Average Daily Return for Portfolio: {}".format(avg_daily_ret)
    print "Average Daily Return for Benchmark: {}".format(avg_daily_ret_Bench)
    print "Cumulative Return of Portfolio : {}".format(cum_ret)
    print "Cumulative Return of Benchmark : {}".format(cum_ret_Bench)
    print "Std for Daily Return of Portfolio : {}".format(std_daily_ret)
    print "Std for Daily Return of Benchmark : {}".format(std_daily_ret_Bench)

    ###########################################################################################
    #plot
    chart_df = pd.concat([prices_portval_normalized, prices_JPM_normalized],
                         axis=1)
    chart_df.columns = ['Portfolio', 'Benchmark']
    pl1=chart_df.plot(title='Manual Strategy and Benchmark Comparison for In Sample',\
                      use_index=True,\
                      color=['Black', 'Blue'],\
                      figsize=(10,5))

    for index, row in df_trades.iterrows():
        if df_trades.loc[index]['Order'] == 'BUY':
            plt.axvline(x=index, color='g', linestyle='-')
        elif df_trades.loc[index]['Order'] == 'SELL':
            plt.axvline(x=index, color='r', linestyle='-')

    #plt.show()
    fig = pl1.get_figure()
    fig.savefig('plots/MS_InSample.png')
    #############################################################################################

    sd = dt.datetime(2010, 1, 1)
    ed = dt.datetime(2011, 12, 31)
    sv = 100000
    df_trades = testPolicy(symbol="JPM",
                           sd=sd,
                           ed=ed,
                           sv=sv,
                           rolling_days=rd,
                           k=k)
    portvals = ms.compute_portvals(df_trades, start_val=sv)

    syms = ['JPM']
    dates = pd.date_range(sd, ed)
    prices_all = get_data(syms, dates)  # automatically adds SPY
    prices_portval_normalized = normalize_stocks(portvals)
    prices = prices_all[syms]
    prices_JPM_normalized = normalize_stocks(prices)

    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = ms.compute_portfolio_stats(
        portvals)
    cum_ret_Bench, avg_daily_ret_Bench, std_daily_ret_Bench, sharpe_ratio_Bench = ms.compute_portfolio_stats(
        prices)

    print(
        '#################################################################################'
    )
    print('Out of Sample Stats:')
    print "Average Daily Return for Portfolio : {}".format(avg_daily_ret)
    print "Average Daily Return for Benchmark: {}".format(avg_daily_ret_Bench)
    print "Cumulative Return of Portfolio : {}".format(cum_ret)
    print "Cumulative Return of Benchmark : {}".format(cum_ret_Bench)
    print "Std for Daily Return of Portfolio : {}".format(std_daily_ret)
    print "Std for Daily Return of Benchmark : {}".format(std_daily_ret_Bench)

    ###########################################################################################
    #plot
    chart_df = pd.concat([prices_portval_normalized, prices_JPM_normalized],
                         axis=1)
    chart_df.columns = ['Portfolio', 'Benchmark']
    pl2=chart_df.plot(title='Manual Strategy and Benchmark Comparison for Out of Sample',\
                      use_index=True,\
                      color=['Black', 'Blue'],\
                      figsize=(10,5))
    fig = pl2.get_figure()
    fig.savefig('plots/MS_OutSample.png')
outsample_result.append([])
outsample_result[1].append('Rule-based')
outsample_result.append([])
outsample_result[2].append('ML-based')

k = 0
for i in ['Benchmark_reg', 'Rule-based', 'ML-based']:
    portvals = indi_df_aapl_ml_out[i]
    start_date = portvals.index[0].strftime('%Y-%m-%d')
    end_date = portvals.index[-1].strftime('%Y-%m-%d')

    prices_SPX = (util.get_data(['$SPX'], pd.date_range(start_date,
                                                        end_date)))[['$SPX']]
    portvals_SPX = mks.get_portfolio_value(prices_SPX)

    cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = mks.compute_portfolio_stats(
        portvals)
    cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = mks.compute_portfolio_stats(
        portvals_SPX)

    outsample_result[k].append(start_date)
    outsample_result[k].append(end_date)
    outsample_result[k].append(float('%.3g' % sharpe_ratio))
    outsample_result[k].append(float('%.3g' % cum_ret))
    outsample_result[k].append(float('%.3g' % std_daily_ret))
    outsample_result[k].append(float('%.3g' % avg_daily_ret))

    print outsample_result[k]
    k += 1

# print "Date Range: {} to {}".format(start_date, end_date)
# print "Sharpe Ratio of SPY : {}".format(sharpe_ratio_SPY)