Пример #1
0
    def lookup_symbol(self, symbol, as_of_date, fuzzy=False):
        """
        Return matching Equity of name symbol in database.

        If multiple Equities are found and as_of_date is not set,
        raises MultipleSymbolsFound.

        If no Equity was active at as_of_date raises SymbolNotFound.
        """
        company_symbol, share_class_symbol, fuzzy_symbol = split_delimited_symbol(symbol)
        if as_of_date:
            # Format inputs
            as_of_date = pd.Timestamp(as_of_date).normalize()
            ad_value = as_of_date.value

            if fuzzy:
                # Search for a single exact match on the fuzzy column
                candidates = self._get_fuzzy_candidates_in_range(fuzzy_symbol, ad_value)

                # If exactly one SID exists for fuzzy_symbol, return that sid
                if len(candidates) == 1:
                    return self._get_best_candidate(candidates)

            # Search for exact matches of the split-up company_symbol and
            # share_class_symbol
            candidates = self._get_split_candidates_in_range(company_symbol, share_class_symbol, ad_value)

            # If exactly one SID exists for symbol, return that symbol
            # If multiple SIDs exist for symbol, return latest start_date with
            # end_date as a tie-breaker
            if candidates:
                return self._get_best_candidate(candidates)

            # If no SID exists for symbol, return SID with the
            # highest-but-not-over end_date
            elif not candidates:
                candidates = self._resolve_no_matching_candidates(company_symbol, share_class_symbol, ad_value)
                if candidates:
                    return self._get_best_candidate(candidates)

            raise SymbolNotFound(symbol=symbol)

        else:
            # If this is a fuzzy look-up, check if there is exactly one match
            # for the fuzzy symbol
            if fuzzy:
                candidates = self._get_fuzzy_candidates(fuzzy_symbol)
                if len(candidates) == 1:
                    return self._get_best_candidate(candidates)

            candidates = self._get_split_candidates(company_symbol, share_class_symbol)
            if len(candidates) == 1:
                return self._get_best_candidate(candidates)
            elif not candidates:
                raise SymbolNotFound(symbol=symbol)
            else:
                raise MultipleSymbolsFound(symbol=symbol, options=self._get_equities_from_candidates(candidates))
Пример #2
0
    def lookup_symbol(self, symbol, as_of_date, fuzzy=False):
        """
        Return matching Equity of name symbol in database.

        If multiple Equities are found and as_of_date is not set,
        raises MultipleSymbolsFound.

        If no Equity was active at as_of_date raises SymbolNotFound.
        """
        company_symbol, share_class_symbol, fuzzy_symbol = \
            split_delimited_symbol(symbol)
        if as_of_date:
            # Format inputs
            as_of_date = pd.Timestamp(as_of_date).normalize()
            ad_value = as_of_date.value

            if fuzzy:
                # Search for a single exact match on the fuzzy column
                candidates = self._get_fuzzy_candidates_in_range(fuzzy_symbol,
                                                                 ad_value)

                # If exactly one SID exists for fuzzy_symbol, return that sid
                if len(candidates) == 1:
                    return self._get_best_candidate(candidates)

            # Search for exact matches of the split-up company_symbol and
            # share_class_symbol
            candidates = self._get_split_candidates_in_range(
                company_symbol,
                share_class_symbol,
                ad_value
            )

            # If exactly one SID exists for symbol, return that symbol
            # If multiple SIDs exist for symbol, return latest start_date with
            # end_date as a tie-breaker
            if candidates:
                return self._get_best_candidate(candidates)

            # If no SID exists for symbol, return SID with the
            # highest-but-not-over end_date
            elif not candidates:
                candidates = self._resolve_no_matching_candidates(
                    company_symbol,
                    share_class_symbol,
                    ad_value
                )
                if candidates:
                    return self._get_best_candidate(candidates)

            raise SymbolNotFound(symbol=symbol)

        else:
            # If this is a fuzzy look-up, check if there is exactly one match
            # for the fuzzy symbol
            if fuzzy:
                candidates = self._get_fuzzy_candidates(fuzzy_symbol)
                if len(candidates) == 1:
                    return self._get_best_candidate(candidates)

            candidates = self._get_split_candidates(company_symbol,
                                                    share_class_symbol)
            if len(candidates) == 1:
                return self._get_best_candidate(candidates)
            elif not candidates:
                raise SymbolNotFound(symbol=symbol)
            else:
                raise MultipleSymbolsFound(
                    symbol=symbol,
                    options=self._get_equities_from_candidates(candidates)
                )
