2024-01-31 09:26:36 – WARNING – 通知 : 与 wss://free-api.shinnytech.com/t/nfmd/front/mobile 的网络连接断开,请检查客户端及网络是否正常
2024-01-31 09:26:36 – WARNING – 通知 : 开始与 wss://free-api.shinnytech.com/t/nfmd/front/mobile 的重新建立网络连接
2024-01-31 09:26:40 – WARNING – 通知 : 与 wss://otg-sim.shinnytech.com/trade 的网络连接断开,请检查客户端及网络是否正常
[趋势策略]交易出错: IH, Traceback (most recent call last):
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/websockets/legacy/client.py”, line 647, in __await_impl_timeout__
return await self.__await_impl__()
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/websockets/legacy/client.py”, line 651, in __await_impl__
_transport, _protocol = await self._create_connection()
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/asyncio/base_events.py”, line 1060, in create_connection
sock = await self._connect_sock(
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/asyncio/base_events.py”, line 969, in _connect_sock
await self.sock_connect(sock, address)
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/asyncio/selector_events.py”, line 501, in sock_connect
return await fut
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/Users/qianhao/PycharmProjects/tqtrading/strategy/trend_strategy.py”, line 326, in run
self.api.wait_update()
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/tqsdk/api.py”, line 1905, in wait_update
self._run_until_task_done(task=update_task, deadline=deadline)
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/tqsdk/baseApi.py”, line 88, in _run_until_task_done
self._run_once()
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/tqsdk/baseApi.py”, line 57, in _run_once
raise self._exceptions.pop(0)
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/tqsdk/connect.py”, line 157, in _run
async with websockets.connect(url, **self._keywords) as client:
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/websockets/legacy/client.py”, line 629, in __aenter__
return await self
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/websockets/legacy/client.py”, line 646, in __await_impl_timeout__
async with asyncio_timeout(self.open_timeout):
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/websockets/legacy/async_timeout.py”, line 169, in __aexit__
self._do_exit(exc_type)
File “/opt/anaconda3/envs/tqsdk310/lib/python3.10/site-packages/websockets/legacy/async_timeout.py”, line 252, in _do_exit
raise asyncio.TimeoutError
asyncio.exceptions.TimeoutError
以下是主程序代码
def run(self):
# 交易前准备工作
self.before_trading()
self.on_record(f”[{self.symbol}]开始交易”)
try:
# 订阅 quote
quote = self.api.get_quote(self.contract_code)
# 订阅 5 分钟 K 线
for contract in self.contracts_to_subscribe:
self.subscribed_klines[contract] = self.api.get_kline_serial(contract, 5*60, (self.roll_days+1)*48)
# 订阅换仓工具
symbol_target_pos = TargetPosTask(self.api, self.contract_code)
while datetime.datetime.now().strftime(“%H: %s”) <= self.end_time:
self.api.wait_update()
if self.api.is_changing(quote, 'datetime'):
self.on_quote()
# 更新止损价
if self.api.is_changing(self.subscribed_klines[self.contract_code], 'close'):
self.on_kline(RailFrequency.M15)
self.on_kline(RailFrequency.H)
# 交易
tart_pos = self.get_tar_pos()
self.set_tar_pos(symbol_target_pos, tart_pos)
except:
self.on_record(f"交易出错: {self.symbol}, {traceback.format_exc()}")
finally:
if self.api:
self.close_api()
self.on_record(f"交易结束: {self.symbol}")
看起来也没什么大问题,上面报错是一个超时退出的报错,看看是不是api.close()在用完后有正确调用,然后把try except先去掉,简化下代码测一下链接如何
策略里也没有加入异步的代码,单策略单合约,系统macos,python3.10