Пример #1
0
    def test_get_context_events(self):
        kwargs = {}
        context = self.view.get_context_events(**kwargs)

        # these should be 4 goals here from setup
        original_context_len = len(context)
        # check number of goals passed matches
        self.assertEqual(original_context_len, 4)

        # check categories
        expected_categories = [gt.name for gt in GoalType.objects.all()]
        actual_categories = [x.get('category') for x in context]
        for category in expected_categories:
            self.assertTrue(category in actual_categories)

        # now lets add some more data and verify it is added
        goal1 = GoalFactory.create()
        goal2 = GoalFactory.create()
        goal3 = GoalFactory.create()

        context = self.view.get_context_events(**kwargs)
        # check number of goals passed matches
        self.assertEqual(original_context_len + 3, len(context))
        # check categories
        expected_categories = [gt.name for gt in GoalType.objects.all()]
        actual_categories = [x.get('category') for x in context]
        for category in expected_categories:
            self.assertTrue(category in actual_categories)
    def setUp(self):
        self.account1 = ClientAccountFactory.create()
        self.broker_acc1 = IBAccountFactory.create(bs_account=self.account1, ib_account=IB_ACC_1)
        self.goal1 = GoalFactory.create(account=self.account1)

        self.account2 = ClientAccountFactory.create()
        self.goal2 = GoalFactory.create(account=self.account2)
        self.broker_acc2 = IBAccountFactory.create(bs_account=self.account2, ib_account=IB_ACC_2)

        self.ticker1 = TickerFactory.create(symbol='GOOG')
        self.ticker2 = TickerFactory.create(symbol='AAPL')
Пример #3
0
    def setUp(self):
        super(FirmAnalyticsMixinTests, self).setUp()
        self.support_group = GroupFactory(name=GROUP_SUPPORT_STAFF)
        self.bonds_type = InvestmentType.Standard.BONDS.get()
        self.stocks_type = InvestmentType.Standard.STOCKS.get()

        self.view = self.DummyView()
        self.today = today = timezone.now().date()
        # Populate the AccountType -> RiskProfileGroup mapping
        for atid, _ in constants.ACCOUNT_TYPES:
            AccountTypeRiskProfileGroupFactory.create(account_type=atid)
        # first client
        self.betasmartz_client = ClientFactory.create()
        self.client_account = ClientAccountFactory.create(
            primary_owner=self.betasmartz_client,
            account_type=constants.ACCOUNT_TYPE_PERSONAL)
        self.firm = self.betasmartz_client.advisor.firm

        self.external_asset1 = ExternalAssetFactory.create(
            owner=self.betasmartz_client)
        self.external_asset2 = ExternalAssetFactory.create(
            owner=self.betasmartz_client)
        self.goal = GoalFactory.create(account=self.client_account)
        self.goal2 = GoalFactory.create(account=self.client_account)
        self.transaction = TransactionFactory.create(
            to_goal=self.goal,
            from_goal=self.goal2,
            amount=self.goal.cash_balance,
            status=Transaction.STATUS_EXECUTED,
            reason=Transaction.REASON_TRANSFER,
            executed=self.today)

        # second client
        self.betasmartz_client2 = ClientFactory.create(
            advisor=self.betasmartz_client.advisor)
        self.client_account2 = ClientAccountFactory.create(
            primary_owner=self.betasmartz_client2,
            account_type=constants.ACCOUNT_TYPE_PERSONAL)
        self.external_asset1 = ExternalAssetFactory.create(
            owner=self.betasmartz_client2)
        self.external_asset2 = ExternalAssetFactory.create(
            owner=self.betasmartz_client2)

        self.goal3 = GoalFactory.create(account=self.client_account2)
        self.goal4 = GoalFactory.create(account=self.client_account2)
        self.transaction2 = TransactionFactory.create(
            to_goal=self.goal3,
            from_goal=self.goal4,
            amount=self.goal3.cash_balance,
            status=Transaction.STATUS_EXECUTED,
            reason=Transaction.REASON_TRANSFER,
            executed=self.today)
