ss.train(force=True) # Realized Data for Simulation prices = ss.get('prices', 'realized', ss.cfg['returns']['sampling_freq']).iloc[1:, :] returns = ss.get('returns', 'realized', ss.cfg['returns']['sampling_freq']) volumes = ss.get('volumes', 'realized', ss.cfg['returns']['sampling_freq']) sigmas = ss.get('sigmas', 'realized', ss.cfg['returns']['sampling_freq']) simulated_tcost = cp.TcostModel(half_spread=0.0005 / 2., nonlin_coeff=1., sigma=sigmas, volume=volumes) simulated_hcost = cp.HcostModel(borrow_costs=0.0001) simulator = cp.MarketSimulator(returns, costs=[simulated_tcost, simulated_hcost], market_volumes=volumes, cash_key=ss.risk_free_symbol) # ## 2. Black Litterman HMM Risk Rewards Frontier # Aggregate market stats for cal market_stats = pd.DataFrame( { 'MarketCap/GDP': [1.25, 1, 1.25, 0.45, 3.5, 0.8, 2, 1.25, 0.3, 0], 'GDP': [ 2543500, 150000, 239000, 853000, 22500, 1037500, 10000, 422500, 164500, 0 ] }, index=ss.universe + ['USDOLLAR']) market_stats.loc[:,
def _evaluate_with_unscaled_input(self, gamma_risk, gamma_tcost, gamma_holding, bid_ask_spread, borrow_cost): # Set up requirements for portfolio simulation self.experiment_id = str(int(torch.randint(1, 100000, torch.Size([1])))) sigmas = pd.read_csv(self.datadir + "sigmas.csv.gz", index_col=0, parse_dates=[0]).iloc[:, :-1] returns = pd.read_csv(self.datadir + "returns.csv.gz", index_col=0, parse_dates=[0]) volumes = pd.read_csv(self.datadir + "volumes.csv.gz", index_col=0, parse_dates=[0]).iloc[:, :-1] w_b = pd.Series(index=returns.columns, data=1) w_b.USDOLLAR = 0.0 w_b /= sum(w_b) start_t = "2012-01-01" end_t = "2016-12-31" simulated_tcost = cp.TcostModel( half_spread=bid_ask_spread / 2.0, nonlin_coeff=1.0, sigma=sigmas, volume=volumes, ) simulated_hcost = cp.HcostModel(borrow_costs=borrow_cost) simulator = cp.MarketSimulator( returns, costs=[simulated_tcost, simulated_hcost], market_volumes=volumes, cash_key="USDOLLAR", ) return_estimate = pd.read_csv(self.datadir + "return_estimate.csv.gz", index_col=0, parse_dates=[0]).dropna() volume_estimate = pd.read_csv(self.datadir + "volume_estimate.csv.gz", index_col=0, parse_dates=[0]).dropna() sigma_estimate = pd.read_csv(self.datadir + "sigma_estimate.csv.gz", index_col=0, parse_dates=[0]).dropna() optimization_tcost = cp.TcostModel( half_spread=bid_ask_spread / 2.0, nonlin_coeff=1.0, sigma=sigma_estimate, volume=volume_estimate, ) optimization_hcost = cp.HcostModel(borrow_costs=borrow_cost) copy_of_risk_model_name = "risk_model_" + self.experiment_id + ".h5" subprocess.call([ "bash", self.script_dir + "/make_copy_of_risk_model.sh", self.datadir + "risk_model.h5", self.datadir + copy_of_risk_model_name, ]) risk_data = pd.HDFStore(self.datadir + copy_of_risk_model_name) risk_model = cp.FactorModelSigma(risk_data.exposures, risk_data.factor_sigma, risk_data.idyos) results = {} policies = {} policies[(gamma_risk, gamma_tcost, gamma_holding)] = cp.SinglePeriodOpt( return_estimate, [ gamma_risk * risk_model, gamma_tcost * optimization_tcost, gamma_holding * optimization_hcost, ], [cp.LeverageLimit(3)], ) warnings.filterwarnings("ignore") results.update( dict( zip( policies.keys(), simulator.run_multiple_backtest( 1e8 * w_b, start_time=start_t, end_time=end_t, policies=policies.values(), parallel=True, ), ))) results_df = pd.DataFrame() results_df[r"$\gamma^\mathrm{risk}$"] = [ el[0] for el in results.keys() ] results_df[r"$\gamma^\mathrm{trade}$"] = [ el[1] for el in results.keys() ] results_df[r"$\gamma^\mathrm{hold}$"] = [ "%g" % el[2] for el in results.keys() ] results_df["Return"] = [ results[k].excess_returns for k in results.keys() ] for k in results.keys(): returns = results[k].excess_returns.to_numpy() returns = returns[:-1] subprocess.call([ "bash", self.script_dir + "/delete_copy_of_risk_model.sh", self.datadir + copy_of_risk_model_name, ]) return np.mean(returns) * 100 * 250
datadir= script_dir + '/portfolio_test_data/' sigmas=pd.read_csv(datadir+'sigmas.csv.gz',index_col=0,parse_dates=[0]).iloc[:,:-1] returns=pd.read_csv(datadir+'returns.csv.gz',index_col=0,parse_dates=[0]) volumes=pd.read_csv(datadir+'volumes.csv.gz',index_col=0,parse_dates=[0]).iloc[:,:-1] w_b = pd.Series(index=returns.columns, data=1) w_b.USDOLLAR = 0. w_b/=sum(w_b) start_t = "2012-01-01" end_t = "2016-12-31" simulated_tcost = cp.TcostModel(half_spread=0.0005/2., nonlin_coeff=1., sigma=sigmas, volume=volumes) simulated_hcost = cp.HcostModel(borrow_costs=0.0001) simulator = cp.MarketSimulator(returns, costs=[simulated_tcost, simulated_hcost], market_volumes=volumes, cash_key='USDOLLAR') return_estimate=pd.read_csv(datadir+'return_estimate.csv.gz',index_col=0,parse_dates=[0]).dropna() volume_estimate=pd.read_csv(datadir+'volume_estimate.csv.gz',index_col=0,parse_dates=[0]).dropna() sigma_estimate=pd.read_csv(datadir+'sigma_estimate.csv.gz',index_col=0,parse_dates=[0]).dropna() optimization_tcost = cp.TcostModel(half_spread=0.0005/2., nonlin_coeff=1., sigma=sigma_estimate, volume=volume_estimate) optimization_hcost=cp.HcostModel(borrow_costs=0.0001) risk_data = pd.HDFStore(datadir + 'risk_model4.h5') risk_model = cp.FactorModelSigma(risk_data.exposures, risk_data.factor_sigma, risk_data.idyos) d = 3 m = 5 def f_unnormalized_inputs(gamma_risk, gamma_tcost, gamma_holding):
gamma_risk, gamma_trade, gamma_hold = 5., 1., 1. leverage_limit = cp.LeverageLimit(3) spo_policy = cp.SinglePeriodOpt(return_forecast=r_hat, costs=[ gamma_risk * risk_model, gamma_trade * tcost_model, gamma_hold * hcost_model ], constraints=[leverage_limit]) # %% [markdown] # We run a backtest, which returns a result object. By calling its summary method we get some basic statistics. # %% market_sim = cp.MarketSimulator(returns, [tcost_model, hcost_model], cash_key='USDOLLAR') init_portfolio = pd.Series(index=returns.columns, data=250000.) init_portfolio.USDOLLAR = 0 results = market_sim.run_multiple_backtest(init_portfolio, start_time='2013-01-03', end_time='2016-12-31', policies=[spo_policy, cp.Hold()]) results[0].summary() # %% [markdown] # The total value of the portfolio in time. # %% results[0].v.plot(figsize=(12, 5)) results[1].v.plot(figsize=(12, 5))