def populate_returns(self): t1 = TickerFactory.create() p1 = [ 1.01, 1.02, 1.03, 1.04, 1.03, 1.05, 1.03, 1.03, 1.04, 1.05, 1.06, 1.07, 1.09, 1.11, 1.13, 1.12 ] t2 = TickerFactory.create() p2 = [ 6.01, 5.92, 6.03, 6.04, 5.93, 5.95, 5.93, 5.87, 5.84, 5.80, 5.78, 5.74, 5.79, 5.76, 5.73, 5.7 ] t3 = TickerFactory.create() p3 = [ 101.01, 101.02, 100.03, 101.04, 101.63, 102.05, 102.03, 101.93, 102.34, 102.55, 103, 102.67, 102.9, 103.11, 102.23, 101.12 ] days = [1, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 18, 19, 20, 21, 22] for i, day in enumerate(days): dt = date(2016, 1, day) DailyPriceFactory.create(instrument=t1.benchmark, date=dt, price=p1[i]) DailyPriceFactory.create(instrument=t2.benchmark, date=dt, price=p2[i]) DailyPriceFactory.create(instrument=t3.benchmark, date=dt, price=p3[i])
def setUp(self): self.support_group = GroupFactory(name=GROUP_SUPPORT_STAFF) self.personal_account_type = AccountType.objects.create( id=ACCOUNT_TYPE_PERSONAL) self.r401k_account_type = AccountType.objects.create( id=ACCOUNT_TYPE_ROTH401K) self.bonds_type = InvestmentType.Standard.BONDS.get() self.stocks_type = InvestmentType.Standard.STOCKS.get() self.bonds_asset_class = AssetClassFactory.create( investment_type=self.bonds_type) self.stocks_asset_class = AssetClassFactory.create( investment_type=self.stocks_type) self.portfolio_set = PortfolioSetFactory.create() self.portfolio_set.asset_classes.add(self.bonds_asset_class, self.stocks_asset_class) self.risk_score_metric = { "type": GoalMetric.METRIC_TYPE_RISK_SCORE, "comparison": GoalMetric.METRIC_COMPARISON_EXACTLY, "configured_val": 0.4, "rebalance_type": GoalMetric.REBALANCE_TYPE_RELATIVE, "rebalance_thr": 0.1 } self.bonds_index = MarketIndexFactory.create() self.stocks_index = MarketIndexFactory.create() self.bonds_ticker = TickerFactory.create( asset_class=self.bonds_asset_class, benchmark=self.bonds_index) self.stocks_ticker = TickerFactory.create( asset_class=self.stocks_asset_class, benchmark=self.stocks_index)
def setUp(self): # ticker checks django contenttype model for some reason so # we have to manage this in fixtures a little, have to be unique per model 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()) self.bonds_ticker = TickerFactory.create(asset_class=self.bonds_asset_class, benchmark_content_type=self.content_type) self.stocks_ticker = TickerFactory.create(asset_class=self.stocks_asset_class, benchmark_content_type=self.content_type)
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 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')
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')
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_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)
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')
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_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) fund1 = TickerFactory.create(symbol='ITOT', asset_class=asset_class2) fund0 = TickerFactory.create(symbol='IPO') 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)
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)
def test_reduce_cash(self): goal1 = Fixture1.goal1() goal1.cash_balance = 1000 cash_available = goal1.cash_balance ticker = TickerFactory.create() ticker.latest_tick = 2 volume = 2000 cash_available, volume = reduce_cash(volume, ticker, cash_available) self.assertAlmostEqual(cash_available, 1.03) self.assertTrue(volume == 497)
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 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_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)
def test_ticker_price(self): ticker = TickerFactory.create() populate_prices(days=500) price = ticker.unit_price self.assertTrue(price == 10) price_object = DailyPrice.objects\ .filter(instrument_object_id=ticker.id, instrument_content_type=ContentType.objects.get_for_model(ticker))\ .order_by('-date')\ .first() ticker.update_latest_tick(price_object.price) ticker = Ticker.objects.get(id=ticker.id) self.assertTrue(ticker.latest_tick == price_object.price)
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)
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 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)
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
def setUp(self): self.login = get_current_login() self.ticker = TickerFactory.create(symbol='GOOG')
def setUp(self): self.con = IBBroker() self.con.connect() self.ticker = TickerFactory.create(symbol='GOOG')