Exemplo n.º 1
0
    def create_scenario(weights_bandwidth=0.02,
                        target_vol=0.01,
                        method='risk_neutral'):
        industry_names = industry_list('sw_adj', 1)
        constraint_risk = [
            'EARNYILD', 'LIQUIDTY', 'GROWTH', 'SIZE', 'BETA', 'MOMENTUM'
        ]
        total_risk_names = constraint_risk + industry_names + [
            'benchmark', 'total'
        ]

        b_type = []
        l_val = []
        u_val = []

        for name in total_risk_names:
            if name == 'benchmark':
                b_type.append(BoundaryType.RELATIVE)
                l_val.append(0.8)
                u_val.append(1.001)
            elif name == 'total':
                b_type.append(BoundaryType.ABSOLUTE)
                l_val.append(-0.001)
                u_val.append(.001)
            elif name == 'EARNYILD':
                b_type.append(BoundaryType.ABSOLUTE)
                l_val.append(-0.001)
                u_val.append(0.60)
            elif name == 'GROWTH':
                b_type.append(BoundaryType.ABSOLUTE)
                l_val.append(-0.20)
                u_val.append(0.20)
            elif name == 'MOMENTUM':
                b_type.append(BoundaryType.ABSOLUTE)
                l_val.append(-0.10)
                u_val.append(0.20)
            elif name == 'SIZE':
                b_type.append(BoundaryType.ABSOLUTE)
                l_val.append(-0.20)
                u_val.append(0.20)
            elif name == 'LIQUIDTY':
                b_type.append(BoundaryType.ABSOLUTE)
                l_val.append(-0.25)
                u_val.append(0.25)
            else:
                b_type.append(BoundaryType.ABSOLUTE)
                l_val.append(-0.01)
                u_val.append(0.01)

        bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)
        running_setting = RunningSetting(weights_bandwidth=weights_bandwidth,
                                         rebalance_method=method,
                                         bounds=bounds,
                                         target_vol=target_vol,
                                         turn_over_target=0.4)

        ret_df, positions = strategy.run(running_setting)
        return ret_df
Exemplo n.º 2
0
    def test_create_box_bounds_single_value(self):
        names = ['a', 'b', 'c']
        b_type = BoundaryType.RELATIVE
        l_val = 0.8
        u_val = 1.1

        bounds = create_box_bounds(names, b_type, l_val, u_val)

        for key, bound in bounds.items():
            l_bound = bound.lower
            u_bound = bound.upper
            self.assertEqual(l_bound.b_type, b_type)
            self.assertEqual(u_bound.b_type, b_type)
            self.assertAlmostEqual(l_bound.val, l_val)
            self.assertAlmostEqual(u_bound.val, u_val)
Exemplo n.º 3
0
    def test_create_box_bounds_multiple_values(self):
        names = ['a', 'b', 'c']
        b_type = BoundaryType.RELATIVE
        l_val = [0.9, 0.8, 1.1]
        u_val = [1.1, 1.2, 1.3]

        bounds = create_box_bounds(names, b_type, l_val, u_val)

        for i, name in enumerate(names):
            bound = bounds[name]
            l_bound = bound.lower
            u_bound = bound.upper
            self.assertEqual(l_bound.b_type, b_type)
            self.assertEqual(u_bound.b_type, b_type)
            self.assertAlmostEqual(l_bound.val, l_val[i])
            self.assertAlmostEqual(u_bound.val, u_val[i])
Exemplo n.º 4
0
    def test_linear_constraints(self):
        cons_mat = np.random.randn(100, 3)
        backbone = np.random.randn(100)
        names = ['a', 'b', 'c']
        cons_mat = pd.DataFrame(cons_mat, columns=names)

        b_type = BoundaryType.ABSOLUTE
        l_val = -0.8
        u_val = 1.1

        bounds = create_box_bounds(names, b_type, l_val, u_val)

        constraints = LinearConstraints(bounds=bounds,
                                        cons_mat=cons_mat,
                                        backbone=backbone)

        l_bounds, u_bounds = constraints.risk_targets()
        risk_exp = constraints.risk_exp

        for i, name in enumerate(names):
            center = risk_exp[:, i] @ backbone
            self.assertAlmostEqual(center + l_val, l_bounds[i])
            self.assertAlmostEqual(center + u_val, u_bounds[i])
Exemplo n.º 5
0
    for name in total_risk_names:
        if name == 'total':
            b_type.append(BoundaryType.ABSOLUTE)
            l_val.append(.0)
            u_val.append(.0)
        elif name == 'benchmark':
            b_type.append(BoundaryType.RELATIVE)
            l_val.append(0.8)
            u_val.append(1.0)
        else:
            b_type.append(BoundaryType.MAXABSREL)
            l_val.append((0.00, 0.0))
            u_val.append((0.00, 0.0))

    bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)

    running_setting = RunningSetting(lbound=None,
                                     ubound=None,
                                     weights_bandwidth=0.01,
                                     rebalance_method='risk_neutral',
                                     bounds=bounds,
                                     target_vol=0.05,
                                     turn_over_target=0.4)

    strategy = Strategy(alpha_model,
                        data_meta,
                        universe=universe,
                        start_date=start_date,
                        end_date=end_date,
                        freq=freq,