非IT企業に勤める中年サラリーマンのIT日記

非IT企業でしかもITとは全く関係ない部署にいる中年エンジニア。唯一の趣味がプログラミングという”自称”プログラマー。

【Python】yfinanceで株価データや財務データを取得する

   

yfinanceとはPythonで株価情報や財務諸表などの株式関連データを取得できるライブラリです。今回は導入から簡単なプログラムを実行するところまで紹介します。

まずyfinanceのインストールは以下の通り。

pip install yfinance

 

以下のコードでアップル【AAPL】の株式情報を取得できます。

import yfinance as yf # type: ignore

appl = yf.Ticker("AAPL") 
print(appl.info)

 

実行すると以下の通りJSON形式で一挙に情報が表示されます。

文字に起こしたのがこちら。

$ python yfinanceTest.py
{'address1': 'One Apple Park Way', 'city': 'Cupertino', 'state': 'CA', 'zip': '95014', 'country': 'United States', 'phone': '408 996 1010',
'website': 'https://www.apple.com', 'industry': 'Consumer Electronics', 'industryKey': 'consumer-electronics', 'industryDisp': 'Consumer
Electronics', 'sector': 'Technology', 'sectorKey': 'technology', 'sectorDisp': 'Technology', 'longBusinessSummary': 'Apple Inc. designs,
manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide. 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, and HomePod. It also provides AppleCare support and cloud 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
Fitness+, a personalized fitness 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 distributes 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 1976 and is headquartered in Cupertino,
California.', 'fullTimeEmployees': 161000, 'companyOfficers': [{'maxAge': 1, 'name': 'Mr. Timothy D. Cook', 'age': 62, 'title': 'CEO &
Director', 'yearBorn': 1961, 'fiscalYear': 2023, 'totalPay': 16239562, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name':
'Mr. Luca Maestri', 'age': 60, 'title': 'CFO & Senior VP', 'yearBorn': 1963, 'fiscalYear': 2023, 'totalPay': 4612242,
'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Jeffrey E. Williams', 'age': 59, 'title': 'Chief Operating
Officer', 'yearBorn': 1964, 'fiscalYear': 2023, 'totalPay': 4637585, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name':
'Ms. Katherine L. Adams', 'age': 59, 'title': 'Senior VP, General Counsel & Secretary', 'yearBorn': 1964, 'fiscalYear': 2023,
'totalPay': 4618064, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': "Ms. Deirdre O'Brien", 'age': 56, 'title': 'Senior
Vice President of Retail', 'yearBorn': 1967, 'fiscalYear': 2023, 'totalPay': 4613369, 'exercisedValue': 0, 'unexercisedValue': 0},
{'maxAge': 1, 'name': 'Mr. Chris Kondo', 'title': 'Senior Director of Corporate Accounting', 'fiscalYear': 2023, 'exercisedValue': 0,
'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. James Wilson', 'title': 'Chief Technology Officer', 'fiscalYear': 2023,
'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Suhasini Chandramouli', 'title': 'Director of Investor Relations',
'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Greg Joswiak', 'title': 'Senior Vice President
of Worldwide Marketing', 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Adrian Perica',
'age': 49, 'title': 'Head of Corporate Development', 'yearBorn': 1974, 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}],
'auditRisk': 5, 'boardRisk': 1, 'compensationRisk': 2, 'shareHolderRightsRisk': 1, 'overallRisk': 1, 'governanceEpochDate': 1711929600,
'compensationAsOfEpochDate': 1703980800, 'irWebsite': 'http://investor.apple.com/', 'maxAge': 86400, 'priceHint': 2, 'previousClose':
169.3, 'open': 173.39, 'dayLow': 173.12, 'dayHigh': 176.03, 'regularMarketPreviousClose': 169.3, 'regularMarketOpen': 173.39,
'regularMarketDayLow': 173.12, 'regularMarketDayHigh': 176.03, 'dividendRate': 0.96, 'dividendYield': 0.0057, 'exDividendDate': 1707436800,
'payoutRatio': 0.14770001, 'fiveYearAvgDividendYield': 0.74, 'beta': 1.276, 'trailingPE': 26.982893, 'forwardPE': 26.051052, 'volume':
66144936, 'regularMarketVolume': 66144936, 'averageVolume': 61565104, 'averageVolume10days': 55002860, 'averageDailyVolume10Day': 55002860,
'bid': 173.28, 'ask': 173.68, 'bidSize': 200, 'askSize': 200, 'marketCap': 2679169613824, 'fiftyTwoWeekLow': 164.08, 'fiftyTwoWeekHigh':
199.62, 'priceToSalesTrailing12Months': 6.9461446, 'fiftyDayAverage': 173.286, 'twoHundredDayAverage': 181.6597,
'trailingAnnualDividendRate': 0.95, 'trailingAnnualDividendYield': 0.0056113405, 'currency': 'USD', 'enterpriseValue': 2649250332672,
'profitMargins': 0.26163, 'floatShares': 15414548871, 'sharesOutstanding': 15441899520, 'sharesShort': 101912593, 'sharesShortPriorMonth':
110863064, 'sharesShortPreviousMonthDate': 1710460800, 'dateShortInterest': 1713139200, 'sharesPercentSharesOut': 0.0066000004,
'heldPercentInsiders': 0.059359998, 'heldPercentInstitutions': 0.56646997, 'shortRatio': 1.58, 'shortPercentOfFloat': 0.0066000004,
'impliedSharesOutstanding': 15441899520, 'bookValue': 4.793, 'priceToBook': 36.19862, 'lastFiscalYearEnd': 1696032000, 'nextFiscalYearEnd':
1727654400, 'mostRecentQuarter': 1703894400, 'earningsQuarterlyGrowth': 0.131, 'netIncomeToCommon': 100912996352, 'trailingEps': 6.43,
'forwardEps': 6.66, 'pegRatio': 2.7, 'lastSplitFactor': '4:1', 'lastSplitDate': 1598832000, 'enterpriseToRevenue': 6.869,
'enterpriseToEbitda': 20.362, '52WeekChange': -0.0017099977, 'SandP52WeekChange': 0.22363698, 'lastDividendValue': 0.24,
'lastDividendDate': 1707436800, 'exchange': 'NMS', 'quoteType': 'EQUITY', 'symbol': 'AAPL', 'underlyingSymbol': 'AAPL', 'shortName': 'Apple
Inc.', 'longName': 'Apple Inc.', 'firstTradeDateEpochUtc': 345479400, 'timeZoneFullName': 'America/New_York', 'timeZoneShortName': 'EDT',
'uuid': '8b10e4ae-9eeb-3684-921a-9ab27e4d87aa', 'messageBoardId': 'finmb_24937', 'gmtOffSetMilliseconds': -14400000, 'currentPrice': 173.5,
'targetHighPrice': 233.55, 'targetLowPrice': 147.6, 'targetMeanPrice': 186.05, 'targetMedianPrice': 186.84, 'recommendationMean': 2.2,
'recommendationKey': 'buy', 'numberOfAnalystOpinions': 39, 'totalCash': 73100001280, 'totalCashPerShare': 4.734, 'ebitda': 130108997632,
'totalDebt': 108040003584, 'quickRatio': 0.92, 'currentRatio': 1.073, 'totalRevenue': 385706000384, 'debtToEquity': 145.803,
'revenuePerShare': 24.648, 'returnOnAssets': 0.21181, 'returnOnEquity': 1.5426899, 'freeCashflow': 86563127296, 'operatingCashflow':
116433002496, 'earningsGrowth': 0.16, 'revenueGrowth': 0.021, 'grossMargins': 0.45027, 'ebitdaMargins': 0.33733, 'operatingMargins':
0.33764, 'financialCurrency': 'USD', 'trailingPegRatio': 2.084}

 

要素ごとに表示させたい場合はこんな風に記述します。

print(appl.info['bid'])

 

スポンサーリンク

 - Python