Пример #4
0
    def setUp(self):
        self.account1 = ClientAccountFactory.create()
        self.goal1 = GoalFactory.create(account=self.account1)

        self.account2 = ClientAccountFactory.create()
        self.goal2 = GoalFactory.create(account=self.account2)

        self.account3 = ClientAccountFactory.create()
        self.goal3 = GoalFactory.create(account=self.account3)

        self.ticker1 = TickerFactory.create(symbol='GOOG')
        self.ticker2 = TickerFactory.create(symbol='AAPL')
        self.ticker3 = TickerFactory.create(symbol='MSFT')
Пример #5
0
    def setUp(self):
        for atid, _ in constants.ACCOUNT_TYPES:
            AccountTypeRiskProfileGroupFactory.create(account_type=atid)
        self.today = today = date(2016, 5, 1)
        self.older_fiscal_year = FiscalYearFactory.create()
        self.older_fiscal_year2 = FiscalYearFactory.create()
        self.current_fiscal_year = FiscalYearFactory.create(
            year=today.year,
            begin_date=datetime(year=today.year, month=1, day=1),
            end_date=datetime(year=today.year, month=12, day=31))
        self.firm = FirmFactory.create()
        self.firm.fiscal_years.add(self.older_fiscal_year)
        self.firm.fiscal_years.add(self.older_fiscal_year2)
        self.firm.fiscal_years.add(self.current_fiscal_year)
        # add an advisor for the firm
        self.advisor = AdvisorFactory.create(firm=self.firm)
        # add a client account for the firm
        self.betasmartz_client = ClientFactory.create(advisor=self.advisor)
        self.client_account = ClientAccountFactory.create(
            primary_owner=self.betasmartz_client)
        # add some goals
        self.goal1 = GoalFactory.create(account=self.client_account)
        self.goal2 = GoalFactory.create(account=self.client_account)
        # add some transaction fees associated with the goals
        # there are two fees here, one for the from_goal1, and one for the to_goal2
        self.fee1 = TransactionFactory.create(
            reason=Transaction.REASON_FEE,
            status=Transaction.STATUS_EXECUTED,
            from_goal=self.goal1,
            to_goal=self.goal2,
            amount=500,
            executed=today - relativedelta(months=1))

        # add new advisor to firm
        # add new clientacounts for advisor and fees
        self.advisor2 = AdvisorFactory.create(firm=self.firm)
        self.betasmartz_client2 = ClientFactory.create(advisor=self.advisor2)
        self.client_account2 = ClientAccountFactory.create(
            primary_owner=self.betasmartz_client2)
        self.goal3 = GoalFactory.create(account=self.client_account2)
        # add some more fees
        self.fee2 = TransactionFactory.create(
            reason=Transaction.REASON_FEE,
            status=Transaction.STATUS_EXECUTED,
            to_goal=self.goal3,
            amount=800,
            executed=self.older_fiscal_year.begin_date +
            relativedelta(months=1))
    def setUp(self):
        self.broker = ETNABroker()
        self.account1 = ClientAccountFactory.create()
        self.broker_acc1 = APEXAccountFactory.create(bs_account=self.account1)
        self.goal1 = GoalFactory.create(account=self.account1)

        self.account2 = ClientAccountFactory.create()
        self.goal2 = GoalFactory.create(account=self.account2)
        self.broker_acc2 = IBAccountFactory.create(bs_account=self.account2)

        self.account3 = ClientAccountFactory.create()
        self.goal3 = GoalFactory.create(account=self.account3)
        self.broker_acc3 = IBAccountFactory.create(bs_account=self.account3)

        self.ticker1 = TickerFactory.create(symbol='GOOG')
        self.ticker2 = TickerFactory.create(symbol='AAPL')
        self.ticker3 = TickerFactory.create(symbol='MSFT')
Пример #7
0
 def test_get_asset_weights_held_less_than1y_without_new_postions(self):
     fund = TickerFactory.create(unit_price=2.1)
     goal = GoalFactory.create()
     today = datetime.date(2016, 1, 1)
     # Create a 6 month old execution, transaction and a distribution that caused the transaction
     Fixture1.create_execution_details(goal, fund, 10, 2, datetime.date(2014, 6, 1))
     ep = ExecutionProviderDjango()
     vals = ep.get_asset_weights_held_less_than1y(goal, today)
     self.assertEqual(len(vals), 0)
