Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from vnstock import Vnstock | |
| from datetime import datetime, timedelta | |
| # 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 | |
| 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 |