def _already_a_trading_rule(rule): return hasallattr(rule, ["function", "data", "other_args"])
def __init__(self, rule, data=list(), other_args=dict()): """ Create a trading rule from a function Functions must be of the form function(*dataargs, **kwargs), where *dataargs are unnamed data items, and **kwargs are named configuration items data, an ordered list of strings identifying data to be used (default, just price) other_args: a dictionary of named arguments to be passed to the trading rule :param rule: Trading rule to be created :type trading_rules: The following describe a rule completely (ignore data and other_args arguments) 3-tuple ; containing (function, data, other_args) dict (containing key "function", and optionally keys "other_args" and "data") TradingRule (object is created out of this rule) The following will be combined with the data and other_args arguments to produce a complete TradingRule: Other callable function str (with path to function eg "systems.provide.example.rules.ewmac_forecast_with_defaults") :param data: (list of) str pointing to location of inputs in a system method call (eg "data.get_instrument_price") (Either passed in separately, or as part of a TradingRule, 3-tuple, or dict object) :type data: single str, or list of str :param other_args: Other named arguments to be passed to trading rule function Any arguments that are prefixed with "_" will be passed to the first data function call Any arguments that are prefixed with "__" will be passed to the second data function call... and so on (Either passed in separately , or as part of a TradingRule, 3-tuple, or dict object) :type other_args: dict :returns: single Tradingrule object """ data_args = None if hasallattr(rule, ["function", "data", "other_args"]): # looks like it is already a trading rule (rule_function, data, other_args, data_args) = (rule.function, rule.data, rule.other_args, rule.data_args) elif isinstance(rule, tuple): if len(data) > 0 or len(other_args) > 0: print( "WARNING: Creating trade rule with 'rule' tuple argument, ignoring data and/or other args" ) if len(rule) != 3: raise Exception( "Creating trading rule with a tuple, must be length 3 exactly (function/name, data [...], args dict(...))" ) (rule_function, data, other_args) = rule elif isinstance(rule, dict): if len(data) > 0 or len(other_args) > 0: print( "WARNING: Creating trade rule with 'rule' dict argument, ignoring data and/or other args" ) try: rule_function = rule['function'] except KeyError: raise Exception( "If you specify a TradingRule as a dict it has to contain a 'function' keyname" ) if "data" in rule: data = rule['data'] else: data = [] if "other_args" in rule: other_args = rule['other_args'] else: other_args = dict() else: rule_function = rule # turn string into a callable function if required rule_function = resolve_function(rule_function) if isinstance(data, str): # turn into a 1 item list or wont' get parsed properly data = [data] if data_args is None: # This will be the case if the rule was built from arguments # Resolve any _ prefixed other_args other_args, data_args = separate_other_args(other_args, data) # fill the object with data setattr(self, "function", rule_function) setattr(self, "data", data) setattr(self, "other_args", other_args) setattr(self, "data_args", data_args)
def __init__(self, rule, data=list(), other_args=dict()): """ Create a trading rule from a function Functions must be of the form function(*dataargs, **kwargs), where *dataargs are unnamed data items, and **kwargs are named configuration items data, an ordered list of strings identifying data to be used (default, just price) other_args: a dictionary of named arguments to be passed to the trading rule :param rule: Trading rule to be created :type trading_rules: The following describe a rule completely (ignore data and other_args arguments) 3-tuple ; containing (function, data, other_args) dict (containing key "function", and optionally keys "other_args" and "data") TradingRule (object is created out of this rule) The following will be combined with the data and other_args arguments to produce a complete TradingRule: Other callable function str (with path to function eg "systems.provide.example.rules.ewmac_forecast_with_defaults") :param data: (list of) str pointing to location of inputs in a system method call (eg "data.get_instrument_price") (Eithier passed in separately, or as part of a TradingRule, 3-tuple, or dict object) :type data: single str, or list of str :param other_args: Other named arguments to be passed to trading rule function (Eithier passed in separately , or as part of a TradingRule, 3-tuple, or dict object) :type other_args: dict :returns: single Tradingrule object """ if hasallattr(rule, ["function", "data", "other_args"]): # looks like it is already a trading rule (rule_function, data, other_args) = ( rule.function, rule.data, rule.other_args) elif isinstance(rule, tuple): if len(data) > 0 or len(other_args) > 0: print( "WARNING: Creating trade rule with 'rule' tuple argument, ignoring data and/or other args") if len(rule) != 3: raise Exception( "Creating trading rule with a tuple, must be length 3 exactly (function/name, data [...], args dict(...))") (rule_function, data, other_args) = rule elif isinstance(rule, dict): if len(data) > 0 or len(other_args) > 0: print( "WARNING: Creating trade rule with 'rule' dict argument, ignoring data and/or other args") try: rule_function = rule['function'] except KeyError: raise Exception( "If you specify a TradingRule as a dict it has to contain a 'function' keyname") if "data" in rule: data = rule['data'] else: data = [] if "other_args" in rule: other_args = rule['other_args'] else: other_args = dict() else: rule_function = rule # turn string into a callable function if required rule_function = resolve_function(rule_function) if isinstance(data, str): # turn into a 1 item list or wont' get parsed properly data = [data] setattr(self, "function", rule_function) setattr(self, "data", data) setattr(self, "other_args", other_args)