nick5363 commited on
Commit
b36715c
·
verified ·
1 Parent(s): 2ebe93b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -16
app.py CHANGED
@@ -43,27 +43,36 @@ if symbols_input:
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 stock_data is None or stock_data.empty or etf_data is None or etf_data.empty:
50
  results.append({"Symbol": symbol, "Sector ETF": etf, "Kết luận": "Không có dữ liệu"})
51
  continue
52
 
53
- if len(stock_data) >= 2 and len(etf_data) >= 2:
54
- pct_stock = (stock_data.iloc[-1] - stock_data.iloc[0]) / stock_data.iloc[0] * 100
55
- pct_sector = (etf_data.iloc[-1] - etf_data.iloc[0]) / etf_data.iloc[0] * 100
56
- verdict = "Outperform" if pct_stock > pct_sector else "Underperform"
57
-
58
- results.append({
59
- "Symbol": symbol,
60
- "Sector ETF": etf,
61
- "% Stock": f"{pct_stock:.2f}%",
62
- "% Sector": f"{pct_sector:.2f}%",
63
- "Kết luận": verdict
64
- })
65
- else:
66
  results.append({"Symbol": symbol, "Sector ETF": etf, "Kết luận": "Dữ liệu không đủ"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  df = pd.DataFrame(results)
69
- st.dataframe(df)
 
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
+ # Download data for both the stock and the ETF
47
+ stock_data = yf.download(symbol, period=period, progress=False)['Close']
48
+ etf_data = yf.download(etf, period=period, progress=False)['Close']
49
 
50
  if stock_data is None or stock_data.empty or etf_data is None or etf_data.empty:
51
  results.append({"Symbol": symbol, "Sector ETF": etf, "Kết luận": "Không có dữ liệu"})
52
  continue
53
 
54
+ # Align the indices by taking the intersection of the dates
55
+ common_dates = stock_data.index.intersection(etf_data.index)
56
+ if len(common_dates) < 2: # Need at least 2 data points to calculate percentage change
 
 
 
 
 
 
 
 
 
 
57
  results.append({"Symbol": symbol, "Sector ETF": etf, "Kết luận": "Dữ liệu không đủ"})
58
+ continue
59
+
60
+ # Reindex both Series to the common dates
61
+ stock_data = stock_data.loc[common_dates]
62
+ etf_data = etf_data.loc[common_dates]
63
+
64
+ # Calculate percentage change
65
+ pct_stock = (stock_data.iloc[-1] - stock_data.iloc[0]) / stock_data.iloc[0] * 100
66
+ pct_sector = (etf_data.iloc[-1] - etf_data.iloc[0]) / etf_data.iloc[0] * 100
67
+ verdict = "Outperform" if pct_stock > pct_sector else "Underperform"
68
+
69
+ results.append({
70
+ "Symbol": symbol,
71
+ "Sector ETF": etf,
72
+ "% Stock": f"{pct_stock:.2f}%",
73
+ "% Sector": f"{pct_sector:.2f}%",
74
+ "Kết luận": verdict
75
+ })
76
 
77
  df = pd.DataFrame(results)
78
+ st.dataframe(df)