Esempio n. 1
0
    def __init__(self,
                 client=None,
                 ipaddress=None,
                 port=None,
                 log=logtoscreen(),
                 mongo_db=None):
        """

        :param client: client id. If not passed then will get from database specified by db_id_tracker
        :param ipaddress: IP address of machine running IB Gateway or TWS. If not passed then will get from private config file, or defaults
        :param port: Port listened to by IB Gateway or TWS
        :param log: logging object
        :param db_id_tracker: Eithier none (to use the default or an object that quacks like class mongoIBclientIDtracker)
        """

        # resolve defaults
        ipaddress, port, idoffset = ib_defaults(ipaddress=ipaddress, port=port)

        # If you copy for another broker include this line
        log.label(broker="IB", clientid=client)
        self._ib_connection_config = dict(ipaddress=ipaddress,
                                          port=port,
                                          client=client)

        # You can pass a client id yourself, or let IB find one
        if client is None:
            # The client id is pulled from a mongo database
            # If for example you want to use a different database you could do something like:
            # connectionIB(mongo_ib_tracker = mongoIBclientIDtracker(database_name="another")
            #

            db_id_tracker = mongoIBclientIDtracker(mongo_db=mongo_db,
                                                   log=log,
                                                   idoffset=idoffset)

            # get and lock a client id
            client = db_id_tracker.get_next_clientid()
            # client IDs are not released; assume we clean them daily

        # IB specific - this is to ensure we don't get reqID conflicts between different processes
        reqIDoffset = client * 1000

        #if you copy for another broker, don't forget the logs
        ibServer.__init__(self, log=log)
        ibClient.__init__(self, wrapper=self, reqIDoffset=reqIDoffset, log=log)

        # if you copy for another broker, don't forget to do this
        self.broker_init_error()

        # this is all very IB specific
        self.connect(ipaddress, port, client)
        thread = Thread(target=self.run)
        thread.start()
        setattr(self, "_thread", thread)
    def __init__(self,
                 client=None,
                 ipaddress=None,
                 port=None,
                 log=logtoscreen("connectionIB"),
                 mongo_db=arg_not_supplied):
        """
        :param client: client id. If not passed then will get from database specified by mongo_db
        :param ipaddress: IP address of machine running IB Gateway or TWS. If not passed then will get from private config file, or defaults
        :param port: Port listened to by IB Gateway or TWS
        :param log: logging object
        :param mongo_db: mongoDB connection
        """

        # resolve defaults
        ipaddress, port, idoffset = ib_defaults(ipaddress=ipaddress, port=port)

        # The client id is pulled from a mongo database
        # If for example you want to use a different database you could do something like:
        # connectionIB(mongo_ib_tracker = mongoIBclientIDtracker(database_name="another")

        # You can pass a client id yourself, or let IB find one

        self.db_id_tracker = mongoIBclientIDtracker(mongo_db=mongo_db,
                                                    log=log,
                                                    idoffset=idoffset)
        client = self.db_id_tracker.return_valid_client_id(client)

        # If you copy for another broker include this line
        log.label(broker="IB", clientid=client)
        self._ib_connection_config = dict(ipaddress=ipaddress,
                                          port=port,
                                          client=client)

        #if you copy for another broker, don't forget the logs
        ibServer.__init__(self, log=log)
        ibClient.__init__(self, log=log)

        # this is all very IB specific
        ib = IB()
        ib.connect(ipaddress, port, clientId=client)

        ## Add handlers, from ibServer methods
        ib.errorEvent += self.error_handler

        self.ib = ib
Esempio n. 3
0
    def __init__(self, ib_connection_config, log=logtoscreen()):

        # If you copy for another broker include this line
        log.label(broker="IB", clientid=ib_connection_config.client)
        self.__ib_connection_config = ib_connection_config

        # IB specific - this is to ensure we don't get reqID conflicts between different processes
        reqIDoffset = ib_connection_config.client * 1000

        #if you copy for another broker, don't forget the logs
        ibServer.__init__(self, log=log)
        ibClient.__init__(self, wrapper=self, reqIDoffset=reqIDoffset, log=log)

        # if you copy for another broker, don't forget to do this
        self.broker_init_error()

        # this is all very IB specific
        self.connect(ib_connection_config.ipaddress,
                     ib_connection_config.portid, ib_connection_config.client)
        thread = Thread(target=self.run)
        thread.start()
        setattr(self, "_thread", thread)