Creating a simple Stock Screener in minutes with Python | by Nikhil Adithyan | CodeX | Nov, 2021

Learn to build your custom stock screener with less than 10 lines of code

Nikhil Adithyan
Photo by Joshua Aragon on Unsplash

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 custom stock screener as you desire. Without wasting any more time, let’s do some coding!

1. Importing Packages

Let’s first start 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: