Example #1
0
    def _receive_reference_response(self, fields):
        response_events = get_response_events(self._session)

        elements = []

        for ev in response_events:
            check_event_for_errors(ev)
            security_data_array = extract_security_data(ev)
            check_security_data_for_errors(security_data_array)

            for i in range(security_data_array.numValues()):
                security_data = security_data_array.getValueAsElement(i)
                check_security_data_for_errors(security_data)

                field_data_array = security_data.getElement(FIELD_DATA)

                for field_name in fields:
                    array = field_data_array.getElement(field_name)
                    for element in array.values():
                        keys_values_dict = {}
                        for elem in element.elements():
                            key = elem.name().__str__()
                            value = element.getElementAsString(key)
                            keys_values_dict[key] = value
                        elements.append(keys_values_dict)

        return elements
Example #2
0
    def _receive_reference_response(self, tickers, fields):
        response_events = get_response_events(self._session)

        tickers_fields_container = QFDataFrame(index=tickers, columns=fields)

        for ev in response_events:
            check_event_for_errors(ev)
            security_data_array = extract_security_data(ev)
            check_security_data_for_errors(security_data_array)

            for i in range(security_data_array.numValues()):
                security_data = security_data_array.getValueAsElement(i)
                check_security_data_for_errors(security_data)

                security_name = security_data.getElementAsString(SECURITY)
                ticker = BloombergTicker.from_string(security_name)
                field_data_array = security_data.getElement(FIELD_DATA)

                for field_name in fields:
                    try:
                        value = field_data_array.getElementAsFloat(field_name)
                    except blpapi.exception.InvalidConversionException:
                        value = field_data_array.getElementAsString(field_name)
                    except blpapi.exception.NotFoundException:
                        value = np.nan

                    tickers_fields_container.loc[ticker, field_name] = value

        return tickers_fields_container
    def _receive_historical_response(
            self, requested_tickers: Sequence[BloombergTicker],
            requested_fields: Sequence[str]):
        ticker_str_to_ticker: Dict[str, BloombergTicker] = {
            t.as_string(): t
            for t in requested_tickers
        }

        response_events = get_response_events(self._session)
        tickers_data_dict = defaultdict(
            lambda: QFDataFrame(columns=requested_fields))

        for event in response_events:
            try:
                check_event_for_errors(event)

                security_data = extract_security_data(event)
                check_security_data_for_errors(security_data)

                field_data_array = security_data.getElement(FIELD_DATA)
                dates = [
                    to_datetime(x.getElementAsDatetime(DATE))
                    for x in field_data_array.values()
                ]

                dates_fields_values = QFDataFrame(np.nan,
                                                  index=dates,
                                                  columns=requested_fields)

                for field_name in requested_fields:
                    dates_fields_values.loc[:, field_name] = [
                        self._get_float_or_nan(data_of_date_elem, field_name)
                        for data_of_date_elem in field_data_array.values()
                    ]
                security_name = security_data.getElementAsString(SECURITY)

                try:
                    ticker = ticker_str_to_ticker[security_name]
                    tickers_data_dict[ticker] = tickers_data_dict[
                        ticker].append(dates_fields_values)
                except KeyError:
                    self.logger.warning(
                        f"Received data for a ticker which was not present in the request: "
                        f"{security_name}. The data for that ticker will be excluded from parsing."
                    )
            except BloombergError as e:
                self.logger.error(e)

        return tickers_dict_to_data_array(tickers_data_dict,
                                          list(tickers_data_dict.keys()),
                                          requested_fields)
