如何使用Yahoo Finance遍历长列表

时间:2018-10-07 16:34:15

标签: list loops finance yahoo

更新-下面的问题是由于列表中的股票行情指示器符号错误所致。


下面的程序将仅遍历代码列表“ X”次,然后显示以下回溯。我尝试在“ yf”函数调用中使用可选的“ retry_count”和“ pause”参数,但这些参数无济于事。有什么想法吗?

输入:

import pandas as pd
import numpy as np
import datetime
pd.core.common.is_list_like = pd.api.types.is_list_like # temp fix
import fix_yahoo_finance as yf
from pandas_datareader import data, wb
from datetime import date

ticker_list = ['AES', 'CNP', 'COG', 'CTL', 'DLPH', 'DO', 'DRE', 'F', 'FCX', 'FITB', 'FOSL', 'FTR', 'GE', 'GPS', 'GT', 'HBAN', 'HBI', 'HCP', 'HPE', 'HPQ', 'HRB', 'HST', 'IPG', 'IVZ', 'JEF', 'JNPR', 'KEY', 'KIM', 'KMI', 'KR', 'LB', 'MAT', 'MGM', 'MRO', 'NFX', 'NI', 'NLSN', 'NWL', 'NWS', 'NWSA', 'OI', 'PBCT', 'PHM', 'RF', 'SWN', 'SYMC', 'TWTR', 'UA', 'UAA', 'WMB', 'WU', 'XRX', 'ACAD', 'ACGL', 'ACGLO', 'ACGLP', 'ACIW', 'AFSI', 'AGNC', 'AGNCB', 'AGNCN', 'AMD', 'AMRN', 'ANGI', 'ARCC', 'ARLP', 'ARRS', 'ARRY', 'AY', 'BATRA', 'BGCP', 'BILI', 'BPR', 'BPY', 'CDEV', 'CG', 'COMM', 'CRZO', 'CSFL', 'CSQ', 'CVBF', 'CY', 'CZR', 'DBX', 'EBAYL', 'ENDP', 'ENTG', 'ERIC', 'EVOP', 'EXEL', 'FEYE', 'FFBC', 'FHB', 'FITB', 'FLEX', 'FMBI', 'FNSR', 'FOLD', 'FULT', 'GDS', 'GGAL', 'GLIBP', 'GLNG', 'GNTX', 'GRFS', 'GRPN', 'GSKY', 'GT', 'HAIN', 'HALO', 'HBAN', 'HDP', 'HOMB', 'HOPE', 'HPT', 'HTHT', 'HZNP', 'IMMP', 'IMMU', 'IQ', 'IRDM', 'IRWD', 'ISBC', 'JBLU', 'JD', 'LAUR', 'LBTYA', 'LBTYK', 'LILA', 'LILAK', 'MAT', 'MDCO', 'MDRX', 'MIK', 'MLCO', 'MMYT', 'MRVL', 'MTFB', 'NAVI', 'NGHHCO', 'NGHCP', 'NTRSP', 'NUAN', 'NVCN', 'NWSA', 'NYMTN', 'NYMTO', 'NYMTP', 'ON', 'ONB', 'PAAS', 'PBCT', 'PDCO', 'PDD', 'PLLL', 'PPC', 'PS', 'PSEC', 'PTEN', 'QRTEA', 'QTT', 'ROIC', 'RRR', 'SABR', 'SBGI', 'SBRA', 'SFIX', 'SFM', 'SFNC', 'SGMS', 'SIRI', 'SLGN', 'SLM', 'SNDE', 'SNH', 'STAY', 'SYMC', 'TELL', 'TERP', 'TFSL', 'TSEM', 'TSG', 'UCBI', 'UMPQ', 'UNIT', 'VEON', 'VIAV', 'VIRT', 'VOD', 'WEN', 'WMGI', 'ZNGA']

for ticker in ticker_list:
    df = yf.download(ticker, start = date (2012, 1, 1), end = (2018, 8, 1), progress = False)
    print ('df for ' + ticker)

输出:

df for AES
df for CNP
...
df for NAVI
Traceback (most recent call last):
  File "C:\stocks\question for stack overflow 12.py", line 14, in <module>
    df = yf.download(ticker, start = date (2012, 1, 1), end = today, progress = False)
  File "C:\Users\david\Anaconda3\lib\site-packages\fix_yahoo_finance\__init__.py", line 202, in download
'Close', 'Adj Close', 'Volume']]
  File "C:\Users\david\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2679, in __getitem__
    return self._getitem_array(key)
  File "C:\Users\david\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2723, in _getitem_array
    indexer = self.loc._convert_to_indexer(key, axis=1)
  File "C:\Users\david\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1308, in _convert_to_indexer
obj, kind=self.name)
 File "C:\Users\david\Anaconda3\lib\site-packages\pandas\core\indexes\multi.py", line 1995, in _convert_listlike_indexer
_, indexer = self.reindex(keyarr, level=level)
  File "C:\Users\david\Anaconda3\lib\site-packages\pandas\core\indexes\multi.py", line 2084, in reindex
keep_order=False)
  File "C:\Users\david\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3952, in _join_level
ngroups = 1 + new_lev_labels.max()
  File "C:\Users\david\Anaconda3\lib\site-packages\numpy\core\_methods.py", line 26, in _amax
    return umr_maximum(a, axis, None, out, keepdims)

   ValueError: zero-size array to reduction operation maximum which has no identity

0 个答案:

没有答案