Пример #8
0
    def setUp(self):
        self.support_group = GroupFactory(name=GROUP_SUPPORT_STAFF)
        # client with some personal assets, cash balance and goals
        self.region = RegionFactory.create()
        self.betasmartz_client_address = AddressFactory(region=self.region)
        self.risk_group = RiskProfileGroupFactory.create(name='Personal Risk Profile Group')
        self.personal_account_type = AccountTypeRiskProfileGroupFactory.create(account_type=0,
                                                                               risk_profile_group=self.risk_group)
        self.user = UserFactory.create()
        self.betasmartz_client = ClientFactory.create(user=self.user)

        self.betasmartz_client_account = ClientAccountFactory(primary_owner=self.betasmartz_client, account_type=ACCOUNT_TYPE_PERSONAL)
        self.external_asset1 = ExternalAssetFactory.create(owner=self.betasmartz_client)
        self.external_asset2 = ExternalAssetFactory.create(owner=self.betasmartz_client)

        self.goal1 = GoalFactory.create(account=self.betasmartz_client_account)
        self.goal2 = GoalFactory.create(account=self.betasmartz_client_account)

        self.betasmartz_client2 = ClientFactory.create()
Пример #9
0
    def setUp(self):
        self.t1 = TickerFactory.create(symbol='SPY', unit_price=5)
        self.t2 = TickerFactory.create(symbol='VEA', unit_price=5)
        self.t3 = TickerFactory.create(symbol='TIP', unit_price=100)
        self.t4 = TickerFactory.create(symbol='IEV', unit_price=100)

        self.t5 = TickerFactory.create(symbol='IEV2',
                                       unit_price=100,
                                       asset_class=self.t4.asset_class)

        self.equity = AssetFeatureValueFactory.create(
            name='equity', assets=[self.t1, self.t2])
        self.bond = AssetFeatureValueFactory.create(name='bond',
                                                    assets=[self.t3, self.t4])

        self.goal_settings = GoalSettingFactory.create()
        asset_classes = [
            self.t1.asset_class, self.t2.asset_class, self.t3.asset_class,
            self.t4.asset_class
        ]
        portfolio_set = PortfolioSetFactory.create(name='set',
                                                   risk_free_rate=0.01,
                                                   asset_classes=asset_classes)
        self.goal = GoalFactory.create(approved_settings=self.goal_settings,
                                       active_settings=self.goal_settings,
                                       cash_balance=100,
                                       portfolio_set=portfolio_set)

        self.tickers = [self.t1, self.t2, self.t3, self.t4, self.t4]
        self.prices = [4, 4, 90, 90, 95]
        self.quantities = [5, 5, 5, 5, 5]
        self.executed = [
            date(2015, 1, 1),
            date(2016, 1, 1),
            date(2015, 1, 1),
            date(2016, 1, 1),
            date(2016, 1, 1)
        ]

        self.execution_details = []
        for i in range(5):
            execution = Fixture1.create_execution_details(
                self.goal, self.tickers[i], self.quantities[i], self.prices[i],
                self.executed[i])
            self.execution_details.append(execution)

        self.data_provider = DataProviderDjango(mocked_now.date())
        self.execution_provider = ExecutionProviderDjango()
        MarkowitzScaleFactory.create()
        self.setup_performance_history()
        self.idata = get_instruments(self.data_provider)

        self.portfolio = PortfolioFactory.create(setting=self.goal_settings)
        self.current_weights = get_held_weights(self.goal)
Пример #10
0
    def test_get_asset_weights_without_tax_winners(self):
        fund = TickerFactory.create(unit_price=3)
        goal = GoalFactory.create()
        today = datetime.date(2016, 1, 1)
        # Create a 6 month old execution, transaction and a distribution that caused the transaction
        Fixture1.create_execution_details(goal, fund, 10, 2, datetime.date(2014, 6, 1))
        Fixture1.create_execution_details(goal, fund, 10, 4, datetime.date(2015, 6, 1))

        ep = ExecutionProviderDjango()
        vals = ep.get_asset_weights_without_tax_winners(goal=goal)
        self.assertAlmostEqual(vals[fund.id], (10*3) / goal.available_balance)
Пример #11
0
 def test_apex_order3(self):
     self.account3 = ClientAccountFactory.create()
     self.goal3 = GoalFactory.create(account=self.account3)
     self.mor3 = MarketOrderRequestFactory.create(account=self.account3)
     er5 = ExecutionRequestFactory.create(goal=self.goal3,
                                          asset=self.asset,
                                          volume=20,
                                          order=self.mor3)
     create_apex_orders()
     etna_orders = OrderETNA.objects.all()
     self.assertTrue(len(etna_orders) == 2)
     self.assertTrue(etna_orders[0].Quantity == 35)
