简体   繁体   English

在 Backtrader 问题中获取数据

[英]Getting data in Backtrader issue

I am trying to write a backtesting strategy on Backtrader in Python, and below is the code that is giving me the error.我正在尝试用 Python 在 Backtrader 上编写一个回测策略,下面是给我错误的代码。 I am using the latest version of backtrader as of July 2, 2021.截至 2021 年 7 月 2 日,我使用的是最新版本的 backtrader。

import backtrader as bt
import backtrader.feeds as btfeeds
from datetime import datetime

cerebro = bt.Cerebro()
cerebro.broker.setcash(100000)
data = btfeeds.YahooFinanceData(dataname="SPY", fromdate=datetime(2016, 6, 25), 
todate=datetime(2021, 6, 25))
cerebro.adddata(data)
cerebro.run()

The error that I am getting is我得到的错误是

Traceback (most recent call last): File "c:\\Users\\risha\\PycharmProjects\\PythonDataScience\\BacktraderBacktesting\\TestingData.py", line 9, in cerebro.run() File "C:\\Users\\risha\\anaconda3\\lib\\site-packages\\backtrader\\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "C:\\Users\\risha\\anaconda3\\lib\\site-packages\\backtrader\\cerebro.py", line 1210, in runstrategies data._start() File "C:\\Users\\risha\\anaconda3\\lib\\site-packages\\backtrader\\feed.py", line 203, in _start self.start() File "C:\\Users\\risha\\anaconda3\\lib\\site-packages\\backtrader\\feeds\\yahoo.py", line 355, in start super(YahooFinanceData, self).start() File "C:\\Users\\risha\\anaconda3\\lib\\site-packages\\backtrader\\feeds\\yahoo.py", line 94, in start super(YahooFinanceCSVData, self).start() File "C:\\Users\\risha\\anaconda3\\lib\\site-packages\\backtrader\\feed.py", line 674, in start self.f = io.open(self.p.dataname, 'r') FileNotFoundError: [Errno 2] No such file or directory: 'SPY'回溯(最近一次调用):文件“c:\\Users\\risha\\PycharmProjects\\PythonDataScience\\BacktraderBacktesting\\TestingData.py”,第 9 行,在 cerebro.run() 文件“C:\\Users\\risha\\anaconda3\\lib\\ site-packages\\backtrader\\cerebro.py”,第 1127 行,运行 runstrat = self.runstrategies(iterstrat) 文件“C:\\Users\\risha\\anaconda3\\lib\\site-packages\\backtrader\\cerebro.py”,第 1210 行, 在 runstrategies data._start() 文件“C:\\Users\\risha\\anaconda3\\lib\\site-packages\\backtrader\\feed.py”,第 203 行,在 _start self.start() 文件“C:\\Users\\risha \\anaconda3\\lib\\site-packages\\backtrader\\feeds\\yahoo.py”,第 355 行,在 start super(YahooFinanceData, self).start() 文件“C:\\Users\\risha\\anaconda3\\lib\\site-packages\\ backtrader\\feeds\\yahoo.py”,第 94 行,在 start super(YahooFinanceCSVData, self).start() 文件“C:\\Users\\risha\\anaconda3\\lib\\site-packages\\backtrader\\feed.py”,第 674 行, 在开始 self.f = io.open(self.p.dataname, 'r') FileNotFoundError: [Errno 2] No such file or directory: 'SPY'

I am confused on why this is happening.我很困惑为什么会这样。 I have tried running this by adding a strategy in Cebro as well, but that still caused the same error.我也尝试通过在 Cebro 中添加策略来运行它,但这仍然导致相同的错误。 Could someone please help me fix this issue?有人可以帮我解决这个问题吗?

I actually figured out the solution.我实际上想出了解决方案。 If you use, the code:如果使用,代码:

data = bt.feeds.PandasData(dataname=yf.download('SPY', '2015-07-06', '2021-07-01', auto_adjust=True)) . data = bt.feeds.PandasData(dataname=yf.download('SPY', '2015-07-06', '2021-07-01', auto_adjust=True)) This will allow you to get the data from online for any ticker.这将允许您从在线获取任何代码的数据。 You will also have to use pip install yfinance before you run this code.在运行此代码之前,您还必须使用 pip install yfinance。

try to upgrade your backtrader.尝试升级您的 backtrader。 On 3rd of July there is a new release. 7 月 3 日有一个新版本。 I did update it but it still does not work.我确实更新了它,但它仍然不起作用。 The problem is that it does not bt.feed.YahooFinance, take the data in the correct format.问题在于它没有 bt.feed.YahooFinance,以正确的格式获取数据。 It is a new bug... I am waiting for them to fix it as well.这是一个新错误......我也在等待他们修复它。

Yahoo!雅虎! Finance has changed slightly their structure.财务结构略有改变。 Now requires headers for the data retreival on the http request.现在需要在 http 请求中获取数据的标头 Since backtrader has a "old version" of yahoo.py on line 271 you need to add the headers.由于 backtrader 在第 271 行有 yahoo.py 的“旧版本”,因此您需要添加标题。 Once done works fine.完成后效果很好。

It happens as well with pandas & pandas-datareader which you'll need to upgrade them if you use it. pandas 和 pandas-datareader 也会发生这种情况,如果您使用它,则需要升级它们。 (Which has been already sorted) (已经整理好了)

For Backtrader in yahoo.py line 271:对于 yahoo.py 第 271 行中的 Backtrader:

 crumb = None
 sess = requests.Session()
 ## ADD HEADERS
 sess.headers['User-Agent'] = 'backtrader'
 ## END HERE
 for i in range(self.p.retries + 1):  # at least once
     resp = sess.get(url, **sesskwargs)
     if resp.status_code != requests.codes.ok:
            

Here you have the original link for the yahoo.py changes .这里有 yahoo.py 更改的原始链接。

Probably backtrader will launch a upgrade soon.可能 backtrader 很快就会推出升级。

For Pandas and Pandas-DataReader对于 Pandas 和 Pandas-DataReader

pip install --upgrade pandas
pip install --upgrade pandas-datareader

Have a nice day ;).祝你今天过得愉快 ;)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM