Streamlit STOCK dashboard using Python 🔴

3 min read 3 hours ago
Published on Nov 06, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you will learn how to build a stock dashboard using Streamlit and Python. This dashboard will enable you to visualize stock prices, analyze fundamental data, and access stock news. Streamlit is an open-source app framework that simplifies the creation of web applications, making it an excellent choice for data-driven projects in finance.

Step 1: Set Up Your Streamlit Environment

  1. Install Streamlit:

    • Open your terminal or command prompt.
    • Run the following command to install Streamlit:
      pip install streamlit
      
  2. Create a New Python File:

    • Create a new directory for your project.
    • Inside this directory, create a Python file (e.g., stock_dashboard.py).
  3. Launch Streamlit:

    • Navigate to your project directory in the terminal.
    • Run the following command to start the Streamlit app:
      streamlit run stock_dashboard.py
      

Step 2: Extract Stock Prices Using yfinance

  1. Install yfinance:

    • In your terminal, run:
      pip install yfinance
      
  2. Import yfinance in Your Script:

    • Add the following import statement at the top of your Python file:
      import yfinance as yf
      
  3. Fetch Stock Data:

    • Create a function to fetch stock prices:
      def get_stock_data(ticker):
          stock = yf.Ticker(ticker)
          return stock.history(period="1d")
      

Step 3: Create Dashboard Tabs

  1. Import Streamlit Components:

    • At the top of your Python file, import Streamlit:
      import streamlit as st
      
  2. Set Up Tabs:

    • Use Streamlit's st.tabs() function to create tabs for pricing, fundamentals, and news:
      tabs = st.tabs(["Pricing", "Fundamentals", "News"])
      

Step 4: Implement Stock Price Analysis

  1. Display Stock Prices:
    • In the "Pricing" tab, fetch and display stock prices:
      with tabs[0]:
          ticker = st.text_input("Enter Stock Ticker")
          if ticker:
              data = get_stock_data(ticker)
              st.line_chart(data['Close'])
      

Step 5: Fetch Fundamental Data Using Alpha Vantage

  1. Sign Up for Alpha Vantage:

    • Create an account at Alpha Vantage to obtain your API key.
  2. Install Alpha Vantage Library:

    • Run the following command:
      pip install alpha-vantage
      
  3. Fetch Fundamental Data:

    • Add code to retrieve and display fundamental data:
      from alpha_vantage.fundamentaldata import FundamentalData
      
      def get_fundamental_data(ticker, api_key):
          fd = FundamentalData(key=api_key, output_format='pandas')
          data, _ = fd.get_company_overview(ticker)
          return data
      
  4. Display Fundamental Data:

    • In the "Fundamentals" tab, call the get_fundamental_data function and display the results.

Step 6: Implement Stock News Analysis

  1. Install Stocknews API:

    • Run the following command:
      pip install stocknews
      
  2. Fetch Stock News:

    • Use the Stocknews API to retrieve news articles related to the stock:
      from stocknews import StockNews
      
      def get_stock_news(ticker):
          sn = StockNews(ticker)
          return sn.get_news()
      
  3. Display News Articles:

    • In the "News" tab, call the get_stock_news function and display the articles.

Conclusion

You have successfully built a stock dashboard using Streamlit and Python. This dashboard allows you to analyze stock prices, fundamental data, and news, providing valuable insights into stock performance.

Next Steps

  • Explore additional features like sentiment analysis for news articles.
  • Consider deploying your Streamlit app using platforms like Streamlit Sharing or Heroku.
  • Experiment with different stock tickers and settings to enhance your dashboard.