Vnstock Logo

Truy xuất và chuẩn hoá dữ liệu thị trường (Market)

Mở rộng

Mục lục

Nguồn của bạn

Chọn nguồn bạn có khoá, mã trên trang sẽ điền theo. Chưa chọn thì mã giữ chỗ trống <nguồn>. Nguồn xếp theo bảng chữ cái, không phải khuyến nghị.

Lớp Market giúp bạn truy xuất và chuẩn hoá dữ liệu giao dịch trên thị trường: nến lịch sử (OHLCV), khớp lệnh từng lệnh, bảng giá, sổ lệnh 3 bậc, dữ liệu mua bán của khối ngoại và tự doanh, cùng dữ liệu chứng chỉ quỹ ETF, trái phiếu, hợp đồng tương lai phái sinh, diễn biến ngành, bộ lọc cổ phiếu, quỹ mở, tiền mã hoá, tỷ giá ngoại hối, hàng hoá và chỉ số quốc tế.

Cú pháp gọi hàm cơ bản của lớp Market

Cấu trúc gọi chuẩn theo thứ tự: Market(nguồn) → nhóm_tài_sản(mã) → tên_hàm(). Bạn cũng có thể truyền nguồn qua tham số source= bên trong hàm như cách viết ở thế hệ 5:

Python
from vnstock import Market

Market("<nguồn>").equity("FPT").ohlcv(start="2026-10-01", end="2026-10-30", interval="1D")
Market().equity("FPT").ohlcv(source="<nguồn>", length=100)     # tương thích cách gọi của thế hệ 5

Mỗi hàm được hỗ trợ bởi một tập nguồn nhất định (ghi rõ tại cột Nguồn ở các bảng bên dưới). Ví dụ khi lấy dữ liệu khớp lệnh trong phiên:

Python
Market("<nguồn>").equity("FPT").trades()

Lấy dữ liệu giao dịch khối ngoại của chỉ số thị trường:

Python
Market("<nguồn>").index("VNINDEX").foreign_flow(start="2026-10-01")

Lấy dữ liệu nến lịch sử của cặp tiền mã hoá:

Python
Market("<nguồn>").crypto("BTCUSDT").ohlcv(interval="1H")

Lấy dữ liệu nến của hợp đồng tương lai chỉ số VN30:

Python
Market("<nguồn>").futures("VN30F1M").ohlcv(length=100)     # chuỗi hợp đồng tương lai liên tục kỳ hạn 1 tháng
Market("<nguồn>").futures("41I1GB000").ohlcv(length=100)   # một mã hợp đồng cụ thể theo chuẩn KRX

Nếu bạn không truyền tên nguồn vào Market() hay source=, thư viện sẽ tự động tra quy tắc nguồn mặc định đã thiết lập qua vnstock.sources (Cách chọn nguồn dữ liệu).

Bảng tra cứu nhanh theo nhu cầu phân tích

Dữ liệu bạn cần lấyCú pháp gọi hàm tiêu biểuNguồn hỗ trợ (source=)
Nến lịch sử cổ phiếu, ETF, chỉ số Việt NamMarket("<nguồn>").equity("FPT").ohlcv(...), .index("VNINDEX").ohlcv(...)dnse, fhsc, ssi, tcbs
Bảng giá trong phiên và sổ lệnh 3 bậc giáMarket("<nguồn>").equity("FPT").quote(), .order_book()fhsc, tcbs (order_book hỗ trợ thêm dnse)
Khớp lệnh chi tiết từng lệnh và khối lượng theo bước giáMarket("<nguồn>").equity("FPT").trades(), .volume_profile()dnse, tcbs
Giao dịch thoả thuận trên sànMarket("tcbs").equity.block_trades(exchange="HOSE")tcbs
Giao dịch khối ngoại, room ngoại và tự doanhMarket("<nguồn>").equity("FPT").foreign_flow(), .foreign_room(), Market("fhsc").equity("FPT").proprietary_flow()dnse, fhsc, ssi (tự doanh cổ phiếu: fhsc; tự doanh chỉ số: ssi)
Hợp đồng tương lai phái sinh và chứng quyền có bảo đảmMarket("<nguồn>").futures("VN30F1M").ohlcv(), Market("<nguồn>").warrant("CFPT2501").quote()dnse, ssi, tcbs (tuỳ hàm)
Chứng chỉ quỹ ETF và lợi suất trái phiếuMarket("<nguồn>").etf("E1VFVN30").ohlcv(), Market("fhsc").bond("VN10Y").yield_history(...)dnse, fhsc, ssi, tcbs (ETF tuỳ hàm); fhsc (trái phiếu)
Diễn biến chỉ số ngành và độ rộng thị trườngMarket("fhsc").sector("banks").ohlcv(), .sector.comparison(), .index("VNINDEX").breadth()fhsc
Sàng lọc cổ phiếu theo tiêu chí thị trườngMarket("fhsc").equity.screener(exchange="HOSE", sort="-change_percent")fhsc
Thống kê cung cầu và mua bán chủ độngMarket("<nguồn>").equity("FPT").active_flow(), Market("tcbs").equity("FPT").supply_demand_daily()fhsc, tcbs
Giá vàng, bạc bán lẻ trong nướcRetail("fhsc").gold(), .silver(), .gold.history("GOLD_BAR_SJC", start=...)fhsc (xem chi tiết tại mục bên dưới)
Lịch sử NAV và hiệu suất tăng trưởng quỹ mởMarket("fhsc").fund("DCDS").history(), .performance()fhsc
Thị trường tiền mã hoá giao ngayMarket("<nguồn>").crypto("BTCUSDT").ohlcv(), .quote(), .order_book(), .trades(), .aggregate_trades(), .best_bid_ask()binance, fhsc, fmp (order_book, trades, aggregate_trades, best_bid_ask: binance)
Tỷ giá ngoại hối, hàng hoá và chỉ số chứng khoán quốc tếMarket("<nguồn>").forex("USDVND").ohlcv(), .commodity("GC").ohlcv(), .index("^GSPC", scope="global").ohlcv()fhsc, fmp

