INVESTAR / StockAnalysisInPython

456 stars 413 forks source link

안녕하세요, 7.6.3 백테스트 코드에 대한 질문이 있습니다. #91

Open whqtker opened 3 years ago

whqtker commented 3 years ago

ch07_08_Backtrader_RSI.py, ch07_09_Backtrader_RSI_SMA.py 을 실행하기 위해 책 p.361의 오류 해결 방법을 통해 yahoo.py 파일을 수정해주었습니다. 그러나 같은 오류가 발생했습니다. 좀 더 구글링을 해 보니, 이 오류가 최신 버전의 backtrader 패키지에는 고처졌다고 해서 최신 backtrader 패키지를 재설치하였습니다. 그러나 같은 오류가 여전히 발생합니다. 수정된 yahoo.py 일부와 오류 메세지 아래에 남기겠습니다. 문제 원인이 뭘까요?

--yahoo.py--- ctype = resp.headers['Content-Type']

Cover as many text types as possible for Yahoo changes

        if not ctype.startswith('text/'):
            self.error = 'Wrong content type: %s' % ctype
            continue  # HTML returned? wrong url?

--오류 메세지-- Traceback (most recent call last): File "C:\Users\성혁\source\repos\StockAnalysisInPython-master\07_Django_and_Automation\ch07_08_Backtrader_RSI.py", line 23, in cerebro.run() # ⑧ File "C:\Users\성혁\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "C:\Users\성혁\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\cerebro.py", line 1210, in runstrategies data._start() File "C:\Users\성혁\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\feed.py", line 203, in _start self.start() File "C:\Users\성혁\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\feeds\yahoo.py", line 355, in start super(YahooFinanceData, self).start() File "C:\Users\성혁\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\feeds\yahoo.py", line 94, in start super(YahooFinanceCSVData, self).start() File "C:\Users\성혁\AppData\Local\Programs\Python\Python38\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: '036570.KS'

INVESTAR commented 3 years ago

backtrader 최신 버전(1.9.76.123)을 설치해보니 제 PC에서도 동일하게 오류(FileNotFoundError)가 발생합니다.

backtrader 최신(1.9.76.123) 버전에서 feeds/yahoo.py의 코드(if not ctype.startswith('text/'):)가 수정됐음에도 불구하고 동일한 메시지가 표시되는 것으로 봐서는 또다른 에러 원인이 존재하는 것 같습니다.

현재 backtrader 구버전(1.9.74.123)을 설치한 후 if ctype not in ['text/csv', 'text/plain']:로 수정하더라도 FileNotFoundError가 발생하고 있습니다. 아무래도 해결책을 찾으려면 시간이 좀 걸릴 것으로 보입니다.

https://community.backtrader.com/topic/2363/errno-2-no-such-file-or-directory

junmarques commented 3 years ago

안녕하세요. 이렇게 해결했던 것 같습니다. yahoo.py 파일을 열어서 270번째 Line에 추가 Code를 넣어보시기 바랍니다.

    sess = requests.Session()
    sess.headers['User-Agent'] = 'backtrader' #추가 GitHub...
    for i in range(self.p.retries + 1):  # at least once

최신 버전을 받으면 반영이 되어 있을지 모르겠네요. backtrader - GitHub SourceCode 참조 모두 수고하세요!!

ghost commented 2 years ago

저는 아래 형태로 수정했더니 해결되더군요.

pip install yfinance

data = bt.feeds.PandasData(dataname=yf.download('036570.KS', '2021-06-01', '2021-10-09', auto_adjust=True))

f1772man commented 2 years ago

추가로 matplotlib 관련 에러 발생 시 아래와 같이 버전을 다운 그레이드 하세요 Backtrader 사용 시 다음과 같은 오류 발생

ImportError: cannot import name 'warnings' from 'matplotlib.dates' matplotlib 상위 버전과 호환에 버그가 있어 다운 그레이드 해야 한다.

pip uninstall matplotlib pip install matplotlib==3.2.2

pip 패키지 버전 확인

pip list

hohoho11111 commented 2 years ago

matplotlib를 다운그레이드 해야 되는군요. 감사합니다.