本文整理汇总了Python中bt.core.StrategyBase.flatten方法的典型用法代码示例。如果您正苦于以下问题:Python StrategyBase.flatten方法的具体用法?Python StrategyBase.flatten怎么用?Python StrategyBase.flatten使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bt.core.StrategyBase
的用法示例。
在下文中一共展示了StrategyBase.flatten方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_strategybase_flatten
# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import flatten [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
示例2: test_strategybase_tree_rebalance_base
# 需要导入模块: from bt.core import StrategyBase [as 别名]
# 或者: from bt.core.StrategyBase import flatten [as 别名]
def test_strategybase_tree_rebalance_base():
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
# check that 2 rebalances of equal weight lead to two different allocs
# since value changes after first call
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
s.rebalance(0.5, 'c2')
assert c2.position == 4
assert c2.value == 400
assert s.capital == 1000 - 501 - 401
assert s.value == 998
assert c2.weight == 400.0 / 998
assert c1.weight == 500.0 / 998
# close out everything
s.flatten()
# adjust to get back to 1000
s.adjust(4)
assert s.value == 1000
assert s.capital == 1000
assert c1.value == 0
assert c2.value == 0
# now rebalance but set fixed base
base = s.value
s.rebalance(0.5, 'c1', base=base)
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
s.rebalance(0.5, 'c2', base=base)
assert c2.position == 5
assert c2.value == 500
assert s.capital == 1000 - 501 - 501
assert s.value == 998
assert c2.weight == 500.0 / 998
assert c1.weight == 500.0 / 998