本文整理汇总了Python中syscore.objects.resolve_function函数的典型用法代码示例。如果您正苦于以下问题:Python resolve_function函数的具体用法?Python resolve_function怎么用?Python resolve_function使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了resolve_function函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, optimise_params):
"""
Create an object which estimates the moments for a single period of data, according to the parameters
The parameters we need are popped from the config dict
:param optimise_params: Parameters for optimisation
:type optimise_params: dict
"""
corr_estimate_params=copy(optimise_params["correlation_estimate"])
mean_estimate_params=copy(optimise_params["mean_estimate"])
vol_estimate_params=copy(optimise_params["vol_estimate"])
corr_estimate_func=resolve_function(corr_estimate_params.pop("func"))
mean_estimate_func=resolve_function(mean_estimate_params.pop("func"))
vol_estimate_func=resolve_function(vol_estimate_params.pop("func"))
setattr(self, "corr_estimate_params", corr_estimate_params)
setattr(self, "mean_estimate_params", mean_estimate_params)
setattr(self, "vol_estimate_params", vol_estimate_params)
setattr(self, "corr_estimate_func", corr_estimate_func)
setattr(self, "mean_estimate_func", mean_estimate_func)
setattr(self, "vol_estimate_func", vol_estimate_func)
开发者ID:MortenMunck,项目名称:pysystemtrade,代码行数:26,代码来源:optimisation.py
示例2: __init__
def __init__(self,
optimise_params,
annualisation=BUSINESS_DAYS_IN_YEAR,
ann_target_SR=.5):
"""
Create an object which estimates the moments for a single period of data, according to the parameters
The parameters we need are popped from the config dict
:param optimise_params: Parameters for optimisation
:type optimise_params: dict
"""
corr_estimate_params = copy(optimise_params["correlation_estimate"])
mean_estimate_params = copy(optimise_params["mean_estimate"])
vol_estimate_params = copy(optimise_params["vol_estimate"])
corr_estimate_func = resolve_function(corr_estimate_params.pop("func"))
mean_estimate_func = resolve_function(mean_estimate_params.pop("func"))
vol_estimate_func = resolve_function(vol_estimate_params.pop("func"))
setattr(self, "corr_estimate_params", corr_estimate_params)
setattr(self, "mean_estimate_params", mean_estimate_params)
setattr(self, "vol_estimate_params", vol_estimate_params)
setattr(self, "corr_estimate_func", corr_estimate_func)
setattr(self, "mean_estimate_func", mean_estimate_func)
setattr(self, "vol_estimate_func", vol_estimate_func)
period_target_SR = ann_target_SR / (annualisation**.5)
setattr(self, "annualisation", annualisation)
setattr(self, "period_target_SR", period_target_SR)
setattr(self, "ann_target_SR", ann_target_SR)
开发者ID:kohehir,项目名称:pysystemtrade,代码行数:35,代码来源:optimisation.py
示例3: calculation_of_raw_instrument_weights
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
"""
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
# 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"))
calcs_of_instrument_weights = self.parent.calc_or_cache(
'calculation_of_raw_instrument_weights', ALL_KEYNAME,
_calculation_of_raw_instrument_weights,
self, weighting_func, **weighting_params)
return calcs_of_instrument_weights
开发者ID:bmaher74,项目名称:pysystemtrade,代码行数:59,代码来源:portfolio.py
示例4: daily_returns_volatility
def daily_returns_volatility(self, instrument_code):
"""
Gets volatility of daily returns (not % returns)
This is done using a user defined function
We get this from:
the configuration object
or if not found, system.defaults.py
The dict must contain func key; anything else is optional
:param instrument_code: Instrument to get prices for
:type trading_rules: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object
>>> from systems.basesystem import System
>>>
>>> (rawdata, data, config)=get_test_object()
>>> system=System([rawdata], data)
>>> ## uses defaults
>>> system.rawdata.daily_returns_volatility("EDOLLAR").tail(2)
vol
2015-12-10 0.054145
2015-12-11 0.058522
>>>
>>> from sysdata.configdata import Config
>>> config=Config("systems.provided.example.exampleconfig.yaml")
>>> system=System([rawdata], data, config)
>>> system.rawdata.daily_returns_volatility("EDOLLAR").tail(2)
vol
2015-12-10 0.054145
2015-12-11 0.058522
>>>
>>> config=Config(dict(volatility_calculation=dict(func="syscore.algos.robust_vol_calc", days=200)))
>>> system2=System([rawdata], data, config)
>>> system2.rawdata.daily_returns_volatility("EDOLLAR").tail(2)
vol
2015-12-10 0.057946
2015-12-11 0.058626
"""
self.log.msg(
"Calculating daily volatility for %s" % instrument_code,
instrument_code=instrument_code)
system = self.parent
dailyreturns = self.daily_returns(instrument_code)
volconfig = copy(system.config.volatility_calculation)
# volconfig contains 'func' and some other arguments
# we turn func which could be a string into a function, and then
# call it with the other ags
volfunction = resolve_function(volconfig.pop('func'))
vol = volfunction(dailyreturns, **volconfig)
return vol
开发者ID:ChrisAllisonMalta,项目名称:pysystemtrade,代码行数:59,代码来源:rawdata.py
示例5: _get_forecast_scalar_estimated_from_instrument_list
def _get_forecast_scalar_estimated_from_instrument_list(
self, instrument_code, rule_variation_name,
forecast_scalar_config):
"""
Get the scalar to apply to raw forecasts
If not cached, these are estimated from past forecasts
:param instrument_code: instrument code, or ALL_KEYNAME if pooling
:type str:
:param rule_variation_name:
:type str: name of the trading rule variation
:param forecast_scalar_config:
:type dict: relevant part of the config
:returns: float
"""
# The config contains 'func' and some other arguments
# we turn func which could be a string into a function, and then
# call it with the other ags
scalar_function = resolve_function(forecast_scalar_config.pop('func'))
"""
instrument_list contains multiple things, might pool everything across
all instruments
"""
if instrument_code == ALL_KEYNAME:
# pooled, same for all instruments
instrument_list = self.parent.get_instrument_list()
else:
## not pooled
instrument_list = [instrument_code]
self.log.msg(
"Getting forecast scalar for %s over %s" %
(rule_variation_name, ", ".join(instrument_list)),
rule_variation_name=rule_variation_name)
# Get forecasts for each instrument
forecast_list = [
self.get_raw_forecast(instrument_code, rule_variation_name)
for instrument_code in instrument_list
]
cs_forecasts = pd.concat(forecast_list, axis=1)
# an example of a scaling function is syscore.algos.forecast_scalar
# must return thing the same size as cs_forecasts
scaling_factor = scalar_function(cs_forecasts,
**forecast_scalar_config)
return scaling_factor
开发者ID:kohehir,项目名称:pysystemtrade,代码行数:58,代码来源:forecast_scale_cap.py
示例6: __init__
def __init__(self, optimise_params, annualisation=BUSINESS_DAYS_IN_YEAR,
ann_target_SR=.5):
corr_estimate_params=copy(optimise_params["correlation_estimate"])
mean_estimate_params=copy(optimise_params["mean_estimate"])
vol_estimate_params=copy(optimise_params["vol_estimate"])
corr_estimate_func=resolve_function(corr_estimate_params.pop("func"))
mean_estimate_func=resolve_function(mean_estimate_params.pop("func"))
vol_estimate_func=resolve_function(vol_estimate_params.pop("func"))
setattr(self, "corr_estimate_params", corr_estimate_params)
setattr(self, "mean_estimate_params", mean_estimate_params)
setattr(self, "vol_estimate_params", vol_estimate_params)
setattr(self, "corr_estimate_func", corr_estimate_func)
setattr(self, "mean_estimate_func", mean_estimate_func)
setattr(self, "vol_estimate_func", vol_estimate_func)
period_target_SR = ann_target_SR / (annualisation**.5)
setattr(self, "annualisation", annualisation)
setattr(self, "period_target_SR", period_target_SR)
setattr(self, "ann_target_SR", ann_target_SR)
开发者ID:caitouwh,项目名称:kod,代码行数:18,代码来源:tw2.py
示例7: get_instrument_correlation_matrix
def get_instrument_correlation_matrix(self):
"""
Returns a correlationList object which contains a history of correlation matricies
:returns: correlation_list object
>>> from systems.tests.testdata import get_test_object_futures_with_pos_sizing_estimates
>>> from systems.basesystem import System
>>> (account, posobject, combobject, capobject, rules, rawdata, data, config)=get_test_object_futures_with_pos_sizing_estimates()
>>> system=System([rawdata, rules, posobject, combobject, capobject,PortfoliosEstimated(), account], data, config)
>>> system.config.forecast_weight_estimate["method"]="shrinkage" ## speed things up
>>> system.config.forecast_weight_estimate["date_method"]="in_sample" ## speed things up
>>> system.config.instrument_weight_estimate["date_method"]="in_sample" ## speed things up
>>> system.config.instrument_weight_estimate["method"]="shrinkage" ## speed things up
>>> ans=system.portfolio.get_instrument_correlation_matrix()
>>> ans.corr_list[-1]
array([[ 1. , 0.56981346, 0.62458477],
[ 0.56981346, 1. , 0.88087893],
[ 0.62458477, 0.88087893, 1. ]])
>>> print(ans.corr_list[0])
[[ 1. 0.99 0.99]
[ 0.99 1. 0.99]
[ 0.99 0.99 1. ]]
>>> print(ans.corr_list[10])
[[ 1. 0.99 0.99 ]
[ 0.99 1. 0.78858156]
[ 0.99 0.78858156 1. ]]
"""
self.log.terse("Calculating instrument correlations")
system = self.parent
# Get some useful stuff from the config
corr_params = copy(system.config.instrument_correlation_estimate)
# which function to use for calculation
corr_func = resolve_function(corr_params.pop("func"))
if hasattr(system, "accounts"):
pandl = self.pandl_across_subsystems().to_frame()
else:
error_msg = "You need an accounts stage in the system to estimate instrument correlations"
self.log.critical(error_msg)
# Need to resample here, because the correlation function won't do
# it properly (doesn't know it's dealing with returns data)
frequency = corr_params['frequency']
pandl = pandl.cumsum().resample(frequency).last().diff()
# The subsequent resample inside the correlation function will have no effect
return corr_func(pandl, **corr_params)
开发者ID:ChrisAllisonMalta,项目名称:pysystemtrade,代码行数:53,代码来源:portfolio.py
示例8: _daily_returns_volatility
def _daily_returns_volatility(system, instrument_code, this_stage):
this_stage.log.msg("Calculating daily volatility for %s" % instrument_code, instrument_code=instrument_code)
dailyreturns = this_stage.daily_returns(instrument_code)
volconfig=copy(system.config.volatility_calculation)
# volconfig contains 'func' and some other arguments
# we turn func which could be a string into a function, and then
# call it with the other ags
volfunction = resolve_function(volconfig.pop('func'))
vol = volfunction(dailyreturns, **volconfig)
return vol
开发者ID:Sayan-Paul,项目名称:kod,代码行数:14,代码来源:rawdata.py
示例9: _daily_returns_volatility
def _daily_returns_volatility(system, instrument_code, this_stage):
print(__file__ + ":" + str(inspect.getframeinfo(inspect.currentframe())[:3][1]) + ":" +"Calculating daily volatility for %s" % instrument_code)
dailyreturns = this_stage.daily_returns(instrument_code)
volconfig=copy(system.config.volatility_calculation)
# volconfig contains 'func' and some other arguments
# we turn func which could be a string into a function, and then
# call it with the other ags
volfunction = resolve_function(volconfig.pop('func'))
vol = volfunction(dailyreturns, **volconfig)
return vol
开发者ID:caitouwh,项目名称:kod,代码行数:14,代码来源:rawdata.py
示例10: _get_instrument_div_multiplier
def _get_instrument_div_multiplier(system, NotUsed, this_stage):
this_stage.log.terse("Calculating instrument div. multiplier")
## Get some useful stuff from the config
div_mult_params=copy(system.config.instrument_div_mult_estimate)
idm_func=resolve_function(div_mult_params.pop("func"))
correlation_list_object=this_stage.get_instrument_correlation_matrix()
weight_df=this_stage.get_instrument_weights()
ts_idm=idm_func(correlation_list_object, weight_df, **div_mult_params)
return ts_idm
开发者ID:SkippyHo,项目名称:pysystemtrade,代码行数:15,代码来源:portfolio.py
示例11: _get_forecast_div_multiplier
def _get_forecast_div_multiplier(system, instrument_code, this_stage):
print(__file__ + ":" + str(inspect.getframeinfo(inspect.currentframe())[:3][1]) + ":" +"Calculating forecast div multiplier for %s" % instrument_code)
## Get some useful stuff from the config
div_mult_params=copy(system.config.forecast_div_mult_estimate)
idm_func=resolve_function(div_mult_params.pop("func"))
correlation_list_object=this_stage.get_forecast_correlation_matrices(instrument_code)
weight_df=this_stage.get_forecast_weights(instrument_code)
ts_fdm=idm_func(correlation_list_object, weight_df, **div_mult_params)
return ts_fdm
开发者ID:caitouwh,项目名称:kod,代码行数:15,代码来源:forecast_combine.py
示例12: _get_forecast_div_multiplier
def _get_forecast_div_multiplier(system, instrument_code, this_stage):
this_stage.log.terse("Calculating forecast div multiplier for %s" % instrument_code,
instrument_code=instrument_code)
## Get some useful stuff from the config
div_mult_params=copy(system.config.forecast_div_mult_estimate)
idm_func=resolve_function(div_mult_params.pop("func"))
correlation_list_object=this_stage.get_forecast_correlation_matrices(instrument_code)
weight_df=this_stage.get_forecast_weights(instrument_code)
ts_fdm=idm_func(correlation_list_object, weight_df, **div_mult_params)
return ts_fdm
开发者ID:yowtzu,项目名称:pysystemtrade,代码行数:16,代码来源:forecast_combine.py
示例13: calculation_of_raw_instrument_weights
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
"""
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_subsystems=[this_stage.pandl_for_subsystem(code)
for code in instrument_codes]
else:
error_msg="You need an accounts stage in the system to estimate instrument weights"
this_stage.log.critical(error_msg)
pandl=pd.concat(pandl_subsystems, axis=1)
pandl.columns=instrument_codes
instrument_weight_results=weighting_func(pandl, log=self.log.setup(call="weighting"), **weighting_params)
return instrument_weight_results
## 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"))
calcs_of_instrument_weights = self.parent.calc_or_cache(
'calculation_of_raw_instrument_weights', ALL_KEYNAME,
_calculation_of_raw_instrument_weights,
self, weighting_func, **weighting_params)
return calcs_of_instrument_weights
开发者ID:Sayan-Paul,项目名称:kod,代码行数:43,代码来源:portfolio.py
示例14: capital_multiplier
def capital_multiplier(self, delayfill=True, roundpositions=False):
"""
Get a capital multiplier
:param delayfill: Lag fills by one day
:type delayfill: bool
:param roundpositions: Round positions to whole contracts
:type roundpositions: bool
:returns: pd.Series
"""
system = self.parent
capmult_params = copy(system.config.capital_multiplier)
capmult_func = resolve_function(capmult_params.pop("func"))
capmult = capmult_func(system, **capmult_params)
capmult = capmult.reindex(self.portfolio().index).ffill()
return capmult
开发者ID:kohehir,项目名称:pysystemtrade,代码行数:22,代码来源:account.py
示例15: _daily_returns_volatility
def _daily_returns_volatility(system, instrument_code, this_stage):
dailyreturns = this_stage.daily_returns(instrument_code)
try:
volconfig = copy(system.config.volatility_calculation)
identify_error = "inherited from config object"
except:
volconfig = copy(system_defaults['volatility_calculation'])
identify_error = "found in system.defaults.py"
if "func" not in volconfig:
raise Exception(
"The volconfig dict (%s) needs to have a 'func' key" % identify_error)
# volconfig contains 'func' and some other arguments
# we turn func which could be a string into a function, and then
# call it with the other ags
volfunction = resolve_function(volconfig.pop('func'))
vol = volfunction(dailyreturns, **volconfig)
return vol
开发者ID:as4724,项目名称:pysystemtrade,代码行数:22,代码来源:rawdata.py
示例16: get_estimated_instrument_diversification_multiplier
def get_estimated_instrument_diversification_multiplier(self):
"""
Estimate the diversification multiplier for the portfolio
Estimated from correlations and weights
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object_futures_with_pos_sizing_estimates
>>> from systems.basesystem import System
>>> (account, posobject, combobject, capobject, rules, rawdata, data, config)=get_test_object_futures_with_pos_sizing_estimates()
>>> system=System([rawdata, rules, posobject, combobject, capobject,PortfoliosEstimated(), account], data, config)
>>> system.config.forecast_weight_estimate["method"]="shrinkage" ## speed things up
>>> system.config.forecast_weight_estimate["date_method"]="in_sample" ## speed things up
>>> system.config.instrument_weight_estimate["date_method"]="in_sample" ## speed things up
>>> system.config.instrument_weight_estimate["method"]="shrinkage" ## speed things up
>>> system.portfolio.get_instrument_diversification_multiplier().tail(3)
IDM
2015-12-09 1.133220
2015-12-10 1.133186
2015-12-11 1.133153
"""
self.log.terse("Calculating instrument div. multiplier")
# Get some useful stuff from the config
div_mult_params = copy(self.parent.config.instrument_div_mult_estimate)
idm_func = resolve_function(div_mult_params.pop("func"))
correlation_list_object = self.get_instrument_correlation_matrix()
weight_df = self.get_instrument_weights()
ts_idm = idm_func(correlation_list_object, weight_df,
**div_mult_params)
return ts_idm
开发者ID:ChrisAllisonMalta,项目名称:pysystemtrade,代码行数:38,代码来源:portfolio.py
示例17: calculation_of_raw_instrument_weights
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()
if hasattr(system, "accounts"):
pandl = self.pandl_across_subsystems()
else:
error_msg = "You need an accounts stage in the system to estimate instrument weights"
self.log.critical(error_msg)
# The optimiser is set up for pooling, but we're not going to do that
# Create a single fake set of return data
data = dict(instrument_pandl = pandl)
weight_func = weighting_func(data, identifier ="instrument_pandl", parent=self,
**weighting_params)
weight_func.optimise()
return weight_func
开发者ID:ChrisAllisonMalta,项目名称:pysystemtrade,代码行数:38,代码来源:portfolio.py
示例18: get_forecast_scalar
def get_forecast_scalar(self, instrument_code, rule_variation_name):
"""
Get the scalar to apply to raw forecasts
If not cached, these are estimated from past forecasts
If configuration variable pool_forecasts_for_scalar is "True", then we do this across instruments.
:param instrument_code:
:type str:
:param rule_variation_name:
:type str: name of the trading rule variation
:returns: float
>>> from systems.tests.testdata import get_test_object_futures_with_rules
>>> from systems.basesystem import System
>>> (rules, rawdata, data, config)=get_test_object_futures_with_rules()
>>> system1=System([rawdata, rules, ForecastScaleCapEstimated()], data, config)
>>>
>>> ## From default
>>> system1.forecastScaleCap.get_forecast_scalar("EDOLLAR", "ewmac8").tail(3)
scale_factor
2015-12-09 5.849888
2015-12-10 5.850474
2015-12-11 5.851091
>>> system1.forecastScaleCap.get_capped_forecast("EDOLLAR", "ewmac8").tail(3)
ewmac8
2015-12-09 0.645585
2015-12-10 -0.210377
2015-12-11 0.961821
>>>
>>> ## From config
>>> scale_config=dict(pool_instruments=False)
>>> config.forecast_scalar_estimate=scale_config
>>> system3=System([rawdata, rules, ForecastScaleCapEstimated()], data, config)
>>> system3.forecastScaleCap.get_forecast_scalar("EDOLLAR", "ewmac8").tail(3)
scale_factor
2015-12-09 5.652174
2015-12-10 5.652833
2015-12-11 5.653444
>>>
"""
def _get_forecast_scalar(
system, Not_Used, rule_variation_name, this_stage, instrument_list,
scalar_function, forecast_scalar_config):
"""
instrument_list contains multiple things, pools everything across all instruments
"""
print(__file__ + ":" + str(inspect.getframeinfo(inspect.currentframe())[:3][1]) + ":" +"Getting forecast scalar for %s over %s" % (rule_variation_name, ", ".join(instrument_list)))
## Get forecasts for each instrument
forecast_list=[
this_stage.get_raw_forecast(instrument_code, rule_variation_name)
for instrument_code in instrument_list]
cs_forecasts=pd.concat(forecast_list, axis=1)
scaling_factor=scalar_function(cs_forecasts, **forecast_scalar_config)
return scaling_factor
## Get some useful stuff from the config
forecast_scalar_config=copy(self.parent.config.forecast_scalar_estimate)
# The config contains 'func' and some other arguments
# we turn func which could be a string into a function, and then
# call it with the other ags
scalarfunction = resolve_function(forecast_scalar_config.pop('func'))
## this determines whether we pool or not
pool_instruments=str2Bool(forecast_scalar_config.pop("pool_instruments"))
if pool_instruments:
## pooled, same for all instruments
instrument_code_key=ALL_KEYNAME
instrument_list=self.parent.get_instrument_list()
else:
## not pooled
instrument_code_key=instrument_code
instrument_list=[instrument_code]
forecast_scalar = self.parent.calc_or_cache_nested(
"get_forecast_scalar", instrument_code_key, rule_variation_name,
_get_forecast_scalar, self, instrument_list, scalarfunction, forecast_scalar_config)
return forecast_scalar
开发者ID:caitouwh,项目名称:kod,代码行数:92,代码来源:forecast_scale_cap.py
示例19: calculation_of_raw_forecast_weights
def calculation_of_raw_forecast_weights(self, instrument_code):
"""
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
"""
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
## Get some useful stuff from the config
weighting_params=copy(self.parent.config.forecast_weight_estimate)
## do we pool our estimation?
pooling=str2Bool(weighting_params.pop("pool_instruments"))
## which function to use for calculation
weighting_func=resolve_function(weighting_params.pop("func"))
if pooling:
## find set of instruments with same trading rules as I have
codes_to_use=self._has_same_rules_as_code(instrument_code)
else:
codes_to_use=[instrument_code]
##
## _get_raw_forecast_weights: function to call if we don't find in cache
## self: this_system stage object
## codes_to_use: instrument codes to get data for
## weighting_func: function to call to calculate weights
## **weighting_params: parameters to pass to weighting function
##
raw_forecast_weights_calcs = self.parent.calc_or_cache(
'calculation_of_raw_forecast_weights', instrument_code,
_calculation_of_raw_forecast_weights,
self, codes_to_use, weighting_func, **weighting_params)
return raw_forecast_weights_calcs
开发者ID:SkippyHo,项目名称:pysystemtrade,代码行数:70,代码来源:forecast_combine.py
示例20: calculation_of_pooled_raw_forecast_weights
def calculation_of_pooled_raw_forecast_weights(self, instrument_code):
"""
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
"""
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
## Get some useful stuff from the config
weighting_params=copy(self.parent.config.forecast_weight_estimate)
## do we pool our estimation?
pooling_returns = str2Bool(weighting_params.pop("pool_gross_returns"))
pooling_costs = self.parent.config.forecast_cost_estimates['use_pooled_costs']
assert pooling_returns and pooling_costs
## which function to use for calculation
weighting_func=resolve_function(weighting_params.pop("func"))
codes_to_use=self.has_same_cheap_rules_as_code(instrument_code)
instrument_code_ref ="_".join(codes_to_use) ## ensures we don't repeat optimisation
##
## _get_raw_forecast_weights: function to call if we don't find in cache
## self: this_system stage object
## codes_to_use: instrument codes to get data for
## weighting_func: function to call to calculate weights
## **weighting_params: parameters to pass to weighting function
##
raw_forecast_weights_calcs = self.parent.calc_or_cache(
'calculation_of_raw_forecast_weights', instrument_code_ref,
_calculation_of_pooled_raw_forecast_weights,
self, codes_to_use, weighting_func, **weighting_params)
return raw_forecast_weights_calcs
开发者ID:caitouwh,项目名称:kod,代码行数:79,代码来源:forecast_combine.py
注:本文中的syscore.objects.resolve_function函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论