def test_goal_metric(self): t1 = TickerFactory.create(symbol='SPY', unit_price=5) t2 = TickerFactory.create(symbol='QQQ', unit_price=5) equity = AssetFeatureValueFactory.create(name='equity', assets=[t1, t2]) goal_settings = GoalSettingFactory.create() GoalMetricFactory.create( group=goal_settings.metric_group, feature=equity, type=GoalMetric.METRIC_TYPE_PORTFOLIO_MIX, rebalance_thr=0.05, configured_val=0.5, rebalance_type=GoalMetric.REBALANCE_TYPE_ABSOLUTE) goal = GoalFactory.create(active_settings=goal_settings) Fixture1.create_execution_details(goal, t1, 1, 2, date(2014, 6, 1)) Fixture1.create_execution_details(goal, t2, 1, 2, date(2014, 6, 1)) metric = GoalMetric.objects.get(group__settings__goal_active=goal) self.assertTrue(10.0 / goal.available_balance == metric.measured_val) self.assertTrue((metric.measured_val - metric.configured_val) / metric.rebalance_thr == metric.drift_score) metric.rebalance_type = GoalMetric.REBALANCE_TYPE_RELATIVE self.assertTrue(((metric.measured_val - metric.configured_val) / metric.configured_val) / metric.rebalance_thr \ == metric.drift_score)
def test_do_not_rebuy_within_30_days(self): # finish test GoalMetricFactory.create( group=self.goal_settings.metric_group, feature=self.equity, type=GoalMetric.METRIC_TYPE_RISK_SCORE, rebalance_type=GoalMetric.REBALANCE_TYPE_ABSOLUTE, rebalance_thr=0.5, configured_val=0.5) weights, instruments, reason = rebalance(self.goal, self.idata, self.data_provider, self.execution_provider) executed = datetime(year=2016, month=5, day=15) for i in range(3, 5): Fixture1.create_execution_details(self.goal, self.tickers[i], -self.quantities[i], self.tickers[i].unit_price, executed) self.goal.cash_balance += self.tickers[i].unit_price * abs( self.quantities[i]) weights, instruments, reason = rebalance(self.goal, self.idata, self.data_provider, self.execution_provider) self.assertAlmostEqual(weights[4], 0)
def test_perturbate_withdrawal(self): Fixture1.create_execution_details(self.goal, self.t4, self.goal.available_balance / 90, 90, date(2016, 1, 1)) TransactionFactory.create(from_goal=self.goal, status=Transaction.STATUS_PENDING, amount=self.goal.total_balance / 2) weights = perturbate_withdrawal(self.goal) self.assertTrue(sum(weights.values()) < 1)
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)
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)
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)
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)
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)
def test_get_goal_positions(self): goal = GoalFactory.create() url = '/api/v1/goals/{}/positions'.format(goal.pk) self.client.force_authenticate(goal.account.primary_owner.user) response = self.client.get(url) self.assertEqual( response.status_code, status.HTTP_200_OK, msg='Goal positions endpoint returns ok for Goal with no positions' ) # Create a 6 month old execution, transaction and a distribution that caused the transaction fund = TickerFactory.create(unit_price=2.1) fund2 = TickerFactory.create(unit_price=4) 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)) response = self.client.get(url) self.assertEqual( response.status_code, status.HTTP_200_OK, msg='Goal positions endpoint returns ok for Goal with positions') self.assertEqual(len(response.data), 2) self.assertDictEqual(response.data[0], { 'ticker': fund.id, 'quantity': 15.0 }) # Sum of the two fills self.assertDictEqual(response.data[1], { 'ticker': fund2.id, 'quantity': 1.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)
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)