Exemplo n.º 1
0
            try:
                # Black Litterman output (HMM views included)
                r_pred = ss.get('returns', 'predicted')
                covariance_pred = ss.get('covariance', 'predicted')
                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
                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
                if mode == 'c':
                    scns = 1
                else:
                    scns = 5
                bl_mps_policy = cp.MultiPeriodScenarioOpt(
Exemplo n.º 2
0
                      end_date=end_date)['Adj. Close'])
          for ticker in tickers]))

returns = prices.pct_change()
returns[["USDOLLAR"]] = quandl.get(
    'FRED/DTB3', start_date=start_date, end_date=end_date) / (250 * 100)
returns = returns.fillna(method='ffill').iloc[1:]
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
Exemplo n.º 3
0
    # Equilibrium results
    start_date = datetime.strptime(bl_ewm_model.cfg['start_date'], '%Y%m%d') + \
                 timedelta(days=bl_ewm_model.cfg['train_len'] * 1.75)
    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
Exemplo n.º 4
0
        try:
            r_pred = ss.get('returns', 'predicted')
            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)