Các tham số dùng chung và quy ước đơn vị, thời gian

Tên tham sốKiểu dữ liệuGiá trị mặc địnhÝ nghĩa và quy ước truyền vào
start, endstr, date, datetimeend là ngày hôm nay; với hàm nến, start mặc định lùi 30 ngày trước endChuỗi ngày dạng YYYY-MM-DD, YYYY-MM, YYYY (tính từ ngày đầu kỳ), hoặc chuỗi ISO có kèm giờ
intervalstr"1D"Khung thời gian nến: 1m 3m 5m 15m 30m 1H 4H 1D 1W 1M (phân biệt chữ hoa/thường: m là phút, M là tháng; vẫn chấp nhận D, day, H, W, month, riêng "DAY" in hoa sẽ báo lỗi). Mỗi nguồn hỗ trợ một số khung nhất định
datestrPhiên gần nhấtNgày giao dịch cần xem ở hàm volume_profile() trên nguồn dnse; truyền vào nguồn chỉ hỗ trợ phiên hiện tại sẽ báo VNSTOCK_INPUT_PARAM
lengthint, strSố lượng nến (100 hoặc "100b" lấy 100 nến gần nhất) hoặc độ dài thời gian tính lùi từ end ("3M", "1Y", "10d", "2W")
count_backintChỉ giữ lại đúng N nến gần nhất ở kết quả cuối
limitintGiới hạn số dòng trả về tối đa (ưu tiên giữ các dòng mới nhất)
boardstr, None"G1"Bảng giao dịch áp dụng cho các hàm trong phiên (quote, order_book, trades, expected_price); "G1" là bảng lô chẵn chính, truyền None để lấy mọi bảng
floatingint2Số chữ số thập phân làm tròn cho các cột giá; truyền None để giữ nguyên số gốc
sourcestrChỉ định nguồn dữ liệu cho riêng lời gọi hàm này
compatstr"v5"Chọn hồ sơ đầu ra: "v5" giữ tên cột, thứ tự và đơn vị như thế hệ 5; "v6" dùng bộ quy ước đồng nhất mới (Chuẩn hoá và bộ cột dữ liệu)
columnsstr"standard"Chọn bộ cột trả về: "standard" (chuẩn), "common" (giao nhau) hoặc "all" (thêm cột riêng của nguồn)

Mỗi hàm chỉ nhận những tham số được liệt kê tại cột Tham số của hàm đó trong bảng danh mục bên dưới, cộng với 4 tham số tuỳ chỉnh chung là source, floating, compat, columns. Nếu truyền tham số không thuộc danh sách này, hàm sẽ báo lỗi VNSTOCK_INPUT_PARAM ngay lập tức.

Quy ước về khối lượng và đơn vị giá:

  • Khối lượng giao dịch: tính theo đơn vị cổ phiếu đồng nhất trên mọi nguồn và mọi bảng giao dịch; riêng hợp đồng tương lai tính theo số hợp đồng. Hàm volume_profile() chỉ tính trên bảng lô chẵn chính G1 (tương tự hàm trades()).
  • Đơn vị giá (ở hồ sơ mặc định v5): giữ nguyên như thế hệ 5 — dữ liệu nến (ohlcv) và khớp lệnh (trades) tính bằng nghìn đồng; trong khi bảng giá (quote), sổ lệnh (order_book), giao dịch thoả thuận (block_trades), khớp lệnh theo bước giá (volume_profile), trade_history() và session_stats() tính bằng đồng. Chỉ số và hợp đồng tương lai chỉ số tính bằng điểm. Tiền mã hoá, ngoại hối và hàng hoá tính theo đồng tiền định giá của cặp tài sản. Khi chuyển sang hồ sơ compat="v6", toàn bộ các cột giá cổ phiếu trên mọi bảng đều được quy về nghìn đồng.

Quy ước về thời gian và gộp nến:

  • Nến ngày mang mốc thời gian 07:00+07:00 (tương ứng 00:00 UTC của ngày giao dịch) giống thế hệ 5. Ở hồ sơ v5, các hàm foreign_flow(), proprietary_flow() và trade_history() sắp xếp phiên mới nhất lên đầu với mốc giờ 00:00+07:00. Khi chuyển sang hồ sơ v6, tất cả các hàm này đều sắp xếp tăng dần theo thời gian và dùng chung mốc 07:00+07:00 với bảng nến để bạn ghép bảng trực tiếp (Chuẩn hoá và bộ cột dữ liệu).
  • Đối với nguồn có nến ngày (1D) nhưng không cung cấp sẵn nến tuần (1W) hoặc nến tháng (1M) như ssi, thư viện sẽ tự động tải dữ liệu nến 1D của trọn kỳ rồi tổng hợp thành nến tuần/tháng, gắn mốc ngày đầu kỳ và ghi nhận df.attrs["resampled_from"] = "1D".
  • Nếu bạn gọi hàm lấy nến ngay trong giờ giao dịch (hoặc khi tuần/tháng hiện tại chưa kết thúc), dòng nến cuối cùng là nến của kỳ đang diễn ra nên giá đóng cửa và khối lượng vẫn còn thay đổi. Thư viện đánh dấu trạng thái này qua cờ df.attrs["last_bar_partial"] = True. Bạn có thể loại bỏ nến chưa đóng bằng cách kiểm tra thuộc tính này:
Python
df = Market("<nguồn>").equity("FPT").ohlcv(start="2026-10-01")
if df.attrs.get("last_bar_partial"):
    df = df.iloc[:-1]

Danh mục hàm chi tiết theo từng lớp tài sản

Bảng sinh từ thư viện bản 6.0.0, hồ sơ v5 (mặc định). "Tên khác" là tên của thế hệ 5 cho cùng dữ liệu. Danh sách đầy đủ trên máy bạn: vnstock tools, hoặc show_api() trong Python.

