Skip to content

Commit 2c97bb7

Browse files
committed
stock data app - hm-3
0 parents  commit 2c97bb7

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

stock-data-app/main.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from datetime import date, datetime
2+
import time
3+
import streamlit as st
4+
import yfinance as yf
5+
6+
# Display a styled title in HTML format
7+
st.header("Stock Price App.")
8+
st.subheader("Jesus Torres")
9+
st.write("""
10+
Reference:
11+
- Streamlit Framework: https://streamlit.io
12+
- Streamlit Documentation: https://docs.streamlit.io/library/api-reference
13+
- yfinance Package: https://aroussi.com/post/python-yahoo-finance
14+
- yfinance Documentation: https://ranaroussi.github.io/yfinance/index.html
15+
""")
16+
17+
# Brief description of the app's purpose
18+
st.write("This app retrieves stock data from Yahoo Finance API and displays it in a user-friendly format.")
19+
20+
# Define a list of popular stock symbols
21+
stock_list = ['MSFT', 'AAPL', 'AMZN', 'GOOGL',
22+
'TSLA', 'META', 'NFLX', 'NVDA', 'PLTR',]
23+
24+
# Create a dropdown menu for selecting a stock
25+
stock_name = st.selectbox('Select a stock to check', options=stock_list)
26+
27+
# Input for selecting the start date of stock data, default is January 1, 2024
28+
start_date = st.date_input('Start Date', datetime(2024, 1, 1))
29+
# Input for selecting the end date of stock data, default is today
30+
end_date = st.date_input("End Date")
31+
32+
# Store today's date for validation
33+
today = date.today()
34+
35+
36+
def stream_summary(summary):
37+
for word in summary.split(" "):
38+
yield word + " "
39+
time.sleep(0.02) # Simulate a delay for streaming effect
40+
41+
42+
# Action to retrieve stock data when the 'Submit' button is clicked
43+
if st.button('Submit'):
44+
# Check if selected dates are valid (not in the future)
45+
if (start_date > today) or (end_date > today) or (start_date > end_date):
46+
st.warning("Please select a valid date period.")
47+
else:
48+
# Get data for the selected stock from yfinance API
49+
stock = yf.Ticker(stock_name)
50+
51+
# Get stock information
52+
stock_info = stock.get_info()
53+
54+
# Retrieve historical data between selected dates
55+
stock_history = stock.history(start=start_date, end=end_date)
56+
57+
# Display Stock information
58+
st.header(
59+
f"****Stock Name: {stock_info['longName']}**** ({stock_info['symbol']})")
60+
st.subheader(f" **Sector:** {stock_info['sector']}")
61+
st.subheader(f"**Industry:** {stock_info['industry']}")
62+
# Display stock business summary using write_stream for better formatting
63+
st.subheader("Business Summary:")
64+
st.write_stream(stream_summary(stock_info['longBusinessSummary']))
65+
66+
st.write("**Raw Data of Stock Price**")
67+
# Display raw data of stock price
68+
st.dataframe(stock_history)
69+
# Plot stock price data (Open, High, Low, Close)
70+
st.line_chart(stock_history, y=['Open', 'High', 'Low', 'Close'])
71+
72+
# Display a success message upon completion
73+
st.success('Done')

0 commit comments

Comments
 (0)