如何抑制 yfinance 的异常?

Posted

技术标签:

【中文标题】如何抑制 yfinance 的异常?【英文标题】:How to suppress exceptions from yfinance? 【发布时间】:2019-12-03 05:52:14 【问题描述】:
import yfinance as yf

try:
    data = yf.download(tickers=all_symbols[:50], start=start, end=end, group_by="ticker")
except:
    pass

似乎应该隐藏 yfinance 抛出的所有异常,但是当找不到代码名称时,我的 Jupyter 笔记本总是会出现异常。如何让它们停止出现?

Exception in thread Thread-333:
Traceback (most recent call last):
  File "/Users/jason/anaconda3/lib/python3.7/site-packages/yfinance/__init__.py", line 313, in history
    quotes = self._parse_quotes(data["chart"]["result"][0])
  File "/Users/jason/anaconda3/lib/python3.7/site-packages/yfinance/__init__.py", line 162, in _parse_quotes
    timestamps = data["timestamp"]
KeyError: 'timestamp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jason/anaconda3/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/Users/jason/anaconda3/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/jason/anaconda3/lib/python3.7/site-packages/multitasking/__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "/Users/jason/anaconda3/lib/python3.7/site-packages/yfinance/__init__.py", line 470, in _download_one_threaded
    period, interval, prepost, proxy)
  File "/Users/jason/anaconda3/lib/python3.7/site-packages/yfinance/__init__.py", line 483, in _download_one
    proxy=proxy)
  File "/Users/jason/anaconda3/lib/python3.7/site-packages/yfinance/__init__.py", line 316, in history
    raise ValueError(self.ticker, err_msg)
ValueError: ('ACCP', 'No data found for this date range, symbol may be delisted')

【问题讨论】:

这些异常消息都不是您在顶部给出的代码行。你确定你是这么称呼它的吗? @JohnGordon 那是我的 jupyter 笔记本中的单元格。这就是牢房里的一切。我运行单元格,它将遍历前 50 个符号,如果 yfinance 包找不到任何符号,我会得到这些错误。 【参考方案1】:

这个怎么样?你能用这个吗?

# https://towardsdatascience.com/efficient-frontier-portfolio-optimisation-in-python-e7844051e7f
import pandas as pd  
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import quandl
import scipy.optimize
np.random.seed(777)


quandl.ApiConfig.api_key = 'you_resy_goes_here'
stocks = ['AAPL', 'AMZN', 'MSFT', 'SBUX']
data = quandl.get_table('WIKI/PRICES', ticker = stocks,
                        qopts =  'columns': ['date', 'ticker', 'adj_close'] ,
                        date =  'gte': '2018-1-1', 'lte': '2019-12-31' , paginate=True)
data.head()

data.info()

df = data.set_index('date')
table = df.pivot(columns='ticker')
# By specifying col[1] in below list comprehension
# You can select the stock names under multi-level column
table.columns = [col[1] for col in table.columns]
table.head()

plt.figure(figsize=(14, 7))
for c in table.columns.values:
    plt.plot(table.index, table[c], lw=3, alpha=0.8,label=c)
plt.legend(loc='upper left', fontsize=12)
plt.ylabel('price in $')


returns = table.pct_change()
plt.figure(figsize=(14, 7))
for c in returns.columns.values:
    plt.plot(returns.index, returns[c], lw=3, alpha=0.8,label=c)
plt.legend(loc='upper right', fontsize=12)
plt.ylabel('daily returns')


###################################################


def portfolio_annualised_performance(weights, mean_returns, cov_matrix):
    returns = np.sum(mean_returns*weights ) *252
    std = np.sqrt(np.dot(weights.T, np.dot(cov_matrix, weights))) * np.sqrt(252)
    return std, returns

def random_portfolios(num_portfolios, mean_returns, cov_matrix, risk_free_rate):
    results = np.zeros((3,num_portfolios))
    weights_record = []
    for i in range(num_portfolios):
        weights = np.random.random(4)
        weights /= np.sum(weights)
        weights_record.append(weights)
        portfolio_std_dev, portfolio_return = portfolio_annualised_performance(weights, mean_returns, cov_matrix)
        results[0,i] = portfolio_std_dev
        results[1,i] = portfolio_return
        results[2,i] = (portfolio_return - risk_free_rate) / portfolio_std_dev
    return results, weights_record


