How to download historical stock data using Python and yfinance

yfinance is a Python package you can use to download free stock market data from Yahoo! Finance. Full detail and reference here. https://pypi.org/project/yfinance/

import yfinance as yf
aapl = yf.Ticker("AAPL")
aapl.info

{‘zip’: ‘95014’, ‘sector’: ‘Technology’, ‘fullTimeEmployees’: 147000, ‘longBusinessSummary’: ‘Apple Inc. designs, manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide. It also sells various related services. The company offers iPhone, a line of smartphones; Mac, a line of personal computers; iPad, a line of multi-purpose tablets; and wearables, home, and accessories comprising AirPods, Apple TV, Apple Watch, Beats products, HomePod, iPod touch, and other Apple-branded and third-party accessories. It also provides AppleCare support services; cloud services store services; and operates various platforms, including the App Store, that allow customers to discover and download applications and digital content, such as books, music, video, games, and podcasts. In addition, the company offers various services, such as Apple Arcade, a game subscription service; Apple Music, which offers users a curated listening experience with on-demand radio stations; Apple News+, a subscription news and magazine service; Apple TV+, which offers exclusive original content; Apple Card, a co-branded credit card; and Apple Pay, a cashless payment service, as well as licenses its intellectual property. The company serves consumers, and small and mid-sized businesses; and the education, enterprise, and government markets. It sells and delivers third-party applications for its products through the App Store. The company also sells its products through its retail and online stores, and direct sales force; and third-party cellular network carriers, wholesalers, retailers, and resellers. Apple Inc. was founded in 1977 and is headquartered in Cupertino, California.’, ‘city’: ‘Cupertino’, ‘phone’: ‘408-996-1010’, ‘state’: ‘CA’, ‘country’: ‘United States’, ‘companyOfficers’: [], ‘website’: ‘http://www.apple.com’, ‘maxAge’: 1, ‘address1’: ‘One Apple Park Way’, ‘industry’: ‘Consumer Electronics’, ‘previousClose’: 134.5, ‘regularMarketOpen’: 134.3, ‘twoHundredDayAverage’: 124.64511, ‘trailingAnnualDividendYield’: 0.006, ‘payoutRatio’: 0.2177, ‘volume24Hr’: None, ‘regularMarketDayHigh’: 134.66, ‘navPrice’: None, ‘averageDailyVolume10Day’: 91347614, ‘totalAssets’: None, ‘regularMarketPreviousClose’: 134.5, ‘fiftyDayAverage’: 124.581474, ‘trailingAnnualDividendRate’: 0.807, ‘open’: 134.3, ‘toCurrency’: None, ‘averageVolume10days’: 91347614, ‘expireDate’: None, ‘yield’: None, ‘algorithm’: None, ‘dividendRate’: 0.82, ‘exDividendDate’: 1612483200, ‘beta’: 1.219525, ‘circulatingSupply’: None, ‘startDate’: None, ‘regularMarketDayLow’: 133.28, ‘priceHint’: 2, ‘currency’: ‘USD’, ‘trailingPE’: 36.387306, ‘regularMarketVolume’: 84922386, ‘lastMarket’: None, ‘maxSupply’: None, ‘openInterest’: None, ‘marketCap’: 2252291440640, ‘volumeAllCurrencies’: None, ‘strikePrice’: None, ‘averageVolume’: 106319762, ‘priceToSalesTrailing12Months’: 7.657339, ‘dayLow’: 133.28, ‘ask’: 134.09, ‘ytdReturn’: None, ‘askSize’: 1800, ‘volume’: 84922386, ‘fiftyTwoWeekHigh’: 145.09, ‘forwardPE’: 28.544683, ‘fromCurrency’: None, ‘fiveYearAvgDividendYield’: 1.39, ‘fiftyTwoWeekLow’: 66.3575, ‘bid’: 134.08, ‘tradeable’: False, ‘dividendYield’: 0.0061000003, ‘bidSize’: 3000, ‘dayHigh’: 134.66, ‘exchange’: ‘NMS’, ‘shortName’: ‘Apple Inc.’, ‘longName’: ‘Apple Inc.’, ‘exchangeTimezoneName’: ‘America/New_York’, ‘exchangeTimezoneShortName’: ‘EDT’, ‘isEsgPopulated’: False, ‘gmtOffSetMilliseconds’: ‘-14400000’, ‘quoteType’: ‘EQUITY’, ‘symbol’: ‘AAPL’, ‘messageBoardId’: ‘finmb_24937’, ‘market’: ‘us_market’, ‘annualHoldingsTurnover’: None, ‘enterpriseToRevenue’: 7.777, ‘beta3Year’: None, ‘profitMargins’: 0.21735, ‘enterpriseToEbitda’: 26.862, ’52WeekChange’: 0.93781817, ‘morningStarRiskRating’: None, ‘forwardEps’: 4.7, ‘revenueQuarterlyGrowth’: None, ‘sharesOutstanding’: 16788100096, ‘fundInceptionDate’: None, ‘annualReportExpenseRatio’: None, ‘bookValue’: 3.936, ‘sharesShort’: 101484668, ‘sharesPercentSharesOut’: 0.006, ‘fundFamily’: None, ‘lastFiscalYearEnd’: 1601078400, ‘heldPercentInstitutions’: 0.59793, ‘netIncomeToCommon’: 63929999360, ‘trailingEps’: 3.687, ‘lastDividendValue’: 0.205, ‘SandP52WeekChange’: 0.48254788, ‘priceToBook’: 34.085365, ‘heldPercentInsiders’: 0.00075999997, ‘nextFiscalYearEnd’: 1664150400, ‘mostRecentQuarter’: 1608940800, ‘shortRatio’: 0.88, ‘sharesShortPreviousMonthDate’: 1614297600, ‘floatShares’: 16770636380, ‘enterpriseValue’: 2287507865600, ‘threeYearAverageReturn’: None, ‘lastSplitDate’: 1598832000, ‘lastSplitFactor’: ‘4:1’, ‘legalType’: None, ‘lastDividendDate’: 1612483200, ‘morningStarOverallRating’: None, ‘earningsQuarterlyGrowth’: 0.293, ‘dateShortInterest’: 1617148800, ‘pegRatio’: 1.84, ‘lastCapGain’: None, ‘shortPercentOfFloat’: 0.006, ‘sharesShortPriorMonth’: 100799312, ‘impliedSharesOutstanding’: None, ‘category’: None, ‘fiveYearAverageReturn’: None, ‘regularMarketPrice’: 134.16, ‘logo_url’: ‘https://logo.clearbit.com/apple.com’}

hist = aapl.history(period="max")
hist

Open High … Dividends Stock Splits
Date …
1980-12-12 0.100922 0.101361 … 0.0 0.0
1980-12-15 0.096096 0.096096 … 0.0 0.0
1980-12-16 0.089075 0.089075 … 0.0 0.0
1980-12-17 0.090830 0.091268 … 0.0 0.0
1980-12-18 0.093463 0.093902 … 0.0 0.0
… … … … … …
2021-04-12 132.520004 132.850006 … 0.0 0.0
2021-04-13 132.440002 134.660004 … 0.0 0.0
2021-04-14 134.940002 135.000000 … 0.0 0.0
2021-04-15 133.820007 135.000000 … 0.0 0.0
2021-04-16 134.300003 134.669998 … 0.0 0.0

[10171 rows x 7 columns]

Back To Top