Cổ phiếu (equity)

HàmTên khácTham sốNguồn (source=)
active_flow()intervalfhsc
active_session()fhsc
block_trades()put_throughexchange, datetcbs
close_price()dnse
expected_price()start, end, boarddnse
foreign_flow()start, end, interval, length, limitdnse, fhsc, ssi
foreign_room()groupdnse, fhsc, ssi
foreign_room_board()exchangetcbs
foreign_room_history()start, endfhsc
foreign_session()fhsc
insider_deal()start, endfhsc
last_trade()dnse
ohlcv()historystart, end, interval, length, count_back, limitdnse, fhsc, ssi, tcbs
order_book()price_depthboarddnse, fhsc, tcbs
order_book_history()start, enddnse
order_book_intraday()fhsc
proprietary_flow()start, end, length, limitfhsc
proprietary_session()fhsc
quote()price_boardboardfhsc, tcbs
screener()exchange, index, sector, window, date, sortfhsc
session_stats()trading_statsstart, end, length, limitssi
session_volume()fhsc
summary()fhsc
supply_demand_daily()investortcbs
supply_demand_intraday()investortcbs
supply_demand_session()investortcbs
trade_history()start, end, length, limitssi
trades()intradaystart, end, limit, boarddnse, tcbs
volume_history()start, endfhsc
volume_profile()matched_by_pricedatednse, tcbs
Cột trả về (hồ sơ v5, mặc định)
HàmCột
active_flow()time, active_buy_volume, active_sell_volume, active_buy_percent, active_sell_percent, active_buy_sell_ratio
active_session()symbol, time, active_buy_volume, active_sell_volume, active_buy_percent, active_sell_percent, buy_order_count, sell_order_count
block_trades()symbol, match_price, match_volume, match_value, accumulated_value, time
close_price()symbol, time, close
expected_price()time, expected_price, expected_volume
foreign_flow()time, buy_vol, buy_val, sell_vol, sell_val, net_vol, net_val
foreign_room()symbol, time, foreign_room, foreign_room_total, foreign_buy_volume, foreign_sell_volume, foreign_owned_volume, foreign_room_limit_percent
foreign_room_board()symbol, foreign_room, foreign_buy_volume, foreign_sell_volume, close, total_volume, total_value
foreign_room_history()time, foreign_room, foreign_owned_volume, max_foreign_volume, max_foreign_percent
foreign_session()symbol, time, foreign_buy_volume, foreign_buy_value, foreign_sell_volume, foreign_sell_value, foreign_net_volume, foreign_net_value
insider_deal()id, symbol, person_name, position, transaction_type, registered_volume, traded_volume, start_date, end_date, status, shares_before, shares_after, ownership_percent_before, ownership_percent_after, relation_type, related_person_name
last_trade()time, price, volume, side, accumulated_volume
ohlcv()time, open, high, low, close, volume
order_book()bid_price_1, bid_vol_1, bid_price_2, bid_vol_2, bid_price_3, bid_vol_3, ask_price_1, ask_vol_1, ask_price_2, ask_vol_2, ask_price_3, ask_vol_3, time, total_bid_volume, total_ask_volume
order_book_history()time, bid_price_1, bid_volume_1, bid_price_2, bid_volume_2, bid_price_3, bid_volume_3, ask_price_1, ask_volume_1, ask_price_2, ask_volume_2, ask_price_3, ask_volume_3
order_book_intraday()time, bid_price_1, bid_volume_1, bid_price_2, bid_volume_2, bid_price_3, bid_volume_3, ask_price_1, ask_volume_1, ask_price_2, ask_volume_2, ask_price_3, ask_volume_3
proprietary_flow()time, buy_vol, buy_val, sell_vol, sell_val, net_vol, net_val
proprietary_session()symbol, time, proprietary_buy_volume, proprietary_buy_value, proprietary_sell_volume, proprietary_sell_value, proprietary_net_volume, proprietary_net_value
quote()symbol, exchange, reference_price, ceiling_price, floor_price, open_price, high_price, low_price, close_price, bid_price_1, bid_vol_1, bid_price_2, bid_vol_2, bid_price_3, bid_vol_3, ask_price_1, ask_vol_1, ask_price_2, ask_vol_2, ask_price_3, ask_vol_3, foreign_buy_volume, foreign_sell_volume, time, price_change, price_change_percent, average_price, total_volume, total_value, foreign_room, next_reference_price, next_ceiling_price, next_floor_price, last_volume
screener()symbol, name, exchange, sector_slug, close, price_change_percent, market_cap, outstanding_shares, pe, pb, eps, total_volume, total_value, foreign_net_value, foreign_net_volume, foreign_room, proprietary_net_value, proprietary_net_volume
session_stats()symbol, time, average_match_volume, average_match_value, average_deal_volume, average_deal_value, average_volume, average_value, total_match_volume, total_match_value, total_deal_volume, total_deal_value, total_volume, total_value, fr_buy_volume_total, fr_buy_value_total, fr_sell_volume_total, fr_sell_value_total, fr_net_volume_total, fr_net_value_total, fr_buy_volume_total_avg, fr_buy_value_total_avg, fr_sell_volume_total_avg, fr_sell_value_total_avg, fr_net_volume_total_avg, fr_net_value_total_avg, total_buy_trade_volume, total_sell_trade_volume, total_buy_trade_volume_avg, total_sell_trade_volume_avg, total_buy_trade, total_sell_trade, total_buy_trade_avg, total_sell_trade_avg, total_net_trade_volume, total_net_trade_volume_avg, average_buy_volume_per_trade, average_sell_volume_per_trade, close_price, reference_price
session_volume()symbol, time, matched_volume, matched_value, block_volume, block_value, total_volume, total_value
summary()eps, pe, pb, roe, dividend_yield, symbol, roa, ev_ebitda, gross_margin, nim, industry_pe, industry_pb, industry_ev_ebitda
supply_demand_daily()time, active_buy_percent, active_sell_percent, buy_sell_ratio
supply_demand_intraday()time, active_buy_percent, active_sell_percent, buy_sell_ratio, active_buy_volume, active_sell_volume, accumulated_buy_volume, accumulated_sell_volume
supply_demand_session()time, active_buy_percent, active_sell_percent, buy_sell_ratio
trade_history()trading_date, reference_price, open, close, price_change, percent_price_change, high, low, average_price, matched_volume, matched_value, deal_volume, deal_value, total_volume, total_value, total_buy_trade, total_buy_trade_volume, total_sell_trade, total_sell_trade_volume, symbol, foreign_buy_volume, foreign_buy_value, foreign_sell_volume, foreign_sell_value, foreign_net_volume, foreign_net_value
trades()time, price, volume, match_type, id, accumulated_volume
volume_history()time, matched_volume, matched_value, block_volume, block_value, total_volume, total_value
volume_profile()price, buy_volume, sell_volume, unknown_volume, total_volume

