from zipline.pipeline import EquityPricing, master, sharadar
from zipline.pipeline.factors import AverageDollarVolume, Latest
def InitialUniverse():
"""
Returns a Pipeline filter that can be used as the
initial universe for a tradable stocks filter. The
initial universe consists of:
- common stocks only (no preferred stocks, ADRs, LPs, or ETFs)
- primary shares only
"""
common_stock = master.SecuritiesMaster.usstock_SecurityType2.latest.eq('Common Stock')
is_primary_share = master.SecuritiesMaster.usstock_PrimaryShareSid.latest.isnull()
initial_universe = common_stock & is_primary_share
return initial_universe
def TradableStocksUS(market_cap_filter=False):
"""
Returns a Pipeline filter of tradable stocks, defined as:
- 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.
This filter is intended to be used in conjunction with the filter
returns by InitialUniverse, above.
"""
tradable_stocks = AverageDollarVolume(window_length=200) >= 2.5e6
tradable_stocks = Latest(EquityPricing.close, mask=tradable_stocks) > 5
tradable_stocks = EquityPricing.close.all_present(200, mask=tradable_stocks)
has_volume = EquityPricing.volume.latest > 0
tradable_stocks = has_volume.all(200, mask=tradable_stocks)
if market_cap_filter:
tradable_stocks = Latest([sharadar.Fundamentals.slice(dimension='ARQ', period_offset=0).MARKETCAP], mask=tradable_stocks) >= 500e6
return tradable_stocks