Esempio n. 1
0
        def _calculation_of_raw_instrument_weights(system, NotUsed1, this_stage, 
                                      weighting_func, **weighting_params):
            
            print(__file__ + ":" + str(inspect.getframeinfo(inspect.currentframe())[:3][1]) + ":" +"Calculating raw instrument weights")

            instrument_codes=system.get_instrument_list()

            weight_func=weighting_func(log=this_stage.log.setup(call="weighting"), **weighting_params)
            if weight_func.need_data():
    
                if hasattr(system, "accounts"):
                    pandl=this_stage.pandl_across_subsystems()
                    (pandl_gross, pandl_costs) = decompose_group_pandl([pandl])
                    
                    weight_func.set_up_data(data_gross = pandl_gross, data_costs = pandl_costs)

                else:
                    error_msg="You need an accounts stage in the system to estimate instrument weights"
                    this_stage.log.critical(error_msg)

            else:
                ## equal weights doesn't need data

                positions=this_stage._get_all_subsystem_positions()                
                weight_func.set_up_data(weight_matrix=positions)

            SR_cost_list = [this_stage.get_instrument_subsystem_SR_cost(instr_code) for instr_code in instrument_codes]

            weight_func.optimise(ann_SR_costs=SR_cost_list)
        
            return weight_func
Esempio n. 2
0
        def _calculation_of_raw_forecast_weights(system, instrument_code, this_stage, 
                                      codes_to_use, weighting_func, pool_costs=False, **weighting_params):

            this_stage.log.terse("Calculating raw forecast weights over %s" % ", ".join(codes_to_use))

            if hasattr(system, "accounts"):
                ## returns a list of accountCurveGroups
                pandl_forecasts=[this_stage.pandl_for_instrument_rules_unweighted(code)
                        for code in codes_to_use]
                
                ## the current curve is special
                pandl_forecasts_this_code=this_stage.pandl_for_instrument_rules_unweighted(instrument_code)
                
                ## have to decode these
                ## returns two lists of pd.DataFrames
                (pandl_forecasts_gross, pandl_forecasts_costs) = decompose_group_pandl(pandl_forecasts, pandl_forecasts_this_code, pool_costs=pool_costs)
                
            else:
                error_msg="You need an accounts stage in the system to estimate forecast weights"
                this_stage.log.critical(error_msg)

            ## The weighting function requires two lists of pd.DataFrames, one gross, one for costs
            output=weighting_func(pandl_forecasts_gross, pandl_forecasts_costs,  
                                  log=self.log.setup(call="weighting"), **weighting_params)

            return output
Esempio n. 3
0
        def _calculation_of_raw_instrument_weights(system, NotUsed1, this_stage, 
                                      weighting_func, **weighting_params):
            
            this_stage.log.terse("Calculating raw instrument weights")

            instrument_codes=system.get_instrument_list()

            weight_func=weighting_func(log=this_stage.log.setup(call="weighting"), **weighting_params)
            if weight_func.need_data():
    
                if hasattr(system, "accounts"):
                    pandl=this_stage.pandl_across_subsystems()
                    (pandl_gross, pandl_costs) = decompose_group_pandl([pandl])
                    
                    weight_func.set_up_data(data_gross = pandl_gross, data_costs = pandl_costs)

                else:
                    error_msg="You need an accounts stage in the system to estimate instrument weights"
                    this_stage.log.critical(error_msg)

            else:
                ## equal weights doesn't need data

                positions=this_stage._get_all_subsystem_positions()                
                weight_func.set_up_data(weight_matrix=positions)

            SR_cost_list = [this_stage.get_instrument_subsystem_SR_cost(instr_code) for instr_code in instrument_codes]

            weight_func.optimise(ann_SR_costs=SR_cost_list)
        
            return weight_func
Esempio n. 4
0
        def _calculation_of_raw_forecast_weights(system, instrument_code,
                                                 this_stage, codes_to_use,
                                                 weighting_func, pool_costs,
                                                 **weighting_params):

            this_stage.log.terse(
                "Calculating raw forecast weights for %s, over %s" %
                (instrument_code, ", ".join(codes_to_use)))

            rule_list = self.apply_cost_weighting(instrument_code)

            weight_func = weighting_func(log=self.log.setup(call="weighting"),
                                         **weighting_params)

            if weight_func.need_data():

                # returns a list of accountCurveGroups
                pandl_forecasts = [
                    this_stage.get_returns_for_optimisation(code)
                    for code in codes_to_use
                ]

                # the current curve is special
                pandl_forecasts_this_code = this_stage.get_returns_for_optimisation(
                    instrument_code)

                # have to decode these
                # returns two lists of pd.DataFrames
                (pandl_forecasts_gross,
                 pandl_forecasts_costs) = decompose_group_pandl(
                     pandl_forecasts,
                     pandl_forecasts_this_code,
                     pool_costs=pool_costs)

                # The weighting function requires two lists of pd.DataFrames,
                # one gross, one for costs

                weight_func.set_up_data(data_gross=pandl_forecasts_gross,
                                        data_costs=pandl_forecasts_costs)
            else:
                # in the case of equal weights, don't need data

                forecasts = this_stage.get_all_forecasts(
                    instrument_code, rule_list)
                weight_func.set_up_data(weight_matrix=forecasts)

            SR_cost_list = [
                this_stage.get_SR_cost_for_instrument_forecast(
                    instrument_code, rule_variation_name)
                for rule_variation_name in rule_list
            ]

            weight_func.optimise(ann_SR_costs=SR_cost_list)

            return weight_func