Пример #12
0
    def test_get_firm_analytics_worth_and_risk_filter(self):
        # /firm/analytics?worth=affluent&risk=0&risk=20&advisor=&client=
        url = reverse('firm:analytics') + '?worth=affluent&risk=0&risk=20&advisor=&client='
        rep = AuthorisedRepresentativeFactory.create(firm=self.firm)
        advisor = AdvisorFactory.create(firm=self.firm)
        aclient = ClientFactory.create(advisor=advisor)
        aaccount = ClientAccountFactory.create(primary_owner=aclient)
        goal = GoalFactory.create(account=aaccount)

        self.client.login(username=rep.user.email, password='******')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
Пример #13
0
 def test_apex_order2(self):
     self.account2 = ClientAccountFactory.create()
     self.goal2 = GoalFactory.create(account=self.account2)
     self.mor2 = MarketOrderRequestFactory.create(account=self.account2)
     self.asset3 = TickerFactory.create(symbol='MSFT')
     er4 = ExecutionRequestFactory.create(goal=self.goal2,
                                          asset=self.asset3,
                                          volume=20,
                                          order=self.mor2)
     create_apex_orders()
     etna_order = OrderETNA.objects.all()
     self.assertTrue(len(etna_order) == 3)
     self.assertTrue(etna_order[2].Quantity == 20)
Пример #14
0
    def test_get_context_positions(self):
        kwargs = {}
        # empty queries tests, should be empty unless Positions are
        # added to setUp

        positions = self.view.get_context_positions(**kwargs)
        self.assertSequenceEqual(positions.get('asset_class'), [])
        self.assertSequenceEqual(positions.get('region'), [])
        self.assertSequenceEqual(positions.get('investment_type'), [])

        # now we're going to add some data and rerun sequence tests
        # have to specify content_type here because ticker uses the django
        # built in contenttype it causes problems here otherwise,
        # TODO: maybe some more elegant factoryboy solution here?
        ticker1 = TickerFactory.create()
        ticker2 = TickerFactory.create(
            benchmark_content_type=ticker1.benchmark_content_type)
        ticker3 = TickerFactory.create(
            benchmark_content_type=ticker1.benchmark_content_type)

        goal = GoalFactory.create()
        today = date(2016, 1, 1)
        # Create a 6 month old execution, transaction and a distribution that caused the transaction
        data1 = Fixture1.create_execution_details(goal, ticker1, 10, 2,
                                                  date(2014, 6, 1))
        data2 = Fixture1.create_execution_details(goal, ticker2, 10, 2,
                                                  date(2014, 6, 1))
        data3 = Fixture1.create_execution_details(goal, ticker3, 10, 2,
                                                  date(2014, 6, 1))

        positions = self.view.get_context_positions(**kwargs)

        # should be three results, one for each position we just added
        self.assertEqual(len(positions.get('asset_class')), 3)
        self.assertEqual(len(positions.get('region')), 3)
        self.assertEqual(len(positions.get('investment_type')), 3)

        # compare sum of values to double check values being passed
        expected_sum = data1[-1].quantity * ticker1.unit_price + \
                       data2[-1].quantity * ticker2.unit_price + \
                       data3[-1].quantity * ticker3.unit_price

        asset_actual_sum = sum(
            [x.get('value') for x in positions.get('asset_class')])
        region_actual_sum = sum(
            [x.get('value') for x in positions.get('region')])
        investment_actual_sum = sum(
            [x.get('value') for x in positions.get('investment_type')])
        self.assertAlmostEqual(expected_sum, asset_actual_sum)
        self.assertAlmostEqual(expected_sum, region_actual_sum)
        self.assertAlmostEqual(expected_sum, investment_actual_sum)
