Create a Simple Stock Screening Application with Streamlit
Written on
Chapter 1: Introduction to the Stock Screening App
Imagine you're ready to invest your hard-earned money in the stock market. As a savvy investor, you only want to choose companies that meet specific criteria, avoiding subpar investments. Perhaps you already have a script for stock screening, but now you're considering building an application for a more user-friendly experience. This guide will walk you through creating a stock screener app using Python.
High-Level Overview of the Stock Screening App
In this tutorial, we will develop a web application in Python designed to identify stocks that align with your investment goals. To accomplish this, we will utilize the following tools:
- Streamlit: A web framework for Python that will serve as the frontend for our application.
- Financial Modeling Prep API: This API will provide the data required for our app.
- Requests: A Python library that simplifies sending HTTP requests, which we will use to communicate with the API.
- Pandas: A library used for data manipulation.
We should consider the layout of our application. Here’s a brief outline:
- The left column will present all screening parameters.
- The middle column will display the results based on our inputs.
- Below the results, a button will enable users to download the results.
Setting Up Your Development Environment
Before starting any project, it's advisable to set up a virtual environment to manage dependencies and ensure your project is reproducible. I prefer using PyCharm for my Python projects since it automatically creates a virtual environment for each new project (unless specified otherwise).
If your IDE does not create a virtual environment automatically, you can set one up manually in your project directory using the following command:
$ python3 -m venv venv
After creating the virtual environment, activate it with:
$ source venv/bin/activate
(venv) $
You are now ready to install the necessary external packages for your project.
Creating the Required Files
At this stage, we have established a virtual environment and acquired the API key from the Financial Modeling Prep developer site. Now, it’s time to start building our application.
We will create the following files:
- main.py: This will contain the user interface code.
- secrets.toml: This file will securely store our API credentials.
- api_logic.py: This file will manage the logic for interacting with the Financial Modeling Prep API.
Begin by creating the secrets.toml file within a directory called .streamlit:
(venv) $ mkdir .streamlit
(venv) $ touch secrets.toml
Store your API key in the secrets.toml file. We can retrieve the API key by using st.secrets.api_credentials.key. Next, we will create the other necessary files:
(venv) $ touch main.py api_logic.py
From Installation to "Hello World"
Now we’re ready to add code to our files and build the app. Start by installing the required packages one by one using pip. For instance, to install Streamlit:
$ pip install streamlit
Once Streamlit is installed, run the following command to launch a basic "Hello World" app in your web browser:
(venv) $ streamlit hello
Next, let’s add some code to the api_logic.py file. While the code should be fairly self-explanatory, additional documentation is included within the file for clarity.
Why Use Two Modules?
Before we proceed to the main.py module, let's discuss the rationale behind having two separate files. We differentiate between the application logic and the business logic.
The business logic includes the fundamental algorithms and functions that define the uniqueness of our application. In our stock screener app, this resides in the api_logic.py file. Conversely, the application logic encompasses the interactive components of the app, such as the layout and buttons, which provide a user interface for the business logic.
Separating these components simplifies maintenance, enhances readability, and encourages collaboration. As Colin Fay notes in "Engineering Shiny":
> Long scripts often lead to increased complexity in software development.
We are nearing the completion of our app. Now, let’s finalize the front-end portion by adding the necessary code to the main.py file.
To see your completed app in action, run the following command:
(venv) $ streamlit run main.py
Congratulations! You have successfully built a stock screening app while also grasping fundamental software engineering concepts. You now appreciate the value of virtual environments and the importance of separating business logic from application logic.
Thank you for following along. For the complete code, click here. Until next time! 👋🏾✌🏾
Chapter 2: YouTube Video Resources
In this chapter, we will provide some video resources to further enhance your understanding of building stock screening applications using Streamlit.
The first video, titled Streamlit STOCK dashboard using Python, offers a comprehensive overview of creating a stock dashboard application.
The second video, Build *EXTENSIVE Stock Screener using Streamlit & Financial Modeling Prep API Python*, dives deeper into building a robust stock screener with practical examples.
References
[1] Engineering Shiny by Colin Fay
Subscribe to DDIntel Here.