Learn to build your own custom stock screener with less than 10 lines of code
Stock screeners are a great way to sort and filter thousands of stocks at a time according to your needs but some people might feel difficult to code it from scratch. That’s when EOD Historical Data comes to the rescue. Most of us are familiar with stock APIs for historical data, real-time data, and intraday data but EOD Historical Data changes the game by offering stock screening APIs which makes it easy for beginners to get started with and create basic stock screeners using Python.
In this article, I’ll walk you through the stock screener APIs provided by EOD Historical and help you utilize those APIs to create your own custom stock screener as you desire. Without wasting any more time, let’s do some coding!
Note: This article is intensely based on the APIs provided by EOD Historical Data. So to follow up with the upcoming content without any distress, make sure that you have an account for yourself as it enables you to access your private API key. You can create an account using the link here. Also, not all APIs mentioned in this article is offered for free but a subscription is needed. View the pricing of all subscriptions here and choose what best fits you.
A Simple Stock Screener with Python
1. Importing Packages
Let’s first start off by importing the required packages into our python environment as it is the foremost process in any programming project. The only packages we need in this article are Requests to make API calls and Pandas for data processing and manipulations. In case you have not yet installed these two packages, enter the following lines of code in your command line:
pip install pandas
pip install requests
After installing the packages, we can now import them into our python environment using the code below:
import pandas as pd
import requests
api_key = 'YOUR API KEY'
We have now successfully imported the essential packages into our environment along with storing the API key into the 'api_key' variable.
2. Basic Screener with Single Filter
We are now going to create a stock screener with just one single condition or filter. Based on our condition, the screener represents stocks that have market capitalization exceeding over 100 billion dollars. Using the following code we can easily create a screener based on the above-described condition :
# SINGLE FILTER
mktcap_url = f'https://eodhistoricaldata.com/api/screener?api_token={api_key}&sort=market_capitalization.desc&filters=[["market_capitalization",">",100000000000]]&limit=10'
mktcap = requests.get(mktcap_url).json()
mktcap_df = pd.DataFrame(mktcap['data']).drop(
[
'sector', 'industry','refund_1d',
'refund_1d_p','refund_5d', 'refund_5d_p'
], axis = 1)
mktcap_df
In the above code, we are first storing the API URL into the 'mktcap_url' variable. Before moving on, we need to understand the structure of the API URL and the parameter it consists of. Here is the URL:
https://eodhistoricaldata.com/api/screener?api_token={api_key}&filters=[["market_capitalization",">",100000000000]]&limit=10
So the first parameter is the 'api_token' where we have to enter our API key. The next parameter is 'sort' where we can sort the numbers of select fields in either ascending or descending order. Followed by that, we have the 'filters' parameter where the condition of the screener goes in. Then finally, there is the 'limit' parameter in which we have to specify the number of stocks we want as output (10 in our case). Though these parameters are the primary ones, there are still a lot of additional parameters which can be utilized for a more specific screener.
Going back to our code, after saving the API URL, we are making an API call using the 'get' function provided by the Requests package and stored the API response in JSON format into the 'mktcap' variable. In order to make more sense out of the data, we are converting the API response into a Pandas DataFrame followed by removing some unwanted columns. The final result is a stock screener with the top 10 stocks having market capitalization greater than 100 billion dollars that looks like this:
3. Stock Screener with Multiple Filters
Now to take our project to the next level, we will add three more conditions to our screener. The conditions are:
MARKET CAPITALIZATION > 100 BILLION USD
EXCHANGE = UNITED STATES
SECTOR = TECHNOLOGY
EARNINGS PER SHARE > 5
Our aim in this section is to build a stock screener that filters stocks based on the above-mentioned conditions. The code for this screener is more or less the same as the previous screener though it has several other conditions and looks like this:
# MULTIPLE FILTERS
mf_url = f'https://eodhistoricaldata.com/api/screener?api_token={api_key}&sort=market_capitalization.desc&filters=[["market_capitalization",">",100000000000],["exchange","=","us"],["sector","=","Technology"],["earnings_share",">",5]]&limit=10'
mf = requests.get(mf_url).json()
mf_df = pd.DataFrame(mf['data']).drop(
[
'currency_symbol', 'refund_1d',
'refund_1d_p','refund_5d', 'refund_5d_p', 'industry'
], axis = 1)
mf_df
As I said before, everything that happens in the code is pretty much similar to the previous’ screener code but the only place which slightly changes is the 'filters' parameter in the API URL. We have specified all our four conditions in that parameter whereas only one was mentioned in the previous section’s code. Otherwise, everything’s the same. The output of the above code is a stock screener which represents the top 10 stocks based on four specified filters and looks like this:
Final Thoughts!
In this article, we learned to create two types of basic stock screeners using EOD Historical Data’s APIs and you could notice it hasn’t even exceeded more than 15 lines of code.
There is still a lot to be experimented with their screener APIs, for example, we have roughly touched upon four filters in this article but nearly ten total filters can be employed to create the perfect screener.
Apart from filters, there is also another interesting parameter which is the signal parameter which can be used to see stocks that have reached new 50-day-high, 200-day-high, negative bookvalue, and many more interesting values. We have not looked up this concept practically in this article but it is worth the time.
Though these APIs might not be a great fit for advanced users or professionals since they prefer more custom filters, it is well-suited for beginners to kick off with and get some basic idea.
Hope you learned something new and useful from this article. If you want to support me and my work, you can do that by availing of an EOD Historical Data subscription plan through the link here (which helps me generate some commission). Thank you very much for reading this article.
Comments