Пример #15
0
    def test_get_positions_all(self):
        fund = TickerFactory.create(unit_price=2.1)
        fund2 = TickerFactory.create(unit_price=4)
        goal = GoalFactory.create()
        today = date(2016, 1, 1)
        # Create a 6 month old execution, transaction and a distribution that caused the transaction

        Fixture1.create_execution_details(goal, fund, 10, 2, date(2014, 6, 1))
        Fixture1.create_execution_details(goal, fund, 5, 2, date(2014, 6, 1))
        Fixture1.create_execution_details(goal, fund2, 1, 2, date(2014, 6, 1))

        positions = goal.get_positions_all()

        self.assertTrue(positions[0]['quantity'] == 15)
        self.assertTrue(positions[1]['quantity'] == 1)
Пример #16
0
    def test_calculate_portfolio_old(self):
        fund0 = TickerFactory.create(symbol='IAGG')
        fund1 = TickerFactory.create(symbol='ITOT')
        fund5 = TickerFactory.create(symbol='GRFXX')
        fund2 = TickerFactory.create(symbol='VEA')
        fund0 = TickerFactory.create(symbol='IPO')
        fund3 = TickerFactory.create(symbol='EEM')
        fund4 = TickerFactory.create(symbol='AGG')

        AssetFeatureValueFactory.create(
            assets=[fund1, fund2, fund3, fund4, fund5])
        ps1 = PortfolioSetFactory \
            .create(asset_classes=[fund1.asset_class, fund2.asset_class, fund3.asset_class, fund4.asset_class, fund5.asset_class])

        # Create a settings object with a metric for a feature with no instruments in the current portfolio set.
        feature = AssetFeatureValueFactory.create()
        settings = GoalSettingFactory.create()
        risk_metric = GoalMetricFactory.create(group=settings.metric_group)
        mix_metric = GoalMetricFactory.create(
            group=settings.metric_group,
            type=GoalMetric.METRIC_TYPE_PORTFOLIO_MIX,
            feature=feature,
            comparison=GoalMetric.METRIC_COMPARISON_MAXIMUM,
            configured_val=.3)
        goal = GoalFactory.create(selected_settings=settings,
                                  portfolio_set=ps1)

        # The below fund has the desired feature, but is not in the goal's portfolio set.

        feature.assets.add(fund1)

        # Create some instrument data for the two assets
        self.m_scale = MarkowitzScaleFactory.create()
        # populate the data needed for the prediction
        # We need at least 500 days as the cycles go up to 70 days and we need at least 7 cycles.
        populate_prices(500, asof=mocked_now.date())
        populate_cycle_obs(500, asof=mocked_now.date())
        populate_cycle_prediction(asof=mocked_now.date())
        data_provider = DataProviderDjango()
        execution_provider = ExecutionProviderDjango()
        idata = build_instruments(data_provider)
        result = calculate_portfolio_old(settings=settings,
                                         data_provider=data_provider,
                                         execution_provider=execution_provider,
                                         idata=idata)
        self.assertTrue(True)
Пример #17
0
    def test_calculate_portfolio(self):
        # TODO
        # constraints -> limit them -> maximum minimum value of 5%, maximum max value of 95%

        asset_class1 = AssetClassFactory.create(name='US_TOTAL_BOND_MARKET')
        asset_class2 = AssetClassFactory.create(name='HEDGE_FUNDS')

        fund0 = TickerFactory.create(symbol='IAGG', asset_class=asset_class1)
        fund0 = TickerFactory.create(symbol='GRFXX', asset_class=asset_class1)
        fund1 = TickerFactory.create(symbol='ITOT', asset_class=asset_class2)
        fund0 = TickerFactory.create(symbol='IPO')
        fund0 = TickerFactory.create(symbol='AGG', asset_class=asset_class1)
        fund6 = TickerFactory.create(symbol='rest')

        ps1 = PortfolioSetFactory \
            .create(asset_classes=[asset_class1, asset_class2, fund6.asset_class])

        feature = AssetFeatureValueFactory.create()
        feature.assets.add(fund6)
        settings = GoalSettingFactory.create()
        risk_metric = GoalMetricFactory.create(
            group=settings.metric_group,
            type=GoalMetric.METRIC_TYPE_RISK_SCORE)
        mix_metric = GoalMetricFactory.create(
            group=settings.metric_group,
            type=GoalMetric.METRIC_TYPE_PORTFOLIO_MIX,
            feature=feature,
            comparison=GoalMetric.METRIC_COMPARISON_MINIMUM,
            configured_val=0.5)
        goal = GoalFactory.create(selected_settings=settings,
                                  portfolio_set=ps1)

        # Create some instrument data for the two assets
        self.m_scale = MarkowitzScaleFactory.create()

        populate_prices(500, asof=mocked_now.date())
        populate_cycle_obs(500, asof=mocked_now.date())
        populate_cycle_prediction(asof=mocked_now.date())
        data_provider = DataProviderDjango()
        execution_provider = ExecutionProviderDjango()
        idata = build_instruments(data_provider)
        result = calculate_portfolio(settings=settings,
                                     data_provider=data_provider,
                                     execution_provider=execution_provider,
                                     idata=idata)
        self.assertTrue(True)
