| name | xtdata |
| description | XtQuant market data module — real-time quotes, K-lines, tick, Level2, financials for QMT/miniQMT. |
| homepage | http://dict.thinktrader.net/nativeApi/xtdata.html |
XtData (XtQuant 行情模块)
xtdata is the market data module of XtQuant, providing real-time and historical market data via the local miniQMT client.
⚠️ Requires miniQMT running locally. xtdata communicates with miniQMT via TCP to fetch data.
Install
pip install xtquant
Connect
from xtquant import xtdata
xtdata.connect()
Optionally specify data directory:
xtdata.data_dir = r'D:\QMT\userdata_mini'
Download historical data
Must download before accessing local data:
xtdata.download_history_data('000001.SZ', '1d', start_time='20240101', end_time='20240630')
xtdata.download_history_data('000001.SZ', '1m', start_time='20240101', end_time='20240630')
xtdata.download_history_data('000001.SZ', 'tick', start_time='20240601', end_time='20240630')
Get K-line data
data = xtdata.get_market_data_ex(
[],
['000001.SZ'],
period='1d',
start_time='20240101',
end_time='20240630',
count=-1,
dividend_type='front',
fill_data=True
)
df = data['000001.SZ']
Get local data (no download needed if already cached)
data = xtdata.get_local_data(
field_list=[],
stock_list=['000001.SZ'],
period='1d',
start_time='20240101',
end_time='20240630'
)
Real-time subscription
Subscribe single stock
def on_data(datas):
for stock_code, data in datas.items():
print(stock_code, data)
xtdata.subscribe_quote('000001.SZ', period='tick', callback=on_data)
xtdata.run()
Subscribe all stocks (全推)
xtdata.subscribe_whole_quote(['SH', 'SZ'], callback=on_data)
xtdata.run()
Get full snapshot
data = xtdata.get_full_tick(['SH', 'SZ'])
Financial data
xtdata.download_financial_data(['000001.SZ'])
data = xtdata.get_financial_data(['000001.SZ'])
Available tables: Balance (资产负债表), Income (利润表), CashFlow (现金流量表), PershareIndex (主要指标), Capital (股本表), Top10holder, Top10flowholder, Holdernum
Ex-rights data (除权数据)
data = xtdata.get_divid_factors('000001.SZ')
Basic info
info = xtdata.get_instrument_detail('000001.SZ')
itype = xtdata.get_instrument_type('000001.SZ')
days = xtdata.get_trading_dates('SH', start_time='20240101', end_time='20240630')
blocks = xtdata.get_stock_list_in_sector('沪深A股')
Index constituents
xtdata.download_index_weight()
weights = xtdata.get_index_weight('000300.SH')
Convertible bond info
xtdata.download_cb_data()
data = xtdata.get_cb_data()
Available periods
tick, 1m, 5m, 15m, 30m, 1h, 1d, 1w, 1mon
Level2 data fields
If your broker supports Level2:
- l2quote — Level2 实时行情快照
- l2order — Level2 逐笔委托
- l2transaction — Level2 逐笔成交
- l2quoteaux — 总买总卖
- l2orderqueue — 委买委卖队列
Tips
- Always call
xtdata.download_history_data() before get_market_data_ex() for first-time data access.
- Data is cached locally after download — subsequent reads are fast.
xtdata.run() blocks the thread — use in a separate thread if combining with trading.
- Docs: http://dict.thinktrader.net/nativeApi/xtdata.html