def test_rebalancing2(self): """ Test rebalancing algorithm. Part 2. Cash is not in the same currency as the assets. """ p = Portfolio() p.add_cash(200., "USD") p.add_cash(250., "GBP") tickers = ["VCN.TO", "XAW.TO", "ZAG.TO"] quantities = [5, 12, 20] p.easy_add_assets(tickers=tickers, quantities=quantities) target_asset_alloc = { "VCN.TO": 40.0, "ZAG.TO": 40.0, "XAW.TO": 20.0, } initial_value = p.value("CAD") p.selling_allowed = False (_, prices, _, _) = p.rebalance(target_asset_alloc, verbose=True) final_value = p.value("CAD") self.assertAlmostEqual(initial_value, final_value, -1) # The prices should be in the tickers' currency for ticker in tickers: self.assertEqual(prices[ticker][1], "CAD") # Since there was no CAD to start off with, # there should be none after rebalacing either # (i.e. amount converted to CAD should be the amount used to purchase CAD assets) self.assertAlmostEqual(p.cash["CAD"].amount, 0., 1)
def test_cash_interface(self): """ Test portfolio's interface related to Cash class. Adding cash of different currency individually to the portfolio. """ p = Portfolio() amount1 = 500.15 p.add_cash(amount1, "cad") amount2 = 200.00 p.add_cash(amount2, "usd") self.assertEqual(p.cash["CAD"].amount, amount1) self.assertEqual(p.cash["USD"].amount, amount2)
def test_rebalancing(self): """ Test rebalancing algorithm. This might break over time as prices increase. If we have enough cash though, the optimizer should ideally be able to match the target asset allocation pretty closely """ p = Portfolio() tickers = ["XBB.TO", "XIC.TO", "ITOT", "IEFA", "IEMG"] quantities = [36, 64, 32, 8, 7] p.easy_add_assets(tickers=tickers, quantities=quantities) p.add_cash(3000, "USD") p.add_cash(515.21, "CAD") p.add_cash(5.00, "GBP") p.selling_allowed = True self.assertTrue(p.selling_allowed) # different order than tickers. # rebalance method should be able to handle such a case target_asset_alloc = { "XBB.TO": 20, "XIC.TO": 20, "IEFA": 20, "ITOT": 36, "IEMG": 4 } initial_value = p.value("CAD") (_, _, _, max_diff) = p.rebalance(target_asset_alloc, verbose=True) final_value = p.value("CAD") self.assertAlmostEqual(initial_value, final_value, 1) self.assertLessEqual(max_diff, 2.) # Error handling with self.assertRaises(Exception): target_asset_alloc = { "XBB.TO": 20, "XIC.TO": 20, "IEFA": 20, } p.rebalance(target_asset_alloc)
from rebalance import Portfolio p = Portfolio() tickers = ["XBB.TO", "XIC.TO", "ITOT", "IEFA", "IEMG"] quantities = [36, 64, 32, 8, 7] p.easy_add_assets(tickers=tickers, quantities = quantities) p.add_cash(5000.00, "GBP") p.add_cash(1000.00, "EUR") target_asset_alloc = { "XBB.TO": 20, "XIC.TO": 20, "IEFA": 20, "ITOT": 36, "IEMG": 4 } initial_value = p.value("CAD") p.selling_allowed = False (_, _, exchange_rates, _) = p.rebalance(target_asset_alloc, verbose=True) final_value = p.value("CAD")