예제 #1
0
def run(sd, ed, vert_lines):
    manual_orders = ms.testPolicy('JPM', sd, ed, 1000000)
    bench_orders = ms.benchmark('JPM', sd, ed, 1000000)

    manual_sim = marketismcode.compute_portvals(manual_orders)
    bench_sim = marketismcode.compute_portvals(bench_orders)

    manual_sim['value'] = manual_sim['value'] / manual_sim['value'][0]
    bench_sim['value'] = bench_sim['value'] / bench_sim['value'][0]

    long_positions = manual_sim['value'].where(
        manual_orders['JPM'] > 0).dropna(how="all")
    short_positions = manual_sim['value'].where(
        manual_orders['JPM'] < 0).dropna(how="all")

    daily_returns = (manual_sim['value'][1:] /
                     manual_sim['value'][:-1].values) - 1

    dr_mean = daily_returns.mean()

    dr_std = daily_returns.std()

    cr = (manual_sim['value'][-1] / manual_sim['value'][0]) - 1
    print str(sd) + ' - ' + str(ed)
    print "Manual-----------------------"
    print "\tCR: " + str(cr)
    print "\tMean of daily returns: " + str(dr_mean)
    print "\tSTD of daily returns: " + str(dr_std) + '\n'

    bench_daily_returns = (bench_sim['value'][1:] /
                           bench_sim['value'][:-1].values) - 1

    bench_dr_mean = bench_daily_returns.mean()

    bench_dr_std = bench_daily_returns.std()

    bench_cr = (bench_sim['value'][-1] / bench_sim['value'][0]) - 1
    print "Bench-----------------------"
    print "\tCR: " + str(bench_cr)
    print "\tMean of daily returns: " + str(bench_dr_mean)
    print "\tSTD of daily returns: " + str(bench_dr_std) + '\n'

    long_positions.columns = ['Long Positions']
    short_positions.columns = ['Short Positions']
    manual_sim.columns = ['Manual Strategy']
    bench_sim.columns = ['Benchmark']
    ax = manual_sim.plot(title="Manual Strategy vs Benchmark\n" + str(sd) +
                         ' - ' + str(ed),
                         color="black")
    bench_sim.plot(ax=ax, color="blue")
    if vert_lines:
        for i in short_positions.index:
            ax.axvline(x=i, c='r')
        for i in long_positions.index:
            ax.axvline(x=i, c='g')
    ax.set_ylabel('Portfolio Value')
    ax.set_xlabel('Date')
예제 #2
0
    def find_best_params(self, symbol, start_date, end_date):
        best_weights = [(0, 0, 0, 0, 0, 0)]
        highest_value = None
        i = 0
        for mom_weight in [2, 3, 4, 8]:
            for rsi_weight in [2, 3, 4, 8]:
                for bol_weight in [2, 3, 4, 8]:
                    if mom_weight + rsi_weight + bol_weight == 0:
                        continue
                    print "on iter " + str(i)
                    self.__init__(bol_weight, mom_weight, rsi_weight)
                    orders = self.testPolicy(symbol, start_date, end_date,
                                             100000)
                    value = marketismcode.compute_portvals(orders)
                    val = value.ix[-1][0]
                    if highest_value == val:
                        print "equivalent high value found!"
                        best_weights.append(
                            (bol_weight, rsi_weight, mom_weight))
                        print(bol_weight, rsi_weight, mom_weight)
                    if highest_value is None or val > highest_value:
                        print "highest value found!"
                        best_weights = [(bol_weight, rsi_weight, mom_weight)]
                        highest_value = val
                        print highest_value
                        print best_weights
                        print('------')
                    i += 1

        print('----------')
        print highest_value
        print best_weights
예제 #3
0
import datetime as dt
import matplotlib.pyplot as plt
import BestPossibleStrategy
import marketismcode

start_date = dt.datetime(2010, 1, 1)
end_date = dt.datetime(2011, 12, 31)

bps = BestPossibleStrategy.BestPossibleStrategy()

best_orders = bps.testPolicy('JPM', start_date, end_date, 100000)
best_values = marketismcode.compute_portvals(best_orders,
                                             commission=0,
                                             impact=0)

