Exemplo n.º 1
0
def get_optimised_positions_data_dict_given_optimisation(
    data_for_objective: dataForObjectiveInstance,
    objective_function: objectiveFunctionForGreedy,
) -> dict:

    optimised_positions = objective_function.optimise_positions()
    optimised_positions = optimised_positions.replace_weights_with_ints()

    optimised_position_weights = get_weights_given_positions(
        optimised_positions,
        per_contract_value=data_for_objective.per_contract_value)
    instrument_list = list(optimised_position_weights.keys())

    minima_weights = portfolioWeights.from_weights_and_keys(
        list_of_keys=instrument_list,
        list_of_weights=list(objective_function.minima_as_np),
    )
    maxima_weights = portfolioWeights.from_weights_and_keys(
        list_of_keys=instrument_list,
        list_of_weights=list(objective_function.maxima_as_np),
    )
    starting_weights = portfolioWeights.from_weights_and_keys(
        list_of_keys=instrument_list,
        list_of_weights=list(objective_function.starting_weights_as_np),
    )

    data_dict = dict([(
        instrument_code,
        get_optimal_position_entry_with_calcs_for_code(
            instrument_code=instrument_code,
            data_for_objective=data_for_objective,
            optimised_position_weights=optimised_position_weights,
            optimised_positions=optimised_positions,
            maxima_weights=maxima_weights,
            starting_weights=starting_weights,
            minima_weights=minima_weights,
        ),
    ) for instrument_code in instrument_list])

    return data_dict
Exemplo n.º 2
0
def optimise_from_processed_estimates(
        estimates: Estimates) -> portfolioWeights:
    stdev_list = estimates.stdev_list
    corrmatrix = estimates.correlation_matrix
    mean_list = estimates.mean_list
    list_of_asset_names = estimates.asset_names

    sigma = sigma_from_corr_and_std(stdev_list=stdev_list,
                                    corrmatrix=corrmatrix)

    weights = optimise_from_sigma_and_mean_list(sigma, mean_list=mean_list)

    portfolio_weights = portfolioWeights.from_weights_and_keys(
        list_of_weights=weights, list_of_keys=list_of_asset_names)

    return portfolio_weights
Exemplo n.º 3
0
def clean_weights(weights: portfolioWeights,
                  must_haves: dict,
                  fraction: float = 0.5) -> portfolioWeights:

    asset_names, list_of_weights, list_of_must_haves = \
        get_lists_from_dicts_of_weights_and_must_haves(weights = weights, must_haves = must_haves)

    cleaned_list_of_weights = clean_list_of_weights(
        weights=list_of_weights,
        must_haves=list_of_must_haves,
        fraction=fraction)

    cleaned_weights = portfolioWeights.from_weights_and_keys(
        list_of_weights=cleaned_list_of_weights, list_of_keys=asset_names)

    return cleaned_weights
Exemplo n.º 4
0
def calculate_contract_positions_from_weights_and_values(
        optimal_weights: portfolioWeights,
        per_contract_value: portfolioWeights) -> portfolioWeights:

    instrument_list = list(optimal_weights.keys())
    optimal_weights_as_np = np.array(
        optimal_weights.as_list_given_keys(instrument_list))
    per_contract_value_as_np = np.array(
        per_contract_value.as_list_given_keys(instrument_list))

    positions_as_np = optimal_weights_as_np / per_contract_value_as_np

    positions = portfolioWeights.from_weights_and_keys(positions_as_np,
                                                       instrument_list)

    return positions
Exemplo n.º 5
0
    def optimise_weights(self) -> portfolioWeights:
        optimal_weights_without_missing_items_as_np = self.optimise_np_for_valid_keys(
        )

        optimal_weights_without_missing_items_as_list = list(
            optimal_weights_without_missing_items_as_np)

        optimal_weights = portfolioWeights.from_weights_and_keys(
            list_of_keys=self.keys_with_valid_data,
            list_of_weights=optimal_weights_without_missing_items_as_list,
        )

        optimal_weights_for_all_keys = (
            optimal_weights.with_zero_weights_for_missing_keys(
                list(self.weights_optimal.keys())))

        return optimal_weights_for_all_keys
Exemplo n.º 6
0
def adjust_weights_for_SR_on_handcrafted_portfolio(
        raw_weights: portfolioWeights,
        handcraft_portfolio: handcraftPortfolio) -> portfolioWeights:

    SR_list = handcraft_portfolio.sharpe_ratio
    avg_correlation = handcraft_portfolio.avg_correlation
    years_of_data = handcraft_portfolio.data_length_years
    asset_names = handcraft_portfolio.asset_names

    weights_as_list = raw_weights.as_list_given_keys(asset_names)

    weights = adjust_weights_for_SR(SR_list=SR_list,
                                    avg_correlation=avg_correlation,
                                    years_of_data=years_of_data,
                                    weights_as_list=weights_as_list)

    weights = portfolioWeights.from_weights_and_keys(list_of_weights=weights,
                                                     list_of_keys=asset_names)

    return weights