示例#1
0
文件: test_core.py 项目: chenqx/bt
def test_strategybase_flatten():
    s = StrategyBase('s')

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)

    s.setup(data)

    i = 0
    s.update(dts[i])

    s.adjust(1000)
    s.allocate(100, 'c1')
    c1 = s['c1']
    s.allocate(100, 'c2')
    c2 = s['c2']

    assert c1.position == 1
    assert c1.value == 100
    assert c2.position == 1
    assert c2.value == 100
    assert s.value == 998

    s.flatten()

    assert c1.position == 0
    assert c1.value == 0
    assert s.value == 996
示例#2
0
def test_strategybase_tree_allocate_child_from_strategy():
    c1 = SecurityBase('c1')
    c2 = SecurityBase('c2')
    s = StrategyBase('p', [c1, c2])
    c1 = s['c1']
    c2 = s['c2']

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)
    data['c1'][dts[1]] = 105
    data['c2'][dts[1]] = 95

    s.setup(data)

    i = 0
    s.update(dts[i], data.ix[dts[i]])

    s.adjust(1000)
    # since children have w == 0 this should stay in s
    s.allocate(1000)

    assert s.value == 1000
    assert s.capital == 1000
    assert c1.value == 0
    assert c2.value == 0

    # now allocate to c1
    s.allocate(500, 'c1')

    assert c1.position == 5
    assert c1.value == 500
    assert s.capital == 1000 - 500
    assert s.value == 1000
    assert c1.weight == 500.0 / 1000
    assert c2.weight == 0
示例#3
0
文件: test_core.py 项目: chenqx/bt
def test_strategybase_tree_allocate_child_from_strategy():
    c1 = SecurityBase('c1')
    c2 = SecurityBase('c2')
    s = StrategyBase('p', [c1, c2])
    c1 = s['c1']
    c2 = s['c2']

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)
    data['c1'][dts[1]] = 105
    data['c2'][dts[1]] = 95

    s.setup(data)

    i = 0
    s.update(dts[i], data.ix[dts[i]])

    s.adjust(1000)
    # since children have w == 0 this should stay in s
    s.allocate(1000)

    assert s.value == 1000
    assert s.capital == 1000
    assert c1.value == 0
    assert c2.value == 0

    # now allocate to c1
    s.allocate(500, 'c1')

    assert c1.position == 5
    assert c1.value == 500
    assert s.capital == 1000 - 501
    assert s.value == 999
    assert c1.weight == 500.0 / 999
    assert c2.weight == 0
示例#4
0
def test_strategybase_flatten():
    s = StrategyBase('s')

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)

    s.setup(data)

    i = 0
    s.update(dts[i])

    s.adjust(1000)
    s.allocate(100, 'c1')
    c1 = s['c1']
    s.allocate(100, 'c2')
    c2 = s['c2']

    assert c1.position == 1
    assert c1.value == 100
    assert c2.position == 1
    assert c2.value == 100
    assert s.value == 1000

    s.flatten()

    assert c1.position == 0
    assert c1.value == 0
    assert s.value == 1000
示例#5
0
def test_strategybase_tree_allocate_level2():
    c1 = SecurityBase('c1')
    c12 = copy.deepcopy(c1)
    c2 = SecurityBase('c2')
    c22 = copy.deepcopy(c2)
    s1 = StrategyBase('s1', [c1, c2])
    s2 = StrategyBase('s2', [c12, c22])
    m = StrategyBase('m', [s1, s2])

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)
    data['c1'][dts[1]] = 105
    data['c2'][dts[1]] = 95

    m.setup(data)

    i = 0
    m.update(dts[i], data.ix[dts[i]])

    m.adjust(1000)
    # since children have w == 0 this should stay in s
    m.allocate(1000)

    assert m.value == 1000
    assert m.capital == 1000
    assert s1.value == 0
    assert s2.value == 0
    assert c1.value == 0
    assert c2.value == 0

    # now allocate directly to child
    s1.allocate(500)

    assert s1.value == 500
    assert m.capital == 1000 - 500
    assert m.value == 1000
    assert s1.weight == 500.0 / 1000
    assert s2.weight == 0

    # now allocate directly to child of child
    c1.allocate(200)

    assert s1.value == 499
    assert s1.capital == 500 - 201
    assert c1.value == 200
    assert c1.weight == 200.0 / 499
    assert c1.position == 2

    assert m.capital == 1000 - 500
    assert m.value == 999
    assert s1.weight == 499.0 / 999
    assert s2.weight == 0

    assert c12.value == 0
