Streamlit STOCK dashboard using Python 🔴
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
-
Install Streamlit:
- Open your terminal or command prompt.
- Run the following command to install Streamlit:
pip install streamlit
-
Create a New Python File:
- Create a new directory for your project.
- Inside this directory, create a Python file (e.g.,
stock_dashboard.py
).
-
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
-
Install yfinance:
- In your terminal, run:
pip install yfinance
- In your terminal, run:
-
Import yfinance in Your Script:
- Add the following import statement at the top of your Python file:
import yfinance as yf
- Add the following import statement at the top of your Python file:
-
Fetch Stock Data:
- Create a function to fetch stock prices:
def get_stock_data(ticker): stock = yf.Ticker(ticker) return stock.history(period="1d")
- Create a function to fetch stock prices:
Step 3: Create Dashboard Tabs
-
Import Streamlit Components:
- At the top of your Python file, import Streamlit:
import streamlit as st
- At the top of your Python file, import Streamlit:
-
Set Up Tabs:
- Use Streamlit's
st.tabs()
function to create tabs for pricing, fundamentals, and news:tabs = st.tabs(["Pricing", "Fundamentals", "News"])
- Use Streamlit's
Step 4: Implement Stock Price Analysis
- 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'])
- In the "Pricing" tab, fetch and display stock prices:
Step 5: Fetch Fundamental Data Using Alpha Vantage
-
Sign Up for Alpha Vantage:
- Create an account at Alpha Vantage to obtain your API key.
-
Install Alpha Vantage Library:
- Run the following command:
pip install alpha-vantage
- Run the following command:
-
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
- Add code to retrieve and display fundamental data:
-
Display Fundamental Data:
- In the "Fundamentals" tab, call the
get_fundamental_data
function and display the results.
- In the "Fundamentals" tab, call the
Step 6: Implement Stock News Analysis
-
Install Stocknews API:
- Run the following command:
pip install stocknews
- Run the following command:
-
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()
- Use the Stocknews API to retrieve news articles related to the stock:
-
Display News Articles:
- In the "News" tab, call the
get_stock_news
function and display the articles.
- In the "News" tab, call the
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.