Exemple #1
0
    def _get_generic_data_for_contract(
            self,
            ibcontract: ibContract,
            log: logger = None,
            bar_freq: Frequency = DAILY_PRICE_FREQ,
            whatToShow: str = "TRADES") -> pd.DataFrame:
        """
        Get historical daily data

        :param contract_object_with_ib_data: contract where instrument has ib metadata
        :param freq: str; one of D, H, 5M, M, 10S, S
        :return: futuresContractPriceData
        """
        if log is None:
            log = self.log

        try:
            barSizeSetting, durationStr = _get_barsize_and_duration_from_frequency(
                bar_freq)
        except Exception as exception:
            log.warn(exception)
            return missing_data

        price_data_raw = self._ib_get_historical_data_of_duration_and_barSize(
            ibcontract,
            durationStr=durationStr,
            barSizeSetting=barSizeSetting,
            whatToShow=whatToShow,
            log=log,
        )

        price_data_as_df = self._raw_ib_data_to_df(
            price_data_raw=price_data_raw, log=log)

        return price_data_as_df
    def ib_resolve_unique_contract(self,
                                   ibcontract_pattern,
                                   log: logger = None):
        """
        Returns the 'resolved' IB contract based on a pattern. We expect a unique contract.

        This is used for FX only, since for futures things are potentially funkier

        :param ibcontract_pattern: ibContract
        :param log: log object
        :return: ibContract or missing_contract
        """
        if log is None:
            log = self.log

        contract_chain = self.ib_get_contract_chain(ibcontract_pattern)

        if len(contract_chain) > 1:
            log.warn(
                "Got multiple contracts for %s when only expected a single contract: Check contract date"
                % str(ibcontract_pattern))
            return missing_contract

        if len(contract_chain) == 0:
            log.warn("Failed to resolve contract %s" % str(ibcontract_pattern))
            return missing_contract

        resolved_contract = contract_chain[0]

        return resolved_contract
Exemple #3
0
    def _raw_ib_data_to_df(
        self, price_data_raw: pd.DataFrame, log: logger
    ) -> pd.DataFrame:

        if price_data_raw is None:
            log.warn("No price data from IB")
            return missing_data

        price_data_as_df = price_data_raw[["open", "high", "low", "close", "volume"]]

        price_data_as_df.columns = ["OPEN", "HIGH", "LOW", "FINAL", "VOLUME"]

        date_index = [
            self._ib_timestamp_to_datetime(price_row)
            for price_row in price_data_raw["date"]
        ]
        price_data_as_df.index = date_index

        return price_data_as_df