使用 mplfinance 创建图形时出错

Error when creating graph with mplfinance

我不确定为什么我的代码不起作用。几个月来我一直在使用相同的代码,但突然出现了一个奇怪的错误。不太确定如何解决问题所在。我正在阅读的 excel sheet 基本上是第一列下方的代码名称列表,例如:

越南国防军
越南国防军
CKPT
SNSE
中网

我的代码:

import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import xlsxwriter as xl
import numpy as np
import yfinance as yf
import pandas as pd 
import mplfinance as mpf 
import pandas_datareader 
from pandas_datareader import data as pdr
yf.pdr_override()
import numpy as np

Individualreport = "C:\Users\Ashley\FromPython.xlsx"

READ = "C:\Users\Ashley\Biotech Data Center 8.1.xlsx"

Ticklist = pd.read_excel(READ,sheet_name='Tickers', header=None)
stocks = Ticklist.values.ravel()

short_sma = 20
mid_sma = 50
long_sma = 100

writer = pd.ExcelWriter(Individualreport, engine='xlsxwriter')

for i in stocks:
    plotdata = pdr.get_data_yahoo(i, start='2020-08-01', end='2021-07-31')
    df = pd.DataFrame(plotdata)

    graph = mpf.plot(df, type = 'candlestick',figratio=(16.7,6),  
         mav=(short_sma,mid_sma,long_sma),
         volume=True, title= str(i), 
         style='default', savefig=dict(fname=''+str(i)+'.png', bbox_inches= "tight"))
    
writer.save()    

错误代码:

PS C:\users\ashley\appdata\roaming\python\python38\site-packages> & C:/Users/Ashley/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/Ashley/OneDrive/Documents/just graphs part 2.py"
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\site-packages\multitasking\__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\site-packages\yfinance\multi.py", line 167, in _download_one_threaded
    data = _download_one(ticker, start, end, auto_adjust, back_adjust,
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\site-packages\yfinance\multi.py", line 179, in _download_one
    return Ticker(ticker).history(period=period, interval=interval,
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\site-packages\yfinance\base.py", line 157, in history
    data = data.json()
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Ashley\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这应该可以解决您的问题

pip uninstall yfinance
pip uninstall pandas-datareader
pip install yfinance --upgrade --no-cache-dir
pip install pandas-datareader

请注意,--no-cache-dir 选项很重要。