当前位置: 首页>>代码示例>>Python>>正文


Python StrategyBase.setup方法代码示例

本文整理汇总了Python中bt.core.StrategyBase.setup方法的典型用法代码示例。如果您正苦于以下问题:Python StrategyBase.setup方法的具体用法?Python StrategyBase.setup怎么用?Python StrategyBase.setup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bt.core.StrategyBase的用法示例。


在下文中一共展示了StrategyBase.setup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_strategybase_tree_setup

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_strategybase_tree_setup():
    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)

    assert len(s.data) == 3
    assert len(c1.data) == 3
    assert len(c2.data) == 3

    assert len(s.prices) == 0
    assert len(c1.prices) == 0
    assert len(c2.prices) == 0

    assert len(s.values) == 0
    assert len(c1.values) == 0
    assert len(c2.values) == 0
开发者ID:chenqx,项目名称:bt,代码行数:28,代码来源:test_core.py

示例2: test_strategybase_tree_rebalance

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_strategybase_tree_rebalance():
    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)

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

    # now rebalance c1
    s.rebalance(0.5, '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
开发者ID:chenqx,项目名称:bt,代码行数:36,代码来源:test_core.py

示例3: test_strategybase_tree_allocate_child_from_strategy

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
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
开发者ID:chenqx,项目名称:bt,代码行数:37,代码来源:test_core.py

示例4: test_fail_if_root_value_negative

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_fail_if_root_value_negative():
    s = StrategyBase('s')
    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)
    data['c1'][dts[0]] = 100
    data['c2'][dts[0]] = 95
    s.setup(data)

    s.adjust(-100)
    # trigger update
    s.update(dts[0])

    assert s.bankrupt

    # make sure only triggered if root negative
    c1 = StrategyBase('c1')
    s = StrategyBase('s', children=[c1])
    c1 = s['c1']

    s.setup(data)

    s.adjust(1000)
    c1.adjust(-100)
    s.update(dts[0])

    # now make it trigger
    c1.adjust(-1000)
    # trigger update
    s.update(dts[0])

    assert s.bankrupt
开发者ID:chenqx,项目名称:bt,代码行数:33,代码来源:test_core.py

示例5: test_fail_if_0_base_in_return_calc

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_fail_if_0_base_in_return_calc():
    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)
    data['c1'][dts[0]] = 100
    data['c2'][dts[0]] = 95

    # must setup tree because if not negative root error pops up first
    c1 = StrategyBase('c1')
    s = StrategyBase('s', children=[c1])
    c1 = s['c1']
    s.setup(data)

    s.adjust(1000)
    c1.adjust(100)
    s.update(dts[0])

    c1.adjust(-100)
    s.update(dts[1])

    try:
        c1.adjust(-100)
        s.update(dts[1])
        assert False
    except ZeroDivisionError, e:
        if 'Could not update' not in str(e):
            assert False
开发者ID:chenqx,项目名称:bt,代码行数:28,代码来源:test_core.py

示例6: test_strategybase_tree_update

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_strategybase_tree_update():
    c1 = SecurityBase('c1')
    c2 = SecurityBase('c2')
    s = StrategyBase('p', [c1, 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]])

    c1.price == 100
    c2.price == 100

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

    c1.price == 105
    c2.price == 95

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

    c1.price == 100
    c2.price == 100
开发者ID:chenqx,项目名称:bt,代码行数:31,代码来源:test_core.py

示例7: test_strategybase_flatten

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
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
开发者ID:chenqx,项目名称:bt,代码行数:30,代码来源:test_core.py

示例8: test_strategybase_tree_allocate_level2

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
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
开发者ID:femtotrader,项目名称:bt,代码行数:56,代码来源:test_core.py

示例9: test_strategybase_prices

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
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)
开发者ID:chenqx,项目名称:bt,代码行数:53,代码来源:test_core.py