示例#6
0
def test_strategybase_prices():
    dts = pd.date_range('2010-01-01', periods=21)
    rawd = [
        13.555, 13.75, 14.16, 13.915, 13.655, 13.765, 14.02, 13.465, 13.32,
        14.65, 14.59, 14.175, 13.865, 13.865, 13.89, 13.85, 13.565, 13.47,
        13.225, 13.385, 12.89
    ]
    data = pd.DataFrame(index=dts, data=rawd, columns=['a'])

    s = StrategyBase('s')
    s.set_commissions(lambda q, p: 1)
    s.setup(data)

    # buy 100 shares on day 1 - hold until end
    # just enough to buy 100 shares + 1$ commission
    s.adjust(1356.50)

    s.update(dts[0])
    # allocate all capital to child a
    # a should be dynamically created and should have
    # 100 shares allocated. s.capital should be 0
    s.allocate(s.value, 'a')

    assert s.capital == 0
    assert s.value == 1355.50
    assert len(s.children) == 1
    aae(s.price, 99.92628, 5)

    a = s['a']
    assert a.position == 100
    assert a.value == 1355.50
    assert a.weight == 1
    assert a.price == 13.555
    assert len(a.prices) == 1

    # update through all dates and make sure price is ok
    s.update(dts[1])
    aae(s.price, 101.3638, 4)

    s.update(dts[2])
    aae(s.price, 104.3863, 4)

    s.update(dts[3])
    aae(s.price, 102.5802, 4)

    # finish updates and make sure ok at end
    for i in range(4, 21):
        s.update(dts[i])

    assert len(s.prices) == 21
    aae(s.prices[-1], 95.02396, 5)
    aae(s.prices[-2], 98.67306, 5)
示例#7
0
文件: test_core.py 项目: chenqx/bt
def test_strategybase_prices():
    dts = pd.date_range('2010-01-01', periods=21)
    rawd = [13.555, 13.75, 14.16, 13.915, 13.655,
            13.765, 14.02, 13.465, 13.32, 14.65,
            14.59, 14.175, 13.865, 13.865, 13.89,
            13.85, 13.565, 13.47, 13.225, 13.385,
            12.89]
    data = pd.DataFrame(index=dts, data=rawd, columns=['a'])

    s = StrategyBase('s')
    s.setup(data)

    # buy 100 shares on day 1 - hold until end
    # just enough to buy 100 shares + 1$ commission
    s.adjust(1356.50)

    s.update(dts[0])
    # allocate all capital to child a
    # a should be dynamically created and should have
    # 100 shares allocated. s.capital should be 0
    s.allocate(s.value, 'a')

    assert s.capital == 0
    assert s.value == 1355.50
    assert len(s.children) == 1
    aae(s.price, 99.92628, 5)

    a = s['a']
    assert a.position == 100
    assert a.value == 1355.50
    assert a.weight == 1
    assert a.price == 13.555
    assert len(a.prices) == 1

    # update through all dates and make sure price is ok
    s.update(dts[1])
    aae(s.price, 101.3638, 4)

    s.update(dts[2])
    aae(s.price, 104.3863, 4)

    s.update(dts[3])
    aae(s.price, 102.5802, 4)

    # finish updates and make sure ok at end
    for i in range(4, 21):
        s.update(dts[i])

    assert len(s.prices) == 21
    aae(s.prices[-1], 95.02396, 5)
    aae(s.prices[-2], 98.67306, 5)
示例#8
0
文件: test_core.py 项目: chenqx/bt
def test_set_commissions():
    s = StrategyBase('s')

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)

    s.setup(data)
    s.update(dts[0])
    s.adjust(1000)

    s.allocate(500, 'c1')
    assert s.capital == 499

    s.set_commissions(lambda x, y: 0.0)
    s.allocate(-500, 'c1')
    assert s.capital == 999
