Beispiel #1
0
def make_pipeline(context):
    """
    Builds a Pipeline with Bollinger bands for top NUM_SYMBOLS stocks by market cap in the tech sector.
    Starts out from the Q500US base universe.
    """

    # Base universe of top 500 US stocks.
    base_universe_filter = Q500US()

    # Stocks of only tech sector.
    tech_sector = Sector(mask=base_universe_filter)
    tech_universe_filter = base_universe_filter & tech_sector.eq(311)

    # Top 10 tech stocks with largest market cap.
    mkt_cap_filter = morningstar.valuation.market_cap.latest
    top_mkt_cap_tech_filter = mkt_cap_filter.top(context.NUM_SYMBOLS,
                                                 mask=tech_universe_filter)

    # Bollinger band factor with Stdev factor 2.
    lower_band_factor, middle_factor, upper_band_factor = BollingerBands(
        window_length=22, k=2, mask=top_mkt_cap_tech_filter)

    # Percent difference between (price, lower_band) and (price, upper_band).
    price = USEquityPricing.close.latest
    buy_percent_factor = ((lower_band_factor - price) * 100) / price
    sell_percent_factor = ((price - upper_band_factor) * 100) / price

    # Mean reversion buy and sell filters.
    # Sell when price exceeds upper-band and buy when price is below lower-band.
    buy_filter = buy_percent_factor > 0
    sell_filter = sell_percent_factor > 0

    # Build and return the Pipeline.
    pipe_bbands = Pipeline(columns={
        'buy_percent': buy_percent_factor,
        'lower_band': lower_band_factor,
        'buy': buy_filter,
        'price': price,
        'sell': sell_filter,
        'upper_band': upper_band_factor,
        'sell_percent': sell_percent_factor
    },
                           screen=top_mkt_cap_tech_filter)

    return pipe_bbands
Beispiel #2
0
def make_pipeline(context):
    ## symbol universe
    base_universe = Q500US() if False else Q1500US()

    ## filters
    # Filter for primary share equities. IsPrimaryShare is a built-in filter.
    primary_share = IsPrimaryShare()
    # Equities listed as common stock (as opposed to, say, preferred stock).
    # 'ST00000001' indicates common stock.
    common_stock = morningstar.share_class_reference.security_type.latest.eq('ST00000001')
    # Non-depositary receipts. Recall that the ~ operator inverts filters,
    # turning Trues into Falses and vice versa
    not_depositary = ~morningstar.share_class_reference.is_depositary_receipt.latest
    # Equities not trading over-the-counter.
    not_otc = ~morningstar.share_class_reference.exchange_id.latest.startswith('OTC')
    # Not when-issued equities.
    not_wi = ~morningstar.share_class_reference.symbol.latest.endswith('.WI')
    # Equities without LP in their name, .matches does a match using a regular expression
    not_lp_name = ~morningstar.company_reference.standard_name.latest.matches('.* L[. ]?P.?$')
    # Equities with a null value in the limited_partnership Morningstar fundamental field.
    not_lp_balance_sheet = morningstar.balance_sheet.limited_partnership.latest.isnull()
    # Equities whose most recent Morningstar market cap is not null have fundamental data and therefore are not ETFs.
    have_market_cap = morningstar.valuation.market_cap.latest.notnull()
    is_cyclical = SuperSector().eq(SuperSector.CYCLICAL)
    is_defensive = SuperSector().eq(SuperSector.DEFENSIVE)
    is_sensitive = SuperSector().eq(SuperSector.SENSITIVE)

    # Filter for stocks that pass all of our previous filters.
    tradeable_stocks = (
        primary_share
        &common_stock
        &not_depositary
        &not_otc
        &not_wi
        &not_lp_name
        &not_lp_balance_sheet
        &have_market_cap
        #&(is_cyclical)
        #&(is_defensive)
        #&(is_sensitive)
        &(is_cyclical | is_defensive | is_sensitive)
    )

    # ToDo この範囲を色々変えてみる.
    dollar_volume = AverageDollarVolume(window_length=30)
    high_dollar_volume = dollar_volume.percentile_between(98, 100)
    daily_volatility = AnnualizedVolatility(window_length=20)/math.sqrt(252)
    sme20 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=20)
    rsi = RSI(inputs=[USEquityPricing.close])
    #sector = ms.asset_classification.morningstar_sector_code
    sector = Sector()
    static_sectors = (sector.eq(311) | 
                      sector.eq(309) | 
                      sector.eq(103) | 
                      sector.eq(101) | 
                      sector.eq(308) | 
                      sector.eq(206) )

    if context.static_asset:
        myscreen = StaticSids(context.special_sids) #context.special_assets
    else: 
        myscreen = (base_universe & tradeable_stocks & high_dollar_volume ) # & static_sectors
        

    pipe = Pipeline(
        columns={
            'prev_close': PrevClose(),
            'prev_volume': PrevVolume(),
            'prev_turnover': PrevClose()*PrevVolume(),
            'dollar_volume': dollar_volume,
            'high_dollar_volume': high_dollar_volume,
            'daily_volatility': daily_volatility,
            'sma20': sme20,
            'rsi': rsi,
            'morningstar_sector_code': sector,
        },
        screen=myscreen,
    )
    return pipe