File size: 2,437 Bytes
f9e9d4f
 
 
afbd858
f9e9d4f
 
 
 
 
 
afbd858
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9e9d4f
 
 
 
 
afbd858
 
f9e9d4f
 
 
e622f3a
 
f9e9d4f
4aab442
 
f9e9d4f
 
e622f3a
 
 
 
b36715c
e622f3a
 
 
 
 
 
 
 
 
f9e9d4f
 
e622f3a
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import yfinance as yf
import streamlit as st
import pandas as pd
import requests

st.title("So sánh Outperform / Underperform theo Sector")

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):")
period = st.selectbox("Chọn khoảng thời gian so sánh:", ['5d', '1mo', '3mo', '6mo'])

# Tự động dò ETF theo sector dựa trên yfinance
sector_to_etf = {
    'Technology': 'XLK',
    'Health Care': 'XLV',
    'Financial Services': 'XLF',
    'Consumer Cyclical': 'XLY',
    'Energy': 'XLE',
    'Industrials': 'XLI',
    'Consumer Defensive': 'XLP',
    'Utilities': 'XLU',
    'Materials': 'XLB',
    'Real Estate': 'XLRE',
    'Communication Services': 'XLC'
}

def get_sector_etf(symbol):
    try:
        ticker = yf.Ticker(symbol)
        info = ticker.info
        sector = info.get("sector", None)
        etf = sector_to_etf.get(sector, None)
        return sector, etf
    except:
        return None, None

if symbols_input:
    symbols = [s.strip().upper() for s in symbols_input.split(",")]
    results = []

    for symbol in symbols:
        sector, etf = get_sector_etf(symbol)
        if not etf:
            results.append({"Symbol": symbol, "Sector ETF": "Không rõ", "Kết luận": "Không phân tích được"})
            continue

        stock_data = yf.download(symbol, period=period)['Close']
        etf_data = yf.download(etf, period=period)['Close']

        if stock_data is None or stock_data.empty or etf_data is None or etf_data.empty:
            results.append({"Symbol": symbol, "Sector ETF": etf, "Kết luận": "Không có dữ liệu"})
            continue

        if len(stock_data) >= 2 and len(etf_data) >= 2:
            pct_stock = float((stock_data.iloc[-1] - stock_data.iloc[0]) / stock_data.iloc[0] * 100)
            pct_sector = float((etf_data.iloc[-1] - etf_data.iloc[0]) / etf_data.iloc[0] * 100)
            verdict = "Outperform" if pct_stock > pct_sector else "Underperform"

            results.append({
                "Symbol": symbol,
                "Sector ETF": etf,
                "% Stock": f"{pct_stock:.2f}%",
                "% Sector": f"{pct_sector:.2f}%",
                "Kết luận": verdict
            })
        else:
            results.append({"Symbol": symbol, "Sector ETF": etf, "Kết luận": "Dữ liệu không đủ"})

    df = pd.DataFrame(results)
    st.dataframe(df)