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

I am trying to buy stocks only when the PE is between 5-15 and the current ratio>1.75. How does this work? And what is going wrong with my code.

def intialize(context):
set_long_only()

context.security=sid(24)  

def before_trading_start(context):

fundamental_df= get_fundamentals(  
    query(  
        fundamentals.valuation_ratios.pe_ratio,  
        fundamentals.operation_ratios.current_ratio,  
    )  

    .filter(fundamentals.valuation_ratios.pe_ratio > 5)  
    .filter(fundamentals.valuation_ratios.pe_ratio < 15)  
    .filter(fundamentals.operation_ratios.current_ratio > 1.75)  

    .order_by(fundamentals.valuation_ratios.pe_ratio.desc()).limit(20)  
)  

context.fundamental_df = fundamental_df  

update_universe(context.fundamental_df.columns.values)  

def handle_data(context,data):
for stock in context.fundamental_df:
if stock in data:
print(stock)

8 responses

Hi Quantdog,

The primary problem I had with the above code is that you've misspelled initialize. You're missing the second 'i' between the n and the t.

Once I fixed that, looks like the code does what I'd expect it to do, though you hit logging limits with your print statement.

Let us know if you have more questions.

Thanks
Josh

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 haha, can I create a loop with context.fundamental?

For stock in context.fundamental_df

I am trying to filter stocks with Ben Grahman advice, and then use an rsi/ ma crossover model.

Yup, that works.

There are some sample Graham algos that folks created when we first introduced fundamentals: http://blog.quantopian.com/fundamentals-contest-winners/

Thanks, I really appreciate it. I am having trouble getting the talib imports to work. I am trying buy those stocks only when the ma25>ma50 and the rsi is below 30. Is that possible in quantopian?

Also, I am not sure how the definitions work, like "def create_weights", why do we make things a definition versus just being in the handle data method.

Sure, take a look at this algo to see how to implement RSI: https://www.quantopian.com/help#rsi

And then you can use the mavg() function to calculate the moving averages: https://www.quantopian.com/help#ide-transforms

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.

I figured out the algorithm, not performing so great. When I am limiting my stocks, how long does it limit it for?

Here is an improved version. Every time I run it I get different results. Is that because it picks a variety of stocks?

Quantdog, I just ran your algorithm a few times and got the same result every time. There's no randomization in your algorithm, so there's no reason why results would be different. However, note that your algorithm's results should be different on every day, because your stock picks depend on the day of execution.