stockprices / app.py
JSenkCC's picture
Update app.py
2897f4c verified
import streamlit as st
import yfinance as yf
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
# Title and user input
st.title("Stock Price Viewer")
ticker = st.text_input("Enter a stock ticker (e.g., AAPL, MSFT, TSLA):")
# Fetch and plot data if ticker is provided
if ticker:
# Set date range for the last 3 months
end_date = datetime.now()
start_date = end_date - timedelta(days=90)
# Fetch stock data
stock_data = yf.download(ticker, start=start_date, end=end_date)
# Check if data exists for the ticker
if not stock_data.empty:
st.write(f"Showing data for {ticker} from the last 3 months")
# Plot the closing prices
plt.figure(figsize=(10, 5))
plt.plot(stock_data.index, stock_data['Close'], label='Close Price')
plt.title(f"{ticker} Stock Price")
plt.xlabel("Date")
plt.ylabel("Price")
plt.legend()
st.pyplot(plt.gcf())
else:
st.error("No data found for this ticker. Please try another.")