Esempio n. 1
0
        def _get_instrument_weights(system, an_ignored_variable, this_stage):

            print(__file__ + ":" +
                  str(inspect.getframeinfo(inspect.currentframe())[:3][1]) +
                  ":" + "Calculating instrument weights")

            raw_instr_weights = this_stage.get_raw_instrument_weights()
            instrument_list = list(raw_instr_weights.columns)

            subsys_positions = [
                this_stage.get_subsystem_position(instrument_code)
                for instrument_code in instrument_list
            ]

            subsys_positions = pd.concat(subsys_positions, axis=1).ffill()
            subsys_positions.columns = instrument_list

            instrument_weights = fix_weights_vs_pdm(raw_instr_weights,
                                                    subsys_positions)

            weighting = system.config.instrument_weight_ewma_span

            # smooth
            instrument_weights = pd.ewma(instrument_weights, weighting)

            return instrument_weights
Esempio n. 2
0
        def _get_instrument_weights(system, notUsed, this_stage):

            this_stage.log.msg("Getting instrument weights")

            raw_instr_weights = this_stage.get_raw_instrument_weights()
            instrument_list = list(raw_instr_weights.columns)

            subsys_positions = [
                this_stage.get_subsystem_position(code)
                for code in instrument_list
            ]

            subsys_positions = pd.concat(subsys_positions, axis=1).ffill()
            subsys_positions.columns = instrument_list

            instrument_weights = fix_weights_vs_pdm(raw_instr_weights,
                                                    subsys_positions)

            weighting_params = copy(system.config.instrument_weight_estimate)

            # smooth
            instrument_weights = pd.ewma(instrument_weights,
                                         weighting_params['ewma_span'])

            return instrument_weights
    def get_instrument_weights(self):
        """
        Get the time series of instrument weights, accounting for potentially missing positions, and weights that don't add up.

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


        """

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

        if self.use_estimated_instrument_weights():
            raw_instr_weights = self.get_raw_estimated_instrument_weights()
        else:
            raw_instr_weights = self.get_raw_fixed_instrument_weights()

        instrument_list = list(raw_instr_weights.columns)

        subsys_positions = [
            self.get_subsystem_position(instrument_code)
            for instrument_code in instrument_list
        ]

        subsys_positions = pd.concat(subsys_positions, axis=1).ffill()
        subsys_positions.columns = instrument_list

        instrument_weights = fix_weights_vs_pdm(raw_instr_weights,
                                                subsys_positions)

        smooth_weighting = self.parent.config.instrument_weight_ewma_span

        # smooth
        instrument_weights = instrument_weights.ewm(smooth_weighting).mean()

        return instrument_weights
Esempio n. 4
0
    def get_instrument_weights(self):
        """
        Get the time series of instrument weights, accounting for potentially missing positions, and weights that don't add up.

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


        """

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

        if self.use_estimated_instrument_weights():
            raw_instr_weights = self.get_raw_estimated_instrument_weights()
        else:
            raw_instr_weights = self.get_raw_fixed_instrument_weights()

        instrument_list = list(raw_instr_weights.columns)

        subsys_positions = [
            self.get_subsystem_position(instrument_code)
            for instrument_code in instrument_list
        ]

        subsys_positions = pd.concat(subsys_positions, axis=1).ffill()
        subsys_positions.columns = instrument_list

        instrument_weights = fix_weights_vs_pdm(raw_instr_weights,
                                                subsys_positions)

        smooth_weighting = self.parent.config.instrument_weight_ewma_span

        # smooth
        instrument_weights = instrument_weights.ewm(smooth_weighting).mean()

        return instrument_weights
Esempio n. 5
0
        def _get_forecast_weights(system,  instrument_code,  this_stage ):
            
            forecast_weights=this_stage.get_raw_forecast_weights(instrument_code)
            rule_variation_list=list(forecast_weights.columns)
            forecasts=[this_stage.get_capped_forecast(instrument_code, rule_variation_name) for rule_variation_name in rule_variation_list]

            forecasts=pd.concat(forecasts, axis=1)
                
            ## adjust weights for missing data
            forecast_weights=fix_weights_vs_pdm(forecast_weights, forecasts)

            return forecast_weights
Esempio n. 6
0
        def _get_forecast_weights(system, instrument_code, this_stage):

            this_stage.log.msg("Calculating forecast weights for %s" % (instrument_code),
                               instrument_code=instrument_code)

            forecast_weights = this_stage.get_raw_forecast_weights(
                instrument_code)
            rule_variation_list = list(forecast_weights.columns)
            forecasts = this_stage.get_all_forecasts(instrument_code, rule_variation_list)

            # adjust weights for missing data
            forecast_weights = fix_weights_vs_pdm(forecast_weights, forecasts)

            return forecast_weights
Esempio n. 7
0
        def _get_clean_instrument_weights(
                system, an_ignored_variable, this_stage):

            raw_instr_weights = this_stage.get_raw_instrument_weights()
            instrument_list = list(raw_instr_weights.columns)

            subsys_positions = [this_stage.get_subsystem_position(instrument_code)
                                for instrument_code in instrument_list]

            subsys_positions = pd.concat(subsys_positions, axis=1).ffill()
            subsys_positions.columns = instrument_list

            instrument_weights = fix_weights_vs_pdm(
                raw_instr_weights, subsys_positions)

            return instrument_weights
Esempio n. 8
0
        def _get_forecast_weights(system, instrument_code, this_stage):
            this_stage.log.msg("Calculating forecast weights for %s" % instrument_code,
                               instrument_code=instrument_code)

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

            forecast_weights = this_stage.get_raw_forecast_weights(
                instrument_code)
            rule_variation_list = list(forecast_weights.columns)
            forecasts = this_stage.get_all_forecasts(instrument_code, rule_variation_list)

            # adjust weights for missing data
            forecast_weights = fix_weights_vs_pdm(forecast_weights, forecasts)
            
            # smooth
            forecast_weights = pd.ewma(forecast_weights, weighting_params['ewma_span']) 

            return forecast_weights
Esempio n. 9
0
        def _get_forecast_weights(system, instrument_code, this_stage):

            print(__file__ + ":" + str(inspect.getframeinfo(inspect.currentframe())[:3][1]) + ":" +"Calculating forecast weights for %s" % (instrument_code))

            forecast_weights = this_stage.get_raw_forecast_weights(
                instrument_code)
            rule_variation_list = list(forecast_weights.columns)
            forecasts = this_stage.get_all_forecasts(instrument_code, rule_variation_list)

            # adjust weights for missing data
            # also aligns them together
            forecast_weights = fix_weights_vs_pdm(forecast_weights, forecasts)

            weighting=system.config.forecast_weight_ewma_span  

            # smooth
            forecast_weights = pd.ewma(forecast_weights, weighting) 

            return forecast_weights
Esempio n. 10
0
        def _get_clean_instrument_weights(system, an_ignored_variable,
                                          this_stage):

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

            raw_instr_weights = this_stage.get_raw_instrument_weights()
            instrument_list = list(raw_instr_weights.columns)

            subsys_positions = [
                this_stage.get_subsystem_position(instrument_code)
                for instrument_code in instrument_list
            ]

            subsys_positions = pd.concat(subsys_positions, axis=1).ffill()
            subsys_positions.columns = instrument_list

            instrument_weights = fix_weights_vs_pdm(raw_instr_weights,
                                                    subsys_positions)

            return instrument_weights
Esempio n. 11
0
        def _get_forecast_weights(system, instrument_code, this_stage):

            this_stage.log.msg("Calculating forecast weights for %s" % (instrument_code),
                               instrument_code=instrument_code)

            forecast_weights = this_stage.get_raw_forecast_weights(
                instrument_code)
            rule_variation_list = list(forecast_weights.columns)
            forecasts = this_stage.get_all_forecasts(instrument_code, rule_variation_list)

            # adjust weights for missing data
            # also aligns them together
            forecast_weights = fix_weights_vs_pdm(forecast_weights, forecasts)

            weighting=system.config.forecast_weight_ewma_span  

            # smooth
            forecast_weights = pd.ewma(forecast_weights, weighting) 

            return forecast_weights
Esempio n. 12
0
        def _get_instrument_weights(system, notUsed, this_stage):

            this_stage.log.msg("Getting instrument weights")

            raw_instr_weights = this_stage.get_raw_instrument_weights()
            instrument_list = list(raw_instr_weights.columns)

            subsys_positions = [this_stage.get_subsystem_position(code)
                                for code in instrument_list]

            subsys_positions = pd.concat(subsys_positions, axis=1).ffill()
            subsys_positions.columns = instrument_list

            instrument_weights = fix_weights_vs_pdm(
                raw_instr_weights, subsys_positions)

            weighting_params=copy(system.config.instrument_weight_estimate)  

            # smooth
            instrument_weights = pd.ewma(instrument_weights, weighting_params['ewma_span']) 

            return instrument_weights
Esempio n. 13
0
        def _get_forecast_weights(system, instrument_code, this_stage):

            this_stage.log.msg("Calculating forecast weights for %s" %
                               (instrument_code),
                               instrument_code=instrument_code)

            forecast_weights = this_stage.get_raw_forecast_weights(
                instrument_code)
            rule_variation_list = list(forecast_weights.columns)
            forecasts = this_stage.get_all_forecasts(instrument_code,
                                                     rule_variation_list)

            # adjust weights for missing data
            # also aligns them together
            forecast_weights = fix_weights_vs_pdm(forecast_weights, forecasts)

            weighting = system.config.forecast_weight_ewma_span

            # smooth
            forecast_weights = pd.ewma(forecast_weights, weighting)

            return forecast_weights
