使用mplFinance创建图形时出错

问题描述

我不确定我的代码无法工作的原因。我已经使用相同的代码好几个月了,突然收到一个奇怪的错误。不太确定如何排除故障。我正在阅读的EXCEL表格基本上是从第一列往下排列的报价器名称列表,如下所示:

VNDA
VNDA
CKPT
SNSE
CNST

我的代码:

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:usersashleyappdataoamingpythonpython38site-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:UsersAshleyAppDataLocalProgramsPythonPython39lib	hreading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:UsersAshleyAppDataLocalProgramsPythonPython39lib	hreading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:UsersAshleyAppDataLocalProgramsPythonPython39libsite-packagesmultitasking\__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "C:UsersAshleyAppDataLocalProgramsPythonPython39libsite-packagesyfinancemulti.py", line 167, in _download_one_threaded
    data = _download_one(ticker, start, end, auto_adjust, back_adjust,
  File "C:UsersAshleyAppDataLocalProgramsPythonPython39libsite-packagesyfinancemulti.py", line 179, in _download_one
    return Ticker(ticker).history(period=period, interval=interval,
  File "C:UsersAshleyAppDataLocalProgramsPythonPython39libsite-packagesyfinancease.py", line 157, in history
    data = data.json()
  File "C:UsersAshleyAppDataLocalProgramsPythonPython39libsite-packagesequestsmodels.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:UsersAshleyAppDataLocalProgramsPythonPython39libjson\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:UsersAshleyAppDataLocalProgramsPythonPython39libjsondecoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:UsersAshleyAppDataLocalProgramsPythonPython39libjsondecoder.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选项很重要。

相关文章