Developing Custom Stock Screeners in Python
Basic, Multiple Filters and Screener with Signal-Based Filtering inside
Stock screeners are invaluable tools for sorting and filtering thousands of stocks simultaneously. Many traders and investors rely on stock screeners to identify stocks based on specific search criteria efficiently. While platforms like TradingView offer impressive screeners, creating a custom screener through coding provides unparalleled flexibility and customization. However, there is a slight hurdle.
Coding a screener from scratch can be daunting for many, as it involves extracting and compiling various types of fundamental data, and integrating it with filtering and sorting functionalities. This is where the EODHD Screener API becomes invaluable.
In this article, we will explore the Screener API offered by EODHD, discussing who might benefit from this API and the features it includes. We will then dive into practical applications using Python to create several stock screeners. Let’s get started!
EODHD Screener API
Building a stock screener from scratch typically requires gathering and synthesizing data from multiple sources, which can be time-consuming. However, EODHD’s Screener API streamlines this process by allowing users to swiftly filter companies based on defined parameters such as market capitalization, exchange, industry, dividend yield, and many others. The efficiency of this API in rapidly extracting and organizing data is what sets it apart.
Now, who is this API really for? Unlike specialized APIs, such as those for News Sentiment or Macroeconomic Data API, which cater to a niche audience, the Screener API has a broad array of applications. For instance, traders and investors can use it to build custom stock screeners tailored to their needs, researchers can employ it to analyze stocks based on specific criteria, and the applications extend beyond these examples.
What exactly does this API offer? Essentially, it provides a dataframe that includes a list of tickers along with fundamental data about each stock, all based on user-defined search criteria. The API offers extensive customization options for setting these criteria, greatly simplifying the screening process.
With a solid understanding of the Screener API’s capabilities, let’s move on to the more exciting part of the article — using Python to create a variety of stock screeners utilizing this API.
Importing Necessary Libraries
The initial step in building stock screeners involves importing the essential packages into the Python environment, a crucial and mandatory action.
For this article, we need only two packages: the EODHD Python Library, “eodhd”, for data extraction, and Pandas for data manipulation and wrangling. Here is how you can import these packages into your Python script:
import pandas as pd
from eodhd import APIClient
If you haven’t yet installed these packages, ensure you do so by using the pip command in your terminal.
API Key Activation
The second step in leveraging the stock screener API to create custom screeners involves activating your API key. It’s essential to register your EODHD API key with the package to access its functions.
If you don’t already have an EODHD API key, begin by visiting their website, complete the registration to create an account, or just jump in with a Google account, and then navigate to the Settings page to locate your secret API key. It’s crucial to keep this key confidential.
Here’s how to activate the API key:
# API KEY ACTIVATION
api_key = '<YOUR API KEY>'
client = APIClient(api_key)
This code is straightforward: the first line stores the secret EODHD API key in the variable `api_key`, and the second line uses the `APIClient` class from the eodhd package to activate the API key, storing the initialized client object in the `client` variable.
Remember to replace `<YOUR API KEY>` with your actual secret EODHD API key. For enhanced security, consider storing the API key using environmental variables or other secure methods.
Creating a Basic Stock Screener
Now that we have everything set up, let’s build a straightforward stock screener with a single filter. We’ll create a screener that only includes stocks listed in the US and sort them in descending order by market capitalization. Here’s the code to implement this criterion:
# SINGLE FILTER SCREENER
mktcap_screener_json = client.stock_market_screener(sort = 'market_capitalization.desc', filters = '[["exchange","=","us"]]', limit = 10, offset = 0)
mktcap_screener_df = pd.DataFrame(mktcap_screener_json['data']).drop(['exchange','currency_symbol','last_day_data_date'], axis = 1)
mktcap_screener_df
In the code above, we use the `stock_market_screener` function from the eodhd library to fetch screener data based on our specified criteria.
This function includes several key parameters:
- `sort`: Determines the field by which the results are sorted.
- `filters`: Defines the specific criteria for our stock screener.
- `signals`: Another important parameter we’ll explore later in this article.
Additionally, the `limit` and `offset` parameters are used to control the number of results displayed and the starting point for data retrieval, respectively.
Here’s what the output of the above code looks like:
As mentioned earlier, the screener provides a lot of fundamental data, which can be incredibly valuable for traders and investors when selecting stocks. This data equips users with the necessary insights to make informed decisions, enhancing their investment strategies.
Creating a Stock Screener Using Multiple Filters
Now, let’s elevate our approach by creating a more advanced stock screener with multiple filters. While our previous screener provided basic sorting, it lacked deeper investment insights. This time, we’ll design a screener that highlights particularly profitable stocks using more nuanced criteria.
We’ll construct a screener that identifies the most profitable stocks based on Earnings Per Share (EPS), with specific conditions:
1. The market capitalization must exceed $100 billion.
2. The company must be operating within the Technology sector.
Here’s how to set up this stock screener:
# MULTIPLE FILTERS SCREENER
mf_screener_json = client.stock_market_screener(sort = 'earnings_share.desc', filters = '[["sector","=","Technology"],["market_capitalization",">",100000000000]]', limit = 10, offset = 0)
mf_screener_df = pd.DataFrame(mf_screener_json['data']).drop(['currency_symbol','last_day_data_date'], axis = 1)
mf_screener_df
This code is similar to our previous example, but with adjustments to the `sort` and `filters` parameters to match our specified search criteria.
The output from this code will display the top 10 most profitable stocks, sorted by Earnings Per Share, all within the Technology sector and with a market capitalization of at least $100 billion. This screener not only refines the selection process but also offers valuable insights into potential investment opportunities in high-value, high-performance stocks.
Creating a Stock Screener with Signal-Based Filtering
Now, let’s explore the “signals” parameter available in the `stock_market_screener` function. This parameter allows for advanced screening based on specific trading signals. For instance, if you’re interested in identifying tickers that have hit new lows over the past 200 days and have a negative Book Value, you can specify these conditions using the `signals` parameter.
This feature is particularly useful for creating custom screeners that require more complex criteria than what can be achieved solely through the `sort` and `filters` parameters. Let’s create a stock screener that identifies US-listed stocks which have reached new 200-day lows. Here’s how to set it up:
newlow_screener_json = client.stock_market_screener(filters = '[["exchange","=","us"]]', signals = '200d_new_lo', limit = 10, offset = 0)
newlow_screener_df = pd.DataFrame(newlow_screener_json['data']).drop(['exchange','currency_symbol','last_day_data_date'], axis = 1)
newlow_screener_df
In this code, apart from the usual filters, we incorporate the `signals` parameter to filter based on the specified trading signal (`200d_new_lo` for new 200-day lows).
The resulting output will display a list of US-listed securities that have recently marked new 200-day lows. Here’s what the output looks like:
Let’s explore another application of the ‘signals’ parameter. This example demonstrates how to create a stock screener that identifies stocks in the Financial Services sector deemed overvalued by Wall Street:
wshigh_screener_json = client.stock_market_screener(filters = '[["sector","=","Financial Services"]]', signals = 'wallstreet_hi', limit = 10, offset = 0)
wshigh_screener_df = pd.DataFrame(wshigh_screener_json['data']).drop(['currency_symbol','last_day_data_date'], axis = 1)
wshigh_screener_df
The code provided follows the same structure as the previous examples, with the only difference being a minor change in the “signals” parameter. Here’s what the resulting screener looks like based on the code provided:
In addition to the “200d_new_lo” and “wallstreet_hi” signals, the “signals” parameter supports several other options to further refine your stock screening:
- “200d_new_hi”: Filters tickers that have reached new highs over the past 200 days, identifying stocks potentially on an upward trajectory.
- “bookvalue_neg” and “bookvalue_pos”: These signals filter stocks based on their book value. “bookvalue_neg” identifies stocks with a negative book value, often considered financially unstable or risky. “bookvalue_pos” filters for stocks with a positive book value, typically seen as financially healthy.
- “wallstreet_lo”: Filters tickers that are currently priced lower than what Wall Street analysts have predicted, potentially indicating undervalued stocks that could be attractive to investors looking for bargains.
These additional fields in the “signals” parameter enable more targeted and specific criteria for screening stocks, allowing for a nuanced approach to stock selection based on various financial metrics and analyst expectations.
Conclusion
In this article, we delved deep into EODHD’s Stock Screener API. We began by understanding what the Screener API entails and subsequently progressed to the practical programming aspect, where we crafted multiple stock screeners using Python.
This article intended to demonstrate some of the applications of the Screener API. However, the capabilities extend far beyond what has been covered here. You are encouraged to experiment with the various parameters and supported fields of the API to discover the myriad of possibilities it offers.
With that, we’ve concluded the article. We hope you found the information new and beneficial. Thank you very much for your time.
Please note that this article is for informational purposes only and should not be taken as financial advice. We do not bear responsibility for any trading decisions made based on the content of this article. Readers are advised to conduct their research or consult with a qualified financial professional before making any investment decisions.
For those eager to delve deeper into such insightful articles and broaden their understanding of different strategies in financial markets, we invite you to follow our account and subscribe for email notifications.
We publish 2 pieces per week!
Stay tuned for more valuable articles that aim to enhance your data science skills and market analysis capabilities.