Esempio n. 14
0
        def _get_instrument_weights(
                system, an_ignored_variable, this_stage):

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

            raw_instr_weights = this_stage.get_raw_instrument_weights()
            instrument_list = list(raw_instr_weights.columns)

            subsys_positions = [this_stage.get_subsystem_position(instrument_code)
                                for instrument_code in instrument_list]

            subsys_positions = pd.concat(subsys_positions, axis=1).ffill()
            subsys_positions.columns = instrument_list

            instrument_weights = fix_weights_vs_pdm(
                raw_instr_weights, subsys_positions)

            weighting = system.config.instrument_weight_ewma_span

            # smooth
            instrument_weights = pd.ewma(instrument_weights, weighting)

            return instrument_weights
Esempio n. 15
0
        def _get_instrument_weights(
                system, an_ignored_variable, this_stage):

            print(__file__ + ":" + str(inspect.getframeinfo(inspect.currentframe())[:3][1]) + ":" +"Calculating instrument weights")

            raw_instr_weights = this_stage.get_raw_instrument_weights()
            instrument_list = list(raw_instr_weights.columns)

            subsys_positions = [this_stage.get_subsystem_position(instrument_code)
                                for instrument_code in instrument_list]

            subsys_positions = pd.concat(subsys_positions, axis=1).ffill()
            subsys_positions.columns = instrument_list

            instrument_weights = fix_weights_vs_pdm(
                raw_instr_weights, subsys_positions)

            weighting=system.config.instrument_weight_ewma_span  

            # smooth
            instrument_weights = pd.ewma(instrument_weights, weighting) 

            return instrument_weights
Esempio n. 16
0
        def _get_forecast_weights(system, instrument_code, this_stage):

            print(__file__ + ":" +
                  str(inspect.getframeinfo(inspect.currentframe())[:3][1]) +
                  ":" + "Calculating forecast weights for %s" %
                  (instrument_code))

            forecast_weights = this_stage.get_raw_forecast_weights(
                instrument_code)
            rule_variation_list = list(forecast_weights.columns)
            forecasts = this_stage.get_all_forecasts(instrument_code,
                                                     rule_variation_list)

            # adjust weights for missing data
            # also aligns them together
            forecast_weights = fix_weights_vs_pdm(forecast_weights, forecasts)

            weighting = system.config.forecast_weight_ewma_span

            # smooth
            forecast_weights = pd.ewma(forecast_weights, weighting)

            return forecast_weights
Esempio n. 17
0
    def get_forecast_weights(self, instrument_code):
        """
        Get the forecast weights

        We forward fill all forecasts. We then adjust forecast weights so that
          they are 1.0 in every period; after setting to zero when no forecast
          is available.

        :param instrument_code:
        :type str:

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

        KEY OUTPUT

        >>> from systems.tests.testdata import get_test_object_futures_with_rules_and_capping
        >>> from systems.basesystem import System
        >>> (fcs, rules, rawdata, data, config)=get_test_object_futures_with_rules_and_capping()
        >>> system=System([rawdata, rules, fcs, ForecastCombineFixed()], data, config)
        >>>
        >>> ## from config
        >>> system.combForecast.get_forecast_weights("EDOLLAR").tail(2)
                    ewmac16  ewmac8
        2015-12-10      0.5     0.5
        2015-12-11      0.5     0.5
        >>>
        >>> config.forecast_weights=dict(EDOLLAR=dict(ewmac8=0.9, ewmac16=0.1))
        >>> system2=System([rawdata, rules, fcs, ForecastCombineFixed()], data, config)
        >>> system2.combForecast.get_forecast_weights("EDOLLAR").tail(2)
                    ewmac16  ewmac8
        2015-12-10      0.1     0.9
        2015-12-11      0.1     0.9
        >>>
        >>> del(config.forecast_weights)
        >>> system3=System([rawdata, rules, fcs, ForecastCombineFixed()], data, config)
        >>> system3.combForecast.get_forecast_weights("EDOLLAR").tail(2)
        WARNING: No forecast weights  - using equal weights of 0.5000 over all 2 trading rules in system
                    ewmac16  ewmac8
        2015-12-10      0.5     0.5
        2015-12-11      0.5     0.5
        """
        self.log.msg("Calculating forecast weights for %s" % (instrument_code),
        instrument_code=instrument_code)

        # note these might include missing weights, eg too expensive, or absent
        # from fixed weights
        forecast_weights = self.get_raw_forecast_weights(instrument_code)

        # we get the rule variations from forecast_weight columns, as if we've dropped
        #   expensive rules (when estimating) then get_trading_rules will give the wrong answer
        rule_variation_list = list(forecast_weights.columns)
        forecasts = self.get_all_forecasts(
            instrument_code, rule_variation_list)

        # adjust weights for missing data
        # also aligns them together with forecasts
        forecast_weights = fix_weights_vs_pdm(forecast_weights, forecasts)

        weighting = self.parent.config.forecast_weight_ewma_span

        # smooth
        forecast_weights = forecast_weights.ewm(weighting).mean()
        return forecast_weights