def process(data_provider, execution_provider, delay): # actually tickers are created here - we need to set proper asset class for each ticker goals = Goal.objects.all() #get_markowitz_scale(self) #MarkowitzScaleFactory.create() data_provider.get_goals() build_instruments(data_provider) # optimization fails due to #portfolios_stats = calculate_portfolios(setting=goal.selected_settings, # data_provider=data_provider, # execution_provider=execution_provider) #portfolio_stats = calculate_portfolio(settings=goal.selected_settings, # data_provider=data_provider, # execution_provider=execution_provider) for goal in goals: weights, instruments, reason = rebalance( idata=get_instruments(data_provider), goal=goal, data_provider=data_provider, execution_provider=execution_provider) new_positions = build_positions(goal, weights, instruments) # create sell requests first mor, requests = create_request(goal, new_positions, reason, execution_provider=execution_provider, data_provider=data_provider, allowed_side=-1) approve_mor(mor) # process sells execute(delay) for goal in goals: # now create buys - but only use cash to finance proceeds of buys mor, requests = create_request(goal, new_positions, reason, execution_provider=execution_provider, data_provider=data_provider, allowed_side=1) approve_mor(mor) execute(delay)
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_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)
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 test_backtest(self): setup = TestSetup() # actually tickers are created here - we need to set proper asset class for each ticker self.create_goal() setup.create_goal(self.goal) setup.data_provider.initialize_tickers() setup.data_provider.move_date_forward() backtester = Backtester() print("backtesting " + str(setup.data_provider.get_current_date())) build_instruments(setup.data_provider) # optimization fails due to portfolios_stats = calculate_portfolios( setting=setup.goal.selected_settings, data_provider=setup.data_provider, execution_provider=setup.execution_provider) portfolio_stats = calculate_portfolio( settings=setup.goal.selected_settings, data_provider=setup.data_provider, execution_provider=setup.execution_provider) weights, instruments, reason = rebalance( idata=get_instruments(setup.data_provider), goal=setup.goal, data_provider=setup.data_provider, execution_provider=setup.execution_provider) new_positions = build_positions(setup.goal, weights, instruments) # create sell requests first mor, requests = create_request( setup.goal, new_positions, reason, execution_provider=setup.execution_provider, data_provider=setup.data_provider, allowed_side=-1) # process sells backtester.execute(mor) # now create buys - but only use cash to finance proceeds of buys mor, requests = create_request( setup.goal, new_positions, reason, execution_provider=setup.execution_provider, data_provider=setup.data_provider, allowed_side=1) backtester.execute(mor) transaction_cost = np.sum([abs(r.volume) for r in requests]) * 0.005 # So, the rebalance could not be in place if the excecution algo might not determine how much it will cost to rebalance. # this does not work - make it work with Django execution provider - use EtnaOrders #performance = backtester.calculate_performance(execution_provider=setup.execution_provider) self.assertTrue(True)
def setUp(self): Region.objects.create(name="AU") Region.objects.create(name="UK") Region.objects.create(name="US") AssetClass.objects.create(name="US_BONDS", investment_type=InvestmentType.Standard.BONDS.get(), display_order=1) AssetClass.objects.create(name="AU_STOCKS", investment_type=InvestmentType.Standard.STOCKS.get(), display_order=2) AssetClass.objects.create(name="AU_STOCK_MUTUALS", investment_type=InvestmentType.Standard.STOCKS.get(), display_order=3) index = MarketIndex.objects.create(id=1, display_name='ASSIndex', region=Region.objects.get(name="AU"), currency='AUD', data_api='bloomberg', data_api_param='MI1') ass = Ticker.objects.create(symbol="ASS", display_name='AU Stock 1', ethical=False, region=Region.objects.get(name="AU"), asset_class=AssetClass.objects.get(name='AU_STOCKS'), ordering=1, description='some stock', benchmark=index, data_api='portfolios.api.bloomberg', data_api_param='ASS') ubs = Ticker.objects.create(symbol="USB", display_name='US Bond 1', ethical=True, region=Region.objects.get(name="US"), asset_class=AssetClass.objects.get(name='US_BONDS'), ordering=1, description='some stock', benchmark=index, data_api='portfolios.api.bloomberg', data_api_param='USB') usb1 = Ticker.objects.create(symbol="USB1", display_name='US Bond 2', ethical=False, region=Region.objects.get(name="US"), asset_class=AssetClass.objects.get(name='US_BONDS'), ordering=1, description='some stock', benchmark=index, data_api='portfolios.api.bloomberg', data_api_param='USB1') aums = Ticker.objects.create(symbol="AUMS", display_name='AU Mutual Stocks 1', ethical=True, region=Region.objects.get(name="AU"), asset_class=AssetClass.objects.get(name='AU_STOCK_MUTUALS'), etf=False, ordering=1, description='some stock', benchmark=index, data_api='portfolios.api.bloomberg', data_api_param='AUMS') self._data_provider = DataProviderDjango() self._execution_provider = ExecutionProviderDjango() self._covars, self._samples, self._instruments, self._masks = build_instruments(self._data_provider) MarkowitzScale.objects.create(date=self._data_provider.get_current_date(), min=-1, max=1, a=1, b=2, c=3)
def instruments_setup(self): self._covars, self._samples, self._instruments, self._masks = build_instruments( self.data_provider)
capitalization[capitalization==0]=np.nan capitalization.fillna('ffill') dataClose.to_csv('fundPrices.csv') capitalization.to_csv('capitalization.csv') ''' setup = TestSetup() backtester = Backtester() requests = [setup.execution_provider.create_empty_market_order()] while setup.data_provider.move_date_forward(): print("backtesting " + str(setup.data_provider.get_current_date())) build_instruments(setup.data_provider) ''' # execute orders from yesterday backtester.place_order(settings=setup.goal, order=requests, data_provider=setup.data_provider, execution_provider=setup.execution_provider) ''' # calculate current portfolio stats portfolios_stats = calculate_portfolios( setting=setup.goal.active_settings, data_provider=setup.data_provider, execution_provider=setup.execution_provider) portfolio_stats = calculate_portfolio(
def process(data_provider, execution_provider, delay, goals=None): # actually tickers are created here - we need to set proper asset class for each ticker if goals is None: goals = Goal.objects.filter(state=Goal.State.ACTIVE.value) #get_markowitz_scale(self) #MarkowitzScaleFactory.create() data_provider.get_goals() build_instruments(data_provider) # optimization fails due to #portfolios_stats = calculate_portfolios(setting=goal.selected_settings, # data_provider=data_provider, # execution_provider=execution_provider) #portfolio_stats = calculate_portfolio(settings=goal.selected_settings, # data_provider=data_provider, # execution_provider=execution_provider) goals_list = [] for goal in goals: weights, instruments, reason = rebalance( idata=get_instruments(data_provider), goal=goal, data_provider=data_provider, execution_provider=execution_provider) new_positions = build_positions(goal, weights, instruments) goals_list.append({'goal': goal, 'new_positions': new_positions}) if settings.DEBUG: print('---------------------------------------------------') print('weights:', weights) print('instruments:', instruments) for item in goals_list: goal = item['goal'] new_positions = item['new_positions'] mor, requests = create_request(goal, new_positions, reason, execution_provider=execution_provider, data_provider=data_provider, allowed_side=-1) if settings.DEBUG: print('---------------------------------------------------') print(requests) print('>>>>>>>>>>>>', 'goal:', 'state->', goal.state, 'account->', goal.account, 'name->', goal.name, 'portfolio_set->', goal.portfolio_set, 'cash_balance->', goal.cash_balance) print('new_positions:', new_positions) approve_mor(mor) # process sells execute(delay) for item in goals_list: goal = item['goal'] new_positions = item['new_positions'] # now create buys - but only use cash to finance proceeds of buys mor, requests = create_request(goal, new_positions, reason, execution_provider=execution_provider, data_provider=data_provider, allowed_side=1) approve_mor(mor) # process buys execute(delay)