VeighNa量化社区
你的开源社区量化交易平台 | vn.py | vnpy
Member
avatar
加入于:
帖子: 1
声望: 0

CTA回测时报错,数据源用的akshare
description

Traceback (most recent call last):
File "C:\Users\junhome\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\core\indexes\base.py", line 3641, in get_loc
return self._engine.get_loc(casted_key)
~~~~^^^^^^^^^^^^
File "pandas/_libs/index.pyx", line 168, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 197, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 7668, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 7676, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\junhome\AppData\Local\Programs\Python\Python313\Lib\site-packages\vnpy_ctabacktester\ui\widget.py", line 299, in process_backtesting_finished_event
self.chart.set_data(df)
~~~~~^^^^
File "C:\Users\junhome\AppData\Local\Programs\Python\Python313\Lib\site-packages\vnpy_ctabacktester\ui\widget.py", line 820, in set_data
self.balance_curve.setData(df["balance"])
~
~~~~~^^^^^^^^^^^^^^^
File "C:\Users\junhome\AppData\Local\Programs\Python\Python313\Lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 1184, in setData
dt = dataType(data)
File "C:\Users\junhome\AppData\Local\Programs\Python\Python313\Lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 1856, in dataType
first = obj[0]
~
^^^
File "C:\Users\junhome\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\core\series.py", line 959, in getitem
return self._get_value(key)
~~~^^^^^
File "C:\Users\junhome\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\core\series.py", line 1046, in _get_value
loc = self.index.get_loc(label)
File "C:\Users\junhome\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\core\indexes\base.py", line 3648, in get_loc
raise KeyError(key) from err
KeyError: 0

Member
avatar
加入于:
帖子: 5993
声望: 369

如果vnpy_ctastrategy不是1.4.1的话,pip install vnpy_ctastrategy --upgrade升级之后再试试看

Member
avatar
加入于:
帖子: 14
声望: 0

xiaohe wrote:

如果vnpy_ctastrategy不是1.4.1的话,pip install vnpy_ctastrategy --upgrade升级之后再试试看
它是可以稳定复现的。各模块的版本如下:
vnpy 4.3.0
vnpy_ctabacktester 1.3.0
vnpy_ctastrategy 1.4.1
vnpy_ctp 6.7.11.2
vnpy_datamanager 1.2.0
vnpy_sqlite 1.1.3
复现方式:
python -m venv C:\vnpy_folder\test_vnpy --system-site-packages
C:\vnpy_folder\test_vnpy\Scripts\Activate.ps1
python -m pip --version
python -m pip install vnpy vnpy_ctabacktester vnpy_ctastrategy vnpy_ctp vnpy_datamanager vnpy_sqlite
python -m pip list
git clone https://gitee.com/vnpy/vnpy.git C:\vnpy_folder\vnpy\
python C:\vnpy_folder\vnpy\examples\veighna_trader\run.py
然后直接“功能”→“CTA回测”→“开始回测”。就触发异常了。
备注:我的 .vntrader\database.db 里面是有数据的,所以不需要安装datafeed了。
麻烦看一下,谢谢。

Member
avatar
加入于:
帖子: 5993
声望: 369

pandas版本是多少呢

Member
avatar
加入于:
帖子: 14
声望: 0

xiaohe wrote:

pandas版本是多少呢

版本是:pandas 3.0.0。更具体的各个包的版本如下:

(test_vnpy) PS C:\vnpy_folder\test_vnpy\Scripts> python -m pip list
Package Version


attrs 25.4.0
build 1.4.0
colorama 0.4.6
deap 1.4.3
fastjsonschema 2.21.2
icdiff 2.0.7
jsonschema 4.26.0
jsonschema-specifications 2025.9.1
jupyter_core 5.9.1
loguru 0.7.3
narwhals 2.15.0
nbformat 5.10.4
numpy 2.4.2
packaging 26.0
pandas 3.0.0
peewee 3.19.0
pip 25.1.1
platformdirs 4.5.1
plotly 6.5.2
pyproject_hooks 1.2.0
pyqtgraph 0.14.0
PySide6 6.8.2.1
PySide6_Addons 6.8.2.1
PySide6_Essentials 6.8.2.1
python-dateutil 2.9.0.post0
pyzmq 27.1.0
QDarkStyle 3.2.3
QtPy 2.4.3
referencing 0.37.0
rpds-py 0.30.0
shiboken6 6.8.2.1
six 1.17.0
TA-Lib 0.6.8
tqdm 4.67.2
traitlets 5.14.3
tzdata 2025.3
tzlocal 5.3.1
vnpy 4.3.0
vnpy_ctabacktester 1.3.0
vnpy_ctastrategy 1.4.1
vnpy_ctp 6.7.11.2
vnpy_datamanager 1.2.0
vnpy_sqlite 1.1.3
win32_setctime 1.2.0

Member
avatar
加入于:
帖子: 5993
声望: 369

pandas版本降到2.3.3之后再试试呢

Member
avatar
加入于:
帖子: 14
声望: 0

xiaohe wrote:

pandas版本降到2.3.3之后再试试呢

牛逼。确实好了。
(test_vnpy) PS C:\vnpy_folder\vnpy\examples\veighna_trader> python -m pip list | Select-String "pandas"
pandas 3.0.0

(test_vnpy) PS C:\vnpy_folder\vnpy\examples\veighna_trader> python -m pip install pandas==2.3.3
。。。略。。。
Successfully installed pandas-2.3.3 pytz-2025.2

(test_vnpy) PS C:\vnpy_folder\vnpy\examples\veighna_trader> python -m pip list | Select-String "pandas"
pandas 2.3.3

(test_vnpy) PS C:\vnpy_folder\vnpy\examples\veighna_trader> python .\run.py
回测正常。

(test_vnpy) PS C:\vnpy_folder\vnpy\examples\veighna_trader> python -m pip install pandas==3.0.0
。。。略。。。
Successfully installed pandas-3.0.0

(test_vnpy) PS C:\vnpy_folder\vnpy\examples\veighna_trader> python .\run.py
回测崩溃。

Member
avatar
加入于:
帖子: 14
声望: 0

xiaohe wrote:

pandas版本降到2.3.3之后再试试呢

对于:https://gitee.com/vnpy/vnpy/blob/master/.github/SUPPORT.md
咱们的【官方QQ群: 262656087】似乎找不到?现在还能加官方QQ群吗?具体群号是多少呀?

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】