from tqsdk import TqApi, TqSim, TargetPosTask
from tqsdk import TqAccount
import threading
from tqsdk.tafunc import ma
class WorkerThread(threading.Thread):
def __init__(self, api, symbol):
threading.Thread.__init__(self)
self.api = api
self.symbol = symbol
def run(self):
SHORT = 30 # 短周期
LONG = 60 # 长周期
data_length = LONG + 2 # k线数据长度
klines = self.api.get_kline_serial(self.symbol, duration_seconds=60, data_length=data_length)
target_pos = TargetPosTask(self.api, self.symbol)
while True:
#print('进行循环')
self.api.wait_update()
if self.api.is_changing(klines.iloc[-1], "datetime"): # 产生新k线:重新计算SMA
short_avg = ma(klines["close"], SHORT) # 短周期
long_avg = ma(klines["close"], LONG) # 长周期
if long_avg.iloc[-2] < short_avg.iloc[-2] and long_avg.iloc[-1] > short_avg.iloc[-1]:
target_pos.set_target_volume(-3)
print("均线下穿,做空")
if short_avg.iloc[-2] < long_avg.iloc[-2] and short_avg.iloc[-1] > long_avg.iloc[-1]:
target_pos.set_target_volume(3)
print("均线上穿,做多")
if __name__ == "__main__":
# api_master = TqApi( )
my_corp = " "
my_account = " "
my_pass = " "
api_master = TqApi(TqAccount(my_corp, my_account, my_pass))
# Create new threads
thread1 = WorkerThread(api_master.copy(), "SHFE.cu2001")
thread2 = WorkerThread(api_master.copy(), "SHFE.rb2001")
# Start new Threads
thread1.start()
thread2.start()
while True:
api_master.wait_update()
运行时出现以下问题;
2019-12-06 10:41:21,937 – INFO – 通知: 登录成功
Exception in thread Thread-1:
Traceback (most recent call last):
File “C:\Python37\lib\threading.py”, line 926, in _bootstrap_inner
self.run()
File “C:/TianQin/strategies/multi_示例.py”, line 21, in run
self.api.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 2 more times]
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: ‘open’
Traceback (most recent call last):
File “C:/TianQin/strategies/multi_示例.py”, line 48, 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 1397, in _merge_diff
value_type = type(diff[key])
KeyError: ‘orders’
python的版本是3.7. tqsk的版本不知道,我用的是完全安装
时间长了就会有了。时间短了不会