示例#1
0
    def ib_get_contract_chain(self, ibcontract_pattern, log=None):
        """
        Get all the IB contracts matching a pattern.

        :param ibcontract_pattern: ibContract which may not fully specify the contract
        :param log: log object
        :return: list of ibContracts
        """

        if log is None:
            log=self.log

        reqId = self.ib_next_req_id()
        ## Make a place to store the data we're going to return
        contract_details_queue = finishableQueue(self.init_contractdetails(reqId))

        self.reqContractDetails(reqId, ibcontract_pattern)

        ## Run until we get a valid contract(s) or get bored waiting
        new_contract_details_list = contract_details_queue.get(timeout=MAX_WAIT_HISTORICAL_DATA_SECONDS)

        self.ib_clear_req_id(reqId)

        while self.broker_is_error():
            log.warn(self.broker_get_error()) ## WHITELIST?

        if contract_details_queue.timed_out():
            self.log.msg("Exceeded maximum wait for wrapper to confirm finished - seems to be normal behaviour")

        ibcontract_list = [contract_details.contract for contract_details in new_contract_details_list]

        return ibcontract_list
示例#2
0
    def ib_get_historical_data(self,
                               ibcontract,
                               durationStr="1 Y",
                               barSizeSetting="1 day",
                               whatToShow="TRADES",
                               log=None):
        """
        Returns historical prices for a contract, up to today
        ibcontract is a Contract
        :returns list of prices in 4 tuples: Open high low close volume
        """

        if log is None:
            log = self.log

        last_call = self.last_historic_price_calltime
        avoid_pacing_violation(last_call, log=log)

        tickerid = self.ib_next_req_id()
        ## Make a place to store the data we're going to return
        historic_data_queue = finishableQueue(
            self.init_historicprices(tickerid))

        # Request some historical data. Native method in EClient
        self.reqHistoricalData(
            tickerid,  # tickerId,
            ibcontract,  # contract,
            datetime.datetime.today().strftime(
                "%Y%m%d %H:%M:%S %Z"),  # endDateTime,
            durationStr,  # durationStr,
            barSizeSetting,  # barSizeSetting,
            whatToShow,  # whatToShow,
            1,  # useRTH,
            1,  # formatDate
            False,  # KeepUpToDate <<==== added for api 9.73.2
            []  ## chartoptions not used
        )

        ## Wait until we get a completed data, an error, or get bored waiting
        log.msg(
            "Getting historical data from the server... could take %d seconds to complete "
            % MAX_WAIT_HISTORICAL_DATA_SECONDS)

        historic_data = historic_data_queue.get(
            timeout=MAX_WAIT_HISTORICAL_DATA_SECONDS)

        while self.wrapper.broker_is_error():
            # WHITELIST
            log.warn(self.broker_get_error())

        if historic_data_queue.timed_out():
            log.msg(
                "Exceeded maximum wait for wrapper to confirm finished - seems to be normal behaviour"
            )

        self.cancelHistoricalData(tickerid)
        self.ib_clear_req_id(tickerid)
        self.last_historic_price_calltime = datetime.datetime.now()

        return historic_data
示例#3
0
    def ib_resolve_contract(self, ibcontract, log=None):

        if log is None:
            log = self.log

        reqId = self.ib_next_req_id()
        ## Make a place to store the data we're going to return
        contract_details_queue = finishableQueue(
            self.init_contractdetails(reqId))

        log.msg("Getting full contract details from the server... ")

        self.reqContractDetails(reqId, ibcontract)

        ## Run until we get a valid contract(s) or get bored waiting

        new_contract_details = contract_details_queue.get(
            timeout=MAX_WAIT_SECONDS)

        self.ib_clear_req_id(reqId)

        while self.broker_is_error():
            self.log.warn(self.broker_get_error())  ## WHITELIST?

        if contract_details_queue.timed_out():
            self.log.msg(
                "Exceeded maximum wait for wrapper to confirm finished - seems to be normal behaviour"
            )

        if len(new_contract_details) == 0:
            self.log.warn(
                "Failed to get additional contract details: returning unresolved contract"
            )
            return ibcontract

        if len(new_contract_details) > 1:
            self.log.warn("Got multiple contracts using first one")

        new_contract_details = new_contract_details[0]

        resolved_ibcontract = new_contract_details.contract

        return resolved_ibcontract