1

数据框 ( df) 看起来像:

            Open   High   Low  Close   VWAP  Volume
Date                                               
1996-01-01  83.0  83.00  83.0   83.0  83.00     100
1996-01-02  83.0  83.95  80.6   80.6  82.79     700
1996-01-03  82.0  82.90  82.0   82.7  82.35     400
1996-01-04  82.5  82.50  82.5   82.5  82.50     200
1996-01-05  80.0  80.00  79.0   79.0  79.50     200

使用这些定义和加载数据:

class pandasDataFeed(bt.feeds.PandasData):
    lines = ('vwap')
    params = (
        ('fromdate', dt.datetime(2005, 1, 1)),
        ('todate', dt.datetime.now().date()),
        ('dtformat', '%Y-%m-%d'),
        ('datetime', None),
        ('high', 'High'),
        ('low', 'Low'),
        ('open', 'Open'),
        ('close', 'Close'),
        ('volume', 'Volume'),
        ('vwap', 'VWAP')
    )
data = pandasDataFeed(dataname=df)
cerebro.adddata(data)

但是,如果我执行cerebro.run()它会给AttributeError: 'datetime.date' object has no attribute 'to_pydatetime'

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "venv\lib\site-packages\backtrader\cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
  File "venv\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies
    data.preload()
  File "venv\lib\site-packages\backtrader\feed.py", line 438, in preload
    while self.load():
  File "venv\lib\site-packages\backtrader\feed.py", line 479, in load
    _loadret = self._load()
  File "venv\lib\site-packages\backtrader\feeds\pandafeed.py", line 268, in _load
    dt = tstamp.to_pydatetime()
AttributeError: 'datetime.date' object has no attribute 'to_pydatetime'

我究竟做错了什么?

4

1 回答 1

2

知道了。

索引在dt.datetime.date,将其更改为dt.datetime。现在可以了。

于 2021-04-21T18:15:07.787 回答