Esempio n. 5
0
    def _calculation_of_fully_pooled_raw_forecast_weights(self, codes_to_use):
        """
        Estimate forecast weights for several instruments, using returns that are pooled
        (both costs and gross returns)

        :param codes_to_use: list of instrument_code

        :returns: TxK pd.DataFrame containing weights, columns are trading rule variation names, T covers all
        """


        # Get some useful stuff from the config
        weighting_params = copy(self.parent.config.forecast_weight_estimate)

        # which function to use for calculation
        weighting_func = resolve_function(weighting_params.pop("func"))

        instrument_code_ref = "_".join(codes_to_use)

        self.log.terse(
            "Calculating pooled raw forecast weights over instruments: %s" %
            instrument_code_ref)

        # an example of a weighting function is syscore.optimisation.GenericOptimiser
        # FIXME: change the way log is passed to a 'parent' style
        weight_func = weighting_func(
            log=self.log.setup(
                call="weighting"),
            **weighting_params)

        # returns a list of accountCurveGroups
        # cost pooling will already have been applied - huh?

        pandl_forecasts = [self.get_returns_for_optimisation(code)
                           for code in codes_to_use]

        # have to split these into gross and net costs
        # returns two lists of pd.DataFrames

        # FIXME: WHY do this? Instead get the optimiser to do it??
        (pandl_forecasts_gross, pandl_forecasts_costs) = decompose_group_pandl(
            pandl_forecasts, pool_costs=True)

        # The weighting function requires two lists of pd.DataFrames,
        # one gross, one for costs

        weight_func.set_up_data(
            data_gross=pandl_forecasts_gross,
            data_costs=pandl_forecasts_costs)

        weight_func.optimise()

        return weight_func
Esempio n. 6
0
        def _calculation_of_pooled_raw_forecast_weights(
                system, instrument_code_ref, this_stage, codes_to_use,
                weighting_func, **weighting_params):

            print(
                __file__ + ":" +
                str(inspect.getframeinfo(inspect.currentframe())[:3][1]) +
                ":" +
                "Calculating pooled raw forecast weights over instruments: %s"
                % instrument_code_ref)

            rule_list = self.apply_cost_weighting(instrument_code)

            weight_func = weighting_func(log=self.log.setup(call="weighting"),
                                         **weighting_params)
            if weight_func.need_data():

                ## returns a list of accountCurveGroups
                ## cost pooling will already have been applied

                pandl_forecasts = [
                    this_stage.get_returns_for_optimisation(code)
                    for code in codes_to_use
                ]

                ## have to decode these
                ## returns two lists of pd.DataFrames
                (pandl_forecasts_gross,
                 pandl_forecasts_costs) = decompose_group_pandl(
                     pandl_forecasts, pool_costs=True)

                ## The weighting function requires two lists of pd.DataFrames, one gross, one for costs

                weight_func.set_up_data(data_gross=pandl_forecasts_gross,
                                        data_costs=pandl_forecasts_costs)
            else:
                ## in the case of equal weights, don't need data

                forecasts = this_stage.get_all_forecasts(
                    instrument_code, rule_list)
                weight_func.set_up_data(weight_matrix=forecasts)

            SR_cost_list = [
                this_stage.get_SR_cost_for_instrument_forecast(
                    instrument_code, rule_variation_name)
                for rule_variation_name in rule_list
            ]

            weight_func.optimise(ann_SR_costs=SR_cost_list)

            return weight_func