bench_orders = bps.benchmark('JPM', start_date, end_date, 1000)
bench_values = marketismcode.compute_portvals(bench_orders,
                                              commission=0,
                                              impact=0)

best_daily_returns = (best_values['value'][1:] /
                      best_values['value'][:-1].values) - 1
bench_daily_returns = (bench_values['value'][1:] /
                       bench_values['value'][:-1].values) - 1

best_dr_mean = best_daily_returns.mean()
bench_dr_mean = bench_daily_returns.mean()

best_dr_std = best_daily_returns.std()
bench_dr_std = bench_daily_returns.std()
예제 #4
0
def run(sd, ed, impact_learner, strategy_learner):
    impact_orders = impact_learner.testPolicy(symbol, sd, ed, 1000000)
    strategy_orders = strategy_learner.testPolicy(symbol, sd, ed, 1000000)
    # manual_orders = ms.testPolicy(symbol,sd,ed,1000000)
    # bench_orders = ms.benchmark(symbol, sd, ed, 1000000)
    #
    # manual_sim = marketismcode.compute_portvals(manual_orders,impact=impact, commission=0)
    # bench_sim = marketismcode.compute_portvals(bench_orders,impact=impact, commission=0)
    strategy_sim = marketismcode.compute_portvals(strategy_orders,
                                                  impact=impact,
                                                  commission=0)
    impact_sim = marketismcode.compute_portvals(impact_orders,
                                                impact=impact,
                                                commission=0)

    long_positions = strategy_sim['value'].where(
        strategy_orders['JPM'] > 0).dropna(how="all")
    short_positions = strategy_sim['value'].where(
        strategy_orders['JPM'] < 0).dropna(how="all")
    impact_long_positions = impact_sim['value'].where(
        impact_orders['JPM'] > 0).dropna(how="all")
    impact_short_positions = impact_sim['value'].where(
        impact_orders['JPM'] < 0).dropna(how="all")

    # manual_sim['value']= manual_sim['value']/manual_sim['value'][0]
    # bench_sim['value']= bench_sim['value']/bench_sim['value'][0]
    strategy_sim['value'] = strategy_sim['value'] / strategy_sim['value'][0]
    impact_sim['value'] = impact_sim['value'] / impact_sim['value'][0]

    # daily_returns = (manual_sim['value'][1:] / manual_sim['value'][:-1].values) - 1
    #
    # dr_mean = daily_returns.mean()
    #
    # dr_std = daily_returns.std()
    #
    # cr = (manual_sim['value'][-1] / manual_sim['value'][0]) - 1
    # print str(sd) + ' - ' + str(ed)
    # print "Manual-----------------------"
    # print "\tCR: " + str(cr)
    # print "\tMean of daily returns: " + str(dr_mean)
    # print "\tSTD of daily returns: " + str(dr_std) + '\n'
    #
    # bench_daily_returns = (bench_sim['value'][1:] / bench_sim['value'][:-1].values) - 1
    #
    # bench_dr_mean = bench_daily_returns.mean()
    #
    # bench_dr_std = bench_daily_returns.std()
    #
    # bench_cr = (bench_sim['value'][-1] / bench_sim['value'][0]) - 1
    # print "Bench-----------------------"
    # print "\tCR: " + str(bench_cr)
    # print "\tMean of daily returns: " + str(bench_dr_mean)
    # print "\tSTD of daily returns: " + str(bench_dr_std) + '\n'
    print "Impact: " + str(impact)
    strategy_daily_returns = (strategy_sim['value'][1:] /
                              strategy_sim['value'][:-1].values) - 1
    strategy_dr_mean = strategy_daily_returns.mean()
    strategy_dr_std = strategy_daily_returns.std()
    strategy_cr = (strategy_sim['value'][-1] / strategy_sim['value'][0]) - 1
    print "Strategy-----------------------"
    print "\tCR: " + str(strategy_cr)
    print "\tMean of daily returns: " + str(strategy_dr_mean)
    print "\tSTD of daily returns: " + str(strategy_dr_std) + '\n'
    print "\tNum Trades: " + str(len(long_positions) + len(short_positions))

    impact_daily_returns = (impact_sim['value'][1:] /
                            impact_sim['value'][:-1].values) - 1
    impact_dr_mean = impact_daily_returns.mean()
    impact_dr_std = impact_daily_returns.std()
    impact_cr = (impact_sim['value'][-1] / impact_sim['value'][0]) - 1
    print "Impact Strategy-----------------------"
    print "\tCR: " + str(impact_cr)
    print "\tMean of daily returns: " + str(impact_dr_mean)
    print "\tSTD of daily returns: " + str(impact_dr_std) + '\n'
    print "\tNum Trades: " + str(
        len(impact_long_positions) + len(impact_short_positions))
    # manual_sim.columns = ['Manual Trader']
    # bench_sim.columns = ['Benchmark']
    strategy_sim.columns = ['Strategy Learner Trained Without Impact']
    impact_sim.columns = ['Strategy Learner Trained With Impact']
    ax = impact_sim.plot(title=symbol + " Trader Performances\n" + str(sd) +
                         ' - ' + str(ed) + '\n Impact: ' + str(impact),
                         color="black")
    # bench_sim.plot(ax=ax, color="blue")
    strategy_sim.plot(ax=ax, color="red")
    # manual_sim.plot(ax=ax, color="green")
    ax.set_ylabel('Portfolio Value')
    ax.set_xlabel('Date')