Пример #3
0
    def lookup_symbol(self, symbol, as_of_date, fuzzy=False):
        """
        Return matching Equity of name symbol in database.

        If multiple Equities are found and as_of_date is not set,
        raises MultipleSymbolsFound.

        If no Equity was active at as_of_date raises SymbolNotFound.
        """

        # Format inputs
        symbol = symbol.upper()
        if as_of_date is not None:
            as_of_date = pd.Timestamp(normalize_date(as_of_date))

        company_symbol, share_class_symbol, fuzzy_symbol = \
            split_delimited_symbol(symbol)

        equities_cols = self.equities.c
        if as_of_date:
            ad_value = as_of_date.value

            if fuzzy:
                # Search for a single exact match on the fuzzy column
                fuzzy_candidates = sa.select((equities_cols.sid,)).where(
                    (equities_cols.fuzzy_symbol == fuzzy_symbol) &
                    (equities_cols.start_date <= ad_value) &
                    (equities_cols.end_date >= ad_value),
                ).execute().fetchall()

                # If exactly one SID exists for fuzzy_symbol, return that sid
                if len(fuzzy_candidates) == 1:
                    return self._retrieve_equity(fuzzy_candidates[0]['sid'])

            # Search for exact matches of the split-up company_symbol and
            # share_class_symbol
            candidates = sa.select((equities_cols.sid,)).where(
                (equities_cols.company_symbol == company_symbol) &
                (equities_cols.share_class_symbol == share_class_symbol) &
                (equities_cols.start_date <= ad_value) &
                (equities_cols.end_date >= ad_value),
            ).execute().fetchall()

            # If exactly one SID exists for symbol, return that symbol
            if len(candidates) == 1:
                return self._retrieve_equity(candidates[0]['sid'])

            # If no SID exists for symbol, return SID with the
            # highest-but-not-over end_date
            elif not candidates:
                sid = sa.select((equities_cols.sid,)).where(
                    (equities_cols.company_symbol == company_symbol) &
                    (equities_cols.share_class_symbol == share_class_symbol) &
                    (equities_cols.start_date <= ad_value),
                ).order_by(
                    equities_cols.end_date.desc(),
                ).scalar()
                if sid is not None:
                    return self._retrieve_equity(sid)

            # If multiple SIDs exist for symbol, return latest start_date with
            # end_date as a tie-breaker
            elif len(candidates) > 1:
                sid = sa.select((equities_cols.sid,)).where(
                    (equities_cols.company_symbol == company_symbol) &
                    (equities_cols.share_class_symbol == share_class_symbol) &
                    (equities_cols.start_date <= ad_value),
                ).order_by(
                    equities_cols.start_date.desc(),
                    equities_cols.end_date.desc(),
                ).scalar()
                if sid is not None:
                    return self._retrieve_equity(sid)

            raise SymbolNotFound(symbol=symbol)

        else:
            # If this is a fuzzy look-up, check if there is exactly one match
            # for the fuzzy symbol
            if fuzzy:
                fuzzy_sids = sa.select((equities_cols.sid,)).where(
                    (equities_cols.fuzzy_symbol == fuzzy_symbol)
                ).execute().fetchall()
                if len(fuzzy_sids) == 1:
                    return self._retrieve_equity(fuzzy_sids[0]['sid'])

            sids = sa.select((equities_cols.sid,)).where(
                (equities_cols.company_symbol == company_symbol) &
                (equities_cols.share_class_symbol == share_class_symbol)
            ).execute().fetchall()
            if len(sids) == 1:
                return self._retrieve_equity(sids[0]['sid'])
            elif not sids:
                raise SymbolNotFound(symbol=symbol)
            else:
                raise MultipleSymbolsFound(
                    symbol=symbol,
                    options=list(map(
                        compose(self._retrieve_equity, itemgetter('sid')),
                        sids,
                    ))
                )
