4.66K 浏览
0

#######################################################################################参数设置
    api_master = TqApi(TqAccount(my_corp, my_account, my_pass))
     account = api_master.get_account()
    print("可用资金", account.available)
     # Create new threads
    thread1 = WorkerThread(api_master.copy(), near_contract1, far_contract1, my_ask_price_list1)
    #thread2 = WorkerThread(api_master.copy(), near_contract2, far_contract2, my_ask_price_list2)
    #thread3 = WorkerThread(api_master.copy(), near_contract3, far_contract3, my_ask_price_list2)
     # Start new Threads
    thread1.start()
    #thread2.start()
    #thread3.start()
    while True:
        api_master.wait_update()

在程序启动10分钟后,程序出现中断,提示如下错误:

C:\Python37\python.exe C:/TianQin/strategies/my_handle_multi.py
2019-12-02 21:44:59,393 – INFO – 通知: 已经连接到交易前置
2019-12-02 21:44:59,409 – INFO – 通知: 登录成功
可用资金 1268219.6877000001
2019-12-02 21:45:03,558 – INFO – 通知: 下单成功
Traceback (most recent call last):
File “C:/TianQin/strategies/my_handle_multi.py”, line 73, in <module>
api_master.wait_update()
File “C:\Python37\lib\site-packages\tqsdk\api.py”, line 783, in wait_update
self._merge_diff(self._data, d, self._prototype, False)
File “C:\Python37\lib\site-packages\tqsdk\api.py”, line 1423, in _merge_diff
TqApi._merge_diff(target, diff[key], tpt, tpersist)
File “C:\Python37\lib\site-packages\tqsdk\api.py”, line 1423, in _merge_diff
TqApi._merge_diff(target, diff[key], tpt, tpersist)
File “C:\Python37\lib\site-packages\tqsdk\api.py”, line 1423, in _merge_diff
TqApi._merge_diff(target, diff[key], tpt, tpersist)
[Previous line repeated 1 more time]
File “C:\Python37\lib\site-packages\tqsdk\api.py”, line 1427, in _merge_diff
result[key] == diff[key] or (diff[key] != diff[key] and result[key] != result[key])):
KeyError: ‘pos_short_his’

Process finished with exit code -1

不知道该怎么解决?如果程序出问题应该是马上中断的啊,他这个一直运行了10分钟。

west 发表新评论 2019年12月6日

simnow账号,等待1个小时就会出现这种情况。每次都会出现。

你这个代码我跑过几次了,用的simnow账号,没有触发问题,你可以把api实例化那一句改成这样,
api_master = TqApi(TqAccount(“simnow”, “xxxxxx”, “xxxxxx”),debug=”debug.log”)
保存日志信息,如果再报错就把它发给我,每次运行之前把已经存在的这个文件删掉,因为下一次它会把信息追加进去,不用了可以把它去掉

1

WorkerThread里是怎么写的呢?

我这边用simnow账户不能重现问题,请提供最简可重现问题的demo程序

leo zhang 发表新评论 2019年12月5日

########################
import threading
from tqsdk import TqApi, TqSim, TargetPosTask
from tqsdk import TqAccount

class WorkerThread(threading.Thread):
def __init__(self, api, near_contract, far_contract, my_ask_price_list):
threading.Thread.__init__(self)
self.api = api
self.near_contract = near_contract
self.far_contract = far_contract
self.my_ask_price_list = my_ask_price_list

def run(self):
near_contract_id = “SHFE.” + self.near_contract
far_contract_id = “SHFE.” + self.far_contract
quote_near = self.api.get_quote(near_contract_id)
quote_far = self.api.get_quote(far_contract_id)

i = 0
while i < len(self.my_ask_price_list):
my_askprice = self.my_ask_price_list[i]
order_far = self.api.insert_order(symbol=far_contract_id, direction="SELL", offset="OPEN",
limit_price=my_askprice, volume=1)
self.api.wait_update()

while order_far.status == 'ALIVE':
self.api.wait_update()
print("跳出循环")

order_near = self.api.insert_order(symbol=near_contract_id, direction="BUY", offset="OPEN",
limit_price=quote_near.ask_price1, volume=1)
self.api.wait_update()
while order_near.status == 'ALIVE':
self.api.wait_update()
i = i + 1

if __name__ == "__main__":
#########################################################################################参数设置
near_contract1 = "pb2001"
far_contract1 = "pb2002"
my_ask_price_list1 = [14855, 14890]

my_corp = "simnow"
my_account = " "
my_pass = " "

#########################################################################################参数设置
api_master = TqApi(TqAccount(my_corp, my_account, my_pass))

account = api_master.get_account()
print("可用资金", account.available)

# Create new threads
thread1 = WorkerThread(api_master.copy(), near_contract1, far_contract1, my_ask_price_list1)
# thread2 = WorkerThread(api_master.copy(), near_contract2, far_contract2, my_ask_price_list2)
# thread3 = WorkerThread(api_master.copy(), near_contract3, far_contract3, my_ask_price_list2)

# Start new Threads
thread1.start()
# thread2.start()
# thread3.start()
while True:
api_master.wait_update()

麻烦帮我看一下

0

请问这个会稳定重现吗?

leo zhang 发表新评论 2019年12月4日

simnow账号,等待1个小时就会出现这种情况。每次都会出现。

好像是历史持仓取得不对?