return df_trades

if __name__ == '__main__':
    symbols = ['JPM']
    start_date = '2008-01-01'
    end_date = '2009-12-31'
#     #for out-sample time using following sd and ed
    # start_date = '2010-01-01'
#     #end_date = '2011-12-31'
    prices = get_data(symbols, pd.date_range(start_date, end_date))
    prices = prices[symbols]
#     # print prices
    df_trades = testPolicy(sd=start_date, ed=end_date)
#     df_joined = df_trades.join(prices, lsuffix='_best', rsuffix='_benchmark')

    portvals = marketsim(symbols[0], df_trades, commission = 9.95, impact = 0.005)

    print_info(portvals)
#     # portvals = marketsim(df_trades, prices, commission = 0, impact = 0)
#     df_joined = df_joined.join(portvals, lsuffix='_best', rsuffix = 'whatever')
#     prices_val = prices.values

#     # to generate Benchmark values
#     d = np.zeros(len(prices.index))
#     d[0] = 1000
#     df_trade_none = pd.DataFrame(data=d, index=prices.index, columns = ['val'])
#     port_benchmark = marketsim(df_trade_none, prices)

#     portvals = portvals / portvals.ix[0]
#     port_benchmark = port_benchmark / port_benchmark.ix[0]
    print 'cumulative return: ' + str(
        float(portvals.values[-1] / portvals.values[0]) - 1)
    print 'Stdev of daily returns: ' + str(float(daily_returns.std()))
    print 'Mean of daily returns: ' + str(float(daily_returns.mean()))


if __name__ == '__main__':
    names = ['JPM']
    start_date = '2008-01-01'
    end_date = '2009-12-31'
    prices = get_data(names, pd.date_range(start_date, end_date))
    prices = prices[names]

    df_trades = testPolicy()
    portvals = marketsim(df_trades, prices)
    print 'Best Possible Policy'
    print_info(portvals)

    d = np.zeros(len(prices.index))
    d[0] = 1000
    df_trade_none = pd.DataFrame(data=d, index=prices.index, columns=['val'])
    port_benchmark = marketsim(df_trade_none, prices)
    print 'Benchmark'
    print_info(port_benchmark)

    portvals = portvals / portvals.ix[0]
    port_benchmark = port_benchmark / port_benchmark.ix[0]
    # df_joined = portvals.join(port_benchmark, lsuffix='_best', rsuffix='_benchmark')
    # plot_data(df_joined)
Example #3
0
    symbol = 'JPM'
    # sd = datetime.datetime(2010, 1, 1)
    # ed = datetime.datetime(2011, 12, 31)
    sd = datetime.datetime(2008, 1, 1)
    ed = datetime.datetime(2009, 12, 31)
    dates = pd.date_range(sd, ed)
    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')
    prices = ut.get_data([symbol], pd.date_range(sd, ed))
    prices = prices[symbol]

    res = []
    num_trades = []
    impacts = [0, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15, 0.175, 0.2, 0.3, 0.4]
    for impact in impacts:
        learner = sl.StrategyLearner(verbose=False,
                                     impact=impact)  # 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
        num_trades.append(np.count_nonzero(df_trades))

        vals_ml = marketsim(df_trades, prices, impact=impact)

        vals_ml = vals_ml / vals_ml.ix[0]
        res.append(vals_ml)

    plt.xlabel('impact')
    plt.ylabel('number of trades')
    plt.xticks(np.arange(11), impacts)
    plt.plot(num_trades)
    plt.show()

    handles = []
    for i in range(len(impacts)):
        mlstrategy, = plt.plot(res[i], label=str(impacts[i]))
        handles.append(mlstrategy)
Example #5
0

if __name__ == '__main__':
    syms = ['JPM']
    start_date = '2008-01-01'
    end_date = '2009-12-31'
    #for out-sample time using following sd and ed
    #start_date = '2010-01-01'
    #end_date = '2011-12-31'
    prices = get_data(syms, pd.date_range(start_date, end_date))
    prices = prices[syms]
    # print prices
    df_trades = testPolicy(sd=start_date, ed=end_date)
    df_joined = df_trades.join(prices, lsuffix='_best', rsuffix='_benchmark')

    portvals = marketsim(df_trades, prices, commission=9.95, impact=0.005)
    # portvals = marketsim(df_trades, prices, commission = 0, impact = 0)
    df_joined = df_joined.join(portvals, lsuffix='_best', rsuffix='whatever')
    prices_val = prices.values

    # to generate Benchmark values
    d = np.zeros(len(prices.index))
    d[0] = 1000
    df_trade_none = pd.DataFrame(data=d, index=prices.index, columns=['val'])
    port_benchmark = marketsim(df_trade_none, prices)

    portvals = portvals / portvals.ix[0]
    port_benchmark = port_benchmark / port_benchmark.ix[0]

    print "My strategy performance"
    print_info(portvals)
Example #6
0
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)
    dates = pd.date_range(sd, ed)
    #import all prices with SPY
    prices_all = ut.get_data([symbol], dates)
    #only prices in symbol
    prices = prices_all[symbol]

    #benchmark portvals table
    trades_benchmark = prices_all[[
        symbol,
    ]].copy(deep=True)
    #benchmark buy and hold
    trades_benchmark.values[:, :] = 0
    trades_benchmark.values[0, :] = 1000
    vals_benchmark = marketsim(trades_benchmark,
                               prices)  #default impact and commission are 0

    #ML portvals table
    rtl = sl.StrategyLearner(verbose=False, impact=0)
    #train learner
    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
