常见问题¶
运行 pip install tqsdk 之后,如何找到安装的文件?¶
可以运行以下命令行查看安装包的位置:
pip show --files tqsdk
安装包位于 Location 位置的 tqsdk/ 目录下,所有源文件都在这里,tqsdk/demo/ 下是所有示例文件。
参考链接: pip文档
运行示例代码后提示 “RuntimeError: Cannot run the event loop while another loop is running”¶
TqSdk 使用了 python3 的原生协程和异步通讯库 asyncio,部分 IDE 不支持 asyncio,例如:
- spyder: 详见 https://github.com/spyder-ide/spyder/issues/7096
- jupyter: 详见 https://github.com/jupyter/notebook/issues/3397
可以直接运行示例代码(例如: “python demo/tutorial/t10.py”),或使用支持 asyncio 的 IDE (例如: pycharm)
关于平今平昨怎么处理?¶
- 直接使用 api.insert_order 下单,在 offset 字段上可以直接指定平今平昨(CLOSETODAY/CLOSE)。
python demo/tutorial/t41.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
from tqsdk import TqApi, TqSim
api = TqApi(TqSim())
# 开仓两手并等待完成
order = api.insert_order(symbol="SHFE.rb1901", direction="BUY", offset="OPEN", limit_price=4310,volume=2)
while order["status"] != "FINISHED":
api.wait_update()
print("已开仓")
# 平今两手并等待完成
order = api.insert_order(symbol="SHFE.rb1901", direction="SELL", offset="CLOSETODAY", limit_price=3925,volume=2)
while order["status"] != "FINISHED":
api.wait_update()
print("已平今")
# 关闭api,释放相应资源
api.close()
|
- 使用 TargetPosTask,目标持仓模型下单,通过参数 offset_priority 设置平今平昨优先级。
python demo/tutorial/t71.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'yanqiong'
from tqsdk import TqApi, TqSim, TargetPosTask
'''
连续3根阴线就做空,连续3根阳线就做多,否则空仓
'''
api = TqApi(TqSim())
# 设定连续多少根阳线/阴线
length = 3
# 获得 rb1901 10秒K线的引用, 长度为 length+1
klines = api.get_kline_serial("SHFE.rb1901", 10, data_length = length + 1)
# 创建 rb1901 的目标持仓 task,该 task 负责调整 rb1901 的仓位到指定的目标仓位, offset_priority的用法详见文档
target_pos = TargetPosTask(api, "SHFE.rb1901", offset_priority="今昨开")
while True:
api.wait_update()
# 只有在新创建出K线时才判断开平仓条件
if api.is_changing(klines[-1], "datetime"):
# 将K线转为pandas.DataFrame, 跳过最后一根刚生成的K线
df = klines.to_dataframe()[:-1]
# 比较收盘价和开盘价,判断是阳线还是阴线
# df["close"] 为收盘价序列, df["open"] 为开盘价序列, ">"(pandas.Series.gt) 返回收盘价是否大于开盘价的一个新序列
up = df["close"] > df["open"]
down = df["close"] < df["open"]
if all(up):
print("连续阳线: 目标持仓 多头1手")
# 设置目标持仓为正数表示多头,负数表示空头,0表示空仓
target_pos.set_target_volume(1)
elif all(down):
print("连续阴线: 目标持仓 空头1手")
target_pos.set_target_volume(-1)
else:
print("目标持仓: 空仓")
target_pos.set_target_volume(0)
|
如何回测策略?¶
在创建 TqApi 实例时可以传入 TqBacktest 启用回测功能
python demo/tutorial/backtest.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chengzhi'
from datetime import date
from contextlib import closing
from tqsdk import TqApi, TqSim, TqBacktest, TargetPosTask
'''
如果当前价格大于5分钟K线的MA15则开多仓
如果小于则平仓
回测从 2018-05-01 到 2018-10-01
'''
# 在创建 api 实例时传入 TqBacktest 就会进入回测模式
api = TqApi(TqSim(), backtest=TqBacktest(start_dt=date(2018, 5, 1), end_dt=date(2018, 10, 1)))
# 获得 m1901 5分钟K线的引用
klines = api.get_kline_serial("DCE.m1901", 5*60, data_length=15)
# 创建 m1901 的目标持仓 task,该 task 负责调整 m1901 的仓位到指定的目标仓位
target_pos = TargetPosTask(api, "DCE.m1901")
# 使用with closing机制确保回测完成后释放对应的资源
with closing(api):
while True:
api.wait_update()
if api.is_changing(klines):
ma = sum(klines.close[-15:])/15
print("最新价", klines.close[-1], "MA", ma)
if klines.close[-1] > ma:
print("最新价大于MA: 目标多头5手")
# 设置目标持仓为多头5手
target_pos.set_target_volume(5)
elif klines.close[-1] < ma:
print("最新价小于MA: 目标空仓")
# 设置目标持仓为空仓
target_pos.set_target_volume(0)
|
模拟交易的成交规则是什么?¶
限价单要求报单价格达到或超过对手盘价格才能成交, 成交价为报单价格, 如果没有对手盘(涨跌停)则无法成交
市价单使用对手盘价格成交, 如果没有对手盘(涨跌停)则自动撤单
模拟交易不会有部分成交的情况, 要成交就是全部成交
如何进行实盘交易?¶
在创建 TqApi 实例时传入 TqAccount 即可进行实盘交易:
api = TqApi(TqAccount("H海通期货", "022631", "123456"))
如果想连接天勤终端进行实盘交易可以只填写帐号,并先在天勤终端内登录交易:
api = TqApi("022631")
网络断线怎么处理?¶
TqSdk 会自动重连服务器, 不需要特殊处理。但是断线时报的单可能会丢掉,因此应尽量确保网络稳定
我想在周末或晚上开发交易程序, 但是行情不跳, 有什么办法么?¶
您可以使用天勤终端提供的复盘功能:
在 Pycharm 中停止正在运行的海龟策略,为何不能保存持仓状态?¶
因为在 Windows 下的 PyCharm 中终止正在运行中的程序,不会执行程序代码文件里 finally 代码段(在Linux中或在Windows的命令行下则无此问题)
可以在运行策略前对 PyCharm 进行相关设置:
- 单击 “Run” 设置按钮
- 选择 “Edit Configurations…” 选项
- 在左侧导航栏中选中该策略代码的py文件
- 勾选 “Emulate terminal in output console”
- 点击 “OK” 确认