Example #1
0
    def fitness_exp(strategy, params):

        strategy_weights = weight.weights(datasets_dict['Sectors'], strategy,
                                          params['NEUTRALIZATION'],
                                          params['LONG LEVERAGE'],
                                          params['SHORT LEVERAGE'])

        #        print("check")

        #Simple Checks
        empty = np.full_like(strategy_weights, np.nan)
        check = ((strategy_weights == empty) |
                 (np.isnan(strategy_weights) & np.isnan(empty))).all()
        if check:
            strategy_weights = np.full_like(strategy_weights, 0)

        non_nan_frac = np.count_nonzero(~np.isnan(strategy_weights)) / np.size(
            strategy_weights)
        if non_nan_frac <= 0.6:
            strategy_weights = np.full_like(strategy_weights, 0)

        clean_values_from_weights = wb.get_valid_index(strategy_weights)
        cleaned_index_weights = (clean_index.values)[clean_values_from_weights]
        cleaned_strategy_weights = strategy_weights[clean_values_from_weights]

        strategy_log_returns, strategy_daily_returns = wb.bets_to_pnl(
            params['STARTING VALUE'], strategy_weights,
            clean_values_from_weights, base_data, params['LONG LEVERAGE'],
            params['SHORT LEVERAGE'], params['ADV THRESHOLD PERCENTAGE'],
            params['COST THRESHOLD BPS'], params['COMMISSION BPS'])

        def equity_curve(amount, ret):
            ret = hf.shift_array(ret, 1, 0)
            return amount * np.cumprod(1 + ret)

        def MDD(returns):
            def returns_to_dollars(amount, ret):
                return amount * np.cumprod(1 + ret)

            doll_series = pd.Series(returns_to_dollars(100, returns))

            Roll_Max = doll_series.cummax()
            Daily_Drawdown = doll_series / Roll_Max - 1.0
            Max_Daily_Drawdown = Daily_Drawdown.cummin()
            return Max_Daily_Drawdown.min()

        if np.sum(strategy_daily_returns) == 0:
            return 0
        else:
            yrs = int(len(cleaned_index_weights)) / 252
            curves = dict()
            curves['Strategy'] = equity_curve(100, strategy_daily_returns)
            cagr_strategy = ((
                (curves['Strategy'][-1] / curves['Strategy'][0])**(1 / yrs)) -
                             1)
            mdd_strat = MDD(strategy_log_returns)
            fitness = (cagr_strategy) / abs(mdd_strat)
            return fitness
    def fitness_exp(strategy, params):

        strategy_weights = weight.weights(datasets_dict['Sectors'], strategy,
                                          params['NEUTRALIZATION'],
                                          params['LONG LEVERAGE'],
                                          params['SHORT LEVERAGE'])

        #        print("check")

        #Simple Checks
        empty = np.full_like(strategy_weights, np.nan)
        check = ((strategy_weights == empty) |
                 (np.isnan(strategy_weights) & np.isnan(empty))).all()
        if check:
            strategy_weights = np.full_like(strategy_weights, 0)

        non_nan_frac = np.count_nonzero(~np.isnan(strategy_weights)) / np.size(
            strategy_weights)
        if non_nan_frac <= 0.6:
            strategy_weights = np.full_like(strategy_weights, 0)

        clean_values_from_weights = wb.get_valid_index(strategy_weights)
        cleaned_index_weights = (clean_index.values)[clean_values_from_weights]
        cleaned_strategy_weights = strategy_weights[clean_values_from_weights]

        strategy_log_returns, strategy_daily_returns = wb.bets_to_pnl(
            params['STARTING VALUE'], strategy_weights,
            clean_values_from_weights, base_data, params['LONG LEVERAGE'],
            params['SHORT LEVERAGE'], params['ADV THRESHOLD PERCENTAGE'],
            params['COST THRESHOLD BPS'], params['COMMISSION BPS'])

        if np.sum(clean_values_from_weights) != 0:
            rf_returns = datasets_dict['rf_rate'][clean_values_from_weights]
        else:
            rf_returns = datasets_dict['rf_rate']

    #    cleaned_index = cleaned_index_weights

        if np.sum(strategy_daily_returns) == 0:
            return 0
        else:
            excess_returns = strategy_log_returns - rf_returns
            left_threshold = np.percentile(excess_returns, 5)
            right_threshold = np.percentile(excess_returns, 95)
            CVAR_left = -1 * (np.nanmean(
                excess_returns[excess_returns <= left_threshold]))
            CVAR_right = (np.nanmean(
                excess_returns[excess_returns >= right_threshold]))
            fitness = CVAR_right / CVAR_left
            return fitness
    def fitness_exp(strategy, params):

        strategy_weights = weight.weights(datasets_dict['Sectors'], strategy,
                                          params['NEUTRALIZATION'],
                                          params['LONG LEVERAGE'],
                                          params['SHORT LEVERAGE'])

        #        print("check")

        #Simple Checks
        empty = np.full_like(strategy_weights, np.nan)
        check = ((strategy_weights == empty) |
                 (np.isnan(strategy_weights) & np.isnan(empty))).all()
        if check:
            strategy_weights = np.full_like(strategy_weights, 0)

        non_nan_frac = np.count_nonzero(~np.isnan(strategy_weights)) / np.size(
            strategy_weights)
        if non_nan_frac <= 0.6:
            strategy_weights = np.full_like(strategy_weights, 0)

        clean_values_from_weights = wb.get_valid_index(strategy_weights)
        cleaned_index_weights = (clean_index.values)[clean_values_from_weights]
        cleaned_strategy_weights = strategy_weights[clean_values_from_weights]

        strategy_log_returns, strategy_daily_returns = wb.bets_to_pnl(
            params['STARTING VALUE'], strategy_weights,
            clean_values_from_weights, base_data, params['LONG LEVERAGE'],
            params['SHORT LEVERAGE'], params['ADV THRESHOLD PERCENTAGE'],
            params['COST THRESHOLD BPS'], params['COMMISSION BPS'])

        if np.sum(clean_values_from_weights) != 0:
            rf_returns = datasets_dict['rf_rate'][clean_values_from_weights]
        else:
            rf_returns = datasets_dict['rf_rate']

    #    cleaned_index = cleaned_index_weights

        if np.sum(strategy_daily_returns) == 0:
            return 0
        else:
            excess_returns = strategy_daily_returns - rf_returns
            returns_less_thresh = excess_returns - (((100)**(1 / 252)) - 1)
            numer = sum(returns_less_thresh[returns_less_thresh > 0.0])
            denom = -1.0 * sum(returns_less_thresh[returns_less_thresh < 0.0])
            fitness = numer / denom
            return fitness
    def fitness_exp(strategy, params):

        strategy_weights = weight.weights(datasets_dict['Sectors'], strategy,
                                          params['NEUTRALIZATION'],
                                          params['LONG LEVERAGE'],
                                          params['SHORT LEVERAGE'])

        #        print("check")

        #Simple Checks
        empty = np.full_like(strategy_weights, np.nan)
        check = ((strategy_weights == empty) |
                 (np.isnan(strategy_weights) & np.isnan(empty))).all()
        if check:
            strategy_weights = np.full_like(strategy_weights, 0)

        non_nan_frac = np.count_nonzero(~np.isnan(strategy_weights)) / np.size(
            strategy_weights)
        if non_nan_frac <= 0.6:
            strategy_weights = np.full_like(strategy_weights, 0)

        clean_values_from_weights = wb.get_valid_index(strategy_weights)
        cleaned_index_weights = (clean_index.values)[clean_values_from_weights]
        cleaned_strategy_weights = strategy_weights[clean_values_from_weights]

        strategy_log_returns, strategy_daily_returns = wb.bets_to_pnl(
            params['STARTING VALUE'], strategy_weights,
            clean_values_from_weights, base_data, params['LONG LEVERAGE'],
            params['SHORT LEVERAGE'], params['ADV THRESHOLD PERCENTAGE'],
            params['COST THRESHOLD BPS'], params['COMMISSION BPS'])

        if np.sum(clean_values_from_weights) != 0:
            benchmark_returns = datasets_dict['benchmark'][
                clean_values_from_weights]
        else:
            benchmark_returns = datasets_dict['benchmark'][
                clean_values_from_weights]

    #    cleaned_index = cleaned_index_weights

        if np.sum(strategy_daily_returns) == 0:
            return 0
        else:
            excess_returns_benchmark = strategy_log_returns - benchmark_returns
            fitness = mth.sqrt(252) * np.mean(
                excess_returns_benchmark) / np.std(excess_returns_benchmark)
            return fitness