[英]Python Backtrader Error: FileNotFoundError: [Errno 2] No such file or directory: 'AAPL'
I am trying to use the backtrader
package in Python 3.8 to run a backtest on AAPL
historical stock prices obtained from Yahoo Finance using backtrader
's YahooFinanceData
module.我正在尝试使用 Python 3.8 中的
backtrader
包对使用backtrader
的YahooFinanceData
模块从雅虎财经获得的AAPL
历史股票价格进行回溯测试。
Problem: The data appears to be downloaded from Yahoo Finance, but during the backtesting process, we get an error:问题:数据似乎是从雅虎财经下载的,但是在回测过程中,我们得到一个错误:
FileNotFoundError: [Errno 2] No such file or directory: 'AAPL'
FileNotFoundError: [Errno 2] 没有这样的文件或目录:'AAPL'
Any idea how we can solve this problem?知道我们如何解决这个问题吗?
System:系统:
Python Code to Reproduce Error重现错误的 Python 代码
from datetime import datetime
import backtrader as bt
class SmaSignal(bt.Signal):
param = (('period', 20), )
def __init__(self):
self.lines.signal = self.data - bt.ind.SMA(period=self.p.period)
data = bt.feeds.YahooFinanceData(dataname='AAPL',
fromdate=datetime(2018, 1, 1),
todate=datetime(2018, 12, 31))
cerebro = bt.Cerebro(stdstats=False)
cerebro.adddata(data)
cerebro.broker.setcash(1000.0)
cerebro.add_signal(bt.SIGNAL_LONG, SmaSignal)
cerebro.addobserver(bt.observers.BuySell)
cerebro.addobserver(bt.observers.Value)
print(f'Starting Portfolio Value: {cerebro.broker.getvalue():.2f}')
cerebro.run()
print(f'Final Portfolio Value: {cerebro.broker.getvalue():.2f}')
cerebro.plot(iplot=True, volume=False)
Error Stack错误堆栈
Traceback (most recent call last):
File "02.py", line 21, in <module>
cerebro.run()
File "/Users/x/opt/anaconda3/envs/test/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "/Users/x/opt/anaconda3/envs/test/lib/python3.8/site-packages/backtrader/cerebro.py", line 1210, in runstrategies
data._start()
File "/Users/x/opt/anaconda3/envs/test/lib/python3.8/site-packages/backtrader/feed.py", line 203, in _start
self.start()
File "/Users/x/opt/anaconda3/envs/test/lib/python3.8/site-packages/backtrader/feeds/yahoo.py", line 352, in start
super(YahooFinanceData, self).start()
File "/Users/x/opt/anaconda3/envs/test/lib/python3.8/site-packages/backtrader/feeds/yahoo.py", line 94, in start
super(YahooFinanceCSVData, self).start()
File "/Users/x/opt/anaconda3/envs/test/lib/python3.8/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: 'AAPL'
Yahoo Finance have changed their output recently, specifically the content type of the API response('text/csv' is expected but 'text/plain' is returned).雅虎财经最近更改了他们的输出,特别是 API 响应的内容类型(需要'text/csv',但返回'text/plain')。 To fix it you have to change to code in the file backtrader/feeds/yahoo.py
要修复它,您必须更改为文件 backtrader/feeds/yahoo.py 中的代码
In line 330, replace:在第 330 行,替换:
if 'text/csv' not in ctype:
With:和:
if ctype not in ['text/csv', 'text/plain']:
There is a pull request open with backtrader to fix this in the package, but it has not been commited yet.有一个使用 backtrader 打开的拉取请求来修复包中的这个问题,但它尚未提交。
The error is that the request to Yahoo API fails, and backtrader
then falls back to trying to read local copies of the data but in a way that also fails.错误是对 Yahoo API 的请求失败,然后
backtrader
回退到尝试读取数据的本地副本,但也失败了。 The Yahoo API changed a few years ago and backtrader
hasn't been updated since then.雅虎 API 几年前发生了变化,
backtrader
起backtrader
就没有更新过。 This is a big issue as even the tutorial example is broken!这是一个大问题,因为即使是教程示例也已损坏!
The change @mathias-thorsen mentioned in his answer was proposed to be included in backtrader
in this Pull Request , but it was closed. @mathias-thorsen 在他的回答中提到的更改被提议包含在这个 Pull Request中的
backtrader
中,但它被关闭了。 backtrader
has slowed development and a fork backtrader2
is vying to be its successor. backtrader
发展已经放缓,一个分支backtrader2
正在争相成为它的继任者。 backtrader2
found that a difference fix resolved the root cause of the Yahoo API request failing. backtrader2
发现差异修复解决了雅虎 API 请求失败的根本原因。 They proposed the same fix into the upstream backtrader
in this Pull Request and it was recently merged.他们在这个 Pull Request 中提出了对上游
backtrader
的相同修复,并且它最近被合并。 With the creator gone though, there's no way to publish a new release to PyPI.但是,随着创建者的离开,无法向 PyPI 发布新版本。
backtrader2
also cannot yet take over the PyPI namespace for backtrader
( https://pypi.org/project/backtrader/ ). backtrader2
还不能接管backtrader
( https://pypi.org/project/backtrader/ ) 的 PyPI 命名空间。
So how do you install the fixed version of backtrader
or the more updated backtrader2
if they aren't published on PyPI?那么如果没有在 PyPI 上发布,你如何安装固定版本的
backtrader
或更新的backtrader2
呢? You could install the latest development version from GitHub using either您可以使用以下任一方法从 GitHub 安装最新的开发版本
pip install git+https://github.com/mementum/backtrader.git#egg=backtrader
pip install git+https://github.com/backtrader2/backtrader.git#egg=backtrader
There's also an issue when backtrader
uses of the latest matplotlib
, where the fix has been merged, but with no release.当
backtrader
使用最新的matplotlib
时也存在一个问题,其中修复程序已合并,但没有发布。 You can find similar approaches to install from source ( https://stackoverflow.com/a/66871735/6068036 ).您可以找到类似的从源代码安装的方法 ( https://stackoverflow.com/a/66871735/6068036 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.