Exemple #1
0
    def test_all_present_classifier_input(self):
        """Test classifier factor input to `AllPresent`"""

        class SomeWindowSafeStringClassifier(Classifier):
            dtype = object_dtype
            inputs = ()
            window_length = 0
            missing_value = ""
            window_safe = True

        input_factor = SomeWindowSafeStringClassifier()

        shape = (10, 6)
        data = RandomState(6).choice(
            array(["a", "e", "i", "o", "u"], dtype=object_dtype),
            size=shape,
            replace=True,
        )
        data[eye(*shape, dtype=bool)] = input_factor.missing_value

        expected_3 = array(
            [
                [1, 0, 0, 0, 1, 1],
                [1, 1, 0, 0, 0, 1],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1],
            ],
            dtype=bool,
        )

        expected_4 = array(
            [
                [0, 0, 0, 0, 1, 1],
                [1, 0, 0, 0, 0, 1],
                [1, 1, 0, 0, 0, 0],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 1, 1],
            ],
            dtype=bool,
        )

        self.check_terms(
            terms={
                "3": AllPresent([input_factor], window_length=3),
                "4": AllPresent([input_factor], window_length=4),
            },
            expected={
                "3": expected_3,
                "4": expected_4,
            },
            initial_workspace={input_factor: data},
            mask=self.build_mask(ones(shape=shape)),
        )
Exemple #2
0
    def test_all_present_int_factor_input(self):
        """Test int factor input to `AllPresent`"""

        class SomeWindowSafeIntFactor(Factor):
            dtype = int64_dtype
            inputs = ()
            window_length = 0
            window_safe = True
            missing_value = 0

        input_factor = SomeWindowSafeIntFactor()

        shape = (10, 6)
        data = RandomState(5).choice(range(1, 5), size=shape, replace=True)
        data[eye(*shape, dtype=bool)] = input_factor.missing_value

        expected_3 = array(
            [
                [1, 0, 0, 0, 1, 1],
                [1, 1, 0, 0, 0, 1],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1],
            ],
            dtype=bool,
        )

        expected_4 = array(
            [
                [0, 0, 0, 0, 1, 1],
                [1, 0, 0, 0, 0, 1],
                [1, 1, 0, 0, 0, 0],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 1, 1],
            ],
            dtype=bool,
        )
        self.check_terms(
            terms={
                "3": AllPresent([input_factor], window_length=3),
                "4": AllPresent([input_factor], window_length=4),
            },
            expected={
                "3": expected_3,
                "4": expected_4,
            },
            initial_workspace={input_factor: data},
            mask=self.build_mask(ones(shape=shape)),
        )
Exemple #3
0
    def test_all_present_float_factor_input(self):
        """Test float factor input to `AllPresent`"""

        class SomeWindowSafeFactor(Factor):
            dtype = float64_dtype
            inputs = ()
            window_length = 0
            window_safe = True

        input_factor = SomeWindowSafeFactor()

        shape = (10, 6)
        data = self.randn_data(seed=10, shape=shape)
        data[eye(*shape, dtype=bool)] = input_factor.missing_value

        expected_3 = array(
            [
                [1, 0, 0, 0, 1, 1],
                [1, 1, 0, 0, 0, 1],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1],
            ],
            dtype=bool,
        )

        expected_4 = array(
            [
                [0, 0, 0, 0, 1, 1],
                [1, 0, 0, 0, 0, 1],
                [1, 1, 0, 0, 0, 0],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 1, 1],
            ],
            dtype=bool,
        )
        self.check_terms(
            terms={
                "3": AllPresent([input_factor], window_length=3),
                "4": AllPresent([input_factor], window_length=4),
            },
            expected={
                "3": expected_3,
                "4": expected_4,
            },
            initial_workspace={input_factor: data},
            mask=self.build_mask(ones(shape=shape)),
        )
Exemple #4
0
    def test_all_present_classifier_input(self):
        """Test classifier factor input to `AllPresent`
        """
        class SomeWindowSafeStringClassifier(Classifier):
            dtype = object_dtype
            inputs = ()
            window_length = 0
            missing_value = ''
            window_safe = True

        input_factor = SomeWindowSafeStringClassifier()

        shape = (10, 6)
        data = choice(
            array(['a', 'e', 'i', 'o', 'u'], dtype=object_dtype),
            size=shape,
            replace=True
        )
        data[eye(*shape, dtype=bool)] = input_factor.missing_value

        expected_3 = array([[1, 0, 0, 0, 1, 1],
                            [1, 1, 0, 0, 0, 1],
                            [1, 1, 1, 0, 0, 0],
                            [1, 1, 1, 1, 0, 0],
                            [1, 1, 1, 1, 1, 0],
                            [1, 1, 1, 1, 1, 1],
                            [1, 1, 1, 1, 1, 1]], dtype=bool)

        expected_4 = array([[0, 0, 0, 0, 1, 1],
                            [1, 0, 0, 0, 0, 1],
                            [1, 1, 0, 0, 0, 0],
                            [1, 1, 1, 0, 0, 0],
                            [1, 1, 1, 1, 0, 0],
                            [1, 1, 1, 1, 1, 0],
                            [1, 1, 1, 1, 1, 1]], dtype=bool)

        self.check_terms(
            terms={
                '3': AllPresent([input_factor], window_length=3),
                '4': AllPresent([input_factor], window_length=4),
            },
            expected={
                '3': expected_3,
                '4': expected_4,
            },
            initial_workspace={input_factor: data},
            mask=self.build_mask(ones(shape=shape))
        )
