Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import yfinance as yf | |
| import matplotlib.pyplot as plt | |
| def analyze_stock(stock_symbol): | |
| df = yf.download(stock_symbol, start="2022-01-01", end="2025-01-01", threads=False) | |
| if df.empty: | |
| return "β No data found. Try another symbol." | |
| # Calculate moving averages | |
| df['SMA_20'] = df['Close'].rolling(window=20).mean() | |
| df['SMA_50'] = df['Close'].rolling(window=50).mean() | |
| # Plot | |
| plt.figure(figsize=(12,6)) | |
| plt.plot(df['Close'], label='Close Price', color='blue') | |
| plt.plot(df['SMA_20'], label='20-day SMA', color='orange') | |
| plt.plot(df['SMA_50'], label='50-day SMA', color='green') | |
| plt.title(f'{stock_symbol} Stock Trend') | |
| plt.xlabel('Date') | |
| plt.ylabel('Price (in local currency)') | |
| plt.legend() | |
| plt.grid(True) | |
| plt.tight_layout() | |
| return plt.gcf() | |
| # Dropdown options: (Label, Ticker) | |
| stock_options = [ | |
| ("BMW", "BMW.DE"), | |
| ("TCS", "TCS.NS"), | |
| ("HDFC Bank", "HDFCBANK.NS"), | |
| ("Infosys", "INFY.NS"), | |
| ("Reliance", "RELIANCE.NS"), | |
| ("Apple", "AAPL"), | |
| ("Google", "GOOGL"), | |
| ("Microsoft", "MSFT") | |
| ] | |
| # Gradio Interface | |
| app = gr.Interface( | |
| fn=analyze_stock, | |
| inputs=gr.Dropdown(choices=[(name, symbol) for name, symbol in stock_options], label="Select Stock"), | |
| outputs="plot", | |
| title="π Stock Market Trend Analyzer", | |
| description="Select a stock to view its trend with 20-day and 50-day moving averages." | |
| ) | |
| app.launch() |