Searching for Super Performance Stocks with Python

Mark Minervini in his book Trade Like a Stock Market Wizard shares a very interesting strategy to identify super performance stocks. During this post, we are going to learn how to apply his approach to find stocks having potential to become superperformers using Python.

Mark Minervini shares a few key elements in order to spot super performer stocks:

  • Stock prices should be having an upward trend.
  • Strong fundamentals. Super performance stocks are constantly beating revenues and earnings.
  • Stock has a catalyst that will push the price up. E.g. New CEO or a new and impactful product.
  • Find the entry points at which to buy the stock. Price graphs are great for this purpose.
  • Know when to exit and limit your risks. Stop Losses are essential.

In this post, we will build a screener to identify stocks having a long upward trend as per Mark’s approach.

Stocks meeting this criteria are the ones that Mark Minervini considers as having the best change to become the next superperformers.

Searching for Super Performance Stocks with Python
Photo by Gabby K on Pexels.com

Screening to Identify Super Performance Stocks

In order to find stocks with a long term upward trend, Mark Minervini provides what he calls a trend template, that is a set of rules that a stock needs to fulfil in order to have chances of becoming a true superperformer stock.

Normally, superperformers are small or mid-cap companies. But from time to time big companies may also be included.

I will list the rules below and in the next section, we will build the Python script in order to build the screener to find stocks meeting his criteria.

For a stock to qualify as a Potential candidate all rules below need to be met:

  1. Current stock price is above the 150 and 200 days moving average (MA).
  2. The 150 days moving average is above the 200 days moving average.
  3. The 200 days moving average is trending up for at least a month.
  4. The 50 day MA is above the 150 and 200 MAs
  5. Stock price is above the 50 day MA
  6. Current share price is at least 30% above its 52 week low
  7. Current stock price is at least within 25% of the 52 week high
  8. Relative Strength ranking (i.e. how good stock performed against market) is greater than 80 or 90

If a stock meets all eight criteria points, we have found a stock that is in a clear uptrend phase.

Finding Superperfomer Stock Candidates with Python

Now that we understand how to indentify stocks in an upward trend as per Mark Minervini trend template, we can build a Python script to do the work for us.

We will perform the screener using all stocks in the S&P 500 index. I will not go into much details on how the code works since it is quite long and the majority of the steps are similar to the ones covered in my previous posts on Python for Finance:

First, we start by getting a list of all tickers in the S&P500. This is our starting point. We can get them by scraping Wikipedia. The result of this be a Python list containing all S&P500 tickers. We insert the SP500 index as well to be the first element of that list:

The reason why need to have the tickers in a Python list is to be able to loop through them to retrieve historical prices with Python. With the stock historical prices, we will be able to calculate all financial metrics required to test the eight conditions of the trend template.

We will use financialmodelingprep in order to retrieve stock prices. Note that an API key is required in order to be able to get the historical prices from the 500 stocks.

We will first loop through each of the stocks and retrieve the data and the stock price for each of the days. That will be added to a dictionary called price.

Next, we convert the dictionary into a Pandas DataFrame. That will let us calculate the required metrics for each of the stocks such as moving average, relative strength, etc. All these metrics are based on the stock prices.

The only metric that is first time introduced in this blog is the relative strength. It tells us how the stock has performed compared to an index (i.e. S&P500). In our case, we calculate the relative strength of each stock respect to the S&P500. Feel free to read more about how to calculat relative strength on the following link.

Note that we calculate and store in a dictionary called metrics all moving averages and other financial metrics required for the test.

Finally, we convert the metrics dictionary into a Pandas DataFrame that we save into a csv file.

The code may seems a bit complicated. Since the script is a bit long, it is not easy to explain line by line in a blog post. I have recorded below video explaining each of the steps in the code. That way you should be able to follow the script logic without any troubles.

Screening for Potential Superperformer Stocks Python

view raw trending_stocks.py hosted with ❤ by GitHub

At the end of the code, by printing the metrics_DF DataFrame we should get below DataFrame. My recommendation is that before running the code for all stocks, limit the list to only a couple of stocks to be sure that it works as expected. It takes around 1 or 2 hours for all data to be retrieved and generated.

If you have troubles running the code, I would recommend you run it in phases of 100 stocks so that you do not hit an API limit usage.

Stock Trending Up with Python
Stock Trending Up with Python

Running the script and getting potential superperformer stocks

The code may take quite a bit of time to be completed. As mentioned before, run the code only for a couple of companies to be sure that it works good. We do not want to run the code and extract the stock prices of 500 stocks to find out after more than one hour waiting that we had an error in a formula.

Once the code finishes, we are ready to test the eight conditions described by Mark Minervini in our 500 stocks.

