4.76K 浏览
0

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 = "simnow"
    my_account = "082749"
    my_pass = "iloveyou"
    api_master = TqApi(TqAccount(my_corp, my_account, my_pass))
    # Create new threads
    thread1 = WorkerThread(api_master.copy(), "SHFE.cu2002")
    thread2 = WorkerThread(api_master.copy(), "SHFE.rb2005")
     # Start new Threads
    thread1.start()
    thread2.start()
     while True:
        api_master.wait_update()

C:\Python37\python.exe C:/TianQin/strategies/multi_示例.py
2019-12-17 21:03:29,607 – INFO – 通知: 已经连接到交易前置
2019-12-17 21:03:29,607 – 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 1397, in _merge_diff
value_type = type(diff[key])
KeyError: ‘datetime’

Process finished with exit code -1

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

把 api更新到最新,再尝试几次,如果还有错误而且每次错误不一样,主要是KeyError后面的内容不一样,建议放弃多线程的写法。

如果你的需求是单策略多合约,那就学一下文档中使用协程的写法。或者看一下这个单策略多合约的一种写法:https://www.shinnytech.com/question/7875/

ringo 发表新评论 2019年12月18日

每次不一样。协成和连接中的方法,您推荐哪一种

为老版本服务器bug,在1.3.1版本中已修复,可以更新后再尝试

能自动升级吗

指的是tqsdk 1.3.1版本 需要自行升级

您正在查看2个答案中的1个,单击此处查看所有答案。