回测时报错,如下:
CE.jd1901,开平:CLOSE,方向:BUY,手数:5,价格:3868.0
INFO – 模拟交易委托单 ad44d700e656d2f52c28f545067307e8: 全部成交
INFO – 模拟交易下单 0466b8ab2f72537aad6e6fd3fe79e5bc: 时间:2018-04-13 14:59:00.000000,合约:DCE.jd1901,开平:OPEN,方向:SELL,手数:5,价格:3884.0
INFO – 模拟交易委托单 0466b8ab2f72537aad6e6fd3fe79e5bc: 全部成交
WARNING – 回测结束
WARNING – 模拟交易成交记录
WARNING – 时间:2018-03-01 09:00:00.000000,合约:DCE.jd1901,开平:OPEN,方向:SELL,手数:6,价格:3836.000,手续费:32.03
WARNING – 时间:2018-03-01 09:30:00.000000,合约:DCE.jd1901,开平:OPEN,方向:SELL,手数:5,价格:3840.000,手续费:26.69
WARNING – 时间:2018-03-01 10:06:00.000000,合约:DCE.jd1901,开平:CLOSE,方向:BUY,手数:5,价格:3826.000,手续费:26.69
WARNING – 时间:2018-03-01 14:54:00.000000,合约:DCE.jd1901,开平:CLOSE,方向:BUY,手数:6,价格:3812.000,手续费:32.03
Error in sys.excepthook:
Traceback (most recent call last):
File “C:\Users\rally\Anaconda3\lib\site-packages\tqsdk\api.py”, line 1525, in _run_until_idle
self._run_once()
File “C:\Users\rally\Anaconda3\lib\site-packages\tqsdk\api.py”, line 1518, in _run_once
raise self._exceptions.pop(0)
File “C:\Users\rally\Anaconda3\lib\site-packages\tqsdk\sim.py”, line 104, in _run
await self._send_stat_report()
File “C:\Users\rally\Anaconda3\lib\site-packages\tqsdk\sim.py”, line 137, in _send_stat_report
self._report()
File “C:\Users\rally\Anaconda3\lib\site-packages\tqsdk\sim.py”, line 407, in _report
while cur_close_volume > 0 and opposite_list[0]:
IndexError: list index out of range
Original exception was:
Traceback (most recent call last):
File “D:/learning 个人学习提高/Python 书籍/VNPY 一类的量化平台/天勤量化/test/Grid_backtesting2.py”, line 144, in <module>
api.wait_update()
File “C:\Users\rally\Anaconda3\lib\site-packages\tqsdk\api.py”, line 861, in wait_update
self._run_once()
File “C:\Users\rally\Anaconda3\lib\site-packages\tqsdk\api.py”, line 1518, in _run_once
raise self._exceptions.pop(0)
File “C:\Users\rally\Anaconda3\lib\site-packages\tqsdk\backtest.py”, line 123, in _run
await self._send_diff()
File “C:\Users\rally\Anaconda3\lib\site-packages\tqsdk\backtest.py”, line 217, in _send_diff
raise BacktestFinished(self._api) from None
tqsdk.exceptions.BacktestFinished: 回测结束
Process finished with exit code 1
def tqFirstTrade(): api = TqApi(backtest=TqBacktest(start_dt=date(2019, 12, 1), end_dt=date(2020, 1, 9)),web_gui=True) account = api.get_account() order = None # 一分钟K线 klines = api.get_kline_serial("SHFE.fu2005", 1800) while True: while True: api.wait_update() if api.is_changing(klines): ma = sum(klines.close.iloc[-55:]) / 55 print("最新价", klines.close.iloc[-1], "55倍 MA", ma) if klines.close.iloc[-1] > ma: print("最新价大于55倍 MA: 市价开仓") order = api.insert_order(symbol="SHFE.fu2005", direction="BUY", offset="OPEN", volume=1) print("账户权益-------------: %.2f" % (account.balance)) break while True: api.wait_update() if api.is_changing(klines): ma = sum(klines.close.iloc[-30:]) / 30 print("最新价", klines.close.iloc[-1], "30倍 MA", ma) if klines.close.iloc[-1] < ma: print("最新价小于30倍MA: 市价平仓") order = api.insert_order(symbol="SHFE.fu2005", direction="SELL", offset="CLOSE", volume=1) print("账户权益-------------: %.2f" % (account.balance)) break pass # 关闭api,释放相应资源 api.close() 我也遇到了,一模一样的错:
INFO – 模拟交易下单 0cf58f4cc68a48f45d216290700cd3d6: 时间:2020-01-09 11:29:00.000000,合约:SHFE.fu2005,开平:OPEN,方向:BUY,手数:1,价格:市价
INFO – 模拟交易委托单 0cf58f4cc68a48f45d216290700cd3d6: 全部成交
INFO – 模拟交易下单 3c2b214efde772a6087a594cb316d16c: 时间:2020-01-09 11:29:00.000000,合约:SHFE.fu2005,开平:CLOSE,方向:SELL,手数:1,价格:市价
INFO – 模拟交易委托单 3c2b214efde772a6087a594cb316d16c: 平仓手数不足
最新价 2372.0 55倍 MA 2373.4363636363637
最新价 2372.0 55倍 MA 2376.090909090909
最新价 2332.0 55倍 MA 2375.3636363636365
最新价 2332.0 55倍 MA 2377.490909090909
WARNING – 回测结束
WARNING – ———– Backtest finished, press [Ctrl + C] to exit. ———–
WARNING – 模拟交易成交记录
WARNING – 时间:2019-11-29 18:00:00.000000,合约:SHFE.fu2005,开平:OPEN,方向:BUY,手数:1,价格:1936.000,手续费:1.20
WARNING – 时间:2019-11-29 21:59:00.000000,合约:SHFE.fu2005,开平:OPEN,方向:BUY,手数:1,价格:1931.000,手续费:1.20
WARNING – 时间:2019-12-04 09:59:00.000000,合约:SHFE.fu2005,开平:OPEN,方向:BUY,手数:1,价格:1911.000,手续费:1.20
WARNING – 时间:2019-12-13 10:14:59.999999,合约:SHFE.fu2005,开平:CLOSE,方向:SELL,手数:1,价格:2064.000,手续费:1.20
WARNING – 时间:2019-12-13 10:14:59.999999,合约:SHFE.fu2005,开平:OPEN,方向:BUY,手数:1,价格:2066.000,手续费:1.20
WARNING – 时间:2019-12-13 11:29:00.000000,合约:SHFE.fu2005,开平:CLOSE,方向:SELL,手数:1,价格:2068.000,手续费:1.20
Error in sys.excepthook:
Traceback (most recent call last):
File “D:\software\Anaconda3\lib\site-packages\tqsdk\exceptions.py”, line 26, in _except_catcher
api.wait_update()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 877, in wait_update
self._run_until_idle()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 1555, in _run_until_idle
最新价 2348.0 55倍 MA 2377.7818181818184
self._run_once()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 1548, in _run_once
raise self._exceptions.pop(0)
File “D:\software\Anaconda3\lib\site-packages\tqsdk\sim.py”, line 104, in _run
await self._send_stat_report()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\sim.py”, line 137, in _send_stat_report
self._report()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\sim.py”, line 407, in _report
while cur_close_volume > 0 and opposite_list[0]:
IndexError: list index out of range
Original exception was:
Traceback (most recent call last):
File “D:/data/learn/trade/stockPro/djangoManager/djangoManager/tq.py”, line 203, in <module>
tqFirstTrade()
File “D:/data/learn/trade/stockPro/djangoManager/djangoManager/tq.py”, line 73, in tqFirstTrade
api.wait_update()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 894, in wait_update
self._run_once()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\api.py”, line 1548, in _run_once
raise self._exceptions.pop(0)
File “D:\software\Anaconda3\lib\site-packages\tqsdk\backtest.py”, line 123, in _run
await self._send_diff()
File “D:\software\Anaconda3\lib\site-packages\tqsdk\backtest.py”, line 217, in _send_diff
raise BacktestFinished(self._api) from None
tqsdk.exceptions.BacktestFinished: 回测结束
Process finished with exit code 1