Пример #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
Пример #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
Пример #3
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.))
Пример #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)