- The
yfinance.Ticker
module describes how to initialize multipleTicker
objects with the following code.
JavaScript
x
10
10
1
import yfinance as yf
2
3
tickers = yf.Tickers('msft aapl goog')
4
# ^ returns a named tuple of Ticker objects
5
6
# access each ticker using (example)
7
tickers.msft.info
8
tickers.aapl.history(period="1mo")
9
tickers.goog.actions
10
- The code results in the following error
JavaScript
1
20
20
1
tickers.aapl.history(period="1mo")
2
3
# Error:
4
---------------------------------------------------------------------------
5
AttributeError Traceback (most recent call last)
6
<ipython-input-6-3111e4668e75> in <module>
7
----> 1 tickers.aapl.history(period="1mo")
8
9
AttributeError: 'Tickers' object has no attribute 'aapl'
10
11
tickers.goog.actions
12
13
# Error:
14
---------------------------------------------------------------------------
15
AttributeError Traceback (most recent call last)
16
<ipython-input-7-47f3d4a48536> in <module>
17
----> 1 tickers.goog.actions
18
19
AttributeError: 'Tickers' object has no attribute 'goog'
20
- How can this issue be resolved, because this is the exact code from the
yfinance
GitHub Repo?
Advertisement
Answer
- I’m pretty sure it’s a mistake in the documentation.
tickers
is the object holding multipleTicker
objects.- The README is missing the correct method call, which is also
tickers
. - Additionally, the ticker symbols data is accessed with uppercase letters as shown in the following code.
- TAB completion at the
.
will show the correct available methods - See AttributeError: ‘Tickers’ object has no attribute ‘msft’ #407 and Update README.rst #408 for future details.
Working Code
- The following code produces the expected output
JavaScript
1
7
1
import yfinance as yf
2
3
tickers = yf.Tickers('msft aapl goog')
4
print(tickers.tickers.GOOG.actions)
5
print(tickers.tickers.AAPL.history(period="1mo"))
6
print(tickers.tickers.MSFT.info)
7
tickers.tickers.GOOG.actions
JavaScript
1
5
1
Dividends Stock Splits
2
Date
3
2014-03-27 0.0 2.002
4
2015-04-27 0.0 1.000
5
tickers.tickers.AAPL.history(period="1mo")
JavaScript
1
25
25
1
Open High Low Close Volume Dividends Stock Splits
2
Date
3
2020-07-06 370.00 375.78 369.87 373.85 29663900 0 0
4
2020-07-07 375.41 378.62 372.23 372.69 28106100 0 0
5
2020-07-08 376.72 381.50 376.36 381.37 29273000 0 0
6
2020-07-09 385.05 385.27 378.69 383.01 31410700 0 0
7
2020-07-10 381.34 383.92 378.82 383.68 22564300 0 0
8
2020-07-13 389.06 399.82 381.03 381.91 47912300 0 0
9
2020-07-14 379.36 389.02 375.51 388.23 42747300 0 0
10
2020-07-15 395.96 396.99 385.96 390.90 38299500 0 0
11
2020-07-16 386.25 389.62 383.62 386.09 27644400 0 0
12
2020-07-17 387.95 388.59 383.36 385.31 23046700 0 0
13
2020-07-20 385.67 394.00 384.25 393.43 22579500 0 0
14
2020-07-21 396.69 397.00 386.97 388.00 25911500 0 0
15
2020-07-22 386.77 391.90 386.41 389.09 22250400 0 0
16
2020-07-23 387.99 388.31 368.04 371.38 49251100 0 0
17
2020-07-24 363.95 371.88 356.58 370.46 46359700 0 0
18
2020-07-27 374.84 379.62 373.92 379.24 30303500 0 0
19
2020-07-28 377.47 378.20 372.99 373.01 25906400 0 0
20
2020-07-29 375.00 380.92 374.85 380.16 22582300 0 0
21
2020-07-30 376.75 385.19 375.07 384.76 39532500 0 0
22
2020-07-31 411.54 425.66 403.30 425.04 93584200 0 0
23
2020-08-03 432.80 446.55 431.57 435.75 76955100 0 0
24
2020-08-04 436.53 443.16 433.56 436.99 35017345 0 0
25
tickers.tickers.MSFT.info
JavaScript
1
162
162
1
{'52WeekChange': 0.55063903,
2
'SandP52WeekChange': 0.14325917,
3
'address1': 'One Microsoft Way',
4
'algorithm': None,
5
'annualHoldingsTurnover': None,
6
'annualReportExpenseRatio': None,
7
'ask': 211.39,
8
'askSize': 1100,
9
'averageDailyVolume10Day': 31499250,
10
'averageVolume': 34679039,
11
'averageVolume10days': 31499250,
12
'beta': 0.933333,
13
'beta3Year': None,
14
'bid': 211.37,
15
'bidSize': 900,
16
'bookValue': 15.626,
17
'category': None,
18
'circulatingSupply': None,
19
'city': 'Redmond',
20
'companyOfficers': [],
21
'country': 'United States',
22
'currency': 'USD',
23
'dateShortInterest': 1594771200,
24
'dayHigh': 214.77,
25
'dayLow': 210.31,
26
'dividendRate': 2.04,
27
'dividendYield': 0.0094,
28
'earningsQuarterlyGrowth': -0.151,
29
'enterpriseToEbitda': 24.277,
30
'enterpriseToRevenue': 11.078,
31
'enterpriseValue': 1584317595648,
32
'exDividendDate': 1597795200,
33
'exchange': 'NMS',
34
'exchangeTimezoneName': 'America/New_York',
35
'exchangeTimezoneShortName': 'EDT',
36
'expireDate': None,
37
'fax': '425-706-7329',
38
'fiftyDayAverage': 203.61559,
39
'fiftyTwoWeekHigh': 216.38,
40
'fiftyTwoWeekLow': 130.78,
41
'fiveYearAverageReturn': None,
42
'fiveYearAvgDividendYield': 1.87,
43
'floatShares': 7456408437,
44
'forwardEps': 7.34,
45
'forwardPE': 28.803814,
46
'fromCurrency': None,
47
'fullTimeEmployees': 163000,
48
'fundFamily': None,
49
'fundInceptionDate': None,
50
'gmtOffSetMilliseconds': '-14400000',
51
'heldPercentInsiders': 0.014249999000000001,
52
'heldPercentInstitutions': 0.7409300000000001,
53
'industry': 'Software—Infrastructure',
54
'isEsgPopulated': False,
55
'lastCapGain': None,
56
'lastDividendValue': None,
57
'lastFiscalYearEnd': 1593475200,
58
'lastMarket': None,
59
'lastSplitDate': 1045526400,
60
'lastSplitFactor': '2:1',
61
'legalType': None,
62
'logo_url': 'https://logo.clearbit.com/microsoft.com',
63
'longBusinessSummary': 'Microsoft Corporation develops, licenses, and '
64
'supports software, services, devices, and solutions '
65
'worldwide. Its Productivity and Business Processes '
66
'segment offers Office, Exchange, SharePoint, '
67
'Microsoft Teams, Office 365 Security and Compliance, '
68
'and Skype for Business, as well as related Client '
69
'Access Licenses (CAL); Skype, Outlook.com, and '
70
'OneDrive; LinkedIn that includes Talent and marketing '
71
'solutions, and subscriptions; and Dynamics 365, a set '
72
'of cloud-based and on-premises business solutions for '
73
'small and medium businesses, large organizations, and '
74
'divisions of enterprises. Its Intelligent Cloud '
75
'segment licenses SQL and Windows Servers, Visual '
76
'Studio, System Center, and related CALs; GitHub that '
77
'provides a collaboration platform and code hosting '
78
'service for developers; and Azure, a cloud platform. '
79
'It also provides support services and Microsoft '
80
'consulting services to assist customers in '
81
'developing, deploying, and managing Microsoft server '
82
'and desktop solutions; and training and certification '
83
'to developers and IT professionals on various '
84
'Microsoft products. Its More Personal Computing '
85
'segment offers Windows OEM licensing and other '
86
'non-volume licensing of the Windows operating system; '
87
'Windows Commercial, such as volume licensing of the '
88
'Windows operating system, Windows cloud services, and '
89
'other Windows commercial offerings; patent licensing; '
90
'Windows Internet of Things; and MSN advertising. It '
91
'also provides Microsoft Surface, PC accessories, and '
92
'other devices; Gaming, including Xbox hardware, and '
93
'Xbox software and services; video games and '
94
'third-party video game royalties; and Search, '
95
'including Bing and Microsoft advertising. It sells '
96
'its products through distributors and resellers; and '
97
'directly through digital marketplaces, online stores, '
98
'and retail stores. It has strategic partnerships with '
99
'Humana Inc., Nokia, Telkomsel, Swiss Re, Kubota '
100
'Corporation, FedEx Corp., and Hitachi. The company '
101
'was founded in 1975 and is headquartered in Redmond, '
102
'Washington.',
103
'longName': 'Microsoft Corporation',
104
'market': 'us_market',
105
'marketCap': 1599952519168,
106
'maxAge': 1,
107
'maxSupply': None,
108
'messageBoardId': 'finmb_21835',
109
'morningStarOverallRating': None,
110
'morningStarRiskRating': None,
111
'mostRecentQuarter': 1593475200,
112
'navPrice': None,
113
'netIncomeToCommon': 44280999936,
114
'nextFiscalYearEnd': 1656547200,
115
'open': 214.17,
116
'openInterest': None,
117
'payoutRatio': 0.34550000000000003,
118
'pegRatio': 2.23,
119
'phone': '425-882-8080',
120
'previousClose': 216.54,
121
'priceHint': 2,
122
'priceToBook': 13.530653,
123
'priceToSalesTrailing12Months': 11.187305,
124
'profitMargins': 0.30962,
125
'quoteType': 'EQUITY',
126
'regularMarketDayHigh': 214.77,
127
'regularMarketDayLow': 210.31,
128
'regularMarketOpen': 214.17,
129
'regularMarketPreviousClose': 216.54,
130
'regularMarketPrice': 214.17,
131
'regularMarketVolume': 35978270,
132
'revenueQuarterlyGrowth': None,
133
'sector': 'Technology',
134
'sharesOutstanding': 7567649792,
135
'sharesPercentSharesOut': 0.0053,
136
'sharesShort': 39894144,
137
'sharesShortPreviousMonthDate': 1592179200,
138
'sharesShortPriorMonth': 42930465,
139
'shortName': 'Microsoft Corporation',
140
'shortPercentOfFloat': 0.0053,
141
'shortRatio': 1.19,
142
'startDate': None,
143
'state': 'WA',
144
'strikePrice': None,
145
'symbol': 'MSFT',
146
'threeYearAverageReturn': None,
147
'toCurrency': None,
148
'totalAssets': None,
149
'tradeable': False,
150
'trailingAnnualDividendRate': 2.04,
151
'trailingAnnualDividendYield': 0.009420892,
152
'trailingEps': 5.76,
153
'trailingPE': 36.70486,
154
'twoHundredDayAverage': 179.41402,
155
'volume': 35978270,
156
'volume24Hr': None,
157
'volumeAllCurrencies': None,
158
'website': 'http://www.microsoft.com',
159
'yield': None,
160
'ytdReturn': None,
161
'zip': '98052'}
162