banner



How To Screen Stocks Based On Historical Data

Learn how to make a powerful stock screener based on Marking Minervini's Tendency Template with Python.

Disclaimer: The fabric in this commodity was based upon Richard Moglen's Youtube Channel for purely educational purposes and should not be taken as professional person investment advice. Invest at your own discretion.

Stock Screeners are a wonderful mode to notice the perfect stocks for your specific trading strategy. However, when I tried looking to find a stock screener using Python, I could hardly find whatsoever functional, automated lawmaking. So I created this article to help others make an like shooting fish in a barrel-to-follow stock screener Python programme based on Mark Minervini's Trend Template (the viii principles on selecting the best stocks). Particularly with the volatility in the current market, I promise this lawmaking will help y'all in your trading endeavors.

Paradigm by Pexels from Pixabay

Before I get into the code, I want to touch upon the stock screening criteria.

  1. The electric current price of the security must be greater than the 150 and 200-twenty-four hour period uncomplicated moving averages.
  2. The 150-day simple moving boilerplate must exist greater than the 200-day simple moving average.
  3. The 200-day unproblematic moving average must be trending up for at to the lowest degree i month.
  4. The l-day simple moving average must exist greater than the 150 elementary moving average and the 200 elementary moving average.
  5. The current cost must exist greater than the l-day simple moving average.
  6. The current price must exist at least 30% in a higher place the 52 week low.
  7. The current price must exist within 25% of the 52 week loftier.
  8. The IBD RS-Rating must exist greater than lxx (the college, the better). The RS rating is a metric of a stock'due south price functioning over the last year compared to all other stocks and the overall market. Bank check out this article to learn more.

You can read more than about this template in Mark Minervini's blog post.

Now that you are familiar with the criteria, we tin can get into the code. Start, import the following dependencies. If yous do not accept one of the post-obit modules installed on your machine, employ "pip install (module name)" in your terminal to download them.

Import these dependencies and override deprecation warnings with yf.pdr_override.

Beginning, we must import the dependencies nosotros will use in the program such as yahoo_fin (to go the listing of tickers) and pandas_datareader.data (to get historical stock information).

Next, we must set upwards the variables for the residue of the program. I outlined beneath what each variable refers to.

  • tickers : All of the tickers in the Due south&P 500
  • index_name : S&P 500 Yahoo Finance symbol
  • start_date : The beginning date for historical data (exactly i year agone)
  • end_date : The stop date for historical data (today)
  • exportList : The values we volition be collecting for each stock
  • returns_multiples : A list to see how each stock performed relative to the market (will be used for computing the RS rating)
Outset of the for-loop.

At present, nosotros can calculate the cumulative render of the S&P 500 index over the past yr and compare that value to the cumulative return for each stock in the S&P 500 during the same flow. The IBD Relative Strength metric essentially calculates how a stock is performing relative to the marketplace and other stocks during a specific time period. Since the metric is exclusively used in IBD services, we can estimate the IBD RS past dividing the cumulative return of each stock over the cumulative render of the alphabetize and then create a percentile ranking for each stock out of 100. For example, if AAPL outperformed the market greater than MSFT in a specified fourth dimension period, information technology would take a higher RS. In Marker Minervini'due south Trend Template, he looks for stocks with an RS value of 70 or college (the top 30% performing stocks in the market place).

Since in this plan the RS metric is calculated with a percentile value relative to other stocks in the given list, it is amend to have a list of a greater corporeality of stocks to make the RS value more accurate. In this program, nosotros have chosen a list of the ~500 stocks in the S&P 500 index so that is a large plenty sample size.

To make the process faster, we can download the historical data for each stock over the by year instead of continually making requests to Yahoo Finance (which could cause errors). The fourth dimension.sleep(1) at the cease of the for loop can likewise help curb potential errors with the influx of requests. Finally, later the data manipulation with quantiles, we accept a dataframe with the top thirty% performing stocks in the given list also as their respective RS values.

Instead of calculating the metrics for each i of the stocks, we tin can just include the top 30% of stocks that pass status eight of Minervini's Tendency Template (an RS value greater than lxx). From here, nosotros can summate the metrics we need to create the weather. The current close price is used by taking the adjusted shut toll for the last twenty-four hour period. The loftier and lows of the by yr are taken by finding the maximum and minimum values in the DataFrame for the past 260 trading days (nearly a yr). The moving averages are used by calculating the rolling averages over the respective corporeality of days. The remainder of the lawmaking actually executes the screener with the principles that were mentioned earlier. If a stock passes every ane of the weather condition, we tin can add together it to our exportList of stocks that passed Minervini's Trend Template!

Lastly, this code volition impress out a DataFrame of all the stocks that made the requirements and download the stocks to an excel file for your convenience. Now yous know how how to create the stock screener used by one of the best traders of all time!

The GitHub Gist beneath contains all the code for the program. I hope this algorithm will testify useful to you in the future. Thanks so much for reading!

If yous enjoyed this commodity, bring together my free investing community on Finary and check out some of my Python for Finance articles beneath!

How To Screen Stocks Based On Historical Data,

Source: https://towardsdatascience.com/making-a-stock-screener-with-python-4f591b198261

Posted by: hoangroustich1959.blogspot.com

0 Response to "How To Screen Stocks Based On Historical Data"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel