Beispiel #1
0
    def test_calculate_next_purchases_with_empty_portfolio(self):
        desired_percentages = {Security('TSLA'): 0.3, Security('AMZN'): 0.4, Security('AAPL'): 0.3}
        amount_to_invest = 1000

        next_purchases = bln.calculate_next_purchases(dict(), desired_percentages, amount_to_invest)

        result_value = sum(next_purchases.values())
        self.assertAlmostEqual(result_value, amount_to_invest, 10)

        result_percentages = {sec: value / result_value for sec, value in next_purchases.items()}
        for security, percentage in result_percentages.items():
            expected_percent = desired_percentages.get(security, 0)
            self.assertAlmostEqual(percentage, expected_percent, 10)
Beispiel #2
0
    def test_calculate_limited_purchases(self):
        desired_percentages = {Security('TSLA'): 0.25, Security('AMZN'): 0.5, Security('AAPL'): 0.1,
                               Security('MSFT'): 0.15}

        total_invested = 10_000
        portfolio = {sec: total_invested * percent for sec, percent in desired_percentages.items()}

        amount_to_invest = 1000
        portfolio[Security('TSLA')] -= amount_to_invest / 2
        portfolio[Security('AMZN')] -= amount_to_invest / 2

        next_purchases = bln.calculate_next_purchases(portfolio, desired_percentages, amount_to_invest,
                                                      purchases_to_keep=2)
        self.assertIn(Security('TSLA'), next_purchases.keys())
        self.assertIn(Security('AMZN'), next_purchases.keys())

        self.assertAlmostEqual(next_purchases[Security('TSLA')], amount_to_invest / 2)
        self.assertAlmostEqual(next_purchases[Security('AMZN')], amount_to_invest / 2)
Beispiel #3
0
def _process_invest_args(args):
    transaction_persistence, allocation_persistence = _read_persistence(args)
    purchase_amount = args.purchase_amount

    current_portfolio = transaction_persistence.read_portfolio()
    portfolio_values = _get_portfolio_values(current_portfolio)

    current_allocations = allocation_persistence.read_allocation_percentages()
    next_purchases = balance.calculate_next_purchases(portfolio_values,
                                                      current_allocations,
                                                      purchase_amount)

    if hasattr(args, 'max_count'):
        max_count = args.max_count
        next_purchases = balance.get_top_buy_purchases(next_purchases,
                                                       max_count)

    print('Next purchases')
    print('--------------')
    _print_security_dictionary(next_purchases)
Beispiel #4
0
    def test_calculate_next_purchases(self):
        desired_percentages = {Security('TSLA'): 0.25, Security('AMZN'): 0.5, Security('AAPL'): 0.1,
                               Security('MSFT'): 0.15}
        actual_percentages = {Security('TSLA'): 0.3, Security('AMZN'): 0.4, Security('AAPL'): 0.3}

        total_invested = 10_000
        portfolio = {sec: total_invested * percent for sec, percent in actual_percentages.items()}

        amount_to_invest = 1000
        next_purchases = bln.calculate_next_purchases(portfolio, desired_percentages, amount_to_invest)

        result_porfolio = {sec: portfolio.get(sec, 0) + value for sec, value in next_purchases.items()}

        result_value = sum(result_porfolio.values())
        self.assertAlmostEqual(result_value, amount_to_invest + total_invested, 10)

        result_percentages = {sec: value / result_value for sec, value in result_porfolio.items()}
        for security, percentage in result_percentages.items():
            expected_percent = desired_percentages.get(security, 0)
            self.assertAlmostEqual(percentage, expected_percent, 10)
Beispiel #5
0
def _process_invest_args(args):
    transaction_persistence, allocation_persistence = _read_persistence(args)
    purchase_amount = args.purchase_amount

    current_portfolio = transaction_persistence.read_portfolio()
    portfolio_values = _get_portfolio_values(current_portfolio)

    current_allocations = allocation_persistence.read_allocation_percentages()
    max_count = None
    if hasattr(args, 'max_count'):
        max_count = args.max_count

    next_purchases = balance.calculate_next_purchases(portfolio_values, current_allocations, purchase_amount,
                                                      purchases_to_keep=max_count)
    deviation_from_ideal = balance.get_deviation_from_ideal(portfolio_values, next_purchases, current_allocations)

    print('Next purchases')
    print('--------------')
    _print_security_dictionary(next_purchases)

    if deviation_from_ideal > 1e-4:
        print(f'The new portfolio deviates from the ideal by a standard error of {100*deviation_from_ideal:.2f}%')