returns = table.pct_change()
mean_returns = returns.mean()
cov_matrix = returns.cov()
num_portfolios = 25000
risk_free_rate = 0.0178


###################################################


def display_simulated_ef_with_random(mean_returns, cov_matrix, num_portfolios, risk_free_rate):
    results, weights = random_portfolios(num_portfolios,mean_returns, cov_matrix, risk_free_rate)

    max_sharpe_idx = np.argmax(results[2])
    sdp, rp = results[0,max_sharpe_idx], results[1,max_sharpe_idx]
    max_sharpe_allocation = pd.DataFrame(weights[max_sharpe_idx],index=table.columns,columns=['allocation'])
    max_sharpe_allocation.allocation = [round(i*100,2)for i in max_sharpe_allocation.allocation]
    max_sharpe_allocation = max_sharpe_allocation.T

    min_vol_idx = np.argmin(results[0])
    sdp_min, rp_min = results[0,min_vol_idx], results[1,min_vol_idx]
    min_vol_allocation = pd.DataFrame(weights[min_vol_idx],index=table.columns,columns=['allocation'])
    min_vol_allocation.allocation = [round(i*100,2)for i in min_vol_allocation.allocation]
    min_vol_allocation = min_vol_allocation.T

    print("-")
    print("Maximum Sharpe Ratio Portfolio Allocation\n")
    print("Annualised Return:", round(rp,2))
    print("Annualised Volatility:", round(sdp,2))
    print("\n")
    print(max_sharpe_allocation)
    print("-")
    print("Minimum Volatility Portfolio Allocation\n")
    print("Annualised Return:", round(rp_min,2))
    print("Annualised Volatility:", round(sdp_min,2))
    print("\n")
    print(min_vol_allocation)

    plt.figure(figsize=(10, 7))
    plt.scatter(results[0,:],results[1,:],c=results[2,:],cmap='YlGnBu', marker='o', s=10, alpha=0.3)
    plt.colorbar()
    plt.scatter(sdp,rp,marker='*',color='r',s=500, label='Maximum Sharpe ratio')
    plt.scatter(sdp_min,rp_min,marker='*',color='g',s=500, label='Minimum volatility')
    plt.title('Simulated Portfolio Optimization based on Efficient Frontier')
    plt.xlabel('annualised volatility')
    plt.ylabel('annualised returns')
    plt.legend(labelspacing=0.8)


display_simulated_ef_with_random(mean_returns, cov_matrix, num_portfolios, risk_free_rate)

【讨论】:

【参考方案2】:

异常位于不同的线程中,这就是为什么您没有捕获和抑制它们的原因。这也是剩余结果继续正常下载的原因。

您的主要问题是您有错误的数据,修复这些错误数据将解决您的问题。 ACCP 还没有 IPO,它可能永远不会像它那样工作至少 3 年。

你有几个选择:

查找要使用的更准确的股票代码来源。 以threads=False 运行或一次运行一个,当遇到错误时会停止,然后删除错误符号并继续。这会减慢进程,因为它不是多线程的。 在第一次运行时忽略错误,然后查看数据中缺少的内容,然后删除这些符号以备将来运行。 编辑您的本地 yfinance 以处理 _download_one_thread 中的异常并抑制错误。

【讨论】:

感谢您发现多线程问题。几周后,我仍然没有发现这是我遇到的问题。【参考方案3】:

问题已在 0.1.46 中修复。所有下载完成后打印错误。

【讨论】:

以上是关于如何抑制 yfinance 的异常?的主要内容,如果未能解决你的问题,请参考以下文章

用 Python 通过雅虎财经获取股票数据

当我提出自己的异常作为响应时,如何更轻松地抑制以前的异常?

如何使用 Aspectj 捕获和抑制 Java 类抛出的异常

如何使用 yfinance 检测失败的下载

如何使用 yfinance 下载数据修复此错误

spark-shell - 如何避免抑制省略堆栈跟踪(异常)