Пример #1
0
    def test_all(self):

        data = np.array(
            [
                [1, 1, 1, 1, 1, 1],
                [0, 1, 1, 1, 1, 1],
                [1, 0, 1, 1, 1, 1],
                [1, 1, 0, 1, 1, 1],
                [1, 1, 1, 0, 1, 1],
                [1, 1, 1, 1, 0, 1],
                [1, 1, 1, 1, 1, 0],
            ],
            dtype=bool,
        )

        # With a window_length of N, 0's should be "sticky" for the (N - 1)
        # days after the 0 in the base data.

        # Note that, the way ``self.run_graph`` works, we compute the same
        # number of output rows for all inputs, so we only get the last 4
        # outputs for expected_3 even though we have enought input data to
        # compute 5 rows.
        expected_3 = np.array(
            [
                [0, 0, 0, 1, 1, 1],
                [1, 0, 0, 0, 1, 1],
                [1, 1, 0, 0, 0, 1],
                [1, 1, 1, 0, 0, 0],
            ],
            dtype=bool,
        )

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

        class Input(Filter):
            inputs = ()
            window_length = 0

        self.check_terms(
            terms={
                "3": All(inputs=[Input()], window_length=3),
                "4": All(inputs=[Input()], window_length=4),
            },
            expected={
                "3": expected_3,
                "4": expected_4,
            },
            initial_workspace={Input(): data},
            mask=self.build_mask(np.ones(shape=data.shape)),
        )
Пример #2
0
    def test_at_least_N(self):

        # With a window_length of K, AtLeastN should return 1
        # if N or more 1's exist in the lookback window

        # This smoothing filter gives customizable "stickiness"

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

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

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

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

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

        class Input(Filter):
            inputs = ()
            window_length = 0

        all_but_one = AtLeastN(inputs=[Input()], window_length=4, N=3)

        all_but_two = AtLeastN(inputs=[Input()], window_length=4, N=2)

        any_equiv = AtLeastN(inputs=[Input()], window_length=4, N=1)

        all_equiv = AtLeastN(inputs=[Input()], window_length=4, N=4)

        results = self.run_graph(
            TermGraph({
                'AllButOne': all_but_one,
                'AllButTwo': all_but_two,
                'AnyEquiv': any_equiv,
                'AllEquiv': all_equiv,
                'Any': Any(inputs=[Input()], window_length=4),
                'All': All(inputs=[Input()], window_length=4)
            }),
            initial_workspace={Input(): data},
            mask=self.build_mask(ones(shape=data.shape)),
        )

        check_arrays(results['Any'], expected_1)
        check_arrays(results['AnyEquiv'], expected_1)
        check_arrays(results['AllButTwo'], expected_2)
        check_arrays(results['AllButOne'], expected_3)
        check_arrays(results['All'], expected_4)
        check_arrays(results['AllEquiv'], expected_4)
Пример #3
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
Пример #4
0
    def test_all(self):

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

        # With a window_length of N, 0's should be "sticky" for the (N - 1)
        # days after the 0 in the base data.

        # Note that, the way ``self.run_graph`` works, we compute the same
        # number of output rows for all inputs, so we only get the last 4
        # outputs for expected_3 even though we have enought input data to
        # compute 5 rows.
        expected_3 = array([[0, 0, 0, 1, 1, 1], [1, 0, 0, 0, 1, 1],
                            [1, 1, 0, 0, 0, 1], [1, 1, 1, 0, 0, 0]],
                           dtype=bool)

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

        class Input(Filter):
            inputs = ()
            window_length = 0

        results = self.run_graph(
            TermGraph({
                '3': All(inputs=[Input()], window_length=3),
                '4': All(inputs=[Input()], window_length=4),
            }),
            initial_workspace={Input(): data},
            mask=self.build_mask(ones(shape=data.shape)),
        )

        check_arrays(results['3'], expected_3)
        check_arrays(results['4'], expected_4)
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
Пример #6
0
    def test_at_least_N(self):

        # With a window_length of K, AtLeastN should return 1
        # if N or more 1's exist in the lookback window

        # This smoothing filter gives customizable "stickiness"

        data = np.array(
            [
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 0, 0, 0, 0],
                [1, 0, 0, 0, 0, 0],
            ],
            dtype=bool,
        )

        expected_1 = np.array(
            [
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 0, 0],
            ],
            dtype=bool,
        )

        expected_2 = np.array(
            [
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 0, 0, 0],
            ],
            dtype=bool,
        )

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

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

        class Input(Filter):
            inputs = ()
            window_length = 0

        all_but_one = AtLeastN(inputs=[Input()], window_length=4, N=3)

        all_but_two = AtLeastN(inputs=[Input()], window_length=4, N=2)

        any_equiv = AtLeastN(inputs=[Input()], window_length=4, N=1)

        all_equiv = AtLeastN(inputs=[Input()], window_length=4, N=4)

        self.check_terms(
            terms={
                "AllButOne": all_but_one,
                "AllButTwo": all_but_two,
                "AnyEquiv": any_equiv,
                "AllEquiv": all_equiv,
                "Any": Any(inputs=[Input()], window_length=4),
                "All": All(inputs=[Input()], window_length=4),
            },
            expected={
                "Any": expected_1,
                "AnyEquiv": expected_1,
                "AllButTwo": expected_2,
                "AllButOne": expected_3,
                "All": expected_4,
                "AllEquiv": expected_4,
            },
            initial_workspace={Input(): data},
            mask=self.build_mask(np.ones(shape=data.shape)),
        )