Ejemplo n.º 1
0
    end_date = datetime.strptime(bl_ewm_model.cfg['end_date'], '%Y%m%d')

    w_equal = pd.Series(index=r_pred.columns, data=[1] * len(r_pred.columns))
    w_equal.loc['USDOLLAR'] = 0.
    w_equal = w_equal / sum(w_equal)

    optimization_tcost = cp.TcostModel(half_spread=0.0005/2., nonlin_coeff=1.,
                                       sigma=sigmas_pred,
                                       volume=volumes_pred)
    optimization_hcost=cp.HcostModel(borrow_costs=0.0001)

    if bl_ewm_model.cfg['covariance']['method'] == 'SS':
        spo_risk_model = cp.FullSigma(bl_ewm_model.get('covariance', 'predicted'))
    elif bl_ewm_model.cfg['covariance']['method'] == 'FF5':
        spo_risk_model = cp.FactorModelSigma(bl_ewm_model.get('exposures', 'predicted'),
                                             bl_ewm_model.get('factor_sigma', 'predicted'),
                                             bl_ewm_model.get('idyos', 'predicted'))
    else:
        raise NotImplemented('The %s risk model is not implemented yet'.format(bl_ewm_model.cfg['risk']))

    logging.warning('Running simulation...')
    # Optimization parameters
    gamma_risk, gamma_trade, gamma_hold = 5., 15., 1.
    leverage_limit = cp.LeverageLimit(1)
    fully_invested = cp.ZeroCash()
    long_only = cp.LongOnly()

    # Optimization policy
    c_mps_policy = cp.MultiPeriodScenarioOpt(alphamodel=bl_ewm_model, horizon=5, scenarios=5,
                                             scenario_mode='c', costs=[gamma_risk*spo_risk_model,
                                                                       gamma_trade*optimization_tcost,
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
 
 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):
     fx = np.empty((m, ))
     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()]
Ejemplo n.º 4
0
            volumes_pred = ss.get('volumes', 'predicted')
            sigmas_pred = ss.get('sigmas', 'predicted')

            # Predicted costs
            optimization_tcost = cp.TcostModel(half_spread=0.0005 / 2.,
                                               nonlin_coeff=1.,
                                               sigma=sigmas_pred,
                                               volume=volumes_pred)
            optimization_hcost = cp.HcostModel(borrow_costs=0.0001)

            # Covariance setup
            if ss.cfg['covariance']['method'] == 'SS':
                risk_model = cp.FullSigma(ss.get('covariance', 'predicted'))
            elif ss.cfg['covariance']['method'] == 'FF5':
                risk_model = cp.FactorModelSigma(
                    ss.get('exposures', 'predicted'),
                    ss.get('factor_sigma', 'predicted'),
                    ss.get('idyos', 'predicted'))
            else:
                raise NotImplemented(
                    'The %s risk model is not implemented yet'.format(
                        ss.cfg['covariance']))

            # Black Litterman policy
            logging.warning('Running backtest')

            # Optimization parameters
            leverage_limit = cp.LeverageLimit(1)
            fully_invested = cp.ZeroCash()
            long_only = cp.LongOnly()

            # Optimization policy