Exemplo n.º 1
0
 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()]
     for k in results.keys():
         returns = results[k].excess_returns.to_numpy()
     for j in range(m):
         fx[j] = np.sum(returns[251*j:251*(j+1)])
     return fx
                # Covariance setup
                bl_risk_model = cp.FullSigma(covariance_pred)

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

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

                # Optimization policy
                bl_spo_policy = cp.SinglePeriodOpt(return_forecast=r_pred,
                                                   costs=[grisk * bl_risk_model,
                                                          gtrd * optimization_tcost,
                                                          gamma_hold * optimization_hcost],
                                                   constraints=[leverage_limit, fully_invested, long_only],
                                                   trading_freq='hour')

                # Backtest
                blu_results = simulator.run_multiple_backtest(1E6*w_mktcap, start_time=start_date,  end_time=end_date,
                                                              policies=[bl_spo_policy],
                                                              loglevel=logging.WARNING, parallel=True)
                result = blu_results[0]
                logging.warning(result.summary())

                # Save down metrics together with parameters
                prtf_vs_params[key] = [vconf, grisk, gtrd,
                                       result.excess_returns.mean() * 100 * result.ppy,
                                       result.excess_returns.std() * 100 * np.sqrt(result.ppy),
                                       result.max_drawdown * 100,
Exemplo n.º 3
0
factor_loadings = pd.get_dummies(pd.Series(np.random.randint(0, 6, 100)))

r_hat_s = pd.Series(np.random.random(100))
r_hat_s.T['USDOLLAR'] = 0
r_hat = pd.DataFrame(r_hat_s, columns=[pd.Timestamp('2017-01-03')]).T

prices_s = pd.Series(np.random.randint(20, 75, 100))
prices_noUSD = pd.DataFrame(prices_s, columns=[pd.Timestamp('2017-01-03')]).T
prices_s.T['USDOLLAR'] = 1.0
prices = pd.DataFrame(prices_s, columns=[pd.Timestamp('2017-01-03')]).T

spo_policy = cp.SinglePeriodOpt(
    return_forecast=r_hat,
    costs=[],
    constraints=[
        cp.LeverageLimit(1),
        cp.constraints.DollarNeutral(),
        cp.constraints.MaxWeights(0.10),
        cp.constraints.MinWeights(-0.10),
        #cp.FactorMaxLimit(factor_loadings, 1.0),
        #cp.FactorMinLimit(factor_loadings, -1.0)
    ])

current_portfolio = pd.Series(index=r_hat.columns, data=0)
current_portfolio.USDOLLAR = 10000
#import pdb; pdb.set_trace()
shares_to_trade = spo_policy.get_rounded_trades(current_portfolio,
                                                prices,
                                                t=pd.Timestamp('2017-01-04'))
Exemplo n.º 4
0
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_model.h5')
risk_model = cp.FactorModelSigma(risk_data.exposures, risk_data.factor_sigma, risk_data.idyos)

results={}

t = datetime(2012,2,1)
print(w_b.shape)
print(locator(risk_model.exposures, t).shape)
print(locator(risk_model.factor_Sigma, t).shape)
print(locator(risk_model.idiosync, t).shape)

policies={}
gamma_risks_coarse=[.1,.3,1,3,10,30,100,300,1000]
gamma_tcosts_coarse=[1,2,5,10,20]
for gamma_risk in gamma_risks_coarse:
    for gamma_tcost in gamma_tcosts_coarse :
        policies[(gamma_risk, gamma_tcost)] = \
        cp.SinglePeriodOpt(return_estimate, [gamma_risk*risk_model,gamma_tcost*optimization_tcost,optimization_hcost],
                                [cp.LeverageLimit(3)])

import warnings
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))))
Exemplo n.º 5
0
returns = returns[-251:]

r_hat = returns.rolling(window=250, min_periods=250).mean().shift(1).dropna()
Sigma_hat = returns.rolling(window=250, min_periods=250).cov().dropna()

tcost_model = cp.TcostModel(half_spread=10E-4)
hcost_model = cp.HcostModel(borrow_costs=1E-4)

risk_model = cp.FullSigma(Sigma_hat)
gamma_risk, gamma_trade, gamma_hold = 5., 1.5, 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])

# mpo_policy = cp.MultiPeriodOpt

current_portfolio = pd.Series(index=r_hat.columns, data=0)
current_portfolio.USDOLLAR = 10000

t = pd.to_datetime('2018-03-27', infer_datetime_format=True)
# t = pd.datetime.today()
shares_to_trade = spo_policy.get_rounded_trades(current_portfolio, prices, t)
print(shares_to_trade)

pd.DataFrame({
Exemplo n.º 6
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
risk_data = pd.HDFStore(datadir+'risk_model.h5')
risk_model = cp.FactorModelSigma(risk_data.exposures, risk_data.factor_sigma, risk_data.idyos)

results={}

# %% [markdown]
# # SPO coarse search

# %%
policies={}
gamma_risks_coarse=[.1,.3,1,3,10,30,100,300,1000]
gamma_tcosts_coarse=[1,2,5,10,20]
for gamma_risk in gamma_risks_coarse:
    for gamma_tcost in gamma_tcosts_coarse : 
        policies[(gamma_risk, gamma_tcost)] =       cp.SinglePeriodOpt(return_estimate, [gamma_risk*risk_model,gamma_tcost*optimization_tcost,optimization_hcost], 
                                [cp.LeverageLimit(3)])

import warnings
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))))


# %%
result_df_coarse=pd.DataFrame()
for k in results:
    if k[0] in gamma_risks_coarse and k[1] in gamma_tcosts_coarse:
        result_df_coarse.loc[k[0], k[1]] = results[k]
        
result_df = result_df_coarse.loc[sorted(result_df_coarse.index), sorted(result_df_coarse.columns)]