示例10: test_strategybase_tree_allocate_long_short

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_strategybase_tree_allocate_long_short():
    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)
    c1.allocate(500)

    assert c1.position == 5
    assert c1.value == 500
    assert c1.weight == 500.0 / 999
    assert s.capital == 1000 - 501
    assert s.value == 999

    c1.allocate(-200)

    assert c1.position == 3
    assert c1.value == 300
    assert c1.weight == 300.0 / 998
    assert s.capital == 1000 - 501 + 199
    assert s.value == 998

    c1.allocate(-400)

    assert c1.position == -1
    assert c1.value == -100
    assert c1.weight == -100.0 / 997
    assert s.capital == 1000 - 501 + 199 + 399
    assert s.value == 997

    # close up
    c1.allocate(-c1.value)

    assert c1.position == 0
    assert c1.value == 0
    assert c1.weight == 0
    assert s.capital == 1000 - 501 + 199 + 399 - 101
    assert s.value == 996
开发者ID:chenqx,项目名称:bt,代码行数:53,代码来源:test_core.py

示例11: test_security_setup_prices

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_security_setup_prices():
    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[0]] = 105
    data['c2'][dts[0]] = 95

    s.setup(data)

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

    assert c1.price == 105
    assert len(c1.prices) == 1
    assert c1.prices[0] == 105

    assert c2.price == 95
    assert len(c2.prices) == 1
    assert c2.prices[0] == 95

    # now with setup
    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[0]] = 105
    data['c2'][dts[0]] = 95

    s.setup(data)

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

    assert c1.price == 105
    assert len(c1.prices) == 1
    assert c1.prices[0] == 105

    assert c2.price == 95
    assert len(c2.prices) == 1
    assert c2.prices[0] == 95
开发者ID:chenqx,项目名称:bt,代码行数:52,代码来源:test_core.py

示例12: test_set_commissions

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
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
开发者ID:chenqx,项目名称:bt,代码行数:18,代码来源:test_core.py

示例13: test_fail_if_root_value_negative

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_fail_if_root_value_negative():
    s = StrategyBase('s')
    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100)
    data['c1'][dts[0]] = 100
    data['c2'][dts[0]] = 95
    s.setup(data)

    try:
        s.adjust(-100)
        # trigger update
        s.update(dts[0])
        assert False
    except ValueError, e:
        if not 'negative root node value' in str(e):
            assert False
开发者ID:femtotrader,项目名称:bt,代码行数:18,代码来源:test_core.py

示例14: test_strategybase_tree_allocate_update

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_strategybase_tree_allocate_update():
    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]])
    assert s.price == 100

    s.adjust(1000)

    assert s.price == 100
    assert s.value == 1000
    assert s._value == 1000

    c1.allocate(500)

    assert c1.position == 5
    assert c1.value == 500
    assert c1.weight == 500.0 / 999
    assert s.capital == 1000 - 501
    assert s.value == 999
    assert s.price == 99.9

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

    assert c1.position == 5
    assert c1.value == 525
    assert c1.weight == 525.0 / 1024
    assert s.capital == 1000 - 501
    assert s.value == 1024
    assert s.price == 102.4
开发者ID:chenqx,项目名称:bt,代码行数:45,代码来源:test_core.py

示例15: test_outlays

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import setup [as 别名]
def test_outlays():
    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[0]] = 105
    data['c2'][dts[0]] = 95

    s.setup(data)

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

    # allocate 1000 to strategy
    s.adjust(1000)

    # now let's see what happens when we allocate 500 to each child
    c1.allocate(500)
    c2.allocate(500)

    # out update
    s.update(dts[i])

    assert c1.data['outlay'][dts[0]] == (4 * 105)
    assert c2.data['outlay'][dts[0]] == (5 * 95)

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

    c1.allocate(-400)
    c2.allocate(100)

    # out update
    s.update(dts[i])

    print c1.data['outlay']
    assert c1.data['outlay'][dts[1]] == (-4 * 100)
    assert c2.data['outlay'][dts[1]] == 100
开发者ID:boyac,项目名称:bt,代码行数:45,代码来源:test_core.py


注:本文中的bt.core.StrategyBase.setup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。