File size: 2,013 Bytes
021d91d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from vnstock import Vnstock
from datetime import datetime, timedelta

@st.cache_data(ttl=300)  # Cache 5 phút
def get_stock_data(symbol, source, start_date, end_date):
    """
    Lấy dữ liệu giá cổ phiếu từ API
    
    Args:
        symbol: Mã cổ phiếu
        source: Nguồn dữ liệu (VCI, TCBS)
        start_date: Ngày bắt đầu
        end_date: Ngày kết thúc
        
    Returns:
        DataFrame chứa dữ liệu giá cổ phiếu
    """
    try:
        stock = Vnstock().stock(symbol=symbol, source=source)
        price_data = stock.quote.history(start=start_date, end=end_date)
        return price_data
    except Exception as e:
        st.error(f"Lỗi khi lấy dữ liệu: {str(e)}")
        return None

@st.cache_data(ttl=300)
def get_financial_data(symbol, source, period='year'):
    """
    Lấy dữ liệu tài chính từ API
    
    Args:
        symbol: Mã cổ phiếu
        source: Nguồn dữ liệu (VCI, TCBS)
        period: Chu kỳ báo cáo (year, quarter)
        
    Returns:
        Tuple chứa (income, balance, cashflow, ratio)
    """
    try:
        stock = Vnstock().stock(symbol=symbol, source=source)
        income = stock.finance.income_statement(period=period, lang='vi')
        balance = stock.finance.balance_sheet(period=period, lang='vi')
        cashflow = stock.finance.cash_flow(period=period, lang='vi')
        ratio = stock.finance.ratio(period=period, lang='vi')
        return income, balance, cashflow, ratio
    except Exception as e:
        st.error(f"Lỗi khi lấy dữ liệu tài chính: {str(e)}")
        return None, None, None, None

def get_default_dates():
    """
    Trả về ngày bắt đầu và kết thúc mặc định (1 năm)
    
    Returns:
        Tuple chứa (start_date, end_date)
    """
    end_date = datetime.now().strftime('%Y-%m-%d')
    start_date = (datetime.now() - timedelta(days=365)).strftime('%Y-%m-%d')
    return start_date, end_date