bparekh99 commited on
Commit
166c5cb
·
verified ·
1 Parent(s): 6672e1c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import yfinance as yf
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+
6
+ st.title('Stock vs S&P 500 and Dow Jones Comparison')
7
+
8
+ # User input
9
+ user_stock = st.text_input('Enter a stock symbol (e.g., AAPL, TSLA, MSFT)', 'MAR')
10
+
11
+ if user_stock:
12
+ tickers = [user_stock.upper(), '^GSPC', '^DJI']
13
+
14
+ try:
15
+ # Download past 1 year of daily data
16
+ data = yf.download(tickers, period='1y', interval='1d')
17
+
18
+ # Check if user stock symbol returned data
19
+ if data.empty or user_stock.upper() not in data.columns.get_level_values(1):
20
+ st.error('Not a valid stock symbol')
21
+ else:
22
+ # If MultiIndex, focus on 'Close' prices
23
+ if isinstance(data.columns, pd.MultiIndex):
24
+ close_prices = data['Close']
25
+ else:
26
+ close_prices = data
27
+
28
+ # Normalize (start at 100)
29
+ normalized = close_prices / close_prices.iloc[0] * 100
30
+
31
+ # Plot
32
+ fig, ax = plt.subplots(figsize=(12, 6))
33
+ for column in normalized.columns:
34
+ ax.plot(normalized.index, normalized[column], label=column)
35
+
36
+ ax.set_title(f'{user_stock.upper()} vs S&P 500 vs Dow Jones (Past 1 Year)')
37
+ ax.set_xlabel('Date')
38
+ ax.set_ylabel('Normalized Price (Start = 100)')
39
+ ax.legend()
40
+ ax.grid(True)
41
+ st.pyplot(fig)
42
+ except Exception as e:
43
+ st.error('Not a valid stock symbol')