Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Is there a way to select specific stock from universe?

Lets's say I am filtering based on 'return on assets' and ordering that return on the 'p/e' ratio like this:

def before_trading_start(context):  
    fundamental_df = get_fundamentals(  
        query(  
            fundamentals.operation_ratios.roa,  
            fundamentals.valuation_ratios.pe_ratio  
        )  
        .filter(fundamentals.operation_ratios.roa > 0.25)  
        .order_by(fundamentals.valuation_ratios.pe_ratio)  
        .limit(10)  
    )  
    context.fundamental_df = fundamental_df  
    update_universe(context.fundamental_df.columns.values)  

When coding the order part of the algorithm is there a way to buy only one of the ten filtered stocks?

For example if I wanted to buy the second on the list?

Something like:

def trade(context, data):  
    for stock in context.fundamental_df:  
        order(**second stock in context.fundamental_df**, 100)  
2 responses

Hi Kane,

You can do something like

def before_trading_start(context):  
    fundamental_df = get_fundamentals(  
        query(  
            fundamentals.operation_ratios.roa,  
            fundamentals.valuation_ratios.pe_ratio  
        )  
        .filter(fundamentals.operation_ratios.roa > 0.25)  
        .order_by(fundamentals.valuation_ratios.pe_ratio)  
        .limit(10)  
    )  
    context.fundamental_df = fundamental_df  
    sids = context.fundamental_df.columns  
    context.stock_to_buy = sids[2]  
    update_universe(context.fundamental_df.columns.values)  

def trade(context, data);  
    if context.stock_to_buy in data:  
        order(context.stock_to_buy)  

Here, what I'm doing is getting the second security from the columns of fundamental_df (which are all the sids that are present from that query), and storing that in a context variable, and ordering that later in def trade(context, data).

You'll have to tweak it a bit to fit your needs, but give it a shot and let me know how it goes

Seong

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.

Hi Seong thanks for that I see how it works.

So just a more general question, does that means that the get_fundamentals() method returns a Pandas dataframe with the first row beings SIDS'? (or the headers being SIDS?) and then the rows beneath contain the 'roa' and 'p/e' data in this example?