Chứng chỉ quỹ ETF (etf)

HàmTên khácTham sốNguồn (source=)
block_trades()put_throughexchange, datetcbs
foreign_flow()start, end, interval, length, limitdnse, fhsc, ssi
ohlcv()historystart, end, interval, length, count_back, limitdnse, fhsc, ssi, tcbs
order_book()price_depthboarddnse, fhsc, tcbs
proprietary_flow()start, end, length, limitfhsc
quote()price_boardboardfhsc, tcbs
session_stats()trading_statsstart, end, length, limitssi
trade_history()start, end, length, limitssi
trades()intradaystart, end, limit, boarddnse, tcbs
volume_profile()matched_by_pricedatednse, tcbs
Cột trả về (hồ sơ v5, mặc định)
HàmCột
block_trades()symbol, match_price, match_volume, match_value, accumulated_value, time
foreign_flow()time, buy_vol, buy_val, sell_vol, sell_val, net_vol, net_val
ohlcv()time, open, high, low, close, volume
order_book()bid_price_1, bid_vol_1, bid_price_2, bid_vol_2, bid_price_3, bid_vol_3, ask_price_1, ask_vol_1, ask_price_2, ask_vol_2, ask_price_3, ask_vol_3, time, total_bid_volume, total_ask_volume
proprietary_flow()time, buy_vol, buy_val, sell_vol, sell_val, net_vol, net_val
quote()symbol, exchange, reference_price, ceiling_price, floor_price, open_price, high_price, low_price, close_price, bid_price_1, bid_vol_1, bid_price_2, bid_vol_2, bid_price_3, bid_vol_3, ask_price_1, ask_vol_1, ask_price_2, ask_vol_2, ask_price_3, ask_vol_3, foreign_buy_volume, foreign_sell_volume, time, price_change, price_change_percent, average_price, total_volume, total_value, foreign_room, next_reference_price, next_ceiling_price, next_floor_price, last_volume
session_stats()symbol, time, average_match_volume, average_match_value, average_deal_volume, average_deal_value, average_volume, average_value, total_match_volume, total_match_value, total_deal_volume, total_deal_value, total_volume, total_value, fr_buy_volume_total, fr_buy_value_total, fr_sell_volume_total, fr_sell_value_total, fr_net_volume_total, fr_net_value_total, fr_buy_volume_total_avg, fr_buy_value_total_avg, fr_sell_volume_total_avg, fr_sell_value_total_avg, fr_net_volume_total_avg, fr_net_value_total_avg, total_buy_trade_volume, total_sell_trade_volume, total_buy_trade_volume_avg, total_sell_trade_volume_avg, total_buy_trade, total_sell_trade, total_buy_trade_avg, total_sell_trade_avg, total_net_trade_volume, total_net_trade_volume_avg, average_buy_volume_per_trade, average_sell_volume_per_trade, close_price, reference_price
trade_history()trading_date, reference_price, open, close, price_change, percent_price_change, high, low, average_price, matched_volume, matched_value, deal_volume, deal_value, total_volume, total_value, total_buy_trade, total_buy_trade_volume, total_sell_trade, total_sell_trade_volume, symbol, foreign_buy_volume, foreign_buy_value, foreign_sell_volume, foreign_sell_value, foreign_net_volume, foreign_net_value
trades()time, price, volume, match_type, id, accumulated_volume
volume_profile()price, buy_volume, sell_volume, unknown_volume, total_volume

Trái phiếu (bond)

HàmTên khácTham sốNguồn (source=)
ohlcv()historystart, end, interval, length, count_back, limitfhsc
order_book()price_depthboardfhsc
quote()price_boardboardfhsc
yield_history()start, endfhsc
Cột trả về (hồ sơ v5, mặc định)
HàmCột
ohlcv()time, open, high, low, close, volume
order_book()bid_price_1, bid_price_2, bid_price_3, ask_price_1, ask_price_2, ask_price_3, time, bid_volume_1, bid_volume_2, bid_volume_3, ask_volume_1, ask_volume_2, ask_volume_3
quote()symbol, exchange, reference_price, time, open, high, low, close, price_change, price_change_percent, total_volume, yield_percent
yield_history()time, yield_percent

Chỉ số (index)

