Back to Community
Trouble with Code

Hi I'm having some trouble with understanding why my code isn't buying any of the stock in the fundamentals. It seems to be running but will not purchase any. I'm currently filtering for pe ratio pb ratio peg and debt to equity and then trying to sell based on those that have PEs less than 20.

Clone Algorithm
1
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 57fc68d22bce8e104ae18d71
There was a runtime error.
3 responses

Couple pointers:

  • First off - your filters are too strict. Currently, no stocks pass the filter hence no orders can be placed
  • Second - the way you place orders for stocks would lead to no stocks being purchased - you should use order_target_percent instead
  • You shouldn't use handle_data for your stock_purchases if you are purchasing on a daily frequency - use a custom rebalance function instead and schedule it via schedule_function

I've updated your algorithm so it works, but you should fiddle around with it to match your intentions:

Some changes:

  1. Using Pipeline instead of get_fundamentals. I keep track of the same data, but only filter based on pe and pb, selecting the top stocks based by market cap.
  2. Use rebalance instead of handle_data for ordering stocks (this is the correct practice).
  3. Use order_target_percent, with the weights based on the desired leverage/number of stocks
  4. removed some extraneous conditions (it's not necessary to check pe_ratio <= 20 since those results have already been filtered
  5. Correctly checking whether the stock is in the portfolio with the line if stock not in context.portfolio.positions

Hope this helps!

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 57ffc1685561ab10530b4c98
There was a runtime error.
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 so much! Any suggestions for someone who is new to Python to learn pipeline?

No problem :)

There is a nice pipeline tutorial here . After working through this you should understand the basics well enough

For a quick tutorial for ordering stocks and other algorithm execution related things check this

If you are looking to gain a solid understanding of basic python Google has a nice introduction

For more complex python related stuff, I recommend getting a textbook/taking an online course