Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pipeline API orders

Hi,

I've been researching how to use the pipeline API, but all the documentation I've found seems somewhat incomplete. I'm trying to write a very simple algorithm that does the following.

  • Creates a pipeline object
    • Uses SMAs like in the example to narrow down the stocks
    • Sort the stocks by ranking
    • Purchase 1 share each of the top 5 stocks

For now I'm just trying to get a basic understanding of Pipeline, later I'd like to add factors like RSI and EMA, but I'll figure that out when I come to it.

I'm having trouble with the order function, I can't seem to figure out how to convert the pipeline results to symbol objects to be able to order. Any help would be greatly appreciated!

Thanks,
Bryce

Here's what I'm working with:

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import SimpleMovingAverage  
from quantopian.pipeline.factors import RSI  
from quantopian.pipeline.data import morningstar

def initialize(context):  
    pipe = Pipeline()  
    pipe = attach_pipeline(pipe, 'my_pipeline')  
    sma_short = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)  
    sma_long = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=100)

    # Combined factors to create new factors  
    sma_val = sma_short/sma_long

    # Create and apply a screen to remove penny stocks  
    remove_penny_stocks = sma_short > 1.0  
    pipe.set_screen(remove_penny_stocks)

    pipe.add(sma_short, 'sma_short')  
    pipe.add(sma_long, 'sma_long')  
    pipe.add(sma_val, 'sma_val')  
    # Rank a factor using a mask to ignore the values we're  
    # filtering out by passing mask=remove_penny_stocks to rank.  
    pipe.add(sma_val.rank(mask=remove_penny_stocks), 'sma_rank')  
def before_trading_start(context):  
    context.output = pipeline_output('my_pipeline')

    # Set the list of securities to short  
    context.short_list = context.output.sort(['sma_rank'], ascending=True).iloc[:200]

    # Set the list of securities to long  
    context.long_list = context.output.sort(['sma_rank'], ascending=True).iloc[-200:]

    # Update your universe with the SIDs of long and short securities  
    update_universe(context.long_list.index.union(context.short_list.index))  


def handle_data(context, data):  
    for item in context.short_list[5:]:  
        stk = symbol(item)  
        order_target(stk,1)
2 responses

Caveat emptor. I'm just getting up to speed with the pipeline API myself.

Right off the bat, it seems like you have a problem with

    for item in context.short_list[5:]:  

I believe from your example context.short_list is length 200. You're taking the last 195. Instead, I think you want:
for item in context.short_list[:5]:

So, putting it all together, maybe something like:

def handle_data(context, data):  
    for stock in context.short_list[:5]:  
        if stock in data:  
            order(stock, 1)  

I hope this helps. I'm not sure if this is really what you wanted as there's a lot in your code that doesn't reflect the intent of your initial question.

Hi Bryce,

When you use update_universe() in before_trading_start(), you are actually adding a reference to each security passed to that update_universe() function to the data variable. Take a look at the example in this post, it should help you get your orders working. Additionally, I'd suggest taking a look at this tutorial video to get a quick overview of how your trading universe and data works.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.