Beispiel #1
0
def test_strategy_tree_proper_universes():
    def do_nothing(x):
        return True

    child1 = Strategy('c1', [do_nothing], ['b', 'c'])
    master = Strategy('m', [do_nothing], [child1, 'a'])

    child1 = master['c1']

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(
        {'a': pd.TimeSeries(data=1, index=dts, name='a'),
         'b': pd.TimeSeries(data=2, index=dts, name='b'),
         'c': pd.TimeSeries(data=3, index=dts, name='c')})

    master.setup(data)

    assert len(master.children) == 2
    assert 'c1' in master.children
    assert 'a' in master.children
    assert len(master.universe.columns) == 2
    assert 'c1' in master.universe.columns
    assert 'a' in master.universe.columns

    assert len(child1.universe.columns) == 2
    assert 'b' in child1.universe.columns
    assert 'c' in child1.universe.columns
Beispiel #2
0
def test_strategy_tree_proper_universes():
    def do_nothing(x):
        return True

    child1 = Strategy('c1', [do_nothing], ['b', 'c'])
    master = Strategy('m', [do_nothing], [child1, 'a'])

    child1 = master['c1']

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame({
        'a': pd.Series(data=1, index=dts, name='a'),
        'b': pd.Series(data=2, index=dts, name='b'),
        'c': pd.Series(data=3, index=dts, name='c')
    })

    master.setup(data)

    assert len(master.children) == 2
    assert 'c1' in master.children
    assert 'a' in master.children
    assert len(master._universe.columns) == 2
    assert 'c1' in master._universe.columns
    assert 'a' in master._universe.columns

    assert len(child1._universe.columns) == 2
    assert 'b' in child1._universe.columns
    assert 'c' in child1._universe.columns
Beispiel #3
0
def composite(params):
    '''
    construct strategy composite for portfolio
    using bt.core.Strategy
    Args: 
        params (dict): containing keys `data`, `strategy` 
            data_table (dict): follow the rules of data.py
            strategy (list): list of dictionary, containing all params that each strategy needs

    --------------------------------------------------
    Strategy(class):
        Args:
        * name (str): Strategy name
        * algos (list): List of Algos to be passed into an AlgoStack
        * children (dict, list): Children - useful when you want to create
            strategies of strategies

        Attributes:
        * stack (AlgoStack): The stack
        * temp (dict): A dict containing temporary data - cleared on each call
            to run. This can be used to pass info to other algos.
        * perm (dict): Permanent data used to pass info from one algo to
            another. Not cleared on each pass.
    '''
    _check_params(params, ['data_table', 'strategy'])

    name = uuid.uuid4()
    data_table = getDataTable(params['data_table'])

    StrategyList = []
    for stra in params['strategy']:
        pkg = importlib.import_module('backtest_tools.backtest.strategy')
        func = getattr(pkg, stra['class'])
        func = run_always(func)

        inputs = {}
        inputs['data_table'] = data_table
        inputs['params'] = {}
        if stra.get('params'):
            inputs['params'] = stra['params']

        StrategyList.append(func(**inputs))

    s = Strategy(**{'name': name, 'algos': StrategyList, 'children': None})
    return data_table, s
Beispiel #4
0
def test_strategy_tree_paper():
    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['a'], data=100.)
    data['a'].ix[dts[1]] = 101
    data['a'].ix[dts[2]] = 102

    s = Strategy('s',
                 [bt.algos.SelectWhere(data > 100),
                  bt.algos.WeighEqually(),
                  bt.algos.Rebalance()])

    m = Strategy('m', [], [s])
    s = m['s']

    m.setup(data)
    m.update(dts[0])
    m.run()

    assert m.price == 100
    assert s.price == 100
    assert s._paper_trade
    assert s._paper.price == 100

    s.update(dts[1])
    m.run()

    assert m.price == 100
    assert m.value == 0
    assert s.value == 0
    assert s.price == 99.9901

    s.update(dts[2])
    m.run()

    assert m.price == 100
    assert m.value == 0
    assert s.value == 0
    aae(s.price, 100.9801, 4)
Beispiel #5
0
def test_strategy_tree_paper():
    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['a'], data=100.)
    data['a'].ix[dts[1]] = 101
    data['a'].ix[dts[2]] = 102

    s = Strategy('s', [
        bt.algos.SelectWhere(data > 100),
        bt.algos.WeighEqually(),
        bt.algos.Rebalance()
    ])

    m = Strategy('m', [], [s])
    s = m['s']

    m.setup(data)
    m.update(dts[0])
    m.run()

    assert m.price == 100
    assert s.price == 100
    assert s._paper_trade
    assert s._paper.price == 100

    s.update(dts[1])
    m.run()

    assert m.price == 100
    assert m.value == 0
    assert s.value == 0
    assert s.price == 100

    s.update(dts[2])
    m.run()

    assert m.price == 100
    assert m.value == 0
    assert s.value == 0
    assert np.allclose(s.price, 100. * (102 / 101.))