HàmTên khácTham sốNguồn (source=)
breadth()fhsc
comparison()exchange, window, sortfhsc
foreign_flow()start, end, length, limitfhsc, ssi
foreign_session()fhsc
ohlcv()historystart, end, interval, length, count_back, limitdnse, fhsc, ssi, tcbs
proprietary_flow()start, end, length, limitssi
quote()price_board, summarystart, enddnse, fhsc, ssi
trade_history()start, end, length, limitssi
Cột trả về (hồ sơ v5, mặc định)
HàmCột
breadth()symbol, time, advance_count, decline_count, unchanged_count, limit_up_count, limit_down_count, constituent_count
comparison()symbol, name, exchange, close, price_change_percent, market_cap, constituent_count, advance_count, decline_count, unchanged_count, total_volume, total_value, foreign_net_value
foreign_flow()time, foreign_buy_volume, foreign_buy_value, foreign_sell_volume, foreign_sell_value, foreign_net_volume, foreign_net_value
foreign_session()symbol, time, foreign_buy_volume, foreign_buy_value, foreign_sell_volume, foreign_sell_value, foreign_net_volume, foreign_net_value
ohlcv()time, open, high, low, close, volume
proprietary_flow()time, proprietary_buy_volume, proprietary_buy_value, proprietary_sell_volume, proprietary_sell_value, proprietary_net_volume, proprietary_net_value
quote()symbol, reference_price, high_price, low_price, close_price, total_volume, time, price_change, percent_change, advances, declines, no_change, accumulated_volume, accumulated_value, put_through_volume, put_through_value, ceiling_count, floor_count, advance_volume, decline_volume, unchanged_volume
trade_history()trading_date, price_change, percent_price_change, reference_price, close, matched_volume, matched_value, deal_volume, deal_value, total_volume, total_value, total_stock_up_price, total_stock_down_price, total_stock_no_change_price, total_stock_ceiling, total_stock_floor

Chỉ số quốc tế (index(..., scope="global"))

HàmTên khácTham sốNguồn (source=)
history()ohlcvstart, end, interval, limitfhsc, fmp
Cột trả về (hồ sơ v5, mặc định)
HàmCột
history()time, open, high, low, close, volume

Ngành, diễn biến thị trường (sector)

HàmTên khácTham sốNguồn (source=)
breadth()fhsc
comparison()level, parent, window, sortfhsc
foreign_flow()start, endfhsc
foreign_session()fhsc
ohlcv()start, endfhsc
performance()fhsc
Cột trả về (hồ sơ v5, mặc định)
HàmCột
breadth()sector_slug, time, advance_count, decline_count, unchanged_count, limit_up_count, limit_down_count, constituent_count
comparison()sector_slug, name, sector_level, close, price_change_percent, market_cap, constituent_count, advance_count, decline_count, unchanged_count, total_volume, total_value, foreign_net_value
foreign_flow()time, foreign_buy_volume, foreign_buy_value, foreign_sell_volume, foreign_sell_value, foreign_net_volume, foreign_net_value
foreign_session()sector_slug, time, foreign_buy_volume, foreign_buy_value, foreign_sell_volume, foreign_sell_value, foreign_net_volume, foreign_net_value
ohlcv()time, open, high, low, close, volume, total_value
performance()sector_slug, name, time, reference_price, open, high, low, close, price_change, price_change_percent, total_volume, total_value

Hợp đồng tương lai (futures)

HàmTên khácTham sốNguồn (source=)
ohlcv()historystart, end, interval, length, count_back, limitdnse, ssi
order_book()price_depthdnse, tcbs
quote()price_board, summarydnse, tcbs
trades()intradaystart, end, limit, boarddnse
Cột trả về (hồ sơ v5, mặc định)
HàmCột
ohlcv()time, open, high, low, close, volume
order_book()bid_price_1, bid_vol_1, bid_price_2, bid_vol_2, bid_price_3, bid_vol_3, ask_price_1, ask_vol_1, ask_price_2, ask_vol_2, ask_price_3, ask_vol_3, time, total_bid_volume, total_ask_volume
quote()symbol, exchange, reference_price, ceiling_price, floor_price, open_price, high_price, low_price, close_price, total_volume, bid_price_1, bid_vol_1, bid_price_2, bid_vol_2, bid_price_3, bid_vol_3, ask_price_1, ask_vol_1, ask_price_2, ask_vol_2, ask_price_3, ask_vol_3, basis, open_interest, foreign_buy_volume, foreign_sell_volume, last_trading_date, time, price_change, price_change_percent, average_price
trades()time, price, volume, match_type, id, accumulated_volume

Chứng quyền (warrant)

HàmTên khácTham sốNguồn (source=)
ohlcv()historystart, end, interval, length, count_back, limitssi, tcbs
order_book()price_depthdnse
quote()price_board, summarytcbs
trades()intradaystart, end, limitdnse
Cột trả về (hồ sơ v5, mặc định)
HàmCột
ohlcv()time, open, high, low, close, volume
order_book()bid_price_1, bid_vol_1, bid_price_2, bid_vol_2, bid_price_3, bid_vol_3, ask_price_1, ask_vol_1, ask_price_2, ask_vol_2, ask_price_3, ask_vol_3, time
quote()symbol, exchange, reference_price, ceiling_price, floor_price, open_price, high_price, low_price, close_price, bid_price_1, bid_vol_1, bid_price_2, bid_vol_2, bid_price_3, bid_vol_3, ask_price_1, ask_vol_1, ask_price_2, ask_vol_2, ask_price_3, ask_vol_3, foreign_buy_volume, foreign_sell_volume, break_even_point, underlying_price, time, price_change, price_change_percent, total_volume, exercise_price, conversion_ratio, maturity_date, issuer, underlying_symbol, next_reference_price, next_ceiling_price, next_floor_price, last_volume
trades()time, price, volume, match_type, id, accumulated_volume

Tiền mã hoá (crypto)

