Back to Community
Need Help with First Algorithm - Small Cap Stock Picking Strategy

Hi,

I'm looking for help in finishing my first algorithm. I'm new to Python so please bear with me if my questions are on the elementary side. Below is a summary of what I'm trying accomplish followed by a list of questions.

Small Cap Stock Picking Strategy  

1. Market Cap between 15M and 150M  
2. Dividend yield >= 2%  
3. Country = USA, no ADR  
4. No OTC stocks  
5. EPS growth this year >= 10% (trying to match Finviz filter)  
6. EPS growth Q/Q >= 20% (trying to match Finviz filter)  
7. Price between $5 and $20  

Rebalance:  
1.  Rebalance monthly, equal weights  
2.  Sort by price closest to 52-week high  
3.  Take the top 5 stocks.

Questions:

  1. How is the fundamental query tied to the fundamental filters? Do I need to have a query for each filter?
  2. I'm trying to match the filter from the FINVIZ stock screener for 'EPS Growth this Year'. Is this the same as 'diluted.eps.growth'?
  3. I'm also trying to match the FINVIZ filter for 'EPS Growth Q/Q', which compares the current quarter EPS to that of the quarter one year ago. How can I add this?
  4. How do I add a filter to only buy stocks in the $5 to $20 price range?
  5. How do I sort and rank filtered stocks based on how close they are to their 52-week highs (top five stocks nearest their 52wk highs).
  6. Does this algorithm currently handle stocks that were delisted or had their symbols changed, etc?

Sorry for all of the questions. Any help that can be provided on any or all of the above would be appreciated. Other comments regarding possible errors or recommendations for improvement are also welcome.

Thanks in advance.

Clone Algorithm
104
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: 5643c45555838610f52c5b04
There was a runtime error.
5 responses

Hi Troy,

This sounds like a job for the Pipeline API! With Pipeline, you can rank and sort stocks to define your own custom tradable universe of securities. In Pipeline, you have access to historical pricing and fundamental data. There are also some sample algos here.

Regarding your questions on Fundamentals data, there is a description for all the fundamentals data fields here. Unfortunately there is no great way to compare a quarterly fundamental value to one from a year ago, but take a look at the algo posted here for a 'hack' solution that compares the current EPS to the previous quarter's EPS.

If you use Pipeline to implement your strategy, you won't need to worry about delisted stocks, OTC, or symbol changes. Behind the scenes, the delisted + OTC stocks will not make it through the filters that you listed (price, EPS growth, etc.), and securities are referenced by a security ID (SID) that takes care of any symbol changes for you. That being said, if you hold position in a security that becomes delisted, it stays in your portfolio. Technically, you can check if a security has stopped trading if you look at the end_date attribute of the stock in data. However, this uses lookahead bias an does not translate to live trading. You're better off trying to develop a strategy that avoids holding position in companies likely to bankrupt. We realize that this is not a great solution, and we are working on developing a better one (for example, emptying your portfolio of delisted securities).

I hope this helps!

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.

Hello Troy,
Welcome to the community. I’ll only answer some of your questions that I think I can help with.
Question 2 – diluted_eps_growth: The growth in the company’s diluted earnings per share (EPS) on a percentage basis. Morningstar calculates the annualized growth percentage based on the underlying diluted EPS reported in the Income Statement within the company filings or reports. This was taken from https://www.quantopian.com/help/fundamentals.
Question 4 – Check out the algo I have shared with you, this should provide a bunch of new tools that you can use. Check out specifically “fundamentals.valuation.market_cap*1.0”
Question 5 - You may find inspiration from what is contained in the def_filter function: volume = history(30, '1d', 'volume')[df_securities].
Question 6 – Look at the extra fundamental filters I have. This should help. What Jamie vaguely referred to can be found in the df_filter function. You may want to also check out the function “has_orders” which prevents adding new orders when orders are not being filled. If you still have a prob with a stock, you may just want to add it to the context.flagged_stocks.
Also try searching the words “value” or “buffet” or “graham” or “fundamental” to try and find other examples of people using fundamental filters that might help you with your other questions.
Best of luck,

Clone Algorithm
25
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: 564a789a1c9c611102f251b4
There was a runtime error.

Jamie,

Thank you for the response and information. This is definitely helpful. I'll look into using Pipeline as noted, and I think the 'hack' you mentioned may be just what I need for the EPS Q/Q data!

Darth,

Thanks for responding to my post with detailed answers and the excellent Algo that I'll be keeping for future reference. Nice idea to get pricing info indirectly using market cap & shares outstanding! I will look into the functions as noted and hopefully be able to stitch together a working Algo with all of my criteria.

Thanks again,

Troy

Troy,
At the very least you have some new ideas to check out. I wouldn't mind seeing your final algo with all the elements you describe working. I like where you are headed with it.
Have a nice day.

Thanks Darth. I will plan on posting the algo when I get it working (or at least as far as I can go without needing more help!).

Troy