1
import yfinance as yf

tickers = yf.Tickers('msft aapl goog')
# ^ returns a named tuple of Ticker objects

# access each ticker using (example)
tickers.msft.info
tickers.aapl.history(period="1mo")
tickers.goog.actions
  • 该代码导致以下错误
tickers.aapl.history(period="1mo")

# Error:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-3111e4668e75> in <module>
----> 1 tickers.aapl.history(period="1mo")

AttributeError: 'Tickers' object has no attribute 'aapl'

tickers.goog.actions

# Error:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-47f3d4a48536> in <module>
----> 1 tickers.goog.actions

AttributeError: 'Tickers' object has no attribute 'goog'
4

2 回答 2

2

工作代码

  • 以下代码产生预期的输出
import yfinance as yf

tickers = yf.Tickers('msft aapl goog')
print(tickers.tickers.GOOG.actions)
print(tickers.tickers.AAPL.history(period="1mo"))
print(tickers.tickers.MSFT.info)

tickers.tickers.GOOG.actions

            Dividends  Stock Splits
Date                               
2014-03-27        0.0         2.002
2015-04-27        0.0         1.000

tickers.tickers.AAPL.history(period="1mo")

             Open    High     Low   Close    Volume  Dividends  Stock Splits
Date                                                                         
2020-07-06  370.00  375.78  369.87  373.85  29663900          0             0
2020-07-07  375.41  378.62  372.23  372.69  28106100          0             0
2020-07-08  376.72  381.50  376.36  381.37  29273000          0             0
2020-07-09  385.05  385.27  378.69  383.01  31410700          0             0
2020-07-10  381.34  383.92  378.82  383.68  22564300          0             0
2020-07-13  389.06  399.82  381.03  381.91  47912300          0             0
2020-07-14  379.36  389.02  375.51  388.23  42747300          0             0
2020-07-15  395.96  396.99  385.96  390.90  38299500          0             0
2020-07-16  386.25  389.62  383.62  386.09  27644400          0             0
2020-07-17  387.95  388.59  383.36  385.31  23046700          0             0
2020-07-20  385.67  394.00  384.25  393.43  22579500          0             0
2020-07-21  396.69  397.00  386.97  388.00  25911500          0             0
2020-07-22  386.77  391.90  386.41  389.09  22250400          0             0
2020-07-23  387.99  388.31  368.04  371.38  49251100          0             0
2020-07-24  363.95  371.88  356.58  370.46  46359700          0             0
2020-07-27  374.84  379.62  373.92  379.24  30303500          0             0
2020-07-28  377.47  378.20  372.99  373.01  25906400          0             0
2020-07-29  375.00  380.92  374.85  380.16  22582300          0             0
2020-07-30  376.75  385.19  375.07  384.76  39532500          0             0
2020-07-31  411.54  425.66  403.30  425.04  93584200          0             0
2020-08-03  432.80  446.55  431.57  435.75  76955100          0             0
2020-08-04  436.53  443.16  433.56  436.99  35017345          0             0

tickers.tickers.MSFT.info