HàmTên khácTham sốNguồn (source=)
aggregate_trades()start, end, limitbinance
best_bid_ask()binance
chart_ohlcv()start, end, interval, limitbinance
daily_stats()summarybinance
last_price()binance, fhsc
ohlcv()historystart, end, interval, limitbinance, fhsc, fmp
order_book()price_depthlimitbinance
quote()price_boardbinance, fhsc
reference_price()fhsc
rolling_stats()binance
trades()intraday, trade_historylimitbinance
vwap()binance
Cột trả về (hồ sơ v5, mặc định)
HàmCột
aggregate_trades()time, price, base_volume, side, trade_id, first_trade_id, last_trade_id
best_bid_ask()symbol, time, bid_price_1, bid_amount_1, ask_price_1, ask_amount_1
chart_ohlcv()time, open, high, low, close, volume, quote_volume, trade_count
daily_stats()symbol, price_change, price_change_percent, weighted_avg_price, open_price, high_price, low_price, last_price, volume, quote_volume, open_time, close_time, first_id, last_id, count
last_price()symbol, price
ohlcv()time, open, high, low, close, volume, quote_volume, trade_count
order_book()bid_price_1, bid_vol_1, bid_price_2, bid_vol_2, bid_price_3, bid_vol_3, bid_price_4, bid_vol_4, bid_price_5, bid_vol_5, bid_price_6, bid_vol_6, bid_price_7, bid_vol_7, bid_price_8, bid_vol_8, bid_price_9, bid_vol_9, bid_price_10, bid_vol_10, ask_price_1, ask_vol_1, ask_price_2, ask_vol_2, ask_price_3, ask_vol_3, ask_price_4, ask_vol_4, ask_price_5, ask_vol_5, ask_price_6, ask_vol_6, ask_price_7, ask_vol_7, ask_price_8, ask_vol_8, ask_price_9, ask_vol_9, ask_price_10, ask_vol_10, time
quote()symbol, reference_price, open_price, high_price, low_price, close_price, bid_price_1, bid_vol_1, ask_price_1, ask_vol_1, time, price_change, price_change_percent, base_volume, quote_volume, average_price, trade_count
reference_price()symbol, reference_price, timestamp
rolling_stats()symbol, price_change, price_change_percent, weighted_avg_price, open_price, high_price, low_price, last_price, volume, quote_volume, open_time, close_time, first_id, last_id, count
trades()time, price, volume, side, id, quote_volume
vwap()mins, price, close_time

Tỷ giá (forex)

HàmTên khácTham sốNguồn (source=)
ohlcv()historystart, end, interval, limitfhsc, fmp
quote()fhsc
Cột trả về (hồ sơ v5, mặc định)
HàmCột
ohlcv()time, open, high, low, close, volume
quote()symbol, time, bid_price, ask_price

Hàng hoá (commodity)

HàmTên khácTham sốNguồn (source=)
ohlcv()historystart, end, interval, limitfhsc, fmp
quote()fhsc
Cột trả về (hồ sơ v5, mặc định)
HàmCột
ohlcv()time, open, high, low, close, volume
quote()symbol, time, reference_price, open, high, low, close, price_change, price_change_percent, total_volume

Quỹ mở (fund)

HàmTên khácTham sốNguồn (source=)
history()nav, ohlcvstart, endfhsc
performance()fhsc
Cột trả về (hồ sơ v5, mặc định)
HàmCột
history()time, nav
performance()period, period_label, nav_start, nav_end, return_percent, annualized

Cấu trúc bảng dữ liệu trả về của các hàm phổ biến

  • Nến lịch sử (equity("FPT").ohlcv()): gồm 6 cột chuẩn — time (kiểu datetime64[us, Asia/Ho_Chi_Minh], nến ngày mang mốc 07:00+07:00), open, high, low, close (kiểu float64, tính bằng nghìn đồng ở cả hai hồ sơ v5 và v6, đã điều chỉnh theo sự kiện quyền) và volume (kiểu int64 ở v5, Int64 ở v6, đơn vị cổ phiếu).
  • Bảng giá (equity("FPT").quote()): gồm 34 cột chuẩn; các cột giá (reference_price, ceiling_price, floor_price, open_price, close_price, bid_price_1..3, ask_price_1..3…) tính bằng đồng ở hồ sơ v5 và được quy đổi đồng nhất về nghìn đồng ở hồ sơ v6 (open, close, bid_volume_1…).
  • Khớp lệnh từng lệnh (equity("FPT").trades()): gồm time, price (nghìn đồng), volume, match_type (Buy, Sell, ATO, ATC, Unknown ở hồ sơ v5; tách thành hai cột side và session ở hồ sơ v6), id (kiểu string — số thứ tự dòng trong lần truy vấn hiện tại, không phải mã lệnh cố định của sở giao dịch để ghép nối giữa hai lần gọi) và accumulated_volume.
  • Giao dịch khối ngoại (equity("FPT").foreign_flow()): gồm time, buy_vol, buy_val, sell_vol, sell_val, net_vol, net_val — ở hồ sơ v5 xếp phiên mới nhất lên đầu với mốc 00:00+07:00; sang hồ sơ v6 xếp tăng dần theo thời gian với mốc 07:00+07:00 để ghép thẳng với bảng nến.

Cách truyền mã hợp đồng tương lai phái sinh

Khi làm việc với nhóm futures(...), bạn có thể truyền mã hợp đồng theo bất kỳ quy ước nào dưới đây. Thư viện sẽ tự động chuyển đổi sang định dạng mà nguồn đang chọn yêu cầu, giúp một mã mang cùng một ý nghĩa xuyên suốt mọi nguồn:

