此问题可能是由于数据源的时间周期设置不正确导致的。如果您的数据源是按小时更新的,则需要在代码中设置时间周期为1小时,如下所示:
import backtrader as bt
class MyStrategy(bt.Strategy):
params = (('timeframe', bt.TimeFrame.Minutes), ('compression', 60),)
def __init__(self):
self.data = self.datas[0]
def next(self):
print(self.data.close[0])
cerebro = bt.Cerebro()
data = bt.feeds.GenericCSVData(dataname='mydata.csv', datetime=0, open=1, high=2, low=3, close=4, volume=5, timeframe=bt.TimeFrame.Hours, compression=1)
cerebro.adddata(data)
cerebro.addstrategy(MyStrategy)
cerebro.run()
在这个示例中,我们使用了GenericCSVData
数据源,并设置了timeframe=bt.TimeFrame.Hours
和compression=1
,来确保我们读取的是按小时更新的数据,并且我们将策略中的时间周期设置为1小时。