nick5363 commited on
Commit
afbd858
·
verified ·
1 Parent(s): 79e5ba4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -1,39 +1,53 @@
1
  import yfinance as yf
2
  import streamlit as st
3
  import pandas as pd
4
-
5
- # Map đơn giản giữa cổ phiếu và ETF ngành (có thể mở rộng)
6
- sector_etf_map = {
7
- 'HIMS': 'XLV', # Healthcare
8
- 'AAPL': 'XLK', # Technology
9
- 'MSFT': 'XLK',
10
- 'TSLA': 'XLY', # Consumer Discretionary
11
- 'JPM': 'XLF', # Financials
12
- 'CVX': 'XLE', # Energy
13
- 'NKE': 'XLY',
14
- 'NVDA': 'XLK'
15
- }
16
 
17
  st.title("So sánh Outperform / Underperform theo Sector")
18
 
19
  symbols_input = st.text_input("Nhập mã cổ phiếu (cách nhau bằng dấu phẩy, ví dụ: HIMS,AAPL,TSLA):")
20
  period = st.selectbox("Chọn khoảng thời gian so sánh:", ['5d', '1mo', '3mo', '6mo'])
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  if symbols_input:
23
  symbols = [s.strip().upper() for s in symbols_input.split(",")]
24
  results = []
25
 
26
  for symbol in symbols:
27
- sector_etf = sector_etf_map.get(symbol, None)
28
- if not sector_etf:
29
  results.append({"Symbol": symbol, "Sector ETF": "Không rõ", "Kết luận": "Không phân tích được"})
30
  continue
31
 
32
  stock_data = yf.download(symbol, period=period)['Close']
33
- etf_data = yf.download(sector_etf, period=period)['Close']
34
 
35
  if len(stock_data) < 2 or len(etf_data) < 2:
36
- results.append({"Symbol": symbol, "Sector ETF": sector_etf, "Kết luận": "Thiếu dữ liệu"})
37
  continue
38
 
39
  pct_stock = (stock_data[-1] - stock_data[0]) / stock_data[0] * 100
@@ -42,7 +56,7 @@ if symbols_input:
42
 
43
  results.append({
44
  "Symbol": symbol,
45
- "Sector ETF": sector_etf,
46
  "% Stock": f"{pct_stock:.2f}%",
47
  "% Sector": f"{pct_sector:.2f}%",
48
  "Kết luận": verdict
 
1
  import yfinance as yf
2
  import streamlit as st
3
  import pandas as pd
4
+ import requests
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  st.title("So sánh Outperform / Underperform theo Sector")
7
 
8
  symbols_input = st.text_input("Nhập mã cổ phiếu (cách nhau bằng dấu phẩy, ví dụ: HIMS,AAPL,TSLA):")
9
  period = st.selectbox("Chọn khoảng thời gian so sánh:", ['5d', '1mo', '3mo', '6mo'])
10
 
11
+ # Tự động dò ETF theo sector dựa trên yfinance
12
+ sector_to_etf = {
13
+ 'Technology': 'XLK',
14
+ 'Health Care': 'XLV',
15
+ 'Financial Services': 'XLF',
16
+ 'Consumer Cyclical': 'XLY',
17
+ 'Energy': 'XLE',
18
+ 'Industrials': 'XLI',
19
+ 'Consumer Defensive': 'XLP',
20
+ 'Utilities': 'XLU',
21
+ 'Materials': 'XLB',
22
+ 'Real Estate': 'XLRE',
23
+ 'Communication Services': 'XLC'
24
+ }
25
+
26
+ def get_sector_etf(symbol):
27
+ try:
28
+ ticker = yf.Ticker(symbol)
29
+ info = ticker.info
30
+ sector = info.get("sector", None)
31
+ etf = sector_to_etf.get(sector, None)
32
+ return sector, etf
33
+ except:
34
+ return None, None
35
+
36
  if symbols_input:
37
  symbols = [s.strip().upper() for s in symbols_input.split(",")]
38
  results = []
39
 
40
  for symbol in symbols:
41
+ sector, etf = get_sector_etf(symbol)
42
+ if not etf:
43
  results.append({"Symbol": symbol, "Sector ETF": "Không rõ", "Kết luận": "Không phân tích được"})
44
  continue
45
 
46
  stock_data = yf.download(symbol, period=period)['Close']
47
+ etf_data = yf.download(etf, period=period)['Close']
48
 
49
  if len(stock_data) < 2 or len(etf_data) < 2:
50
+ results.append({"Symbol": symbol, "Sector ETF": etf, "Kết luận": "Thiếu dữ liệu"})
51
  continue
52
 
53
  pct_stock = (stock_data[-1] - stock_data[0]) / stock_data[0] * 100
 
56
 
57
  results.append({
58
  "Symbol": symbol,
59
+ "Sector ETF": etf,
60
  "% Stock": f"{pct_stock:.2f}%",
61
  "% Sector": f"{pct_sector:.2f}%",
62
  "Kết luận": verdict