Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Execution based on Fundamental ratios

Hi

  1. why is it NOT executing buy orders based on the filters given?

  2. Iteration over the asset is deprecated. How do I apply proper migration?

Thanks in advance

def initialize(context):  
    context.limit = 10  
def before_trading_start(context):  
    context.fundamentals= get_fundamentals(  
        query(  
            fundamentals.valuation_ratios.pb_ratio,  
            fundamentals.valuation_ratios.pe_ratio  
            # put your query in here by typing "fundamentals."  
        )  
        .filter(  
            fundamentals.valuation_ratios.pe_ratio < 14  
            # put your filters here  
       .filter(  
            fundamentals.valuation_ratios.pb_ratio < 2  
            # put your filters here  
        )  
        .order_by(  
            fundamentals.valuation.market_cap.desc()  
            # sort your query  
        )  
        .limit(context.limit)  
    )  
def handle_data(context, data):  
        #context:tracking info about our portfolio  
        #data: referts to the universe  
      cash = context.portfolio.cash  
      current_position = context.portfolio.positions  
      for stock in data:  
        current_position = context.portfolio.positions[stock].amount  
        stock_price = data[stock].price  
        plausible_investment = cash/10.0  
        share_amount = int(plausible_investment/stock_price)  
        try:  
            if stock_price < plausible_investment:  
                if current_position == 0:  
                    if context.fundamentals[stock]['pe_ratio'] < 11:  
                        order(stock, share_amount)  
        except Exception as e:  
            print(str(e))  

2 responses

Hi Andrew,

This algorithm won't order anything because, since iterating over data is deprecated, the for loop for stock in data simply doesn't do anything.

The new way to write this kind of algorithm is to use Pipeline. Try taking a look at the Pipeline tutorial. With Pipeline, you can get all the fundamentals data you need every day, and filter by criteria like you have set in your get_fundamentals query. Then you can iterate over the equities that Pipeline outputs.

For a simple way to get this particular algorithm running, try iterating over context.fundamentals.columns instead of data to try the logic for just those equities for which you have fundamentals data.

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.

Thank you Nathan!!

I definitely need to dive deeper for Pipeline materials, as there have been lots of migrations that some of the tutorials that I am following are still teaching older versions.

Andrew