def get_raw_fixed_instrument_weights(self) -> pd.DataFrame: """ Get the instrument weights These are 'raw' because we need to account for potentially missing positions, and weights that don't add up. From: (a) passed into subsystem when created (b) ... if not found then: in system.config.instrument_weights :returns: TxK pd.DataFrame containing weights, columns are instrument names, T covers all subsystem positions >>> from systems.tests.testdata import get_test_object_futures_with_pos_sizing >>> from systems.basesystem import System >>> (posobject, combobject, capobject, rules, rawdata, data, config)=get_test_object_futures_with_pos_sizing() >>> config.instrument_weights=dict(EDOLLAR=0.1, US10=0.9) >>> system=System([rawdata, rules, posobject, combobject, capobject,Portfolios()], data, config) >>> >>> ## from config >>> system.portfolio.get_instrument_weights().tail(2) EDOLLAR US10 2015-12-10 0.1 0.9 2015-12-11 0.1 0.9 >>> >>> del(config.instrument_weights) >>> system2=System([rawdata, rules, posobject, combobject, capobject,Portfolios()], data, config) >>> system2.portfolio.get_instrument_weights().tail(2) WARNING: No instrument weights - using equal weights of 0.3333 over all 3 instruments in data BUND EDOLLAR US10 2015-12-10 0.333333 0.333333 0.333333 2015-12-11 0.333333 0.333333 0.333333 """ self.log.msg("Calculating raw instrument weights") try: instrument_weights_dict = self.config.instrument_weights except: instrument_weights_dict = self.get_equal_instrument_weights_dict() instrument_weights_dict = self._add_zero_instrument_weights( instrument_weights_dict ) # Now we have a dict, fixed_weights. # Need to turn into a timeseries covering the range of subsystem positions instrument_list = self.get_instrument_list() subsystem_positions = self._get_all_subsystem_positions() position_series_index = subsystem_positions.index # CHANGE TO TXN DATAFRAME instrument_weights = from_dict_of_values_to_df( instrument_weights_dict, position_series_index, columns=instrument_list ) return instrument_weights
def get_raw_fixed_forecast_weights(self, instrument_code): """ Get the forecast weights for this instrument From: (a) passed into subsystem when created (b) ... if not found then: in system.config.instrument_weights :param instrument_code: :type str: :returns: TxK pd.DataFrame containing weights, columns are trading rule variation names, T covers all >>> 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_raw_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_raw_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_raw_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 """ system = self.parent # Let's try the config if "forecast_weights" in dir(system.config): if instrument_code in system.config.forecast_weights: # nested dict fixed_weights = system.config.forecast_weights[ instrument_code] else: # assume it's a non nested dict fixed_weights = system.config.forecast_weights else: rules = self.get_trading_rule_list(instrument_code) equal_weight = 1.0 / len(rules) warn_msg = "WARNING: No forecast weights - using equal weights of %.4f over all %d trading rules in system" % ( equal_weight, len(rules)) self.log.warn(warn_msg, instrument_code=instrument_code) fixed_weights = dict([(rule_name, equal_weight) for rule_name in rules]) # Now we have a dict, fixed_weights. # Need to turn into a timeseries covering the range of forecast # dates rule_variation_list = sorted(fixed_weights.keys()) forecasts_ts = self.get_all_forecasts( instrument_code, rule_variation_list) forecast_weights = from_dict_of_values_to_df(fixed_weights, forecasts_ts.index, columns = forecasts_ts.columns) return forecast_weights
def get_raw_fixed_forecast_weights(self, instrument_code: str) -> pd.DataFrame: """ Get the forecast weights for this instrument From: (a) passed into subsystem when created (b) ... if not found then: in system.config.instrument_weights :param instrument_code: :type str: :returns: TxK pd.DataFrame containing weights, columns are trading rule variation names, T covers all >>> 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_raw_monthly_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_raw_monthly_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_raw_monthly_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 """ # Now we have a dict, fixed_weights. # Need to turn into a timeseries covering the range of forecast # dates fixed_weights = self._get_fixed_forecast_weights_as_dict( instrument_code) rule_variation_list = sorted(fixed_weights.keys()) forecasts = self.get_all_forecasts(instrument_code, rule_variation_list) forecasts_time_index = forecasts.index forecast_columns_to_align = forecasts.columns # Turn into a 2 row data frame aligned to forecast names forecast_weights = from_dict_of_values_to_df( fixed_weights, forecasts_time_index, columns=forecast_columns_to_align) return forecast_weights