Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Running two pipelines for buying and selling

I'm trying to figure out how to search on two criteria. One to buy and one to sell. For example if I buy for pe 17. I'm trying to do this using QTradeableUS() and order_optimal_portfolio() as required by the contest

I can create the pipeline for the buy using this:

    base_universe = Q1500US()

    sentiment_score = SimpleMovingAverage(  
        inputs=[stocktwits.bull_minus_bear],  
        window_length=3,  
        mask=QTradableStocksUS()  
    )  
    # Factor of yesterday's close price.  
    yesterday_close = USEquityPricing.close.latest  
    pe_ratio = Fundamentals.pe_ratio.latest  
    pe_ratio_filter = (pe_ratio < 14)  & pe_ratio.notnull()  
    pipe = Pipeline(  
        screen = (base_universe & pe_ratio_filter & sentiment_score.notnull()),  
        ...  
    )  

And place the order using this:

    context.output = pipeline_output('my_pipeline')  
    context.max_leverage = 1.0  
    context.max_pos_size = .01  
    context.max_turnover = 0.95  
    objective = opt.MaximizeAlpha(  
      context.output['sentiment_score']  
    )

    # Create position size constraint  
    constrain_pos_size = opt.PositionConcentration.with_equal_bounds(  
        -context.max_pos_size,  
        context.max_pos_size  
    )

    # Ensure long and short books  
    # are roughly the same size  
    dollar_neutral = opt.DollarNeutral()

    # Constrain target portfolio's leverage  
    max_leverage = opt.MaxGrossExposure(context.max_leverage)

    # Constrain portfolio turnover  
    max_turnover = opt.MaxTurnover(context.max_turnover)

    # Rebalance portfolio using objective  
    # and list of constraints  
    order_optimal_portfolio(  
        objective=objective,  
        constraints=[  
            max_leverage,  
            dollar_neutral,  
            constrain_pos_size,  
            max_turnover,  
        ]  
    )  

But I can't see how to setup the second pipeline for the sell. I'd like to retain stocks that have pe between 14 and 17

2 responses

A couple of things. Everyone has their own preferences and style and there is always more than one way to accomplish things.

My preference is to not use multiple pipelines unless there is a real good reason. Use the pipeline to just return data. Do all the stock selection logic in code. Get all the data one needs assembled into columns in the pipeline. This can include calculated fields and boolean values. I find it easier to read and debug if the data is separate from the logic. The code will also generally run faster with a single pipeline. (There is another post to this effect here https://www.quantopian.com/posts/creating-a-pipeline-from-two-datasets )

I like using the '.query' method to easily do logic across factors in the pipeline dataframe. http://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.query.html

One issue with separating the buys and sells using 'order_optimal_portfoio' is that, by default, this method will close any positions not referenced in the objective data set. One can't easily just say 'close these' because it will also close everything else. I didn't exactly understand what your trading logic was but here is an algo which incorporates two ideas. 1) use pipeline just for data, do logic in code 2) use the 'CannotHold' constraint to close securities. One can also use the 'Freeze' constraint to hold securities.

Good luck.

Thanks Dan that was very helpful