{'52WeekChange': 0.55063903,
 'SandP52WeekChange': 0.14325917,
 'address1': 'One Microsoft Way',
 'algorithm': None,
 'annualHoldingsTurnover': None,
 'annualReportExpenseRatio': None,
 'ask': 211.39,
 'askSize': 1100,
 'averageDailyVolume10Day': 31499250,
 'averageVolume': 34679039,
 'averageVolume10days': 31499250,
 'beta': 0.933333,
 'beta3Year': None,
 'bid': 211.37,
 'bidSize': 900,
 'bookValue': 15.626,
 'category': None,
 'circulatingSupply': None,
 'city': 'Redmond',
 'companyOfficers': [],
 'country': 'United States',
 'currency': 'USD',
 'dateShortInterest': 1594771200,
 'dayHigh': 214.77,
 'dayLow': 210.31,
 'dividendRate': 2.04,
 'dividendYield': 0.0094,
 'earningsQuarterlyGrowth': -0.151,
 'enterpriseToEbitda': 24.277,
 'enterpriseToRevenue': 11.078,
 'enterpriseValue': 1584317595648,
 'exDividendDate': 1597795200,
 'exchange': 'NMS',
 'exchangeTimezoneName': 'America/New_York',
 'exchangeTimezoneShortName': 'EDT',
 'expireDate': None,
 'fax': '425-706-7329',
 'fiftyDayAverage': 203.61559,
 'fiftyTwoWeekHigh': 216.38,
 'fiftyTwoWeekLow': 130.78,
 'fiveYearAverageReturn': None,
 'fiveYearAvgDividendYield': 1.87,
 'floatShares': 7456408437,
 'forwardEps': 7.34,
 'forwardPE': 28.803814,
 'fromCurrency': None,
 'fullTimeEmployees': 163000,
 'fundFamily': None,
 'fundInceptionDate': None,
 'gmtOffSetMilliseconds': '-14400000',
 'heldPercentInsiders': 0.014249999000000001,
 'heldPercentInstitutions': 0.7409300000000001,
 'industry': 'Software—Infrastructure',
 'isEsgPopulated': False,
 'lastCapGain': None,
 'lastDividendValue': None,
 'lastFiscalYearEnd': 1593475200,
 'lastMarket': None,
 'lastSplitDate': 1045526400,
 'lastSplitFactor': '2:1',
 'legalType': None,
 'logo_url': 'https://logo.clearbit.com/microsoft.com',
 'longBusinessSummary': 'Microsoft Corporation develops, licenses, and '
                        'supports software, services, devices, and solutions '
                        'worldwide. Its Productivity and Business Processes '
                        'segment offers Office, Exchange, SharePoint, '
                        'Microsoft Teams, Office 365 Security and Compliance, '
                        'and Skype for Business, as well as related Client '
                        'Access Licenses (CAL); Skype, Outlook.com, and '
                        'OneDrive; LinkedIn that includes Talent and marketing '
                        'solutions, and subscriptions; and Dynamics 365, a set '
                        'of cloud-based and on-premises business solutions for '
                        'small and medium businesses, large organizations, and '
                        'divisions of enterprises. Its Intelligent Cloud '
                        'segment licenses SQL and Windows Servers, Visual '
                        'Studio, System Center, and related CALs; GitHub that '
                        'provides a collaboration platform and code hosting '
                        'service for developers; and Azure, a cloud platform. '
                        'It also provides support services and Microsoft '
                        'consulting services to assist customers in '
                        'developing, deploying, and managing Microsoft server '
                        'and desktop solutions; and training and certification '
                        'to developers and IT professionals on various '
                        'Microsoft products. Its More Personal Computing '
                        'segment offers Windows OEM licensing and other '
                        'non-volume licensing of the Windows operating system; '
                        'Windows Commercial, such as volume licensing of the '
                        'Windows operating system, Windows cloud services, and '
                        'other Windows commercial offerings; patent licensing; '
                        'Windows Internet of Things; and MSN advertising. It '
                        'also provides Microsoft Surface, PC accessories, and '
                        'other devices; Gaming, including Xbox hardware, and '
                        'Xbox software and services; video games and '
                        'third-party video game royalties; and Search, '
                        'including Bing and Microsoft advertising. It sells '
                        'its products through distributors and resellers; and '
                        'directly through digital marketplaces, online stores, '
                        'and retail stores. It has strategic partnerships with '
                        'Humana Inc., Nokia, Telkomsel, Swiss Re, Kubota '
                        'Corporation, FedEx Corp., and Hitachi. The company '
                        'was founded in 1975 and is headquartered in Redmond, '
                        'Washington.',
 'longName': 'Microsoft Corporation',
 'market': 'us_market',
 'marketCap': 1599952519168,
 'maxAge': 1,
 'maxSupply': None,
 'messageBoardId': 'finmb_21835',
 'morningStarOverallRating': None,
 'morningStarRiskRating': None,
 'mostRecentQuarter': 1593475200,
 'navPrice': None,
 'netIncomeToCommon': 44280999936,
 'nextFiscalYearEnd': 1656547200,
 'open': 214.17,
 'openInterest': None,
 'payoutRatio': 0.34550000000000003,
 'pegRatio': 2.23,
 'phone': '425-882-8080',
 'previousClose': 216.54,
 'priceHint': 2,
 'priceToBook': 13.530653,
 'priceToSalesTrailing12Months': 11.187305,
 'profitMargins': 0.30962,
 'quoteType': 'EQUITY',
 'regularMarketDayHigh': 214.77,
 'regularMarketDayLow': 210.31,
 'regularMarketOpen': 214.17,
 'regularMarketPreviousClose': 216.54,
 'regularMarketPrice': 214.17,
 'regularMarketVolume': 35978270,
 'revenueQuarterlyGrowth': None,
 'sector': 'Technology',
 'sharesOutstanding': 7567649792,
 'sharesPercentSharesOut': 0.0053,
 'sharesShort': 39894144,
 'sharesShortPreviousMonthDate': 1592179200,
 'sharesShortPriorMonth': 42930465,
 'shortName': 'Microsoft Corporation',
 'shortPercentOfFloat': 0.0053,
 'shortRatio': 1.19,
 'startDate': None,
 'state': 'WA',
 'strikePrice': None,
 'symbol': 'MSFT',
 'threeYearAverageReturn': None,
 'toCurrency': None,
 'totalAssets': None,
 'tradeable': False,
 'trailingAnnualDividendRate': 2.04,
 'trailingAnnualDividendYield': 0.009420892,
 'trailingEps': 5.76,
 'trailingPE': 36.70486,
 'twoHundredDayAverage': 179.41402,
 'volume': 35978270,
 'volume24Hr': None,
 'volumeAllCurrencies': None,
 'website': 'http://www.microsoft.com',
 'yield': None,
 'ytdReturn': None,
 'zip': '98052'}
于 2020-08-04T18:52:09.000 回答
0

对于任何想要迭代多个符号而不知道这些符号是什么的人,包括我未来的自己,这就是我想出的:

免责声明:我对 python 有点陌生,对 yFinance 也很陌生,所以也许有更好的方法。但是我所做的所有谷歌搜索并没有导致更好的方法。

    symbols = ['appl', 'goog', 'msft']
    tickers = yfinance.Tickers(','.join(symbols))
    for symbol in symbols:
        print(tickers.tickers[symbol.upper()].info)
于 2022-01-11T11:46:45.000 回答