Định dạng mãVí dụ minh hoạÝ nghĩa
Mã chuẩn KRX41I1GB000Chỉ định đúng hợp đồng cụ thể đó, tính từ ngày niêm yết đến ngày đáo hạn
Mã theo năm thángVN30F2611Chỉ định đúng hợp đồng đáo hạn tháng 11/2026, tương đương với mã KRX ở trên
Mã kỳ hạn liên tụcVN30F1M, VN30F2M, VN30F1Q, VN30F2QChuỗi nối tiếp các hợp đồng theo vị trí kỳ hạn (tháng hiện tại, tháng kế tiếp, quý hiện tại, quý kế tiếp)
Python
Market("<nguồn>").futures("41I1GB000").ohlcv(start="2026-10-01", end="2026-11-07")
Market("<nguồn>").futures("VN30F2611").ohlcv(start="2026-10-01", end="2026-11-07")   # trả về cùng một kết quả
  • Lưu ý khi dùng mã liên tục (VN30F1M...): chuỗi giá sẽ có bước nhảy khi chuyển giao sang hợp đồng mới vào ngày đáo hạn. Thuộc tính df.attrs["contract_since"] cho biết ngày bắt đầu của hợp đồng hiện hành trong chuỗi. Nếu mô hình định lượng của bạn nhạy cảm với bước nhảy ngày đáo hạn, hãy truyền mã chuẩn KRX hoặc mã theo năm tháng của từng hợp đồng.
  • Với những nguồn không lưu lịch sử nến theo từng mã hợp đồng riêng lẻ, thư viện sẽ tự động ghép từ các chuỗi kỳ hạn liên tục (mỗi vị trí kỳ hạn là một yêu cầu mạng, tính theo quy tắc đáo hạn vào thứ Năm tuần thứ ba của tháng). Nếu ngày đáo hạn thực tế bị dời sớm do trùng ngày nghỉ lễ, phiên chuyển giao đó có thể chênh lệch nhẹ.
  • Nếu truyền nhầm mã cổ phiếu cơ sở vào nhóm phái sinh (chẳng hạn futures("FPT")), thư viện sẽ báo lỗi VNSTOCK_INPUT_SYMBOL kèm hướng dẫn các định dạng mã hợp lệ.

Lưu ý khi lấy dữ liệu nến trong phiên và sau giờ đóng cửa

  • Các thanh nến trong ngày có khối lượng bằng 0 (nến đệm không có giao dịch) đều được lọc bỏ tự động ở mọi nguồn; số lượng dòng đã lược bỏ được ghi tại df.attrs["rows_dropped_padding"].
  • Phiên khớp lệnh định kỳ đóng cửa (ATC) được gom vào thanh nến mốc 14:45. Một số nguồn cập nhật thanh nến này muộn hơn thời điểm 14:45 vài phút; nếu bạn cần dữ liệu phiên đã chốt đầy đủ, hãy truy xuất sau 15:30.
  • Sàn HNX có thêm phiên giao dịch sau giờ từ 14:45 đến 15:00 (PLO). Tuỳ thuộc vào quy ước của từng nguồn dữ liệu mà khối lượng khớp trong khung giờ này có thể được gộp hoặc không gộp vào nến ngày.

Tra cứu giá vàng và bạc bán lẻ trong nước (Lớp Retail)

Lớp Retail giúp bạn lấy bảng giá mua vào – bán ra của các sản phẩm vàng và bạc bán lẻ từ các thương hiệu trong nước (qua nguồn fhsc). Bạn không cần nhớ mã từng loại vàng: mỗi lời gọi tự động trả về đầy đủ danh mục sản phẩm.

Python
from vnstock import Retail

Retail("fhsc").gold()                                              # bảng giá hiện tại của 10 sản phẩm vàng
Retail("fhsc").gold(date="2026-11-05")                             # bảng giá tại một ngày trong quá khứ
Retail("fhsc").gold.history("GOLD_BAR_SJC", start="2026-10-01")    # chuỗi lịch sử của một sản phẩm cụ thể
Retail("fhsc").gold.history(start="2026-11-01")                    # chuỗi lịch sử của toàn bộ sản phẩm
Retail("fhsc").silver()                                            # bảng giá bạc bán lẻ hiện tại

Bảng kết quả gồm các cột name, buy_price, sell_price, time, product_code, seller, seller_name và quantity_unit. Trong đó, buy_price là giá cửa hàng mua vào và sell_price là giá cửa hàng bán ra, tính theo đơn vị nghìn đồng một chỉ (ví dụ: giá trị 13700 tương ứng với 13,7 triệu đồng/chỉ). Lưu ý khi gọi gold.history() cho toàn bộ sản phẩm, mỗi sản phẩm sẽ phát sinh một yêu cầu tới nguồn.

Đặc thù dữ liệu tiền mã hoá: khớp lệnh gộp và nến biểu đồ

  • Khớp lệnh gộp (aggregate_trades(start=None, end=None, limit=None)): gộp các lệnh khớp diễn ra cùng thời điểm, cùng mức giá của cùng một lệnh đặt chủ động thành một dòng duy nhất; hai cột first_trade_id và last_trade_id cho biết khoảng mã lệnh gốc đã được gộp. Khi truyền tham số start, thư viện tự động lật trang đến hết ngày theo giờ UTC (tối đa 250 trang); với những cặp có mật độ giao dịch rất lớn vượt ngưỡng 250 trang, kết quả sẽ kèm cảnh báo VNSTOCK_DATA_TRUNCATED.
  • Giá chào mua/bán tốt nhất (best_bid_ask()): trả về mức giá đặt mua cao nhất và giá chào bán thấp nhất kèm khối lượng tại thời điểm gọi.
  • Nến vẽ biểu đồ (chart_ohlcv(...)): trả về chuỗi nến phục vụ hiển thị biểu đồ của nguồn (không phải giá điều chỉnh). Qua đối chiếu thực nghiệm trên 142.932 thanh nến (tính đến tháng 11/2026), chỉ có 1 thanh nến khác biệt so với hàm ohlcv() chuẩn — sự khác biệt này chỉ xuất hiện ở những mã từng tạm ngừng giao dịch rồi mở cửa trở lại (nguồn lược bỏ đoạn lịch sử trước lần gián đoạn và mức giá biến động mạnh lúc mở lại). Do đó, khi nghiên cứu và kiểm thử định lượng, bạn nên dùng hàm ohlcv() để giữ trọn vẹn lịch sử giao dịch.

Cách tải toàn bộ dữ liệu khớp lệnh trong phiên (trades)

Hàm trades() tự động lật qua từng trang dữ liệu để lấy đủ các lệnh khớp trong khoảng thời gian bạn yêu cầu. Với nguồn dnse, nếu không truyền ngày, thư viện sẽ lấy phiên giao dịch gần nhất (nếu gọi vào cuối tuần sẽ lấy dữ liệu phiên thứ Sáu).

