数据以unix时间戳的形式传入(以毫秒为单位(。所以我使用的是#dtformat=lambda x:datetime.datetime.utcromtimestamp(int(x(/1000(。
import backtrader as bt
import datetime
cerebro = bt.Cerebro()
data = bt.feeds.GenericCSVData(dataname="ftx1h.csv", dtformat=lambda x: datetime.datetime.utcfromtimestamp(int(x) / 1000))
cerebro.adddata(data)
cerebro.run()
cerebro.plot()
但现在出现了#IndexError:列表索引超出范围的问题
Traceback (most recent call last):
File "/Users/talha/VScode/Backtest/Strategy5.py", line 7, in
cerebro.run()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/cerebro.py", line 1212, in runstrategies
data.preload()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/feed.py", line 688, in preload
while self.load():
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/feed.py", line 479, in load
_loadret = self._load()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/feed.py", line 710, in _load
return self._loadline(linetokens)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/feeds/csvgeneric.py", line 148, in _loadline
csvfield = linetokens[csvidx]
IndexError: list index out of range
数据
166043520000024454.024528.0255.024527.067147303.3027166043880000024527.024682.024422.024598.0116587503.2913166044240000024598.024632.024514.0424608.034892443.4722166044600000024608.024635.024553.024584.022989205.8046166044960000024584.024652.024506.024581.044973942.229916604532000024581.024636.024540.024603.047360860.8874166045680000024613.024695.024603.024689.032459071.9839166046040000024689.025065.024682.024900.0301312472.1925166046400000024900.024902.024608.024673.0170952143.2637166046760000024672.024790.024643.24758.070969336.6324166047120000024758.024759.024618.024627.064221863.5382166047480000024627.024682.024525.024569.076070172.2453166047840000024569.024593.024475.024535.059713290.94811660482000000024535.024538.024406.024481.0899233652962166048560002441.024534.024466.024518.049835330.613116604892000024518.024549.024468.024528.0465556537.1289166049280000024528.024560024313.024315.0108465182.89291660496400000245.024332.024178.024232.0176757581.0963166050000000024232.024300.024155.024282.068322221.496416605036000024281.024327.024244.024289.034982570.2998166050720000024289.024352.024255.024315.0407992224.029166051080000024315.024448.024278.024349.09470976.3767166051440000024349.024372.024182.024256.075704499.2116166051800000024265.02458.024240.024310.046011851.11851660521600002410.024418.024171.024406.090810435.293166052520000024406.024665.024340.024655.50233812433.3012166052880000024654.0245.024653.024337.0223521467061166053240000024937.025232.024813.024883.073106086.273166053600000024883.2490.024696.024863.0172798798.982166053960000024863.24998.024762.024835.0110243603.1486166054320000024835.024841.024088.024129.0338761936.3576166054680000024130.024218.023950.024026.0287354302.5387166055040000024025.024089.023873.024044.0151992609.28421660554000004044.024210.023992.024176.0132115004.5245166055760000024180.0024300.024133.024275.0088762538.3025166056120000024275.024316.024009.024057.0116716391.6228166056480000024057.024197.023891.02396.0135401782.9878
您的数据都是一条非常长的行,至少当我将其粘贴到geany中时是这样。有行尾吗?可能只是我的浏览器有问题。这可能是你的问题。