TqSdk routes a Python strategy through two gateways over Diff
TianQin quant SDK for futures trading: real-time market data, historical data and live trading.
At a glance
- What is it?
- TqSdk is a Python library from 信易科技 that puts historical data, realtime quotes, backtests, simulated trading and live orders behind one TqApi surface, with quotes and trading data held in memory rather than a database you maintain. The parts worth reading closely are the gateway pair, the dependency set, and the credentials that separate a backtest from a live order.
- Who is it for?
- Use TqSdk when you want one Python codebase for research, backtest, simulation and live futures trading, and when your broker is reachable through one of the supported counters. Do not expect it to store your strategy state, to pin its own dependencies, or to work without the quote gateway.
- Can I use it commercially?
- Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 27 days ago.
- What is it written in?
- Mainly Python, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 21, 2026, and from our analysis. They are not legal advice.
Star history
- shinnytech/tqsdk-python5,148 ★ · +102
| Repository | 2026-09-23 | 2026-11-21 |
|---|---|---|
| shinnytech/tqsdk-python | 5k | 5.1k |
102 Stars in the last 59 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
Two gateways speak Diff, and your strategy never sees either
The architecture is four lines long, and they decide what your code depends on. A quote gateway provides realtime quotes and historical data. A trade relay gateway connects to a futures company's trading system. Both gateways serve everything below them over the Diff protocol, and TqSdk connects to both following that protocol. The consequence is that a data pull, a backtest and a live order all travel the same wire format, so one TqApi surface serves all three modes. The other consequence is that the library is not self contained at runtime: it needs the quote gateway reachable, and live trading additionally needs the relay gateway plus an account on the far side of it. Supported paths include CTP direct connections, asset management counters such as 众期, 融航 and 杰宜斯, and high speed counters such as 易达 and ctpmini, with live trading stated to cover 90% of the futures companies in the market. If your broker sits outside those paths, nothing in the package routes around it.
Nothing is written to a database you own
One stated feature is that users need not build or maintain a database: quotes and trading data all live in an in-memory database with no access latency. That is a design decision rather than a slogan, and it cuts both ways. Tick and K-line data for every tradable contract from listing onward is served from memory, so a strategy can ask for deep history at startup without writing a schema or an extract job, and there is no disk path to tune. What the feature does not give you is anywhere to keep your own derived state. Anything your strategy computes that has to survive a process exit, positions you track yourself, fills you reconcile later, equity curves you want to compare across runs, has to be written by your own code, because nothing in the setup writes one for you.
setup.py and requirements.txt set two floors for websockets
setup.py pins install_requires with websockets>=10.1, while requirements.txt, the file kept for humans, lists websockets>=8.1 in its user required block. Two floors for the same dependency, and the one pip obeys when you install the package is the one in setup.py. The rest of the user required list has the same names, though setup.py caps pandas at <3.1 and requires tqsdk_ctpse>=1.2.1: requests, numpy, aiohttp, simplejson, pyjwt, psutil>=5.9.6, shinny_structlog, sgqlc, filelock, tqsdk_ctpse, tqsdk_sm, pandas>=1.1.0, scipy, certifi and packaging. requirements.txt then adds a second block for test and doc work only: selenium, jieba, Sphinx, nbsphinx, autodocsumm, sphinx_rtd_theme, sphinxcontrib-googleanalytics, lz4, memory_profiler, pytest, pytest-rerunfailures, pytest-cov and pytest-xdist. If you build an environment by syncing requirements.txt instead of installing tqsdk, you end up with the looser websockets floor and a pile of documentation and test packages you never asked for.
Two vendor packages ride along with every install
Every install pulls tqsdk_ctpse and tqsdk_sm, even for a script that only replays historical bars. Alongside them come pyjwt and certifi for token and certificate handling, requests and aiohttp for HTTP, websockets for the Diff channel, psutil for process inspection, filelock, sgqlc, shinny_structlog, simplejson, packaging, numpy, pandas>=1.1.0 and scipy. The classifiers in setup.py say Python 3, Apache Software License and Operating System :: OS Independent, and python_requires is >=3.9, matching the stated support for Python 3.9 and later. One thing to weigh before you depend on it: OS Independent describes the Python package, not the relay: the gateway connection into a futures company's trading system still has to work from wherever you run.
One API object, two credentials, and a snippet that stops mid expression
The quick start wires two accounts into a single api object: a broker account through TqAccount, and a TqAuth account for the data side. The snippet as published:
from tqsdk import TqApi, TqAuth, TqAccount, TargetPosTask
api = TqApi(TqAccount("H海通期货", "4003242", "123456"), auth=TqAuth("快期账户", "账户密码")) # 创建 TqApi 实例, 指定交易账户
q_2610 = api.get_quote("SHFE.rb2610") # 订阅近月合约行情
t_2610 = TargetPosTask(api, "SHFE.rb2610") # 创建近月合约调仓工具
q_2701 = api.get_quote("SHFE.rb2701") # 订阅远月合约行情
t_2701 = TargetPosTask(api, "SHFE.rb2701") # 创建远月合约调仓工具
while True:
api.wait_update() # 等待数据更新
spread = q_2610["last_price"] - q_2701[It ends on an unfinished line while computing a spread, so take the complete version from the ten minute quick start guide rather than pasting this into a file. The shape still says more than the missing line does. Quotes come from api.get_quote with an exchange prefixed symbol such as "SHFE.rb2610", TargetPosTask takes that same symbol and rebalances the contract for you, and api.wait_update() blocks until new data arrives. Multiple live and simulated accounts can trade at the same time, so the account is a per object choice rather than a per process one.
The AI guidance ships as a zip and the chat channel is star gated
The README tells you to make an AI tool or agent read a downloaded skill pack before it writes any TqSdk code: fetch https://doc.shinnytech.com/tqsdk/latest/ai_editor/skills/tqsdk-trading-and-data.zip, unpack it, then read SKILL.md inside and the references/ notes as the task requires. Companion pages cover using TqSdk with AI tools, with Trae, with WorkBuddy and with Codex, plus one explaining the skills archive. None of that lives in the repository, so an editor that only has the source tree gets none of it. Support is split the same way. Documentation is an HTML site at doc.shinnytech.com/tqsdk/latest, the 天勤AI助手 offers function explanations, strategy-writing guidance and error analysis, and a QQ group, 611806823, which the README says admits only people who have starred the project and asks for a GitHub username when they join.
setup.py carries version 3.10.2 and the README is what the index shows
Packaging is plain setuptools. setup.py sets version="3.10.4", name='tqsdk', description='TianQin SDK', reads README.md as long_description with long_description_content_type="text/markdown", points url at shinnytech.com/tqsdk, and calls find_packages(exclude=["tqsdk.test", "tqsdk.test.*"]) with include_package_data=True. The top level holds .readthedocs.yaml, doc/, requirements.txt, setup.cfg, setup.py, MANIFEST.in, PKG-INFO, CONTRIBUTING.md and the tqsdk/ package itself. The version in setup.py matches the 3.10.4 release dated 2026-10-26, next to 3.10.3 on 2026-10-21 and 3.10.2 on 2026-08-18, and the last push to the repository is 2026-10-26 with nothing archived. Because the long description is the README, the Chinese quick start with its unfinished line is what a pip user reads before deciding to install.
Editorial conclusion
Use TqSdk when you want one Python codebase for research, backtest, simulation and live futures trading, and when your broker is reachable through one of the supported counters. Do not expect it to store your strategy state, to pin its own dependencies, or to work without the quote gateway. Before the first live run, confirm the protocol floor on websockets, write your own persistence, and read the account wiring in the quick start line by line.
Frequently asked questions
Can Python be used for automated trading?
TqSdk is a Python library for futures, options and stock trading that covers historical data, realtime data, backtesting, simulated trading, live trading, monitoring and risk management. It supports Python 3.9 and later and installs with pip install tqsdk.
Which Python versions does TqSdk support?
Python 3.9 and later. setup.py sets python_requires='>=3.9' and the classifiers list Programming Language :: Python :: 3.
Does TqSdk need me to run a database?
No. Users need not build or maintain a database: quotes and trading data are held in an in-memory database with no access latency, so any state your own strategy must keep across restarts is code you write.
Which brokers and counters can TqSdk trade through?
CTP direct connections are supported, along with asset management counters such as 众期, 融航 and 杰宜斯 and high speed counters such as 易达 and ctpmini. Live trading is stated to cover 90% of the futures companies in the market.
What does installing tqsdk pull in?
install_requires lists websockets>=10.1, requests, numpy, pandas>=1.1.0,<3.1, scipy, simplejson, aiohttp, certifi, pyjwt, psutil>=5.9.6, shinny_structlog, sgqlc, filelock, tqsdk_ctpse>=1.2.1, tqsdk_sm and packaging.
How does TqSdk handle backtest versus live trading?
One api object drives it: TqAccount carries the broker account, TqAuth the data account, TargetPosTask rebalances a contract, and api.wait_update() blocks until data updates. Multiple live and simulated accounts can trade at the same time.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/shinnytech-tqsdk-python)Star history chart for your README
[](https://hysenlabs.com/projects/shinnytech-tqsdk-python)Was this useful?