Back to Community
why am i getting error update universe only allowed in before_start_trading function? seems nested to me?
# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    context.limit = 10  


# Will be called on every trade event for the securities you specify.  
def before_tradings_start(context):  
    context.fundamentals = get_fundamentals(  
        query(  
            fundamentals.valuation_ratios.pb_ratio,  
            fundamentals.valuation_ratios.pe_ratio,  
        )  
        .filter(  
            fundamentals.valuation_ratios.pe_ratio < 14  
        )  
        .filter(  
            fundamentals.valuation_ratios.pb_ratio < 2  
        )  
        .order_by(  
           fundamentals.valuation.market_cap.desc()  
        )  
        .limit(context.limit)  
    )  
    update_universe(context.fundamentals.columns.values)




def handle_data(context, data):  
    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))  
5 responses

You spelled the method as before_tradings_start it is actually spelled before_trading_start

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.

o wow! such a simple mistake! thank you for catching it!

No problem Kevin!

If you have more questions reply to this thread and I'll get an email, happy coding!

Thanks a bunch! literally just started on this site today and its awesome, prepared to learn lots, i do have one question do you have any advice on where i could pick up some more knowledge on the finance side of things? i have been referencing ivestapedia quite a bit, just curious if there are any better places to learn in your opinion.

Investopedia is a great resource, that I still frequent.

As far as finance learning resources go I'd check out Kahn Academy's finance section to get started, in addition browse the community forum on here as there can be pretty cool threads that you can learn a lot from, stay up to date with financial news and look up the subjects you don't understand while reading them. Check out these notes from Eric Zivot's finance classes at University of Washington: Investment, Capital, and Finance, Intro to Computational Finance. Finally, and this is drifting away from your finance specific request but we are developing a quant finance curriculum led by @Delaney, it is incredibly awesome and you can learn a lot on the statistical side that will really help you develop quality algorithms. Check out the sidebar for the webinar dates for those, or you can come in person if you are in the Boston area. Here is a link to his first lecture about linear regression. . Those are some of the things that have really helped and still do help me.

A few notes about the Kahn Academy Stuff...

The videos are about 10 minutes long but totally worth it. Sal, the narrator/instructor, can talk slow sometimes so you can speed the videos up to about 1.25x and still comprehend most of the stuff. He's also fairly knowledgeable if I recall correctly he used to be an analyst at a hedge fund.

Enjoy learning, this is an exciting field to be in right now, lots of opportunities!