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 = "" my_pass = "" 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()
2019-12-18 22:32:15,790 – INFO – 通知: 与 wss://opentd.shinnytech.com/trade/user0 的网络连接已建立
2019-12-18 22:32:15,790 – INFO – 通知: 登录成功
2019-12-18 22:32:15,870 – INFO – 通知: 与 wss://openmd.shinnytech.com/t/md/front/mobile 的网络连接已建立
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 858, in wait_update
self._merge_diff(self._data, d, self._prototype, False)
File “C:\Python37\lib\site-packages\tqsdk\api.py”, line 1779, in _merge_diff
TqApi._merge_diff(target, diff[key], tpt, tpersist)
KeyError: ‘charts’
2019-12-18 22:33:56,011 – INFO – 通知: 成交通知,合约:SHFE.al2003,手数:1
2019-12-18 22:44:27,650 – INFO – 通知: 与 wss://opentd.shinnytech.com/trade/user0 的网络连接已建立
2019-12-18 22:44:27,660 – INFO – 通知: 登录成功
2019-12-18 22:44:27,740 – INFO – 通知: 与 wss://openmd.shinnytech.com/t/md/front/mobile 的网络连接已建立
Exception in thread Thread-1:
Traceback (most recent call last):
File “C:Python37libthreading.py”, line 926, in _bootstrap_inner
self.run()
File “C:/TianQin/strategies/multi_示例.py”, line 21, in run
self.api.wait_update()
File “C:Python37libsite-packagestqsdkapi.py”, line 858, in wait_update
self._merge_diff(self._data, d, self._prototype, False)
File “C:Python37libsite-packagestqsdkapi.py”, line 1779, in _merge_diff
TqApi._merge_diff(target, diff[key], tpt, tpersist)
File “C:Python37libsite-packagestqsdkapi.py”, line 1779, in _merge_diff
TqApi._merge_diff(target, diff[key], tpt, tpersist)
KeyError: ‘PYSDK_realtime_59217249a8c93c879e93c938f3cd931f’
Traceback (most recent call last):
File “C:/TianQin/strategies/multi_示例.py”, line 48, in
api_master.wait_update()
File “C:Python37libsite-packagestqsdkapi.py”, line 858, in wait_update
self._merge_diff(self._data, d, self._prototype, False)
File “C:Python37libsite-packagestqsdkapi.py”, line 1779, in _merge_diff
TqApi._merge_diff(target, diff[key], tpt, tpersist)
File “C:Python37libsite-packagestqsdkapi.py”, line 1779, in _merge_diff
TqApi._merge_diff(target, diff[key], tpt, tpersist)
File “C:Python37libsite-packagestqsdkapi.py”, line 1779, in _merge_diff
TqApi._merge_diff(target, diff[key], tpt, tpersist)
[Previous line repeated 2 more times]
File “C:Python37libsite-packagestqsdkapi.py”, line 1753, in _merge_diff
value_type = type(diff[key])
KeyError: ‘open_oi’