Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Beginner filtering questions?

I could really use some help in some beginner problems. This is a shorting strategy. If you can help with either one or all of the problems below I would really appreciate it! I'm trying to accomplish a few things with my algorithm:

  1. Filter out to select only stocks that are in the NYSE or NASDAQ.
  2. Small cap stocks only. (I'll try this on micro cap as well eventually).
  3. Spend no more than $900 on any one position.

Any help would be much appreciated. Thanks!
-Ben

1 response

Something like the following should filter to just the NYSE and NASDAQ (note that in the Morningstar data there is a switchover in naming from NYSE to NYS so check for both). You can also easily make filters out factors with typical > and < operators.

def my_pipeline(context):  
    '''  
    Define the pipline data columns  
    '''  
    # Create filter for just the stocks we want to trade  
    universe = Filters.default_us_equity_universe_mask()

    # Create any factors or classifiers we need (use a mask to potentially speed up the query)  
    market_cap = Factors.morningstar.MarketCap(mask = universe)  
    exchange = morningstar.share_class_reference.exchange_id.latest

    # Create any filters based upon above factors  
    # This assumes 'small cap' is defined as between 300M and 2B  
    small_cap = (market_cap > 300e+6) & (market_cap < 2000e6)  
    my_exchanges = exchange.element_of(['NYSE','NYS','NAS'])

    return Pipeline(  
    columns={  
        'market_cap': market_cap  
    },  
    screen = universe & my_exchanges & small_cap  
    )

Not sure what you mean by "Spend no more than $900 on any one position". If it's that you don't wan't the stock price to be more than $900 then create another filter like this

less_than_900 = USEquityPricing.close.latest < 900

However, if you only ever want to open a position worth no more than $900, then that's a bit trickier. How do you want to distribute your available cash? If you have $900 in cash and there are 10 potential stocks do you want to open $900 of the one? Which one? Or, do you want to open a $90 position of each. What if the stock price is more than $90 in that case? Do you want to account for rounding and use up as much of the $900 as possible (but then you may have a different percentage of each).

I'd recommend maybe not going down that path.