Exemple #1
0
    def contracts_with_price_data_for_instrument_code(self, instrument_code):
        """
        Valid contracts for a given instrument code

        :param instrument_code: str
        :return: list of contracts
        """
        new_log = self.log.setup(instrument_code=instrument_code)

        instrument_object_with_ib_config = self._get_instrument_object_with_IB_metadata(
            instrument_code)
        if instrument_object_with_ib_config is missing_instrument:
            new_log.warn(
                "Can't get list of contracts for illdefined instrument %s" %
                instrument_code,
                instrument_code=instrument_code)
            return listOfFuturesContracts([])

        list_of_contract_dates = self.ibconnection.broker_get_futures_contract_list(
            instrument_object_with_ib_config)
        list_of_contracts = [
            futuresContract(instrument_code, contract_date)
            for contract_date in list_of_contract_dates
        ]

        return listOfFuturesContracts(list_of_contracts)
def get_contract_chain(instrument_code, data):
    diag_contracts = diagContracts(data)
    diag_prices = diagPrices(data)

    roll_parameters = diag_contracts.get_roll_parameters(instrument_code)

    # Get the last contract currently being used
    multiple_prices = diag_prices.get_multiple_prices(instrument_code)
    current_contract_dict = multiple_prices.current_contract_dict()
    current_contract_list = list(current_contract_dict.values())
    furthest_out_contract_date = max(current_contract_list)
    furthest_out_contract = contractDateWithRollParameters(
        roll_parameters, furthest_out_contract_date)

    # To give us wiggle room, and ensure we start collecting the new forward a
    # little in advance
    final_contract = furthest_out_contract.next_priced_contract()

    contract_date_chain = (
        final_contract.get_unexpired_contracts_from_now_to_contract_date())

    # We have a list of contract_date objects, need futureContracts
    # create a 'bare' instrument object
    instrument_object = futuresInstrument(instrument_code)

    contract_object_chain_as_list = [
        futuresContract(instrument_object, contract_date_object)
        for contract_date_object in contract_date_chain
    ]

    contract_object_chain = listOfFuturesContracts(
        contract_object_chain_as_list)

    return contract_object_chain