Ejemplo n.º 1
0
    def __init__(self, stage_list, data, config=None, log=logtoscreen("base_system")):
        """
        Create a system object for doing simulations or live trading

        :param stage_list: A list of stages
        :type stage_list: list of systems.stage.SystemStage (or anything that inherits from it)

        :param data: data for doing simulations
        :type data: sysdata.data.Data (or anything that inherits from that)

        :param config: Optional configuration
        :type config: sysdata.configdata.Config

        :returns: new system object

        >>> from systems.stage import SystemStage
        >>> stage=SystemStage()
        >>> from sysdata.csvdata import csvFuturesData
        >>> data=csvFuturesData()
        >>> System([stage], data)
        System with stages: unnamed

        """

        if config is None:
            # Default - for very dull systems this is sufficient
            config = Config()

        config.fill_with_defaults()
        
        setattr(self, "data", data)
        setattr(self, "config", config)
        setattr(self, "log", log)
        
        setattr(data, "log", log.setup(stage="data"))
        setattr(config, "log", log.setup(stage="config"))

        protected = []
        nopickle=[]
        stage_names = []

        assert isinstance(stage_list, list)

        for stage in stage_list:

            """
            This is where we put the methods to store various stages of the process

            """

            # Stages have names, which are also how we find them in the system
            # attributes
            sub_name = stage.name

            # Each stage has a link back to the parent system
            stage._system_init(self)
            
            # and a log
            log=log.setup(stage=sub_name)
            setattr(stage, "log", log)

            if sub_name in stage_names:
                raise Exception(
                    "You have duplicate subsystems with the name %s. Remove "
                    "one of them, or change a name." % sub_name)

            setattr(self, sub_name, stage)

            stage_names.append(sub_name)

            # list of attributes / methods of the stage which are protected
            stage_protected = getattr(stage, "_protected", [])
            stage_protected = [(sub_name, protected_item, "*") for protected_item in stage_protected]
            protected += stage_protected
            
            stage_nopickle=getattr(stage, "_nopickle", [])
            stage_nopickle = [(sub_name, protected_item, "*") for protected_item in stage_nopickle]
            nopickle += stage_nopickle
            

        setattr(self, "_stage_names", stage_names)

        """
        The cache hides all intermediate results

        We call optimal_positions and then that propogates back finding all the
        data we need

        The results are then cached in the object. Should we call
            delete_instrument_data (in base class system) then everything
            related to a particular instrument is removed from these 'nodes'
            except for protected items

        This is very useful in live trading when we don't want to update eg
            cross sectional data every sample
        """

        setattr(self, "_cache", dict())
        setattr(self, "_protected", protected)
        setattr(self, "_nopickle", nopickle)
Ejemplo n.º 2
0
    def __init__(self, stage_list, data, config=None,
                 log=logtoscreen("base_system")):
        """
        Create a system object for doing simulations or live trading

        :param stage_list: A list of stages
        :type stage_list: list of systems.stage.SystemStage (or anything that inherits from it)

        :param data: data for doing simulations
        :type data: sysdata.data.Data (or anything that inherits from that)

        :param config: Optional configuration
        :type config: sysdata.configdata.Config

        :returns: new system object

        >>> from systems.stage import SystemStage
        >>> stage=SystemStage()
        >>> from sysdata.csvdata import csvFuturesData
        >>> data=csvFuturesData()
        >>> System([stage], data)
        System with stages: unnamed

        """

        if config is None:
            # Default - for very dull systems this is sufficient
            config = Config()

        config.fill_with_defaults()

        setattr(self, "data", data)
        setattr(self, "config", config)
        setattr(self, "log", log)

        setattr(data, "log", log.setup(stage="data"))
        setattr(config, "log", log.setup(stage="config"))

        protected = []
        nopickle = []
        stage_names = []

        assert isinstance(stage_list, list)

        for stage in stage_list:

            """
            This is where we put the methods to store various stages of the process

            """

            # Stages have names, which are also how we find them in the system
            # attributes
            sub_name = stage.name

            # Each stage has a link back to the parent system
            stage._system_init(self)

            # and a log
            log = log.setup(stage=sub_name)
            setattr(stage, "log", log)

            if sub_name in stage_names:
                raise Exception(
                    "You have duplicate subsystems with the name %s. Remove "
                    "one of them, or change a name." % sub_name)

            setattr(self, sub_name, stage)

            stage_names.append(sub_name)

            # list of attributes / methods of the stage which are protected
            stage_protected = getattr(stage, "_protected", [])
            stage_protected = [(sub_name, protected_item, "*")
                               for protected_item in stage_protected]
            protected += stage_protected

            stage_nopickle = getattr(stage, "_nopickle", [])
            stage_nopickle = [(sub_name, protected_item, "*")
                              for protected_item in stage_nopickle]
            nopickle += stage_nopickle

        setattr(self, "_stage_names", stage_names)

        """
        The cache hides all intermediate results

        We call optimal_positions and then that propogates back finding all the
        data we need

        The results are then cached in the object. Should we call
            delete_instrument_data (in base class system) then everything
            related to a particular instrument is removed from these 'nodes'
            except for protected items

        This is very useful in live trading when we don't want to update eg
            cross sectional data every sample
        """

        setattr(self, "_cache", dict())
        setattr(self, "_protected", protected)
        setattr(self, "_nopickle", nopickle)