TqSdk is an open source python library initiated by Xinyi Technology and contributed the main code. Relying on Kuaiqi’s mature trading and market server system accumulated over many years, TqSdk supports users to build various types of quantitative trading strategy programs with a very small amount of code, and Provides historical data including futures, options, and stocks - real-time data - development and debugging - strategy backtesting - simulated trading - real trading - operation monitoring - a complete set of risk management solutions.
from tqsdk import TqApi, TqAuth, TqAccount, TargetPosTask
api = TqApi(TqAccount("H海通期货", "4003242", "123456"), auth=TqAuth("快期账户", "账户密码")) # 创建 TqApi 实例, 指定交易账户
q_1910 = api.get_quote("SHFE.rb1910") # 订阅近月合约行情
t_1910 = TargetPosTask(api, "SHFE.rb1910") # 创建近月合约调仓工具
q_2001 = api.get_quote("SHFE.rb2001") # 订阅远月合约行情
t_2001 = TargetPosTask(api, "SHFE.rb2001") # 创建远月合约调仓工具
while True:
api.wait_update() # 等待数据更新
spread = q_1910["last_price"] - q_2001["last_price"] # 计算近月合约-远月合约价差
print("当前价差:", spread)
if spread > 250:
print("价差过高: 空近月,多远月")
t_1910.set_target_volume(-1) # 要求把1910合约调整为空头1手
t_2001.set_target_volume(1) # 要求把2001合约调整为多头1手
elif spread < 200:
print("价差回复: 清空持仓") # 要求把 1910 和 2001合约都调整为不持仓
t_1910.set_target_volume(0)
t_2001.set_target_volume(0)
To quickly learn how to use TqSdk, you can visit our ten-minute quick start guide.
The functions provided by TqSdk can support various policy programs from simple to complex.
TqSdk only supports Python 3.7 and above. To install TqSdk, use pip:
$ pip install tqsdk
Read the HTML version of the document online: https://doc.shinnytech.com/tqsdk/latest
Zhihu account [Tianqin Quantification]: https://www.zhihu.com/org/tian-qin-liang-hua/activities
Tianqin AI Assistant: https://udify.app/chat/im02prcHNEOVbPAx It can explain functions, write demo strategies, and analyze the causes of error reports!
User communication QQ group: 611806823 (Currently only students who have clicked STAR for us are allowed to join, please provide your github username when joining the group)
TqSdk has its own web_gui function. A simple line of parameters can support calling the graphical interface. For details, please refer to web_gui.
Xinyi Technology is a professional futures software supplier and exchange-authorized market service provider. Its fast-term series products have been serving the market for more than 10 years. TqSdk is part of the company's open source plan.