Esempio n. 7
0
        def _calculation_of_raw_forecast_weights(system, instrument_code, this_stage,
                                                 codes_to_use, weighting_func, pool_costs, **weighting_params):

            this_stage.log.terse(
                "Calculating raw forecast weights for %s, over %s" %
                (instrument_code, ", ".join(codes_to_use)))

            rule_list = self.apply_cost_weighting(instrument_code)

            weight_func = weighting_func(
                log=self.log.setup(
                    call="weighting"),
                **weighting_params)

            if weight_func.need_data():

                # returns a list of accountCurveGroups
                pandl_forecasts = [this_stage.get_returns_for_optimisation(code)
                                   for code in codes_to_use]

                # the current curve is special
                pandl_forecasts_this_code = this_stage.get_returns_for_optimisation(
                    instrument_code)

                # have to decode these
                # returns two lists of pd.DataFrames
                (pandl_forecasts_gross, pandl_forecasts_costs) = decompose_group_pandl(
                    pandl_forecasts, pandl_forecasts_this_code, pool_costs=pool_costs)

                # The weighting function requires two lists of pd.DataFrames,
                # one gross, one for costs

                weight_func.set_up_data(
                    data_gross=pandl_forecasts_gross,
                    data_costs=pandl_forecasts_costs)
            else:
                # in the case of equal weights, don't need data

                forecasts = this_stage.get_all_forecasts(
                    instrument_code, rule_list)
                weight_func.set_up_data(weight_matrix=forecasts)

            SR_cost_list = [this_stage.get_SR_cost_for_instrument_forecast(instrument_code, rule_variation_name)
                            for rule_variation_name in rule_list]

            weight_func.optimise(ann_SR_costs=SR_cost_list)

            return weight_func
Esempio n. 8
0
        def _calculation_of_raw_instrument_weights(system, NotUsed1, this_stage, 
                                      weighting_func, **weighting_params):
            
            this_stage.log.terse("Calculating raw instrument weights")

            instrument_codes=system.get_instrument_list()
            if hasattr(system, "accounts"):
                pandl=this_stage.pandl_across_subsystems()
            else:
                error_msg="You need an accounts stage in the system to estimate instrument weights"
                this_stage.log.critical(error_msg)

            (pandl_gross, pandl_costs) = decompose_group_pandl([pandl])

            instrument_weight_results=weighting_func(pandl_gross, pandl_costs,  log=self.log.setup(call="weighting"), **weighting_params)
        
            return instrument_weight_results
Esempio n. 9
0
        def _calculation_of_pooled_raw_forecast_weights(system, instrument_code_ref, this_stage,
                                                        codes_to_use, weighting_func, **weighting_params):

            this_stage.log.terse(
                "Calculating pooled raw forecast weights over instruments: %s" %
                instrument_code_ref)

            rule_list = self.apply_cost_weighting(instrument_code)

            weight_func = weighting_func(
                log=self.log.setup(
                    call="weighting"),
                **weighting_params)
            if weight_func.need_data():

                # returns a list of accountCurveGroups
                # cost pooling will already have been applied

                pandl_forecasts = [this_stage.get_returns_for_optimisation(code)
                                   for code in codes_to_use]

                # have to decode these
                # returns two lists of pd.DataFrames
                (pandl_forecasts_gross, pandl_forecasts_costs) = decompose_group_pandl(
                    pandl_forecasts, pool_costs=True)

                # The weighting function requires two lists of pd.DataFrames,
                # one gross, one for costs

                weight_func.set_up_data(
                    data_gross=pandl_forecasts_gross,
                    data_costs=pandl_forecasts_costs)
            else:
                # in the case of equal weights, don't need data

                forecasts = this_stage.get_all_forecasts(
                    instrument_code, rule_list)
                weight_func.set_up_data(weight_matrix=forecasts)

            SR_cost_list = [this_stage.get_SR_cost_for_instrument_forecast(instrument_code, rule_variation_name)
                            for rule_variation_name in rule_list]

            weight_func.optimise(ann_SR_costs=SR_cost_list)

            return weight_func
Esempio n. 10
0
    def calculation_of_raw_instrument_weights(self):
        """
        Estimate the instrument weights

        Done like this to expose calculations

        :returns: TxK pd.DataFrame containing weights, columns are instrument names, T covers all

        """

        # Get some useful stuff from the config
        weighting_params = copy(self.parent.config.instrument_weight_estimate)

        # which function to use for calculation
        weighting_func = resolve_function(weighting_params.pop("func"))

        system = self.parent

        self.log.terse("Calculating raw instrument weights")

        instrument_codes = system.get_instrument_list()

        weight_func = weighting_func(log=self.log.setup(call="weighting"),
                                     **weighting_params)

        if hasattr(system, "accounts"):
            pandl = self.pandl_across_subsystems()
            (pandl_gross, pandl_costs) = decompose_group_pandl([pandl])

            weight_func.set_up_data(data_gross=pandl_gross,
                                    data_costs=pandl_costs)

        else:
            error_msg = "You need an accounts stage in the system to estimate instrument weights"
            self.log.critical(error_msg)

        SR_cost_list = [
            self.get_instrument_subsystem_SR_cost(instr_code)
            for instr_code in instrument_codes
        ]

        weight_func.optimise()

        return weight_func
