Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from datetime import datetime, timedelta
|
5 |
+
|
6 |
+
# Title and user input
|
7 |
+
st.title("Stock Price Viewer")
|
8 |
+
ticker = st.text_input("Enter a stock ticker (e.g., AAPL, MSFT, TSLA):")
|
9 |
+
|
10 |
+
# Fetch and plot data if ticker is provided
|
11 |
+
if ticker:
|
12 |
+
# Set date range for the last 3 months
|
13 |
+
end_date = datetime.now()
|
14 |
+
start_date = end_date - timedelta(days=90)
|
15 |
+
|
16 |
+
# Fetch stock data
|
17 |
+
stock_data = yf.download(ticker, start=start_date, end=end_date)
|
18 |
+
|
19 |
+
# Check if data exists for the ticker
|
20 |
+
if not stock_data.empty:
|
21 |
+
st.write(f"Showing data for {ticker} from the last 3 months")
|
22 |
+
|
23 |
+
# Plot the closing prices
|
24 |
+
plt.figure(figsize=(10, 5))
|
25 |
+
plt.plot(stock_data.index, stock_data['Close'], label='Close Price')
|
26 |
+
plt.title(f"{ticker} Stock Price")
|
27 |
+
plt.xlabel("Date")
|
28 |
+
plt.ylabel("Price")
|
29 |
+
plt.legend()
|
30 |
+
st.pyplot(plt.gcf())
|
31 |
+
else:
|
32 |
+
st.error("No data found for this ticker. Please try another.")
|