示例#9
0
def test_set_commissions():
    s = StrategyBase('s')

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)

    s.setup(data)
    s.update(dts[0])
    s.adjust(1000)

    s.allocate(500, 'c1')
    assert s.capital == 499

    s.set_commissions(lambda x, y: 0.0)
    s.allocate(-500, 'c1')
    assert s.capital == 999
示例#10
0
def test_child_weight_above_1():
    # check for child weights not exceeding 1
    s = StrategyBase('s')

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(np.random.randn(3, 2) + 100,
                        index=dts, columns=['c1', 'c2'])
    s.setup(data)

    i = 0
    s.update(dts[i])

    s.adjust(1e6)
    s.allocate(1e6, 'c1')
    c1 = s['c1']

    assert c1.weight <= 1
示例#11
0
文件: test_core.py 项目: chenqx/bt
def test_strategy_tree_proper_return_calcs():
    s1 = StrategyBase('s1')
    s2 = StrategyBase('s2')
    m = StrategyBase('m', [s1, s2])

    s1 = m['s1']
    s2 = m['s2']

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)
    data.loc['c1', dts[1]] = 105
    data.loc['c2', dts[1]] = 95

    m.setup(data)

    i = 0
    m.update(dts[i], data.ix[dts[i]])

    m.adjust(1000)
    # since children have w == 0 this should stay in s
    m.allocate(1000)

    assert m.value == 1000
    assert m.capital == 1000
    assert m.price == 100
    assert s1.value == 0
    assert s2.value == 0

    # now allocate directly to child
    s1.allocate(500)

    assert m.capital == 500
    assert m.value == 1000
    assert m.price == 100
    assert s1.value == 500
    assert s1.weight == 500.0 / 1000
    assert s1.price == 100
    assert s2.weight == 0

    # allocate to child2 via master method
    m.allocate(500, 's2')

    assert m.capital == 0
    assert m.value == 1000
    assert m.price == 100
    assert s1.value == 500
    assert s1.weight == 500.0 / 1000
    assert s1.price == 100
    assert s2.value == 500
    assert s2.weight == 500.0 / 1000
    assert s2.price == 100

    # now allocate and incur commission fee
    s1.allocate(500, 'c1')

    assert m.capital == 0
    assert m.value == 999
    assert m.price == 99.9
    assert s1.value == 499
    assert s1.weight == 499.0 / 999.0
    assert s1.price == 99.8
    assert s2.value == 500
    assert s2.weight == 500.0 / 999.0
    assert s2.price == 100
示例#12
0
def test_strategy_tree_proper_return_calcs():
    s1 = StrategyBase('s1')
    s2 = StrategyBase('s2')
    m = StrategyBase('m', [s1, s2])

    s1 = m['s1']
    s2 = m['s2']

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)
    data.loc['c1', dts[1]] = 105
    data.loc['c2', dts[1]] = 95

    m.setup(data)

    i = 0
    m.update(dts[i], data.ix[dts[i]])

    m.adjust(1000)
    # since children have w == 0 this should stay in s
    m.allocate(1000)

    assert m.value == 1000
    assert m.capital == 1000
    assert m.price == 100
    assert s1.value == 0
    assert s2.value == 0

    # now allocate directly to child
    s1.allocate(500)

    assert m.capital == 500
    assert m.value == 1000
    assert m.price == 100
    assert s1.value == 500
    assert s1.weight == 500.0 / 1000
    assert s1.price == 100
    assert s2.weight == 0

    # allocate to child2 via master method
    m.allocate(500, 's2')

    assert m.capital == 0
    assert m.value == 1000
    assert m.price == 100
    assert s1.value == 500
    assert s1.weight == 500.0 / 1000
    assert s1.price == 100
    assert s2.value == 500
    assert s2.weight == 500.0 / 1000
    assert s2.price == 100

    # now allocate and incur commission fee
    s1.allocate(500, 'c1')

    assert m.capital == 0
    assert m.value == 1000
    assert m.price == 100
    assert s1.value == 500
    assert s1.weight == 500.0 / 1000
    assert s1.price == 100
    assert s2.value == 500
    assert s2.weight == 500.0 / 1000.0
    assert s2.price == 100