Do số bản ghi tối đa trên mỗi trang do từng nguồn quy định (dnse tối đa 1.000 lệnh/trang, tcbs tối đa 100 lệnh/trang), những mã có thanh khoản lớn sẽ cần nhiều yêu cầu tuần tự. Trên nguồn dnse, thư viện đặt ngưỡng dừng an toàn ở 500 trang (tương đương 500.000 lệnh) và ghi cảnh báo vào log. Khi gặp cảnh báo chạm trần phân trang, bạn chỉ cần chia nhỏ khoảng thời gian (start, end) thành các khung giờ ngắn hơn để lấy tiếp.

Các hàm chưa hỗ trợ và bí danh tương thích từ thế hệ 5

Những hàm từng có ở thế hệ 5 nhưng hiện chưa có nguồn dữ liệu tương ứng trên cổng Open API của thế hệ 6 vẫn giữ nguyên tên để không gây lỗi cú pháp, nhưng khi gọi sẽ phát cảnh báo CompatibilityWarning và trả về lỗi UnsupportedError có mã VNSTOCK_ROUTE_UNSUPPORTED (thay vì trả về bảng rỗng dễ gây hiểu lầm). Bạn có thể dùng dir(Market("<nguồn>").equity("FPT")) hoặc chạy lệnh vnstock tools Market --all trong Terminal để xem toàn bộ danh sách hàm, bao gồm cả những hàm chưa có nguồn.

Các cách gọi tắt trực tiếp trên Market() từ thế hệ 5 vẫn hoạt động bình thường và trả về đúng bảng của nhóm cổ phiếu:

  • Market("<nguồn>").quote("FPT,VNM") tương đương với Market("<nguồn>").equity.quote("FPT,VNM");
  • Market("<nguồn>").put_through() tương đương với Market("<nguồn>").equity.block_trades();
  • Market("<nguồn>").future(...) tương đương với Market("<nguồn>").futures(...).

Quy ước mã chỉ số quốc tế, tỷ giá ngoại hối và hàng hoá

Python
Market("<nguồn>").index("SPX", scope="global").ohlcv(start="2026-10-01")
Market("<nguồn>").forex("USDVND").ohlcv(start="2026-10-01")
Market("<nguồn>").commodity("GC").ohlcv(start="2026-10-01")

Bảng mã chuẩn cho chỉ số chứng khoán quốc tế: Bạn có thể truyền mã chuẩn ở cột đầu tiên cho bất kỳ nguồn nào. Các cách viết quen thuộc khác (như ^GSPC, SP500, USA500, USA30, JPN225…) vẫn được thư viện tự động nhận diện và ánh xạ về mã chuẩn chung (ghi nhận tại df.attrs["symbol"]). Nếu nguồn đang chọn không có dữ liệu của chỉ số đó, hàm sẽ báo lỗi VNSTOCK_INPUT_SYMBOL kèm danh sách các mã khả dụng trên nguồn.

Mã chuẩn chungTên chỉ số quốc tếMã gốc trên fmpMã gốc trên fhsc
SPXS&P 500 (Mỹ)^GSPCSP500
DJIDow Jones Industrial Average (Mỹ)^DJIDOWJONES
IXICNasdaq Composite (Mỹ)^IXICNASDAQ
NDXNasdaq 100 (Mỹ)^NDX—
RUTRussell 2000 (Mỹ)^RUTRUSSELL2000
VIXCBOE Volatility Index^VIXVIX
DXYU.S. Dollar IndexDX-Y.NYBDXY
N225Nikkei 225 (Nhật Bản)^N225NIKKEI225
HSIHang Seng (Hồng Kông)^HSIHANGSENG
SSECShanghai Composite (Trung Quốc)000001.SSSHANGHAI
KS11KOSPI (Hàn Quốc)^KS11KOSPI
SETSET Index (Thái Lan)^SET.BKSET
JKSEJakarta Composite (Indonesia)^JKSEJCI
PSEIPSEi (Philippines)—PSEI
FTSEFTSE 100 (Anh)^FTSE—
GDAXIDAX (Đức)^GDAXI—
FCHICAC 40 (Pháp)^FCHI—
STOXX50EEuro Stoxx 50 (Châu Âu)^STOXX50E—
AXJOS&P/ASX 200 (Úc)^AXJO—
FTSEEMFTSE Emerging Markets—FTSEEM

Các mã nằm ngoài bảng quy đổi trên sẽ được gửi nguyên văn tới nguồn dữ liệu.

Tất cả các nhóm tài sản quốc tế đều trả về cột thời gian time theo múi giờ chuẩn UTC. Khi truyền mã tài sản, bạn lưu ý:

  • Chỉ số quốc tế (index): dùng chung phương thức .index(...) nhưng thêm tham số scope="global" (mặc định scope="vn" dành cho chỉ số trong nước). Lưu ý trên nguồn fhsc, dữ liệu lịch sử chỉ số quốc tế chỉ đáng tin cậy ở cột giá đóng cửa (close), và bạn không nên trộn lẫn số liệu giữa hai nguồn trong cùng một chuỗi thời gian.
  • Tỷ giá ngoại hối (forex) và tiền mã hoá (crypto): viết liền cặp tiền tệ định giá như USDVND, EURUSD, BTCUSDT.
  • Hàng hoá quốc tế (commodity): truyền mã hợp đồng hàng hoá như GC hoặc GCUSD tuỳ theo nguồn (bạn có thể dùng Reference("fmp").search.symbol(...) để tìm mã trên fmp hoặc Reference("fhsc").commodity.list() trên fhsc). Riêng bảng tỷ giá niêm yết tại các ngân hàng thương mại của lớp Retail thế hệ 5 hiện chưa có trên các cổng Open API (Retail().exchange_rate() sẽ trả về mã lỗi VNSTOCK_ROUTE_UNSUPPORTED).