Example #7
0
    print 'cumulative return: ' + str(
        float(portvals.values[-1] / portvals.values[0]) - 1)
    print 'Stdev of daily returns: ' + str(float(daily_returns.std()))
    print 'Mean of daily returns: ' + str(float(daily_returns.mean()))


if __name__ == '__main__':
    names = ['JPM']
    start_date = '2008-01-01'
    end_date = '2009-12-31'
    prices = get_data(names, pd.date_range(start_date, end_date))
    prices = prices[names]

    trades_df = testPolicy()
    portvals = marketsim(trades_df, prices)
    print 'Best Possible Policy performance'
    print_info(portvals)

    d = np.zeros(len(prices.index))
    d[0] = 1000
    trade_df_none = pd.DataFrame(data=d, index=prices.index, columns=['val'])
    port_benchmark = marketsim(trade_df_none, prices)
    print 'Benchmark performance'
    print_info(port_benchmark)

    #normalization the portvals
    portvals = portvals / portvals.ix[0]
    port_benchmark = port_benchmark / port_benchmark.ix[0]

    # plot the graph
Example #8
0
    prices = ut.get_data([symbol], pd.date_range(sd, ed))
    prices = prices[symbol]

    res = []
    trades_num = []
    impacts = [0, 0.025, 0.05, 0.1, 0.2, 0.4]
    for impact in impacts:
        learner = sl.StrategyLearner(verbose = False, impact = impact)
        # constructor
        #training the learner
        learner.addEvidence(symbol = symbol, sd=sd, ed=ed, sv = 100000)
        #testing Period
        trades_df = learner.testPolicy(symbol = symbol, sd=sd, ed=ed, sv = 100000)
        trades_num.append(np.count_nonzero(trades_df))

        vals_rtl = marketsim(trades_df, prices, impact = impact)

        vals_rtl = vals_rtl / vals_rtl.ix[0]
        print_info(vals_rtl)
        print trades_num
        res.append(vals_rtl)


    plt.xlabel('impact')
    plt.ylim(ymax = 60)
    plt.ylabel('number of trades')
    plt.xticks(np.arange(6), impacts)
    plt.plot(trades_num)
    plt.title("Impact on number of trades")
    plt.show()
if __name__ == '__main__':
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)
    dates = pd.date_range(sd, ed)
    symbol = 'JPM'
    prices_all = ut.get_data([symbol], dates)
    prices = prices_all[symbol]
    summary1 = []
    trades_number = []
    impacts = [0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30]
    for impact in impacts:
        rtl = sl.StrategyLearner(verbose=False, impact=impact)
        rtl.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=100000)
        trades_rtl = rtl.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=100000)
        trades_number.append(np.count_nonzero(trades_rtl))
        values_rtl = marketsim(symbol, trades_rtl, impact=impact)
        values_rtl = values_rtl / values_rtl.iloc[0]
        information(values_rtl)
        print trades_number
        summary1.append(values_rtl)
    plt.xlabel('Impact')
    plt.ylabel('Number of trades')
    plt.xticks(np.arange(7), impacts)
    plt.plot(trades_number)
    plt.title("Impact VS. Number of trades")
    plt.show()
    summary2 = []
    for i in range(len(impacts)):
        rtlstrategy, = plt.plot(summary1[i], label=str(impacts[i]))
        summary2.append(rtlstrategy)
    plt.gcf().subplots_adjust(left=0.1,
        float(daily_returns.std()))


if __name__ == "__main__":
    sd = dt.datetime(2008, 1, 1)
    ed = dt.datetime(2009, 12, 31)
    dates = pd.date_range(sd, ed)
    symbol = 'JPM'
    prices_all = ut.get_data([symbol], dates)
    prices = prices_all[symbol]
    benchmark_record = prices_all[[
        symbol,
    ]].copy()
    benchmark_record.values[:, :] = 0
    benchmark_record.values[0, :] = 1000
    values_benchmark = marketsim(symbol, benchmark_record)
    rtl = sl.StrategyLearner(verbose=False, impact=0)
    rtl.addEvidence(symbol=symbol, sd=sd, ed=ed, sv=100000)
    trades_rtl = rtl.testPolicy(symbol=symbol, sd=sd, ed=ed, sv=100000)
    values_rtl = marketsim(symbol, trades_rtl)
    trades_manual = manu.testPolicy(symbol=symbol, sd=sd, ed=ed)
    values_manual = marketsim(symbol, trades_manual)
    values_benchmark = values_benchmark / values_benchmark.iloc[0]
    values_manual = values_manual / values_manual.iloc[0]
    values_rtl = values_rtl / values_rtl.iloc[0]
    benchmark_record, = plt.plot(values_benchmark, "b", label="benchmark")
    rtl, = plt.plot(values_rtl, "g", label="RTLearner")
    manual_strategy, = plt.plot(values_manual, "r", label="Manual Strategy")
    plt.legend(handles=[benchmark_record, manual_strategy, rtl], loc=0)
    plt.gcf().subplots_adjust(left=0.1,
                              bottom=0.05,