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


Python StrategyBase.run方法代码示例

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


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

示例1: test_strategybase_multiple_calls_preset_secs

# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import run [as 别名]
def test_strategybase_multiple_calls_preset_secs():
    c1 = SecurityBase('c1')
    c2 = SecurityBase('c2')
    s = StrategyBase('s', [c1, c2])

    c1 = s['c1']
    c2 = s['c2']

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

    data.c2[dts[0]] = 95
    data.c1[dts[1]] = 95
    data.c2[dts[2]] = 95
    data.c2[dts[3]] = 95
    data.c2[dts[4]] = 95
    data.c1[dts[4]] = 105

    s.setup(data)

    # define strategy logic
    def algo(target):
        # close out any open positions
        target.flatten()

        # get stock w/ lowest price
        c = target.universe.ix[target.now].idxmin()

        # allocate all capital to that stock
        target.allocate(target.value, c)

    # replace run logic
    s.run = algo

    # start w/ 1000
    s.adjust(1000)

    # loop through dates manually
    i = 0

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

    assert len(s.children) == 2
    assert s.value == 1000

    # run t0
    s.run(s)

    assert len(s.children) == 2
    assert s.value == 999
    assert s.capital == 49

    assert c2.value == 950
    assert c2.weight == 950.0 / 999
    assert c2.price == 95

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

    assert len(s.children) == 2
    assert s.value == 999
    assert s.capital == 49

    assert c2.value == 950
    assert c2.weight == 950.0 / 999
    assert c2.price == 95

    # update t1
    i = 1
    s.update(dts[i])

    assert s.value == 1049
    assert s.capital == 49
    assert len(s.children) == 2

    assert c2.value == 1000
    assert c2.weight == 1000.0 / 1049.0
    assert c2.price == 100

    # run t1 - close out c2, open c1
    s.run(s)

    assert c1.value == 1045
    assert c1.weight == 1045.0 / 1047
    assert c1.price == 95

    assert c2.value == 0
    assert c2.weight == 0
    assert c2.price == 100

    assert len(s.children) == 2
    assert s.value == 1047
    assert s.capital == 2

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

    assert len(s.children) == 2
    assert s.value == 1047
#.........这里部分代码省略.........
开发者ID:chenqx,项目名称:bt,代码行数:103,代码来源:test_core.py


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