File size: 10,621 Bytes
caf8c54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dcd140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caf8c54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
yf_docs = """
# YFinance Documentation

## Single Ticker Usage

```python
import yfinance as yf
ticker = yf.Ticker('MSFT')
```

## Multi-Ticker Usage

### Initialization

```python
import yfinance as yf
tickers = yf.Tickers('MSFT AAPL GOOG')
```

### Download Historical Data

Get historical market data for multiple tickers:

```python
# Method 1: Using Tickers object
tickers = yf.Tickers('MSFT AAPL GOOG')
data = tickers.download(period='1mo')

# Method 2: Using download function directly
data = yf.download(['MSFT', 'AAPL', 'GOOG'], period='1mo')
```

Returns:
```python

DataFrame with columns: [('Close', 'AAPL'), ('Close', 'GOOG'), ('Close', 'MSFT'), ('Dividends', 'AAPL'), ('Dividends', 'GOOG'), ('Dividends', 'MSFT'), ('High', 'AAPL'), ('High', 'GOOG'), ('High', 'MSFT'), ('Low', 'AAPL'), ('Low', 'GOOG'), ('Low', 'MSFT'), ('Open', 'AAPL'), ('Open', 'GOOG'), ('Open', 'MSFT'), ('Stock Splits', 'AAPL'), ('Stock Splits', 'GOOG'), ('Stock Splits', 'MSFT'), ('Volume', 'AAPL'), ('Volume', 'GOOG'), ('Volume', 'MSFT')]
Shape: (21, 21)

```

### News

Get news for multiple tickers:

```python
tickers = yf.Tickers('MSFT AAPL GOOG')
news = tickers.news()
```

Returns:
```python

- MSFT: list
- AAPL: list
- GOOG: list
```

### Accessing Individual Tickers

Access individual ticker data from a multi-ticker object:

```python
tickers = yf.Tickers('MSFT AAPL GOOG')
msft_info = tickers.tickers['MSFT'].info
aapl_history = tickers.tickers['AAPL'].history(period='1mo')
```

## Single Ticker Methods

### Info

```python
ticker.info
```

Returns:
```python

- address1: str
- city: str
- state: str
- zip: str
- country: str
- phone: str
- website: str
- industry: str
- industryKey: str
- industryDisp: str
- sector: str
- sectorKey: str
- sectorDisp: str
- longBusinessSummary: str
- fullTimeEmployees: int
- companyOfficers: list
- auditRisk: int
- boardRisk: int
- compensationRisk: int
- shareHolderRightsRisk: int
- overallRisk: int
- governanceEpochDate: int
- compensationAsOfEpochDate: int
- irWebsite: str
- maxAge: int
- priceHint: int
- previousClose: float
- open: float
- dayLow: float
- dayHigh: float
- regularMarketPreviousClose: float
- regularMarketOpen: float
- regularMarketDayLow: float
- regularMarketDayHigh: float
- dividendRate: float
- dividendYield: float
- exDividendDate: int
- payoutRatio: float
- fiveYearAvgDividendYield: float
- beta: float
- trailingPE: float
- forwardPE: float
- volume: int
- regularMarketVolume: int
- averageVolume: int
- averageVolume10days: int
- averageDailyVolume10Day: int
- bid: float
- ask: float
- bidSize: int
- askSize: int
- marketCap: int
- fiftyTwoWeekLow: float
- fiftyTwoWeekHigh: float
- priceToSalesTrailing12Months: float
- fiftyDayAverage: float
- twoHundredDayAverage: float
- currency: str
- enterpriseValue: int
- profitMargins: float
- floatShares: int
- sharesOutstanding: int
- sharesShort: int
- sharesShortPriorMonth: int
- sharesShortPreviousMonthDate: int
- dateShortInterest: int
- sharesPercentSharesOut: float
- heldPercentInsiders: float
- heldPercentInstitutions: float
- shortRatio: float
- shortPercentOfFloat: float
- impliedSharesOutstanding: int
- bookValue: float
- priceToBook: float
- lastFiscalYearEnd: int
- nextFiscalYearEnd: int
- mostRecentQuarter: int
- earningsQuarterlyGrowth: float
- netIncomeToCommon: int
- trailingEps: float
- forwardEps: float
- lastSplitFactor: str
- lastSplitDate: int
- enterpriseToRevenue: float
- enterpriseToEbitda: float
- 52WeekChange: float
- SandP52WeekChange: float
- lastDividendValue: float
- lastDividendDate: int
- exchange: str
- quoteType: str
- symbol: str
- underlyingSymbol: str
- shortName: str
- longName: str
- firstTradeDateEpochUtc: int
- timeZoneFullName: str
- timeZoneShortName: str
- uuid: str
- messageBoardId: str
- gmtOffSetMilliseconds: int
- currentPrice: float
- targetHighPrice: float
- targetLowPrice: float
- targetMeanPrice: float
- targetMedianPrice: float
- recommendationMean: float
- recommendationKey: str
- numberOfAnalystOpinions: int
- totalCash: int
- totalCashPerShare: float
- ebitda: int
- totalDebt: int
- quickRatio: float
- currentRatio: float
- totalRevenue: int
- debtToEquity: float
- revenuePerShare: float
- returnOnAssets: float
- returnOnEquity: float
- grossProfits: int
- freeCashflow: int
- operatingCashflow: int
- earningsGrowth: float
- revenueGrowth: float
- grossMargins: float
- ebitdaMargins: float
- operatingMargins: float
- financialCurrency: str
- trailingPegRatio: float
```

### History

```python
ticker.history(period='1mo')
```

Returns:
```python

DataFrame with columns: ['Open', 'High', 'Low', 'Close', 'Volume', 'Dividends', 'Stock Splits']
Shape: (21, 7)

```

### Financial Statements

#### Income Statement

```python
ticker.income_stmt
```

Returns:
```python

DataFrame with columns: [Timestamp('2024-06-30 00:00:00'), Timestamp('2023-06-30 00:00:00'), Timestamp('2022-06-30 00:00:00'), Timestamp('2021-06-30 00:00:00')]
Shape: (47, 4)

```

## Common Parameters

### Period Options

- `1d`: 1 day
- `5d`: 5 days
- `1mo`: 1 month
- `3mo`: 3 months
- `6mo`: 6 months
- `1y`: 1 year
- `2y`: 2 years
- `5y`: 5 years
- `10y`: 10 years
- `ytd`: Year to date
- `max`: Maximum available data

### Interval Options

- `1m`: 1 minute
- `2m`: 2 minutes
- `5m`: 5 minutes
- `15m`: 15 minutes
- `30m`: 30 minutes
- `60m`: 60 minutes
- `90m`: 90 minutes
- `1h`: 1 hour
- `1d`: 1 day
- `5d`: 5 days
- `1wk`: 1 week
- `1mo`: 1 month
- `3mo`: 3 months


####
In dealing with financial data from multiple tickers, specifically using yfinance and pandas, the process can be broken down into a few key steps: downloading the data, organizing it in a structured format, and accessing it in a way that aligns with the user's needs. Below, the answer is organized into clear, actionable segments.

Downloading Data for Multiple Tickers
Direct Download and DataFrame Creation
Single Ticker, Single DataFrame Approach:

For individual tickers, the DataFrame downloaded directly from yfinance comes with single-level column names but lacks a ticker column. By iterating over each ticker, adding a ticker column, and then combining these into a single DataFrame, a clear structure for each ticker's data is maintained.
import yfinance as yf
import pandas as pd

tickerStrings = ['AAPL', 'MSFT']
df_list = []
for ticker in tickerStrings:
    data = yf.download(ticker, group_by="Ticker", period='2d')
    data['ticker'] = ticker  # Add ticker column
    df_list.append(data)

# Combine all dataframes into a single dataframe
df = pd.concat(df_list)
df.to_csv('ticker.csv')
Condensed Single DataFrame Approach:

Achieve the same result as above with a one-liner using list comprehension, streamlining the process of fetching and combining data.
# Download 2 days of data for each ticker in tickerStrings, add a 'ticker' column for identification, and concatenate into a single DataFrame with continuous indexing.
df = pd.concat([yf.download(ticker, group_by="Ticker", period='2d').assign(ticker=ticker) for ticker in tickerStrings], ignore_index=True)
Multi-Ticker, Structured DataFrame Approach
When downloading data for multiple tickers simultaneously, yfinance groups data by ticker, resulting in a DataFrame with multi-level column headers. This structure can be reorganized for easier access.
Unstacking Column Levels:
# Define a list of ticker symbols to download
tickerStrings = ['AAPL', 'MSFT']

# Download 2 days of data for each ticker, grouping by 'Ticker' to structure the DataFrame with multi-level columns
df = yf.download(tickerStrings, group_by='Ticker', period='2d')

# Transform the DataFrame: stack the ticker symbols to create a multi-index (Date, Ticker), then reset the 'Ticker' level to turn it into a column
df = df.stack(level=0).rename_axis(['Date', 'Ticker']).reset_index(level=1)
Handling CSV Files with Multi-Level Column Names
To read a CSV file that has been saved with yfinance data (which often includes multi-level column headers), adjustments are necessary to ensure the DataFrame is accessible in the desired format.

Reading and Adjusting Multi-Level Columns:
# Read the CSV file. The file has multi-level headers, hence header=[0, 1].
df = pd.read_csv('test.csv', header=[0, 1])

# Drop the first row as it contains only the Date information in one column, which is redundant after setting the index.
df.drop(index=0, inplace=True)

# Convert the 'Unnamed: 0_level_0', 'Unnamed: 0_level_1' column (which represents dates) to datetime format.
# This assumes the dates are in the 'YYYY-MM-DD' format.
df[('Unnamed: 0_level_0', 'Unnamed: 0_level_1')] = pd.to_datetime(df[('Unnamed: 0_level_0', 'Unnamed: 0_level_1')])

# Set the datetime column as the index of the DataFrame. This makes time series analysis more straightforward.
df.set_index(('Unnamed: 0_level_0', 'Unnamed: 0_level_1'), inplace=True)

# Clear the name of the index to avoid confusion, as it previously referred to the multi-level column names.
df.index.name = None
Flattening Multi-Level Columns for Easier Access
Depending on the initial structure of the DataFrame, multi-level columns many need to be flattened to a single level, adding clarity and simplicity to the dataset.

Flattening and Reorganizing Based on Ticker Level:
For DataFrames where the ticker symbol is at the top level of the column headers:
df.stack(level=0).rename_axis(['Date', 'Ticker']).reset_index(level=1)
If the ticker symbol is at the bottom level:
df.stack(level=1).rename_axis(['Date', 'Ticker']).reset_index(level=1)
Individual Ticker File Management
For those preferring to manage each ticker's data separately, downloading and saving each ticker's data to individual files can be a straightforward approach.

Downloading and Saving Individual Ticker Data:
for ticker in tickerStrings:
    # Downloads historical market data from Yahoo Finance for the specified ticker.
    # The period ('prd') and interval ('intv') for the data are specified as string variables.
    data = yf.download(ticker, group_by="Ticker", period='prd', interval='intv')

    # Adds a new column named 'ticker' to the DataFrame. This column is filled with the ticker symbol.
    # This step is helpful for identifying the source ticker when multiple DataFrames are combined or analyzed separately.
    data['ticker'] = ticker

    # Saves the DataFrame to a CSV file. The file name is dynamically generated using the ticker symbol,
    # allowing each ticker's data to be saved in a separate file for easy access and identification.
    # For example, if the ticker symbol is 'AAPL', the file will be named 'ticker_AAPL.csv'.
    data.tocsv(f'ticker{ticker}.csv')
"""