File size: 1,033 Bytes
2897f4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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.")