0

我正在与 backtrader 一起制定回测策略。我正在用像这样的通用 csv 数据喂 cerebro:

1502942400000,4261.48000000,4280.56000000,4261.32000000,4261.45000000,11.30892600,1502944199999,48224.75400837,49,3.93617400,16793.03995768,7922.57993329

第一个属性是时间戳,如图所示是一个int,所以在添加数据的时候我选择dtformat=1

data = bt.feeds.GenericCSVData(dataname="./file.csv", dtformat=1)
cerebro.adddata(data)

当我打电话时cerebro.run(),我不断收到同样的错误:

Traceback (most recent call last):
  File "C:\Users\jorge\Desktop\cerebro\btc.py", line 8, in <module>
    cerebro.run()
  File "C:\Users\jorge\Desktop\cerebro\env\lib\site-packages\backtrader\cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
  File "C:\Users\jorge\Desktop\cerebro\env\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies
    data.preload()
  File "C:\Users\jorge\Desktop\cerebro\env\lib\site-packages\backtrader\feed.py", line 688, in preload
    while self.load():
  File "C:\Users\jorge\Desktop\cerebro\env\lib\site-packages\backtrader\feed.py", line 479, in load
    _loadret = self._load()
  File "C:\Users\jorge\Desktop\cerebro\env\lib\site-packages\backtrader\feed.py", line 710, in _load
    return self._loadline(linetokens)
  File "C:\Users\jorge\Desktop\cerebro\env\lib\site-packages\backtrader\feeds\csvgeneric.py", line 116, in _loadline
    dt = self._dtconvert(dtfield)
  File "C:\Users\jorge\Desktop\cerebro\env\lib\site-packages\backtrader\feeds\csvgeneric.py", line 96, in <lambda>
    self._dtconvert = lambda x: datetime.utcfromtimestamp(int(x))
OSError: [Errno 22] Invalid argument

我无法弄清楚为什么它不能被转换,也尝试过dtformat=2,但在这种情况下失败了,因为它需要一个浮点数。

这是完整的代码:

import backtrader as bt

cerebro = bt.Cerebro()
cerebro.broker.setcash(1000)
data = bt.feeds.GenericCSVData(dataname="./file.csv", dtformat=1)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.adddata(data)
cerebro.run()
cerebro.plot()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

我很感激你可以借给我的任何帮助,

谢谢!

4

0 回答 0