Пример #4
0
    def lookup_symbol(self, symbol, as_of_date, fuzzy=False):
        """
        Return matching Equity of name symbol in database.

        If multiple Equities are found and as_of_date is not set,
        raises MultipleSymbolsFound.

        If no Equity was active at as_of_date raises SymbolNotFound.
        """

        # Format inputs
        if as_of_date is not None:
            as_of_date = pd.Timestamp(normalize_date(as_of_date))

        company_symbol, share_class_symbol, fuzzy_symbol = \
            split_delimited_symbol(symbol)

        equities_cols = self.equities.c
        if as_of_date:
            ad_value = as_of_date.value

            if fuzzy:
                # Search for a single exact match on the fuzzy column
                fuzzy_candidates = sa.select((equities_cols.sid, )).where(
                    (equities_cols.fuzzy_symbol == fuzzy_symbol) &
                    (equities_cols.start_date <= ad_value) &
                    (equities_cols.end_date >=
                     ad_value), ).execute().fetchall()

                # If exactly one SID exists for fuzzy_symbol, return that sid
                if len(fuzzy_candidates) == 1:
                    return self._retrieve_equity(fuzzy_candidates[0]['sid'])

            # Search for exact matches of the split-up company_symbol and
            # share_class_symbol
            candidates = sa.select((equities_cols.sid, )).where(
                (equities_cols.company_symbol == company_symbol) &
                (equities_cols.share_class_symbol == share_class_symbol) &
                (equities_cols.start_date <= ad_value) &
                (equities_cols.end_date >= ad_value), ).execute().fetchall()

            # If exactly one SID exists for symbol, return that symbol
            if len(candidates) == 1:
                return self._retrieve_equity(candidates[0]['sid'])

            # If no SID exists for symbol, return SID with the
            # highest-but-not-over end_date
            elif not candidates:
                sid = sa.select((equities_cols.sid, )).where(
                    (equities_cols.company_symbol == company_symbol) &
                    (equities_cols.share_class_symbol == share_class_symbol) &
                    (equities_cols.start_date <= ad_value), ).order_by(
                        equities_cols.end_date.desc(), ).scalar()
                if sid is not None:
                    return self._retrieve_equity(sid)

            # If multiple SIDs exist for symbol, return latest start_date with
            # end_date as a tie-breaker
            elif len(candidates) > 1:
                sid = sa.select((equities_cols.sid, )).where(
                    (equities_cols.company_symbol == company_symbol) &
                    (equities_cols.share_class_symbol == share_class_symbol) &
                    (equities_cols.start_date <= ad_value), ).order_by(
                        equities_cols.start_date.desc(),
                        equities_cols.end_date.desc(),
                    ).scalar()
                if sid is not None:
                    return self._retrieve_equity(sid)

            raise SymbolNotFound(symbol=symbol)

        else:
            # If this is a fuzzy look-up, check if there is exactly one match
            # for the fuzzy symbol
            if fuzzy:
                fuzzy_sids = sa.select((equities_cols.sid, )).where(
                    (equities_cols.fuzzy_symbol == fuzzy_symbol
                     )).execute().fetchall()
                if len(fuzzy_sids) == 1:
                    return self._retrieve_equity(fuzzy_sids[0]['sid'])

            sids = sa.select((equities_cols.sid, )).where(
                (equities_cols.company_symbol == company_symbol)
                & (equities_cols.share_class_symbol == share_class_symbol)
            ).execute().fetchall()
            if len(sids) == 1:
                return self._retrieve_equity(sids[0]['sid'])
            elif not sids:
                raise SymbolNotFound(symbol=symbol)
            else:
                raise MultipleSymbolsFound(symbol=symbol,
                                           options=list(
                                               map(
                                                   compose(
                                                       self._retrieve_equity,
                                                       itemgetter('sid')),
                                                   sids,
                                               )))