Market Data
Fetches stock market data from TDX (通达信) protocol servers.
Real Code Reference
tradinglearn/pytdx2/client/quotationClient.py—QuotationClientwithget_KLine_data(),get_security_quotes(), etc.tradinglearn/pytdx2/client/baseStockClient.py—BaseStockClientsocket layer with auto-retrytradinglearn/pytdx2/const.py—MARKET,CATEGORY,PERIODenums, 43+ server hosts
Capabilities
- Real-time quotes:
get_security_quotes()/get_security_quotes_details()(with 5-level depth) - K-line (OHLCV) data:
get_KLine_data(market, code, period, start, count)— 1min to yearly - Market categories: Shanghai A, Shenzhen A, Growth Enterprise, Beijing A; futures, options, HK stocks
- Market overview:
get_top_stock_board(category)— top gainers/losers/amplitude,get_index_info()
Typical Workflow
from pytdx2.client.quotationClient import QuotationClient
from pytdx2.const import MARKET, PERIOD
client = QuotationClient()
client.connect().login()
# Daily K-line for 平安银行
klines = client.get_KLine_data(MARKET.SZ, "000001", PERIOD.DAY, start=0, count=800)
# Real-time quotes for a batch
quotes = client.get_security_quotes(["000001", "000002"])
client.disconnect()
Notes
- K-line prices divided by 1000; quote prices divided by 100
- Always call
client.disconnect()— sockets leak otherwise data_fetcher.pywraps this with retry logic and DataFrame normalization