Пример #18
0
    def setUp(self):
        self.account = ClientAccountFactory.create()
        self.goal = GoalFactory.create(account=self.account)
        self.mor = MarketOrderRequestFactory.create(account=self.account)

        self.asset = TickerFactory.create(symbol='GOOG')
        self.asset2 = TickerFactory.create(symbol='AAPL')
        er1 = ExecutionRequestFactory.create(goal=self.goal,
                                             asset=self.asset,
                                             volume=5,
                                             order=self.mor)
        er2 = ExecutionRequestFactory.create(goal=self.goal,
                                             asset=self.asset,
                                             volume=10,
                                             order=self.mor)
        er3 = ExecutionRequestFactory.create(goal=self.goal,
                                             asset=self.asset2,
                                             volume=10,
                                             order=self.mor)
Пример #19
0
    def test_sum_stocks_for_goal(self):
        self.content_type = ContentTypeFactory.create()
        self.bonds_asset_class = AssetClassFactory.create(
            investment_type=InvestmentType.Standard.BONDS.get())
        self.stocks_asset_class = AssetClassFactory.create(
            investment_type=InvestmentType.Standard.STOCKS.get())
        fund1 = TickerFactory.create(asset_class=self.stocks_asset_class,
                                     benchmark_content_type=self.content_type,
                                     etf=True)
        goal = GoalFactory.create()

        Fixture1.create_execution_details(goal, fund1, 10, 2, date(2014, 6, 1))

        weight_stocks = goal.stock_balance
        weight_bonds = goal.bond_balance
        weight_core = goal.core_balance
        self.assertTrue(weight_stocks == 100)
        self.assertTrue(weight_bonds == 0)
        self.assertTrue(weight_core == 100)
Пример #20
0
    def setUp(self):
        self.t1 = TickerFactory.create(symbol='SPY', unit_price=5)
        self.t2 = TickerFactory.create(symbol='VEA', unit_price=5)
        self.t3 = TickerFactory.create(symbol='TIP', unit_price=100)
        self.t4 = TickerFactory.create(symbol='IEV', unit_price=100)

        self.equity = AssetFeatureValueFactory.create(
            name='equity', assets=[self.t1, self.t2])
        self.bond = AssetFeatureValueFactory.create(name='bond',
                                                    assets=[self.t3, self.t4])

        self.goal_settings = GoalSettingFactory.create()
        asset_classes = [
            self.t1.asset_class, self.t2.asset_class, self.t3.asset_class,
            self.t4.asset_class
        ]
        portfolio_set = PortfolioSetFactory.create(name='set',
                                                   risk_free_rate=0.01,
                                                   asset_classes=asset_classes)
        self.goal = GoalFactory.create(approved_settings=self.goal_settings,
                                       cash_balance=100,
                                       portfolio_set=portfolio_set)

        Fixture1.create_execution_details(self.goal, self.t1, 5, 4,
                                          date(2016, 1, 1))
        Fixture1.create_execution_details(self.goal, self.t2, 5, 4,
                                          date(2016, 1, 1))
        Fixture1.create_execution_details(self.goal, self.t3, 5, 90,
                                          date(2016, 1, 1))
        Fixture1.create_execution_details(self.goal, self.t4, 5, 90,
                                          date(2016, 1, 1))
        Fixture1.create_execution_details(self.goal, self.t4, 5, 90,
                                          date(2016, 1, 1))

        self.data_provider = DataProviderDjango()
        self.execution_provider = ExecutionProviderDjango()
        MarkowitzScaleFactory.create()
        self.setup_performance_history()
        self.idata = get_instruments(self.data_provider)
