Creating a Finance Web App in 3 Minutes! | Towards Data Science

A vital part of any data-driven project is its ability to be easily explained and viewable even for others who may not know anything about the data beforehand. Introducing Streamlit in Python!
Streamlit is an extremely easy to use and intuitive tool for building highly interactive, data-driven web applications in Python. With this tool, you can simply focus on the data aspect without worrying about a tedious deployment with Flask or Django.
The process is entirely intuitive and by the end of this article, you too should be able to deploy your web app within minutes and only a few lines of code!
To get started with our Streamlit web app, we must first download it using PyPi (Python’s Package Manager). Simply type the following in your terminal for any packages you may not have (including streamlit) and we should be good to go.
pip install streamlit
If you don’t have pip installed on your computer, you can take a quick look at this article and return back here once it's installed.
First, we must import all the dependencies we will need throughout the program. The primary libraries we will use will be Yfinance, Streamlit, and TaLib. Yfinance will allow us to receive historical stock prices for any ticker, Streamlit will allow us to deploy the web app onto the localhost, and TaLib will allow us to calculate the technical indicators we will show on the app later on.
Now that we have the dependencies set up, we can finally get into the building of our app!
First, in order to run the program, head over to your terminal and type in the command streamlit run file_name.py. Replace file_name.py with the file name you choose to create from this program. A web app should then open up on your local host!
In order to write text on the website, we can use the .write() method in Streamlit. To show bigger and bolder text, we can use a hashtag (#) before and Streamlit will automatically change the text to make it look like a title (Markdown formatting). To bold text, we can simply surround the text we want to bold with two asterisks.
Next, we can create a header using the header method and set up a function to get the inputs from the user. Since we are creating a technical analysis web app, we need a stock ticker, start date, and end date. We can set values to be there automatically just incase the user doesn’t input there own. In this case, I went with Apple’s ticker (AAPL), 2019–01–01 as the start date, and today’s date for the end.
In order to make the web app look a little nicer, we can create a function that sends a request to Yahoo Finance and retrieve the companies name for any ticker we input!
We can convert the start and end dates from strings to DateTime format using the pandas.to_datetime() method just so we don’t get any errors later on. Lastly, we can retrieve the historical stock data with the Yfinance module that was mentioned earlier. Just to keep track of where we are currently, this is how the web app should look now.
Now, all that is left is creating the graphs we will display.
As I mentioned before, the wonderful TaLib library makes it ridiculously easy to calculate the technical indicators we will be plotting. For this project, I’ve chose the popular indicators such as Moving Average Crossovers, Bollinger Bands, Moving Average Convergence Divergence, Commodity Channel Index, Relative Strength Index, and On Balance Volume. To get more in-depth explanations on what these indicators can show, I would recommend looking them up on Investopedia.com.
For this section of code, there are three primary sections for each indicator we will show. First, we must calculate the indicator using TaLib, then we must set a header title that includes what the indicator actually is, and finally, we can display the chart using streamlit.line_chart(). That’s all it takes!
Now we have a fully interactive, live, responsive, and easy-to-use web application within a matter of minutes and with less than 100 lines of code. If you’ve been following along this entire time with the code we should have a similar-looking website as down below.
I’ve included the entire code for this program in the GitHub Gist below!
In terms of the next steps, you could expand on the data we showed as Streamlit allows users to show virtually any type of data, including Pandas DataFrames. Or you can decide to deploy this to the cloud using Heroku (if this is something you would like to do, check out this amazing article by Hamilton Chang). Now that you know how to create your own data web application, the possibilities are truly endless!
Thank you so much for reading and I hope you enjoyed it!
Note: If you are deploying this specific app to Heroku, remember to remove TaLib from the requirements.txt and instead, add a buildpack for it!
If you enjoyed this article, join my free investing community on Finary and check out some of my other articles below!