from tqsdk import TqApi, TqSim, tafunc
from tqsdk.ta import MACD
api = TqApi(TqSim())
klines = api.get_kline_serial(“DCE.i2009”, 24 * 60 * 60)
macd=MACD(klines,12,26,9)
mbar=macd[“bar”]
mbar_crossup_0 = tafunc.crossup(mbar,0)
api.close()
运行时总是出现错误提示:
crossdown_data = pd.Series(np.where((a < b) & (a.shift(1) >= b.shift(1)), 1, 0))
AttributeError: ‘int’ object has no attribute ‘shift’
Task was destroyed but it is pending!
应如何解决?
如果刚上手天勤和python,并遇到报错,一般首先百度一下那条报错信息,很多教程文章能有比我们的回答更详细的说明和解决方法。另外,在论坛提问时可以把报错信息完整贴出来,我们才知道你遇到的是什么问题哈
加上target_pos = TargetPosTask(api,SYMBOL)后,还是会出现错误提示:
from datetime import date, datetime
from tqsdk import TqApi, TqSim, TargetPosTask, TqAccount, tafunc, TqBacktest, TqReplay
from tqsdk.ta import MACD
api = TqApi(TqSim(),backtest=TqBacktest(start_dt=date(2020,5,1), end_dt=date(2020,6,12)),web_gui=”:1234″) #回测
SYMBOL=”DCE.i2009″
klines = api.get_kline_serial(SYMBOL, 24 * 60 * 60)
target_pos = TargetPosTask(api,SYMBOL)
quote = api.get_quote(SYMBOL)
macd=MACD(klines,12,26,9)
while True:
api.wait_update()
if api.is_changing(klines.iloc[-1], [“datetime”, “open”]):
macd=MACD(klines,12,26,9)
if api.is_changing(quote, “last_price”):
mdif=macd[“diff”]
mdea=macd[“dea”]
if tafunc.crossup(mdif,mdea) :
target_pos.set_target_volume(1)
api.close()
Traceback (most recent call last):
File “cross.py”, line 28, in
if tafunc.crossup(mdif,mdea) :
File “C:Python38libsite-packagespandascoregeneric.py”, line 1478, in __nonzero__
raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Task was destroyed but it is pending!
因为crossup比较的是两个序列的关系,你第二个传入的0只是一个int类型数字
换成diff金叉dea,还是会出错:
from datetime import date, datetime
from tqsdk import TqApi, TqSim, TargetPosTask, TqAccount, tafunc, TqBacktest, TqReplay
from tqsdk.ta import MACD
api = TqApi(TqSim(),backtest=TqBacktest(start_dt=date(2020,5,1), end_dt=date(2020,6,12)),web_gui=”:1234″)
SYMBOL=”DCE.i2009″
klines = api.get_kline_serial(SYMBOL, 24 * 60 * 60)
quote = api.get_quote(SYMBOL)
macd=MACD(klines,12,26,9)
while True:
api.wait_update()
if api.is_changing(klines.iloc[-1], [“datetime”, “open”]):
macd=MACD(klines,12,26,9)
if api.is_changing(quote, “last_price”):
mdif=macd[“diff”]
mdea=macd[“dea”]
if tafunc.crossup(mdif,mdea) :
target_pos.set_target_volume(1)
api.close()
运行后出现错误提示。
你后来提供的这段代码里,没有定义target_pos,你再检查下呢