Esempio n. 11
0
        def _calculation_of_raw_forecast_weights(system,
                                                 instrument_code,
                                                 this_stage,
                                                 codes_to_use,
                                                 weighting_func,
                                                 pool_costs=False,
                                                 **weighting_params):

            this_stage.log.terse("Calculating raw forecast weights over %s" %
                                 ", ".join(codes_to_use))

            if hasattr(system, "accounts"):
                ## returns a list of accountCurveGroups
                pandl_forecasts = [
                    this_stage.pandl_for_instrument_rules_unweighted(code)
                    for code in codes_to_use
                ]

                ## the current curve is special
                pandl_forecasts_this_code = this_stage.pandl_for_instrument_rules_unweighted(
                    instrument_code)

                ## have to decode these
                ## returns two lists of pd.DataFrames
                (pandl_forecasts_gross,
                 pandl_forecasts_costs) = decompose_group_pandl(
                     pandl_forecasts,
                     pandl_forecasts_this_code,
                     pool_costs=pool_costs)

            else:
                error_msg = "You need an accounts stage in the system to estimate forecast weights"
                this_stage.log.critical(error_msg)

            ## The weighting function requires two lists of pd.DataFrames, one gross, one for costs
            output = weighting_func(pandl_forecasts_gross,
                                    pandl_forecasts_costs,
                                    log=self.log.setup(call="weighting"),
                                    **weighting_params)

            return output
Esempio n. 12
0
        def _calculation_of_raw_instrument_weights(system, NotUsed1,
                                                   this_stage, weighting_func,
                                                   **weighting_params):

            this_stage.log.terse("Calculating raw instrument weights")

            instrument_codes = system.get_instrument_list()
            if hasattr(system, "accounts"):
                pandl = this_stage.pandl_across_subsystems()
            else:
                error_msg = "You need an accounts stage in the system to estimate instrument weights"
                this_stage.log.critical(error_msg)

            (pandl_gross, pandl_costs) = decompose_group_pandl([pandl])

            instrument_weight_results = weighting_func(
                pandl_gross,
                pandl_costs,
                log=self.log.setup(call="weighting"),
                **weighting_params)

            return instrument_weight_results
Esempio n. 13
0
    def _calculation_of_raw_forecast_weights_for_instrument(
            self, instrument_code):
        """
        Does an optimisation for a single instrument

        We do this if we can't do the special case of a fully pooled optimisation (both costs and returns pooled)

        Estimate the forecast weights for this instrument

        We store this intermediate step to expose the calculation object

        :param instrument_code:
        :type str:

        :returns: TxK pd.DataFrame containing weights, columns are trading rule variation names, T covers all
        """

        # usual pool fix
        # Get some useful stuff from the config
        weighting_params = copy(self.parent.config.forecast_weight_estimate)

        # do we pool our estimation? (note if both are pooled would better calling the function for full pooling
        # eithier gross returns or costs can be pooled
        pooling_returns = str2Bool(
            self.parent.config.forecast_weight_estimate["pool_gross_returns"])
        pool_costs = str2Bool(
            self.parent.config.forecast_cost_estimates["use_pooled_costs"])


        # which function to use for calculation
        weighting_func = resolve_function(weighting_params.pop("func"))

        # FIXME: Returns and costs are pooled in different places, very confusing
        if pooling_returns:
            # find set of instruments with same trading rules as I have
            codes_to_use = self.has_same_cheap_rules_as_code(instrument_code)
        else:
            codes_to_use = [instrument_code]

        self.log.terse(
            "Calculating raw forecast weights for %s, over %s" %
            (instrument_code, ", ".join(codes_to_use)))

        rule_list = self.check_for_cheap_enough_rules(instrument_code)

        # FIXME: change the way log is passed to a 'parent' style
        weight_func = weighting_func(
            log=self.log.setup(
                call="weighting"),
            **weighting_params)


        # returns a list of accountCurveGroups
        pandl_forecasts = [self.get_returns_for_optimisation(code)
                           for code in codes_to_use]

        # the current curve is special
        # FIXME couldn't the optimiser do this?:
        pandl_forecasts_this_code = self.get_returns_for_optimisation(
            instrument_code)

        # have to decode these
        # returns two lists of pd.DataFrames
        # FIXME: WHY do this? Instead get the optimiser to do it??

        (pandl_forecasts_gross, pandl_forecasts_costs) = decompose_group_pandl(
            pandl_forecasts, pandl_forecasts_this_code, pool_costs=pool_costs)

        # The weighting function requires two lists of pd.DataFrames,
        # one gross, one for costs

        weight_func.set_up_data(
            data_gross=pandl_forecasts_gross,
            data_costs=pandl_forecasts_costs)

        weight_func.optimise()

        return weight_func