コード例 #1
0
    def test_portfolio_value(self):
        """
        Test total market value, total cash value, and total value methods.
        """

        p = Portfolio()

        tickers = ["VCN.TO", "ZAG.TO", "XAW.TO", "TSLA"]
        quantities = [2, 20, 10, 4]
        p.easy_add_assets(tickers=tickers, quantities=quantities)

        mv = p.market_value("CAD")

        total_mv = np.sum(
            [asset.market_value_in("CAD") for asset in p.assets.values()])

        self.assertAlmostEqual(mv, total_mv, 1)

        amounts = [500.15, 200.00]
        currencies = ["CAD", "USD"]
        p.easy_add_cash(amounts, currencies)

        cv = p.cash_value("CAD")

        usd_to_cad = CurrencyRates().get_rate("USD", "CAD")
        total_cv = np.sum(amounts[0] + amounts[1] * usd_to_cad)
        self.assertAlmostEqual(cv, total_cv, 1)

        self.assertAlmostEqual(p.value("CAD"), total_mv + total_cv, 1)
コード例 #2
0
    def test_cash_interface2(self):
        """
        Test portfolio's interface related to Cash class.

        Collectively adding cash to the portfolio.
        """
        p = Portfolio()
        amounts = [500.15, 200.00]
        currencies = ["CAD", "USD"]
        p.easy_add_cash(amounts, currencies)

        self.assertEqual(p.cash[currencies[0]].amount, amounts[0])
        self.assertEqual(p.cash[currencies[1]].amount, amounts[1])
コード例 #3
0
    def test_exchange(self):
        """
        Test currency exchange in Portfolio.
        """

        p = Portfolio()

        amounts = [500.15, 200.00]
        currencies = ["CAD", "USD"]
        p.easy_add_cash(amounts, currencies)

        cad_to_usd = CurrencyRates().get_rate("CAD", "USD")

        p.exchange_currency(to_currency="CAD",
                            from_currency="USD",
                            to_amount=100)
        self.assertAlmostEqual(p.cash["CAD"].amount, 500.15 + 100., 1)
        self.assertAlmostEqual(p.cash["USD"].amount, 200. - 100. * cad_to_usd,
                               1)

        p.exchange_currency(from_currency="USD",
                            to_currency="CAD",
                            from_amount=50)
        self.assertAlmostEqual(p.cash["CAD"].amount,
                               500.15 + 100 + 50 / cad_to_usd, 1)
        self.assertAlmostEqual(p.cash["USD"].amount,
                               200. - 100. * cad_to_usd - 50, 1)

        # error handling:
        with self.assertRaises(Exception):
            p.exchange_currency(to_currency="CAD",
                                from_currency="USD",
                                to_amount=100,
                                from_amount=20)

        # error handling
        with self.assertRaises(Exception):
            p.exchange_currency(to_currency="CAD", from_currency="USD")
コード例 #4
0
from rebalance import Portfolio

# My portfolio
p = Portfolio()

# Cash in portfolio
cash_amounts = [5000]
cash_currency = ["CAD"]
p.easy_add_cash(amounts=cash_amounts, currencies=cash_currency)

# Assets in portfolio
# The price will be retrieved automatically
tickers = ["XIC.TO", "VCN.TO", "TSLA"]
quantities = [20, 20, 10]
p.easy_add_assets(tickers=tickers, quantities=quantities)

# Target asset allocation (in %)
target_asset_alloc = {"XIC.TO": 20, "VCN.TO": 30, "TSLA": 50}

# rebalance
p.selling_allowed = True  # Don't allow selling while rebalancing
p.rebalance(target_asset_alloc, verbose=True)