Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
First Algorithm - Using Pipeline - How do I create an order?

This is the first program I've written in probably 3ish years. I had moderate experience programming back in the day, but have basically forgotten most of it to the point that I can understand most examples but can't create them at all.

Anyways, I'm trying to create an algorithm that invests in stocks with RSIs above 50. I've not even attempted rebalancing yet, and I can't seem to figure out how to create an order to buy all of the securities that have RSIs above 50. The program fails at the symbol(stock).

Can someone help me to figure out what I'm doing wrong?

#Determine which stocks to invest in based on RSI and invests in them

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 RSI


def initialize(context):  
    pipe = Pipeline()  
    attach_pipeline(pipe, name='my_pipeline')  
    RSI_Holder = RSI(inputs=[USEquityPricing.close], window_length=15)  
    reduced_universe = (RSI_Holder>50)  
    pipe.set_screen(reduced_universe)  
    pipe.add(RSI_Holder, name="RSI_Holder")  
    pipe.add(reduced_universe, name='reduced_universe')  
def before_trading_start(context, data):  
    context.output = pipeline_output('my_pipeline')  
    context.secs = context.output[context.output['reduced_universe']]  
    context.long_list = context.output.sort(['reduced_universe'], ascending=True)  
    #print results.head(5)  
    print context.secs  
    update_universe(context.output)  
    #print results  
#"""  
def handle_data(context, data):  
    for stock in context.long_list:  
            stk = symbol(stock)  
            order(stk, 1)  
3 responses

Hi Matthew,

You're pretty close! In the example you shared, stock is a reference to the Equity object itself in handle_data so in the for loop, you can just call order(stock, 1). I would strongly recommend checking out the Getting Started Tutorial if you haven't already. It discusses referencing securities as well as ordering them.

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.

Thanks Jamie. I tried just using the order and it is still giving the error.

UnsupportedOrderParameters: Passing non-Asset argument to 'order()' is not supported. Use 'sid()' or 'symbol()' methods to look up an Asset.  
There was a runtime error on line 33.  

Determine which stocks to invest in based on RSI and invest in them

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 RSI

def initialize(context):
pipe = Pipeline()
attach_pipeline(pipe, name='my_pipeline')
RSI_Holder = RSI(inputs=[USEquityPricing.close], window_length=15)
reduced_universe = (RSI_Holder>50)
pipe.set_screen(reduced_universe)
pipe.add(RSI_Holder, name="RSI_Holder")
pipe.add(reduced_universe, name='reduced_universe')
def before_trading_start(context, data):
context.output = pipeline_output('my_pipeline')
context.secs = context.output[context.output['reduced_universe']]
context.long_list = context.output.sort(['reduced_universe'], ascending=True)
#print results.head(5)
#print context.secs
update_universe(context.long_list)
#print results

"""

def handle_data(context, data):
for stock in context.long_list:
#print stock
order(stock, 1)

"""

Matthew,

Add .index[:] to your list assignment statement as below.

context.long_list = context.output.sort(['reduced_universe'], ascending=True).index[:]

The pipeline_output returns a dataframe which is a two dimensional structure of the assets (stocks) AND the associated data. You just want the one dimensional list of assets. By adding the .index[:] you are pointing to the index of the dataframe which is defined as the list of assets (stocks) and in a way coercing the results into a list . There is a marginally better explanation of this in the documentation (see https://www.quantopian.com/help#using-results and http://pandas.pydata.org/pandas-docs/stable/indexing.html#index-objects).

Hope that helps.