Пример #21
0
    def initialize_backtest(cls, tickers):
        ticker_list = list()
        equity_asset_class = AssetClassFactory\
            .create(name='US_MUNICIPAL_BONDS', investment_type=InvestmentTypeFactory.create(name='US_MUNICIPAL_BONDS'))

        for t in tickers:
            market_index = MarketIndexFactory.create()
            ticker = TickerFactory.create(symbol=t,
                                          asset_class=equity_asset_class,
                                          benchmark=market_index)
            ticker_list.append(ticker)

        portfolio_set = PortfolioSetFactory.create(
            name='portfolio_set1',
            risk_free_rate=0.02,
            asset_classes=[equity_asset_class],
            portfolio_provider=get_default_provider())
        goal_settings = GoalSettingFactory.create(
            target=100000,
            completion=datetime.date(2000, 1, 1),
            hedge_fx=False,
            rebalance=True,
        )
        goal_metric = GoalMetricFactory.create(
            group=goal_settings.metric_group,
            type=GoalMetric.METRIC_TYPE_RISK_SCORE)
        PortfolioFactory.create(setting=goal_settings)
        #GoalMetricGroupFactory.create()

        return GoalFactory.create(account=Fixture1.personal_account1(),
                                  name='goal1',
                                  type=Fixture1.goal_type1(),
                                  cash_balance=10000,
                                  approved_settings=goal_settings,
                                  selected_settings=goal_settings,
                                  active_settings=goal_settings,
                                  portfolio_set=portfolio_set)
Пример #22
0
    def test_calc_opt_inputs_no_assets_for_constraint(self):
        """
        Makes sure when we have no assets filling a constraint, we behave appropriately.
        """

        # This fund has a different feature to the one in the mix metric, but it is in the correct portfolio set.
        fund1 = TickerFactory.create()
        AssetFeatureValueFactory.create(assets=[fund1])
        ps1 = PortfolioSetFactory.create(asset_classes=[fund1.asset_class])

        # Create a settings object with a metric for a feature with no instruments in the current portfolio set.
        feature = AssetFeatureValueFactory.create()
        settings = GoalSettingFactory.create()
        risk_metric = GoalMetricFactory.create(group=settings.metric_group)
        mix_metric = GoalMetricFactory.create(
            group=settings.metric_group,
            type=GoalMetric.METRIC_TYPE_PORTFOLIO_MIX,
            feature=feature,
            comparison=GoalMetric.METRIC_COMPARISON_MAXIMUM,
            configured_val=.3)
        goal = GoalFactory.create(selected_settings=settings,
                                  portfolio_set=ps1)

        # The below fund has the desired feature, but is not in the goal's portfolio set.
        fund2 = TickerFactory.create()
        feature.assets.add(fund2)

        # Create some instrument data for the two assets
        self.m_scale = MarkowitzScaleFactory.create()
        # populate the data needed for the prediction
        # We need at least 500 days as the cycles go up to 70 days and we need at least 7 cycles.
        populate_prices(500, asof=mocked_now.date())
        populate_cycle_obs(500, asof=mocked_now.date())
        populate_cycle_prediction(asof=mocked_now.date())
        data_provider = DataProviderDjango()
        idata = build_instruments(data_provider)

        execution_provider = ExecutionProviderDjango()

        # Get the opt inputs, there should be no constraint for the max for the feature with no funds.
        result = calc_opt_inputs(settings=settings,
                                 idata=idata,
                                 data_provider=data_provider,
                                 execution_provider=execution_provider)
        xs, lam, constraints, settings_instruments, settings_symbol_ixs, lcovars = result
        self.assertEqual(len(constraints), 3)  # All positive, and sum to 1

        # Then create a fund in the portfolio I want. We should get a constraint for the maximum for the feature.
        fund3 = TickerFactory.create(asset_class=fund1.asset_class)
        feature.assets.add(fund3)
        delete_data()
        populate_prices(500, asof=mocked_now.date())
        populate_cycle_obs(500, asof=mocked_now.date())
        populate_cycle_prediction(asof=mocked_now.date())
        idata = build_instruments(data_provider)
        result = calc_opt_inputs(settings=settings,
                                 idata=idata,
                                 data_provider=data_provider,
                                 execution_provider=execution_provider)
        xs, lam, constraints, settings_instruments, settings_symbol_ixs, lcovars = result
        self.assertEqual(len(constraints),
                         4)  # All positive, sum to 1, and the max constraint