Exemple #5
0
def TradableStocksUS(sector):
    # Equities listed as common stock (not preferred stock, ETF, ADR, LP, etc)
    common_stock = master.SecuritiesMaster.usstock_SecurityType2.latest.eq(
        'Common Stock')

    # Filter for primary share equities; primary shares can be identified by a
    # null usstock_PrimaryShareSid field (i.e. no pointer to a primary share)
    is_primary_share = master.SecuritiesMaster.usstock_PrimaryShareSid.latest.isnull(
    )

    in_sector = master.SecuritiesMaster.usstock_Sector.latest.eq(sector)

    # combine the security type filters to begin forming our universe
    tradable_stocks = common_stock & is_primary_share & in_sector

    # also require high dollar volume
    tradable_stocks = AverageDollarVolume(
        window_length=200, mask=tradable_stocks).percentile_between(90, 100)

    # also require price > $5. Note that we use Latest(...) instead of EquityPricing.close.latest
    # so that we can pass a mask
    tradable_stocks = Latest([USEquityPricing.close],
                             mask=tradable_stocks) > 10

    # also require no missing data for 200 days
    tradable_stocks = AllPresent(inputs=[USEquityPricing.close],
                                 window_length=200,
                                 mask=tradable_stocks)
    tradable_stocks = All([USEquityPricing.volume.latest > 0],
                          window_length=200,
                          mask=tradable_stocks)

    return tradable_stocks
Exemple #6
0
    def test_all_present_filter_input(self):
        """Test error is raised when filter factor is input to `AllPresent`
        """
        with self.assertRaises(TypeError) as err:
            AllPresent([Mask()], window_length=4)

        self.assertEqual("Input to filter `AllPresent` cannot be a Filter.",
                         str(err.exception))
def TradableStocksUS(market_cap_filter=False):
    """
    Returns a Pipeline filter of tradable stocks, defined as:
    
    - Common stocks only (no preferred stocks, ADRs, LPs, or ETFs)
    - Primary shares only
    - 200-day average dollar volume >= $2.5M
    - price >= $5
    - 200 continuous days of price and volume.

    If market_cap_filter=True, also requires market cap > $500M.
    """
    # Equities listed as common stock (not preferred stock, ETF, ADR, LP, etc)
    common_stock = master.SecuritiesMaster.usstock_SecurityType2.latest.eq(
        'Common Stock')

    # Filter for primary share equities; primary shares can be identified by a
    # null usstock_PrimaryShareSid field (i.e. no pointer to a primary share)
    is_primary_share = master.SecuritiesMaster.usstock_PrimaryShareSid.latest.isnull(
    )

    # combine the security type filters to begin forming our universe
    tradable_stocks = common_stock & is_primary_share

    # also require high dollar volume
    tradable_stocks = AverageDollarVolume(window_length=200,
                                          mask=tradable_stocks) >= 2.5e6

    # also require price > $5. Note that we use Latest(...) instead of EquityPricing.close.latest
    # so that we can pass a mask
    tradable_stocks = Latest([EquityPricing.close], mask=tradable_stocks) > 5

    # also require no missing data for 200 days
    tradable_stocks = AllPresent(inputs=[EquityPricing.close],
                                 window_length=200,
                                 mask=tradable_stocks)
    tradable_stocks = All([EquityPricing.volume.latest > 0],
                          window_length=200,
                          mask=tradable_stocks)

    if market_cap_filter:
        # also require market cap over $500M
        tradable_stocks = Latest([
            sharadar.Fundamentals.slice(dimension='ARQ',
                                        period_offset=0).MARKETCAP
        ],
                                 mask=tradable_stocks) >= 500e6

    return tradable_stocks
Exemple #8
0
 def test_all_present_filter_input(self):
     """Test error is raised when filter factor is input to `AllPresent`"""
     expected_msg = "Input to filter `AllPresent` cannot be a Filter."
     with pytest.raises(TypeError, match=expected_msg):
         AllPresent([Mask()], window_length=4)