To see which of our 500 stocks meet the 8 conditions, we will create a new Python script that will first read the csv file that we created in the previous section.

Then, we test for each stock all eight conditions and return only the stocks that meet all of the criteria.

After running the code, we can see a list of 10 or 20 candidates. Now, we are ready to look into each of these stocks and move forward with the fundamental analysis to identify super performance stocks.

Potential SuperPerformers

What Stocks are Considered as Superperformers?

After running the Python script to identify which stocks are having a long term upward trend, we must look at the fundamentals. The screener is only useful to narrow down our potential candidates.

If earnings and sales of the stocks returned by our Python stock screener are great and growing (i.e. beating estimates constantly), we can further continue with our fundamental analysis of the stocks.

To look at the company revenue and net income growth from last years, I recommend you to have a look at below video where I show how to build an Income Statement Dashboard for multiple companies:

Analysing Company Revenue Growth

Some other questions to answer during our fundamental analysis are:

  • Are institutions buying? Look at the volume and see if there is a significant increase in volume in up days.
  • Does the volume reflects that there is a higher demand for the stock compared to selling pressure? For example, lot of volume of uptrend days versus lower volume on down days.
  • There are small amounts of shares outstanding and the stock has a small capitalization? If this is the case, stock may appreciate very quickly.

Wrapping Up

We have learnt how to use Python to build a screener to identify stocks having a long upward trend. As per Mark Minervini, stocks meeting this criteria have a great potential to become superperformers stocks.

These screener can help to identify super performance stocks. However, and as Mark also mentioned in his book, the key for a stock price appreciation is company fundamentals. If a company is not growing revenues and the bottom line, it will never be a superperformer in the stock market. As simple as that.

Thank you for reading the blog. Hope that you have enjoyed this article.

As a side note, I totally recommend Mark Minervini book. He covers key aspects to be looked at in order to identify stocks that have potential to become superperformers. And it is done in a very interesting and detailed way with plenty of examples. In addition, he also covers key risk management aspects that may be useful to limit your potential losses when markets go the wrong way.

Note: All posts in codingandfun.com are only intended for educational purposes. We do not offer financial advise. Also, we do not recommend you to buy or sell any stock. You are responsible to perform your own due diligence before investing or trading any security.

11 thoughts on “Searching for Super Performance Stocks with Python

  1. Hi,
    I tried to run the script for about 20 stocks from the S&P 500 and everything was fine.

    When I tried to replace the list of the stocks with few other stocks some of the values became NaN.

    Could you please let me know if I can PM you to consult the problem?

    Thanks

    1. Hi David. It happened to me as well.
      Try to use maybe another API to get prices like Alpha Vantage and that worked for me.

        1. I think it should be rather easy. Just try to get to the same dataframe with stock prices, then you have it

  2. Hi,

    thanks for your project, this is exactly what i was looking for!
    I am an absolute phyton noob and currently struggle with two problems.

    1. I get this error:
    Traceback (most recent call last):
    File “C: \ Users \ mario \ PycharmProjects \ Screener \ venv \ lib \ site-packages \ pandas \ core \ indexes \ base.py”, line 3080, in get_loc
    return self._engine.get_loc (casted_key)
    File “pandas \ _libs \ index.pyx”, line 70, in pandas._libs.index.IndexEngine.get_loc
    File “pandas \ _libs \ index.pyx”, line 101, in pandas._libs.index.IndexEngine.get_loc
    File “pandas \ _libs \ hashtable_class_helper.pxi”, line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
    File “pandas \ _libs \ hashtable_class_helper.pxi”, line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
    KeyError: ‘Relative Strength’

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File “C: \ Users \ mario \ PycharmProjects \ Screener \ main.py”, line 65, in
    metrics_DF [‘pct_rank’] = metrics_DF [‘Relative Strength’]. rank (pct = True)
    File “C: \ Users \ mario \ PycharmProjects \ Screener \ venv \ lib \ site-packages \ pandas \ core \ frame.py”, line 3024, in __getitem__
    indexer = self.columns.get_loc (key)
    File “C: \ Users \ mario \ PycharmProjects \ Screener \ venv \ lib \ site-packages \ pandas \ core \ indexes \ base.py”, line 3082, in get_loc
    raise KeyError (key) from err
    KeyError: ‘Relative Strength’

    2. Problems with the csv file

    I already have your Excel dashboard and in the csv file it always writes all data in one column. How can I program it so that each value has its own column.

    Thanks and sorry for the long comment!

    greetings
    Mario

    1. With the csv file, it may be the regional settings where instead of ; you may need to use , as the separator.

      The first error i am not sure what it may be. Check that the metrics DF dataframe has a column with the name Relative Strength

Leave a Reply

Your email address will not be published. Required fields are marked *