def make_pipeline(context):
    mask = Q500US()
    #lb_13 = -Returns(window_length=13, mask=mask)

    weekly_return = Returns(window_length=6, mask=mask)
    pipe = Pipeline(
        columns={
            'weekly_return': weekly_return,
            'sector': Sector(),
        },
        # combined_alpha will be NaN for all stocks not in our universe,
        # but we also want to make sure that we have a sector code for everything
        # we trade.
        screen=weekly_return.notnull() & Sector().notnull(),
    )
    return pipe
def make_pipeline():
    """
    A function that creates and returns our pipeline.

    We break this piece of logic out into its own function to make it easier to
    test and modify in isolation. In particular, this function can be
    copy/pasted into research and run by itself.

    Returns
    -------
    pipe : Pipeline
        Represents computation we would like to perform on the assets that make
        it through the pipeline screen.
    """
    
    sentiment_score = SimpleMovingAverage(
        inputs=[stocktwits.bull_minus_bear],
        window_length=7,
    )
    
    Asset_Growth_7d = Returns(window_length=8);
    
    eveb = morningstar.valuation_ratios.ev_to_ebitda.latest
    
    cash_return = morningstar.valuation_ratios.cash_return.latest
    
    total_yield = morningstar.valuation_ratios.total_yield.latest

    #universe = Q1500US()
    universe = QTradableStocksUS()
    
    # We winsorize our factor values in order to lessen the impact of outliers
    # For more information on winsorization, please see
    # https://en.wikipedia.org/wiki/Winsorizing
    
    sentiment_score_winsorized = sentiment_score.winsorize(min_percentile=0.05,                                                                             max_percentile=0.95)
    
    Asset_Growth_7d_winsorized = Asset_Growth_7d.winsorize(min_percentile=0.05, max_percentile=0.95)
    
    eveb_winsorized = eveb.winsorize(min_percentile=0.05, max_percentile=0.95)
    cash_return_winsorized = cash_return.winsorize(min_percentile=0.05, max_percentile=0.95)
    total_yield_winsorized = total_yield.winsorize(min_percentile=0.05, max_percentile=0.95)

    # Here we combine our winsorized factors, z-scoring them to equalize their influence
    """
    combined_factor = -0.3*Asset_Growth_7d_winsorized.zscore() -0.25*sentiment_score_winsorized.zscore() +0.05*eveb_winsorized.zscore() +0.05*cash_return_winsorized.zscore() +0.35*total_yield_winsorized.zscore();
    """
   
    combined_factor = -0.3*Asset_Growth_7d_winsorized.zscore() -0.2*sentiment_score_winsorized.zscore() +0.1*eveb_winsorized.zscore() +0.1*cash_return_winsorized.zscore() +0.3*total_yield_winsorized.zscore();
    

    # Build Filters representing the top and bottom baskets of stocks by our
    # combined ranking system. We'll use these as our tradeable universe each
    # day.
    longs = combined_factor.top(TOTAL_POSITIONS//2, mask=universe)
    shorts = combined_factor.bottom(TOTAL_POSITIONS//2, mask=universe)

    # The final output of our pipeline should only include
    # the top/bottom 300 stocks by our criteria
    long_short_screen = (longs | shorts)

    # Create pipeline
    pipe = Pipeline(
        columns={
            'longs': longs,
            'shorts': shorts,
            'combined_factor': combined_factor
        },
        screen=long_short_screen & sentiment_score.notnull() & Asset_Growth_7d.notnull(),
    )
    return pipe