Example #4
0
    def _receive_historical_response(self, requested_tickers,
                                     requested_fields):
        response_events = get_response_events(self._session)

        # mapping: ticker -> DataArray[dates, fields]
        tickers_data_dict = dict()  # type: Dict[BloombergTicker, pd.DataFrame]

        for event in response_events:
            check_event_for_errors(event)
            security_data = extract_security_data(event)

            security_name = security_data.getElementAsString(SECURITY)
            ticker = BloombergTicker.from_string(security_name)

            try:
                check_security_data_for_errors(security_data)

                field_data_array = security_data.getElement(FIELD_DATA)
                field_data_list = [
                    field_data_array.getValueAsElement(i)
                    for i in range(field_data_array.numValues())
                ]
                dates = [
                    pd.to_datetime(x.getElementAsDatetime(DATE))
                    for x in field_data_list
                ]

                data = np.empty((len(dates), len(requested_fields)))
                data[:] = np.nan

                dates_fields_values = pd.DataFrame(data,
                                                   index=dates,
                                                   columns=requested_fields)

                for field_name in requested_fields:
                    dates_fields_values.loc[:, field_name] = [
                        self._get_float_or_nan(data_of_date_elem, field_name)
                        for data_of_date_elem in field_data_list
                    ]

                tickers_data_dict[ticker] = dates_fields_values

            except BloombergError:
                self.logger.exception(
                    "Error in the received historical response")

        return tickers_dict_to_data_array(tickers_data_dict, requested_tickers,
                                          requested_fields)
Example #5
0
    def _receive_futures_response(
        self,
        random_ticker_string_to_future_ticker: Dict[str,
                                                    BloombergFutureTicker]):
        response_events = get_response_events(self._session)
        ticker_to_futures_chain = {}

        def data_is_correct(data):
            if data.hasElement(SECURITY_ERROR):
                return False
            if security_data.hasElement(FIELD_EXCEPTIONS):
                field_exceptions = data.getElement(FIELD_EXCEPTIONS)
                if field_exceptions.numValues() > 0:
                    return False
            return True

        for event in response_events:
            check_event_for_errors(event)
            security_data_array = extract_security_data(event)

            for i in range(security_data_array.numValues()):

                security_data = security_data_array.getValueAsElement(i)

                if data_is_correct(security_data):
                    security_name = security_data.getElementAsString(SECURITY)
                    family_id_template = random_ticker_string_to_future_ticker[
                        security_name].family_id
                    # Family id are in the following form: CT{} Comdty -> {} should be replaced with a correct month
                    # code
                    regex_template = family_id_template.replace(
                        "{}", "[A-Z][0-9]{1,2}")

                    future_chain_array = security_data.getElement(
                        FIELD_DATA).getElement(FUT_CHAIN)
                    ticker_to_futures_chain[security_name] = []

                    for j in range(future_chain_array.numValues()):
                        future_ticker = future_chain_array.getValueAsElement(j)
                        ticker_value = future_ticker.getElementAsString(
                            "Security Description")
                        if re.match(regex_template, ticker_value):
                            ticker_to_futures_chain[security_name].append(
                                ticker_value)

        return ticker_to_futures_chain
Example #6
0
    def _receive_reference_response(self, tickers, fields):
        ticker_str_to_ticker: Dict[str, BloombergTicker] = {
            t.as_string(): t
            for t in tickers
        }

        response_events = get_response_events(self._session)
        tickers_fields_container = QFDataFrame(index=tickers, columns=fields)

        for ev in response_events:
            try:
                check_event_for_errors(ev)
                security_data_array = extract_security_data(ev)
                check_security_data_for_errors(security_data_array)

                for security_data in security_data_array.values():
                    check_security_data_for_errors(security_data)
                    field_data_array = security_data.getElement(FIELD_DATA)

                    security_name = security_data.getElementAsString(SECURITY)

                    try:
                        ticker = ticker_str_to_ticker[security_name]
                        for field_name in fields:
                            value = self._parse_value(field_data_array,
                                                      field_name)
                            tickers_fields_container.loc[ticker,
                                                         field_name] = value

                    except KeyError:
                        self.logger.warning(
                            f"Received data for a ticker which was not present in the request: "
                            f"{security_name}. The data for that ticker will be excluded from parsing."
                        )

            except BloombergError as e:
                self.logger.error(e)

        return tickers_fields_container