예제 #5
0
def run(sd, ed, strategy_learner):
    strategy_orders = strategy_learner.testPolicy(symbol, sd, ed, 1000000)
    manual_orders = ms.testPolicy(symbol, sd, ed, 1000000)
    bench_orders = ms.benchmark(symbol, sd, ed, 1000000)

    manual_sim = marketismcode.compute_portvals(manual_orders,
                                                commission=0,
                                                impact=impact)
    bench_sim = marketismcode.compute_portvals(bench_orders,
                                               commission=0,
                                               impact=impact)
    strategy_sim = marketismcode.compute_portvals(strategy_orders,
                                                  commission=0,
                                                  impact=impact)

    manual_sim['value'] = manual_sim['value'] / manual_sim['value'][0]
    bench_sim['value'] = bench_sim['value'] / bench_sim['value'][0]
    strategy_sim['value'] = strategy_sim['value'] / strategy_sim['value'][0]

    daily_returns = (manual_sim['value'][1:] /
                     manual_sim['value'][:-1].values) - 1

    dr_mean = daily_returns.mean()

    dr_std = daily_returns.std()

    cr = (manual_sim['value'][-1] / manual_sim['value'][0]) - 1
    print str(sd) + ' - ' + str(ed)
    print "Manual-----------------------"
    print "\tCR: " + str(cr)
    print "\tMean of daily returns: " + str(dr_mean)
    print "\tSTD of daily returns: " + str(dr_std) + '\n'

    bench_daily_returns = (bench_sim['value'][1:] /
                           bench_sim['value'][:-1].values) - 1

    bench_dr_mean = bench_daily_returns.mean()

    bench_dr_std = bench_daily_returns.std()

    bench_cr = (bench_sim['value'][-1] / bench_sim['value'][0]) - 1
    print "Bench-----------------------"
    print "\tCR: " + str(bench_cr)
    print "\tMean of daily returns: " + str(bench_dr_mean)
    print "\tSTD of daily returns: " + str(bench_dr_std) + '\n'

    strategy_daily_returns = (strategy_sim['value'][1:] /
                              strategy_sim['value'][:-1].values) - 1

    strategy_dr_mean = strategy_daily_returns.mean()

    strategy_dr_std = strategy_daily_returns.std()

    strategy_cr = (strategy_sim['value'][-1] / strategy_sim['value'][0]) - 1
    print "Strategy-----------------------"
    print "\tCR: " + str(strategy_cr)
    print "\tMean of daily returns: " + str(strategy_dr_mean)
    print "\tSTD of daily returns: " + str(strategy_dr_std) + '\n'

    manual_sim.columns = ['Manual Trader']
    bench_sim.columns = ['Benchmark']
    strategy_sim.columns = ['Strategy Learner']
    ax = manual_sim.plot(title=symbol + " Trader Performances\n" + str(sd) +
                         ' - ' + str(ed),
                         color="black")
    bench_sim.plot(ax=ax, color="blue")
    strategy_sim.plot(ax=ax, color="red")
    ax.set_ylabel('Portfolio Value')
    ax.set_xlabel('Date')