def test_strategybase_tree_rebalance_to_0(): 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 # now rebalance c1 s.rebalance(0, 'c1') assert c1.position == 0 assert c1.value == 0 assert s.capital == 1000 - 501 + 499 assert s.value == 998 assert c1.weight == 0 assert c2.weight == 0
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 / 1000 assert s.capital == 1000 - 500 assert s.value == 1000 assert s.price == 100 i = 1 s.update(dts[i], data.ix[dts[i]]) assert c1.position == 5 assert c1.value == 525 assert c1.weight == 525.0 / 1025 assert s.capital == 1000 - 500 assert s.value == 1025 assert np.allclose(s.price, 102.5)
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
def test_strategybase_tree_adjust(): 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) s.adjust(1000) assert s.capital == 1000 assert s.value == 1000 assert c1.value == 0 assert c2.value == 0 assert c1.weight == 0 assert c2.weight == 0
def test_node_members(): s1 = SecurityBase('s1') s2 = SecurityBase('s2') s = StrategyBase('p', [s1, s2]) s1 = s['s1'] s2 = s['s2'] actual = s.members assert len(actual) == 3 assert s1 in actual assert s2 in actual assert s in actual actual = s1.members assert len(actual) == 1 assert s1 in actual actual = s2.members assert len(actual) == 1 assert s2 in actual
def test_strategybase_tree_setup(): 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) 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
def test_strategybase_tree_allocate(): 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 directly to child c1.allocate(500) 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
def test_strategybase_tree_decimal_position_rebalance(): c1 = SecurityBase('c1') c2 = SecurityBase('c2') s = StrategyBase('p', [c1, c2]) s.use_integer_positions(False) 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) s.setup(data) i = 0 s.update(dts[i], data.ix[dts[i]]) s.adjust(1000.2) s.rebalance(0.42, 'c1') s.rebalance(0.58, 'c2') aae(c1.value, 420.084) aae(c2.value, 580.116) aae(c1.value + c2.value, 1000.2)
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 == 1000 assert s.capital == 50 assert c2.value == 950 assert c2.weight == 950.0 / 1000 assert c2.price == 95 # update out t0 s.update(dts[i]) assert len(s.children) == 2 assert s.value == 1000 assert s.capital == 50 assert c2.value == 950 assert c2.weight == 950.0 / 1000 assert c2.price == 95 # update t1 i = 1 s.update(dts[i]) assert s.value == 1050 assert s.capital == 50 assert len(s.children) == 2 assert c2.value == 1000 assert c2.weight == 1000.0 / 1050. assert c2.price == 100 # run t1 - close out c2, open c1 s.run(s) assert c1.value == 1045 assert c1.weight == 1045.0 / 1050 assert c1.price == 95 assert c2.value == 0 assert c2.weight == 0 assert c2.price == 100 assert len(s.children) == 2 assert s.value == 1050 assert s.capital == 5 # update out t1 s.update(dts[i]) assert len(s.children) == 2 assert s.value == 1050 assert s.capital == 5 assert c1.value == 1045 assert c1.weight == 1045.0 / 1050 assert c1.price == 95 assert c2.value == 0 assert c2.weight == 0 assert c2.price == 100 # update t2 i = 2 s.update(dts[i]) assert len(s.children) == 2 assert s.value == 1105 assert s.capital == 5 assert c1.value == 1100 assert c1.weight == 1100.0 / 1105 assert c1.price == 100 assert c2.value == 0 assert c2.weight == 0 assert c2.price == 95 # run t2 s.run(s) assert len(s.children) == 2 assert s.value == 1105 assert s.capital == 60 assert c1.value == 0 assert c1.weight == 0 assert c1.price == 100 assert c2.value == 1045 assert c2.weight == 1045.0 / 1105 assert c2.price == 95 # update out t2 s.update(dts[i]) assert len(s.children) == 2 assert s.value == 1105 assert s.capital == 60 assert c1.value == 0 assert c1.weight == 0 assert c1.price == 100 assert c2.value == 1045 assert c2.weight == 1045.0 / 1105 assert c2.price == 95 # update t3 i = 3 s.update(dts[i]) assert len(s.children) == 2 assert s.value == 1105 assert s.capital == 60 assert c1.value == 0 assert c1.weight == 0 assert c1.price == 100 assert c2.value == 1045 assert c2.weight == 1045.0 / 1105 assert c2.price == 95 # run t3 s.run(s) assert len(s.children) == 2 assert s.value == 1105 assert s.capital == 60 assert c1.value == 0 assert c1.weight == 0 assert c1.price == 100 assert c2.value == 1045 assert c2.weight == 1045.0 / 1105 assert c2.price == 95 # update out t3 s.update(dts[i]) assert len(s.children) == 2 assert s.value == 1105 assert s.capital == 60 assert c1.value == 0 assert c1.weight == 0 assert c1.price == 100 assert c2.value == 1045 assert c2.weight == 1045.0 / 1105 assert c2.price == 95 # update t4 i = 4 s.update(dts[i]) assert len(s.children) == 2 assert s.value == 1105 assert s.capital == 60 assert c1.value == 0 assert c1.weight == 0 # accessing price should refresh - this child has been idle for a while - # must make sure we can still have a fresh prices assert c1.price == 105 assert len(c1.prices) == 5 assert c2.value == 1045 assert c2.weight == 1045.0 / 1105 assert c2.price == 95 # run t4 s.run(s) assert len(s.children) == 2 assert s.value == 1105 assert s.capital == 60 assert c1.value == 0 assert c1.weight == 0 assert c1.price == 105 assert c2.value == 1045 assert c2.weight == 1045.0 / 1105 assert c2.price == 95 # update out t4 s.update(dts[i]) assert len(s.children) == 2 assert s.value == 1105 assert s.capital == 60 assert c1.value == 0 assert c1.weight == 0 assert c1.price == 105 assert c2.value == 1045 assert c2.weight == 1045.0 / 1105 assert c2.price == 95
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]) s1 = m['s1'] s2 = m['s2'] c1 = s1['c1'] c2 = s1['c2'] c12 = s2['c1'] c22 = s2['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 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 == 500 assert s1.capital == 500 - 200 assert c1.value == 200 assert c1.weight == 200.0 / 500 assert c1.position == 2 assert m.capital == 1000 - 500 assert m.value == 1000 assert s1.weight == 500.0 / 1000 assert s2.weight == 0 assert c12.value == 0
def test_fixed_commissions(): c1 = SecurityBase('c1') c2 = SecurityBase('c2') s = StrategyBase('p', [c1, c2]) # fixed $1 commission per transaction s.set_commissions(lambda q, p: 1) 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) 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.value == 400 assert c2.value == 400 assert s.capital == 198 # de-alloc 100 from c1. This should force c1 to sell 2 units to raise at # least 100 (because of commissions) c1.allocate(-100) s.update(dts[i]) assert c1.value == 200 assert s.capital == 198 + 199 # allocate 100 to c2. This should leave things unchaged, since c2 cannot # buy one unit since the commission will cause total outlay to exceed # allocation c2.allocate(100) s.update(dts[i]) assert c2.value == 400 assert s.capital == 198 + 199 # ok try again w/ 101 allocation. This time, it should work c2.allocate(101) s.update(dts[i]) assert c2.value == 500 assert s.capital == 198 + 199 - 101 # ok now let's close the whole position. Since we are closing, we expect # the allocation to go through, even though the outlay > amount c2.allocate(-500) s.update(dts[i]) assert c2.value == 0 assert s.capital == 198 + 199 - 101 + 499 # now we are going to go short c2 # we want to 'raise' 100 dollars. Since we need at a minimum 100, but we # also have commissions, we will actually short 2 units in order to raise # at least 100 c2.allocate(-100) s.update(dts[i]) assert c2.value == -200 assert s.capital == 198 + 199 - 101 + 499 + 199
def test_strategybase_tree_rebalance_base(): c1 = SecurityBase('c1') c2 = SecurityBase('c2') s = StrategyBase('p', [c1, c2]) s.set_commissions(lambda q, p: 1) 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 == 4 assert c1.value == 400 assert s.capital == 1000 - 401 assert s.value == 999 assert c1.weight == 400.0 / 999 assert c2.weight == 0 s.rebalance(0.5, 'c2') assert c2.position == 4 assert c2.value == 400 assert s.capital == 1000 - 401 - 401 assert s.value == 998 assert c2.weight == 400.0 / 998 assert c1.weight == 400.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 == 4 assert c1.value == 400 assert s.capital == 1000 - 401 assert s.value == 999 assert c1.weight == 400.0 / 999 assert c2.weight == 0 s.rebalance(0.5, 'c2', base=base) assert c2.position == 4 assert c2.value == 400 assert s.capital == 1000 - 401 - 401 assert s.value == 998 assert c2.weight == 400.0 / 998 assert c1.weight == 400.0 / 998
def test_strategybase_tree_rebalance_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]) s1 = m['s1'] s2 = m['s2'] c1 = s1['c1'] c2 = s1['c2'] c12 = s2['c1'] c22 = s2['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 m.setup(data) i = 0 m.update(dts[i], data.ix[dts[i]]) m.adjust(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 rebalance child s1 - since its children are 0, no waterfall alloc m.rebalance(0.5, 's1') 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 s1.rebalance(0.4, 'c1') assert s1.value == 500 assert s1.capital == 500 - 200 assert c1.value == 200 assert c1.weight == 200.0 / 500 assert c1.position == 2 assert m.capital == 1000 - 500 assert m.value == 1000 assert s1.weight == 500.0 / 1000 assert s2.weight == 0 assert c12.value == 0 # now rebalance child s1 again and make sure c1 also gets proportional # increase m.rebalance(0.8, 's1') assert s1.value == 800 aae(m.capital, 200, 1) assert m.value == 1000 assert s1.weight == 800 / 1000 assert s2.weight == 0 assert c1.value == 300.0 assert c1.weight == 300.0 / 800 assert c1.position == 3 # now rebalance child s1 to 0 - should close out s1 and c1 as well m.rebalance(0, 's1') assert s1.value == 0 assert m.capital == 1000 assert m.value == 1000 assert s1.weight == 0 assert s2.weight == 0 assert c1.weight == 0