VN Data Fetcher — Lấy dữ liệu thị trường CK Việt Nam
Tổng quan
Wrapper CLI quanh thư viện open-source vnstock để lấy:
- OHLCV lịch sử (cổ phiếu, chỉ số, ETF) —
ohlcv subcommand
- Thông tin niêm yết (ngành, vốn hoá, ngày niêm yết) —
info subcommand
- Lịch sử cổ tức (cash + stock dividend) —
dividends subcommand
- Fundamentals (P/E, P/B, ROE, EPS, payout ratio, ...) —
fundamentals subcommand
- Foreign flow snapshot (point-in-time, không có daily series) —
foreign-flow subcommand
Mỗi lệnh có cache CSV/parquet dưới state/vn_market_data/ để tránh fetch lại data đã có. Có fixture mode để test offline mà không cần kết nối Internet hoặc vnstock.
Khi nào dùng
- Một skill VN khác cần OHLCV cho một mã (vd:
vn-vcp-screener, vn-breakout-trade-planner)
- Cần thông tin định danh cho mã: sàn, ngành, room ngoại, số CP lưu hành
- Cần series foreign flow cho phân tích sentiment khối ngoại
- User hỏi "giá đóng cửa của VIC 30 phiên gần nhất", "khối ngoại mua/bán ròng FPT tháng trước"
KHÔNG dùng skill này để:
- Lập kế hoạch giao dịch — dùng
vn-position-sizer hoặc vn-breakout-trade-planner
- Phân tích room ngoại theo watchlist — dùng
vn-foreign-room-tracker (sắp ra mắt)
Điều kiện tiên quyết
- Python 3.9+
vnstock>=3.0 (cài qua pip install -e ".[vn]" hoặc pip install vnstock)
- Kết nối Internet (trừ khi dùng
--fixture)
- Pandas (transitively từ vnstock)
Workflow
Bước 1: Xác định subcommand
| Subcommand |
Mục đích |
ohlcv |
Open/High/Low/Close/Volume theo ngày, tuần, tháng, hoặc intraday |
info |
Thông tin niêm yết (sàn, ngành, vốn hoá, số CP lưu hành) |
dividends |
Lịch sử cổ tức (cash + stock). Tự động phân loại type, convert % → VND/CP |
fundamentals |
Báo cáo tỷ số tài chính theo quarter hoặc year (P/E, P/B, ROE, EPS, payout) |
foreign-flow |
Snapshot khối ngoại (point-in-time only — không có daily series) |
Lưu ý: foreign-flow chỉ trả snapshot tại thời điểm gọi. vnstock không cung cấp daily-series endpoint thống nhất cho khối ngoại. Để xây history theo ngày, dùng vn-foreign-room-tracker record với CSV nhập thủ công.
Bước 2: Chạy lệnh
# OHLCV: 1 mã, 1 ngày tròn năm
python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py ohlcv \
--symbol VIC --start 2025-05-01 --end 2026-05-12 \
--interval 1D --source VCI \
--output-dir reports/
# OHLCV: nhiều mã batch
python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py ohlcv \
--symbols VIC,HPG,VNM,FPT \
--start 2026-01-01 --end 2026-05-12 \
--output-dir reports/
# Info công ty
python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py info \
--symbol VIC --source VCI \
--output-dir reports/
# Foreign flow snapshot (point-in-time)
python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py foreign-flow \
--symbol VIC \
--output-dir reports/
# Lịch sử cổ tức (cash + stock)
python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py dividends \
--symbol VIC --source VCI \
--output-dir reports/
# Fundamentals theo quý (P/E, P/B, ROE, EPS, payout)
python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py fundamentals \
--symbol VIC --period quarter \
--output-dir reports/
# Fundamentals theo năm
python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py fundamentals \
--symbol VIC --period year \
--output-dir reports/
# Test offline với fixture
python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py ohlcv \
--symbol VIC --start 2026-05-01 --end 2026-05-12 \
--fixture tests/fixtures/vic_daily.csv \
--output-dir reports/
# Skip cache (lấy data tươi)
python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py ohlcv \
--symbol VIC --start 2026-05-01 --end 2026-05-12 \
--no-cache --output-dir reports/
Bước 3: Đọc output
Mỗi lệnh xuất hai file:
<symbol>_<subcommand>_<timestamp>.json — metadata + dữ liệu (record-oriented)
<symbol>_<subcommand>_<timestamp>.csv (cho ohlcv) — CSV phẳng dễ đọc
Cache lưu tại state/vn_market_data/<symbol>_<source>_<interval>.csv — không có timestamp; lần fetch sau cùng phủ lên (full refresh).
Bước 4: Báo cáo cho user
Tóm tắt số dòng, khoảng ngày, mã, và ghi chú nếu data thiếu hoặc có gap.
Output Format
OHLCV JSON
{
"schema_version": "1.0",
"subcommand": "ohlcv",
"symbol": "VIC",
"source": "VCI",
"interval": "1D",
"range": {
"start": "2026-05-01",
"end": "2026-05-12"
},
"row_count": 7,
"fetched_at": "2026-05-13T07:45:00+07:00",
"cache_status": "miss",
"data": [
{"time": "2026-05-04", "open": 217.0, "high": 220.0, "low": 204.0, "close": 212.0, "volume": 3884000},
...
]
}
Info JSON
{
"schema_version": "1.0",
"subcommand": "info",
"symbol": "VIC",
"source": "VCI",
"exchange": "HOSE",
"industry": "Bất động sản",
"listed_date": "2007-09-19",
"outstanding_shares": 3823661561,
"market_cap_vnd": null
}
Resources
scripts/vn_data_fetcher.py — CLI chính với các subcommand
scripts/tests/ — pytest suite + fixtures CSV
- Tham chiếu:
skills/vn-market-mechanics/references/vn_data_sources.md cho quy ước data sources và conventions
Lưu ý vận hành
- Rate limit: Các nguồn upstream (TCBS/VCI/SSI) có thể rate-limit. Cache hợp lý giúp giảm tải. Nếu gặp lỗi rate-limit, retry sau 30-60s.
- Độ trễ T+0: Một số endpoint có data của phiên hôm nay chỉ vào cuối ngày. Không kỳ vọng intraday real-time chính xác.
- Đơn vị giá: vnstock thường trả giá theo nghìn VND (ví dụ
217.0 = 217,000 VND/CP) — script ghi nhận thông tin này trong metadata.
- Mã hợp lệ: Cổ phiếu HOSE/HNX/UPCOM dùng 3-5 ký tự in hoa. Chỉ số:
VNINDEX, VN30, HNX-INDEX. Phái sinh: VN30F1M, VN30F2M.
- Fixture cho test: Định dạng CSV phải có cột
time, open, high, low, close, volume. Có thể tạo từ output cache cũ.
Nguyên tắc
- Cache đầu tiên — tránh fetch lại data đã có. User dùng
--no-cache khi cần data tươi.
- Fixture cho test — mọi test offline phải dùng
--fixture, không gọi vnstock thật.
- Log rõ ràng — báo cho user biết hit cache hay miss, có gap dữ liệu không.
- Fail fast — nếu vnstock không cài hoặc symbol sai, báo lỗi rõ ràng và dừng.
1---2name: vn-data-fetcher3description: Lấy dữ liệu thị trường chứng khoán Việt Nam qua thư viện vnstock — OHLCV, thông tin niêm yết, lịch sử cổ tức, fundamentals (P/E, P/B, ROE, EPS, payout), và snapshot khối ngoại. Cache local dưới state/vn_market_data/. Hỗ trợ fixture mode cho test offline. Kích hoạt khi cần lấy giá lịch sử, thông tin niêm yết, cổ tức, hoặc tỷ số tài chính cho cổ phiếu HOSE/HNX/UPCOM. Vietnam stock data fetcher — OHLCV, info, dividends, fundamentals (ratios), foreign-flow snapshot via vnstock.4---56# VN Data Fetcher — Lấy dữ liệu thị trường CK Việt Nam78## Tổng quan910Wrapper CLI quanh thư viện open-source `vnstock` để lấy:1112- **OHLCV lịch sử** (cổ phiếu, chỉ số, ETF) — `ohlcv` subcommand13- **Thông tin niêm yết** (ngành, vốn hoá, ngày niêm yết) — `info` subcommand14- **Lịch sử cổ tức** (cash + stock dividend) — `dividends` subcommand15- **Fundamentals** (P/E, P/B, ROE, EPS, payout ratio, ...) — `fundamentals` subcommand16- **Foreign flow snapshot** (point-in-time, không có daily series) — `foreign-flow` subcommand1718Mỗi lệnh có **cache CSV/parquet** dưới `state/vn_market_data/` để tránh fetch lại data đã có. Có **fixture mode** để test offline mà không cần kết nối Internet hoặc vnstock.1920## Khi nào dùng2122- Một skill VN khác cần OHLCV cho một mã (vd: `vn-vcp-screener`, `vn-breakout-trade-planner`)23- Cần thông tin định danh cho mã: sàn, ngành, room ngoại, số CP lưu hành24- Cần series foreign flow cho phân tích sentiment khối ngoại25- User hỏi "giá đóng cửa của VIC 30 phiên gần nhất", "khối ngoại mua/bán ròng FPT tháng trước"2627**KHÔNG** dùng skill này để:28- Lập kế hoạch giao dịch — dùng `vn-position-sizer` hoặc `vn-breakout-trade-planner`29- Phân tích room ngoại theo watchlist — dùng `vn-foreign-room-tracker` (sắp ra mắt)3031## Điều kiện tiên quyết3233- Python 3.9+34- `vnstock>=3.0` (cài qua `pip install -e ".[vn]"` hoặc `pip install vnstock`)35- Kết nối Internet (trừ khi dùng `--fixture`)36- Pandas (transitively từ vnstock)3738## Workflow3940### Bước 1: Xác định subcommand4142| Subcommand | Mục đích |43| --- | --- |44| `ohlcv` | Open/High/Low/Close/Volume theo ngày, tuần, tháng, hoặc intraday |45| `info` | Thông tin niêm yết (sàn, ngành, vốn hoá, số CP lưu hành) |46| `dividends` | Lịch sử cổ tức (cash + stock). Tự động phân loại type, convert % → VND/CP |47| `fundamentals` | Báo cáo tỷ số tài chính theo `quarter` hoặc `year` (P/E, P/B, ROE, EPS, payout) |48| `foreign-flow` | Snapshot khối ngoại (point-in-time only — không có daily series) |4950> **Lưu ý:** `foreign-flow` chỉ trả snapshot tại thời điểm gọi. vnstock không cung cấp daily-series endpoint thống nhất cho khối ngoại. Để xây history theo ngày, dùng `vn-foreign-room-tracker record` với CSV nhập thủ công.5152### Bước 2: Chạy lệnh5354```bash55# OHLCV: 1 mã, 1 ngày tròn năm56python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py ohlcv \57 --symbol VIC --start 2025-05-01 --end 2026-05-12 \58 --interval 1D --source VCI \59 --output-dir reports/6061# OHLCV: nhiều mã batch62python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py ohlcv \63 --symbols VIC,HPG,VNM,FPT \64 --start 2026-01-01 --end 2026-05-12 \65 --output-dir reports/6667# Info công ty68python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py info \69 --symbol VIC --source VCI \70 --output-dir reports/7172# Foreign flow snapshot (point-in-time)73python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py foreign-flow \74 --symbol VIC \75 --output-dir reports/7677# Lịch sử cổ tức (cash + stock)78python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py dividends \79 --symbol VIC --source VCI \80 --output-dir reports/8182# Fundamentals theo quý (P/E, P/B, ROE, EPS, payout)83python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py fundamentals \84 --symbol VIC --period quarter \85 --output-dir reports/8687# Fundamentals theo năm88python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py fundamentals \89 --symbol VIC --period year \90 --output-dir reports/9192# Test offline với fixture93python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py ohlcv \94 --symbol VIC --start 2026-05-01 --end 2026-05-12 \95 --fixture tests/fixtures/vic_daily.csv \96 --output-dir reports/9798# Skip cache (lấy data tươi)99python3 skills/vn-data-fetcher/scripts/vn_data_fetcher.py ohlcv \100 --symbol VIC --start 2026-05-01 --end 2026-05-12 \101 --no-cache --output-dir reports/102```103104### Bước 3: Đọc output105106Mỗi lệnh xuất hai file:107108- `<symbol>_<subcommand>_<timestamp>.json` — metadata + dữ liệu (record-oriented)109- `<symbol>_<subcommand>_<timestamp>.csv` (cho ohlcv) — CSV phẳng dễ đọc110111Cache lưu tại `state/vn_market_data/<symbol>_<source>_<interval>.csv` — không có timestamp; lần fetch sau cùng phủ lên (full refresh).112113### Bước 4: Báo cáo cho user114115Tóm tắt số dòng, khoảng ngày, mã, và ghi chú nếu data thiếu hoặc có gap.116117## Output Format118119### OHLCV JSON120121```json122{123 "schema_version": "1.0",124 "subcommand": "ohlcv",125 "symbol": "VIC",126 "source": "VCI",127 "interval": "1D",128 "range": {129 "start": "2026-05-01",130 "end": "2026-05-12"131 },132 "row_count": 7,133 "fetched_at": "2026-05-13T07:45:00+07:00",134 "cache_status": "miss",135 "data": [136 {"time": "2026-05-04", "open": 217.0, "high": 220.0, "low": 204.0, "close": 212.0, "volume": 3884000},137 ...138 ]139}140```141142### Info JSON143144```json145{146 "schema_version": "1.0",147 "subcommand": "info",148 "symbol": "VIC",149 "source": "VCI",150 "exchange": "HOSE",151 "industry": "Bất động sản",152 "listed_date": "2007-09-19",153 "outstanding_shares": 3823661561,154 "market_cap_vnd": null155}156```157158## Resources159160- `scripts/vn_data_fetcher.py` — CLI chính với các subcommand161- `scripts/tests/` — pytest suite + fixtures CSV162- Tham chiếu: `skills/vn-market-mechanics/references/vn_data_sources.md` cho quy ước data sources và conventions163164## Lưu ý vận hành1651661. **Rate limit:** Các nguồn upstream (TCBS/VCI/SSI) có thể rate-limit. Cache hợp lý giúp giảm tải. Nếu gặp lỗi rate-limit, retry sau 30-60s.1672. **Độ trễ T+0:** Một số endpoint có data của phiên hôm nay chỉ vào cuối ngày. Không kỳ vọng intraday real-time chính xác.1683. **Đơn vị giá:** vnstock thường trả giá theo **nghìn VND** (ví dụ `217.0` = 217,000 VND/CP) — script ghi nhận thông tin này trong metadata.1694. **Mã hợp lệ:** Cổ phiếu HOSE/HNX/UPCOM dùng 3-5 ký tự in hoa. Chỉ số: `VNINDEX`, `VN30`, `HNX-INDEX`. Phái sinh: `VN30F1M`, `VN30F2M`.1705. **Fixture cho test:** Định dạng CSV phải có cột `time, open, high, low, close, volume`. Có thể tạo từ output cache cũ.171172## Nguyên tắc1731741. **Cache đầu tiên** — tránh fetch lại data đã có. User dùng `--no-cache` khi cần data tươi.1752. **Fixture cho test** — mọi test offline phải dùng `--fixture`, không gọi vnstock thật.1763. **Log rõ ràng** — báo cho user biết hit cache hay miss, có gap dữ liệu không.1774. **Fail fast** — nếu vnstock không cài hoặc symbol sai, báo lỗi rõ ràng và dừng.