Stock Information Application with Python

Simple and interactive stock application using python streamlit

Introduction

In this tutorial, we'll use Python and the libraries streamlit and yfinance to create a stock information app. By entering a stock ticker user of this application can get real-time information about a stock such as its price, volume, and market capitalization.

The lightweight module yfinance makes it simple to obtain stock data from Yahoo Finance, while streamlit makes it simple to create interactive, web-based apps in Python. Together, these technologies make it simple and quick to create a useful stock information app.

Designing an application

You should know basic Python before continuing with this tutorial. You'll also need to have the streamlit and yfinance libraries installed on your machine. If you don't have these libraries installed, you can install them using a pip

pip install streamlit
pip install yfinance

Setting up the development environment

Create a new directory for your project and enter its path on the command line to begin. Make the "app.py" file and then open it in your editor. The code for our stock information app will be written here.

Let's import the required libraries and configure our streamlit program next.

import yfinance as yf
import streamlit as st

st.write("""
## Simple Stock Information App """)

This code generates a simple Streamlit app with a title and imports the streamlit and yfinance libraries. You should see a blank page with the title "Simple Stock Information App" if you run this code and go to http://localhost:8501 in your web browser.

Retrieving stock data with yfinance

Now, let's retrieve some stock data using yfinance. Below the code you just added, add the following

# Get stock symbol from user
symbol = st.text_input("Stock Symbol")
Data = yf.Ticker(symbol)
info = Data.info

This code prompts the user to enter a stock ticker using a streamlit text input widget and then uses yfinance to retrieve the relevant data for that ticker. The data is stored in a dictionary called "info" and we extract the data by keys like current price, volume, market capitalization and all other information.

Displaying stock data in the application

Now that we have the stock information, let's display other data in our app.

if type(info) == dict:
    # check if variable having value
    # get some important values
    if st.button('More Information'):
        st.write("Name:  {}  ".format(info.get('shortName')))
        st.write("Sector:  {}".format(info.get('sector')))
        st.write("Country:  {}".format(info.get('country')))
        st.write("Industry:  {}".format(info.get('industry')))
        st.write("Total Revenue:  {}".format(info.get('totalRevenue')))
        st.write("Pre Market Price:  {}".format(info.get('preMarketPrice')))
        st.write("Open:  {}".format(info.get('open')))
        st.write("Current Price:  {}".format(info.get('currentPrice')))
        st.write("Fifty Two Week High:  {}".format(info.get('fiftyTwoWeekHigh')))
        st.write("fifty Two Week Low:  {}".format(info.get('fiftyTwoWeekLow')))
        st.write("Website:  {}".format(info.get('website')))

    else:
        pass

This code displays the stock data in the app using streamlit's "write" method. We are obtaining values for a given key from a dictionary. To determine whether the dictionary contains data or not, we use the type function.

# Balance sheet
if st.button('Balance sheet'):
        st.write(Data.balancesheet)
# Cash flow statement
if st.button('Cashflow sheet'):
        st.write(Data.cashflow)
# Financials statement
if st.button('Financials sheet'):
        st.write(Data.financials)

This code uses streamlit's "button" function to display the stock data in the application. We are creating button conditions for displaying the balance sheet, cash flow, and financial data.

# closing price chart graph
tickerDf = Data.history(period='1d', start='2021-01-01', end='2022-12-30')
st.write("""
    ### Closing Price """)
# volume graph
st.line_chart(tickerDf.Close)
st.write("""
    ### Volume """)
st.line_chart(tickerDf.Volume)

To display a chart about closing and volume from 2021-01-01 to 2022-12-30, we are utilizing the streamlit line chart function.

Finally, we present a choice for displaying closing information, including closing price and volume.

 # Information in dataframe
option = st.selectbox(
        'Seleted from following',
        ('Historical Data', 'Closing Price', 'Volume'))
if option == 'Historical Data':
      st.write(Data.history())
elif option == "Closing Price":
      st.write("""
        ### Closing Price """, tickerDf.Close)
else:
     st.write("""
        ### Volume """, tickerDf.Volume)

In conclusion, with just a few lines of code, we were able to create an interactive web app that allows users to enter a stock ticker and retrieve real-time data about the stock.

This tutorial just scratches the surface of what's possible with Streamlit and yfinance. With a little bit of creativity and some additional features, you can turn this app into a full-featured stock tracking tool or even a stock market analysis platform.

source code can be found on Github

Did you find this article valuable?

Support Niraj's by becoming a sponsor. Any amount is appreciated!