105 views
0

我的代码例子:test_stock.py

# coding: utf-8
import sys
import importlib
import os
from datetime import date, datetime, timedelta
sys.path.append(‘/home/odoo/addons’)
from tqsdk import TqApi, TqBacktest, TqSim
# 创建API实例
#api = TqApi(_stock= True)
start_dt = date(2020, 5,20 )
end_dt = date(2020, 6, 25)
init_balance = 100000
port = ‘53318’
account = TqSim(init_balance=init_balance)
backtest = TqBacktest(start_dt=start_dt, end_dt=end_dt)
api = TqApi(account=account, backtest=backtest, web_gui=f”:{port}”,_stock=True )
# 获得上期所 cu2001 的行情引用,当行情有变化时 quote 中的字段会对应更新
klines  =api.get_kline_serial([“SZSE.002593”],duration_seconds=300, data_length=2)
#SSE上海
#SZSE深圳
#print(klines)
while True:
    try:
        # 调用 wait_update 等待业务信息发生变化,例如: 行情发生变化, 委托单状态变化, 发生成交等等
        api.wait_update()
        # 每当业务信息有变化时就输出 cu2001 的最新行情时间和最新价
        # 注意:其他合约的行情的更新也会触发业务信息变化,因此这里可能会将同一笔行情输出多次
        #print(quote.datetime, quote.last_price)
        #print(quote.volume)
        #print(quote.volume)
        #print(klines)
        #print(klines.iloc[-1])
        print(klines)
    except Exception  as e:
        print(e)
api.close()
print(“api close”)

执行之后的错误

[root@osboxes strategy]# python test_stock.py
test_stock.py:3: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module’s documentation for alternative uses
import imp
在使用天勤量化之前,默认您已经知晓并同意以下免责条款,如果不同意请立即停止使用:https://www.shinnytech.com/blog/disclaimer/
INFO – 您可以访问 http://127.0.0.1:53318 查看策略绘制出的 K 线图形。
Traceback (most recent call last):
File “test_stock.py”, line 25, in <module>
klines =api.get_kline_serial([“SZSE.002593”],duration_seconds=300, data_length=2)
File “/data/anaconda3/lib/python3.7/site-packages/tqsdk/api.py”, line 470, in get_kline_serial
if not self.wait_update(deadline=deadline):
File “/data/anaconda3/lib/python3.7/site-packages/tqsdk/api.py”, line 882, in wait_update
self._run_once()
File “/data/anaconda3/lib/python3.7/site-packages/tqsdk/api.py”, line 1556, in _run_once
raise self._exceptions.pop(0)
File “/data/anaconda3/lib/python3.7/site-packages/tqsdk/backtest.py”, line 118, in _run
await self._ensure_serial(pack[“ins_list”], pack[“duration”])
File “/data/anaconda3/lib/python3.7/site-packages/tqsdk/backtest.py”, line 269, in _ensure_serial
await self._fetch_serial((ins, dur))
File “/data/anaconda3/lib/python3.7/site-packages/tqsdk/backtest.py”, line 278, in _fetch_serial
s[“timestamp”], s[“diff”], s[“quotes”] = await s[“generator”].__anext__()
File “/data/anaconda3/lib/python3.7/site-packages/tqsdk/backtest.py”, line 377, in _gen_serial
yield timestamp, diff, self._get_quotes_from_kline_open(self._data[“quotes”][ins], timestamp,
File “/data/anaconda3/lib/python3.7/site-packages/tqsdk/entity.py”, line 22, in __getitem__
return self.__dict__.__getitem__(key)
KeyError: ‘SZSE.002593’
Task was destroyed but it is pending!
task: <Task pending coro=<TqApi._notify_watcher() running at /data/anaconda3/lib/python3.7/site-packages/tqsdk/api.py:1598> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f20bb9fc750>()]> cb=[TqApi._on_task_done()]>
Exception ignored in: <coroutine object TqApi._notify_watcher at 0x7f20c0b99680>
Traceback (most recent call last):

Answered question