{ "cells": [ { "cell_type": "markdown", "id": "93db67d4", "metadata": {}, "source": [ "## yahooquery\n", "\n", "

\n", "\n", " \n", "yahooquery is a Python api to the Yahoo! finance site.\n", " \n", "This file resides at /c/cs458/www/lectures/yquery.ipynb" ] }, { "cell_type": "markdown", "id": "dddccf83", "metadata": {}, "source": [ "## Everything you need to know about yahooquery and finance\n", "\n", "The fundamental law of finance is:\n", "\n", "> Buy low, sell high.\n", "\n", "But what do you buy? When they think of finance and investing, most people\n", "think about the Dow Jones Industrial Average, which comprises 30 large cap US stocks, traded on the New York Stock Exchange (NYSE) and the NASDAQ exchange. (A large cap stock has a market capitalization greater than $10 billion.\n", "The market cap is simply the price of a share of stock times the number of shares\n", "outstanding.)\n", "\n", "By owning a share of stock, you become an owner of the company. You have an equity stake in running the company. Major decisions by the company are ratified by a shareholder vote. If you own more than 50% of the stock in a company, you control the company. Stock is also known as equity which is just one asset class\n", "in which you might choose to invest. Others include bonds (aka, fixed income or debt), \n", "real estate, commodities (e.g., wheat, oil, gold), foreign exchance (currency, like dollars, euros, and yen), derivatives (such as mortgage back securities), and infrastructure (such as roads and airports).\n", "\n", "We are going to focus on equity, that is, stocks. The yfinance Python module provides a\n", "boatload of information about US stocks and other securities.\n", "\n" ] }, { "cell_type": "markdown", "id": "2b4aa50a", "metadata": {}, "source": [ "## Risk Management\n", "\n", "As we learned from Tversky and Kahnemann and behavioral economists, people are risk\n", "averse. They don't like losing money. In 1952, Harry Markowitz came up with \n", "Modern Portfolio Theory which, among other things, \n", "showed how you can construct a collection or portfolio of stocks to reduce or even\n", "minimize the risk of the overall investment. One of Markowitz's insights was that \n", "the volatility of a stock's price was a suitable measure of the risk of the investment. You can reduce the overall volatility of a portfolio \n", "by investing stocks that are not correlated with each other. Thus, one stock may\n", "go up with inflation and another might go down. These movements can offset each other\n", "and reduce the portfolio's volatility or risk.\n", "\n", "You can fully diversify your portfolio by holding 30 or so stocks - assuming that they\n", "are not correlated. One hundred years ago, investors thought they were diversified if they owned 20 different railroad stocks. Go figure.\n", "\n", "These days we measure volatility of the entire stock market with the volatility index or VIX. It is also\n", "known as the fear index or fear gauge. The higher the VIX, the riskier the market. A rule of thumb is if the VIX is below 14, the market is not risky.\n", "\n" ] }, { "cell_type": "markdown", "id": "fc239b01", "metadata": {}, "source": [ "## Portfolio Management: Growth and Value\n", "\n", "\n", "We assume that everyone wants to avoid losing money. However, there are many different\n", "ways to make money. Many stocks pay dividends (usually quarterly) which can provide \n", "income to the investor. Usually companies that pay dividends are older and more stable\n", "and established, such as banks and utilities. These stocks are often called value stocks.\n", "\n", "Many stocks do not pay dividends. Instead they reinvest that money in the company to \n", "help it grow and expand. These stocks are often called growth stocks.\n", "\n", "Usually, growth stocks are more volatile than value stocks, which is to say that they are more risky.\n", "\n", "These categories are not always clear cut.\n", "\n", "Another metric that relates to MPT, is beta, a measure of a stock's volatility relative to the market as a whole. A beta of 1 means that the stock has the same volatility \n", "as the entire market (usually the S and P 500). A stable stock will have a beta below 1. A\n", "risky stock will have a beta over 1." ] }, { "cell_type": "markdown", "id": "096203ac", "metadata": {}, "source": [ "## Portfolio Management: Sector\n", "\n", "To diversify the risk in a portfolio, you need to buy stocks with different volatility patterns. One common way to do this is by investing in different sectors or\n", "industries.\n", "\n", "Different sectors respond differently to market conditions. In recent years, technology has outperformed sectors like materials or real estate. However, there is no \n", "clear pattern over time.\n" ] }, { "cell_type": "markdown", "id": "d2639468", "metadata": {}, "source": [ "## Security Analysis\n", "\n", "Many Wall Street firms, like Morgan Stanley, Goldman Sachs, and Merrill Lynch, \n", "employ finance professionals who advise investors which stocks to buy and sell. For \n", "example, see a recent Merrill Lynch report on Apple (AAPL). An analysis will usually assign a recommendation of buy, sell, or hold, as\n", "well as a price target or price objective - their prediction of the price within \n", "the next 12 months. Merrill Lynch assigns Apple a BUY rating and a price objective of 325 from the current price of 259 (as of the date of the report). This is pretty bullish. Note that Merrill Lynch also reports an ESGMeter score of high for Apple. ESG stands for Environmant, Social, and Governance which measures a company's commitment to climate change, diversity and related initiatives. The hope is that a company can do well by doing good.\n", "According to Merrill Lynch, Apple is one of the good guys.\n", "\n", "One focus of a security analyst is to predict future earnings. This figure becomes\n", "part of price/earnings ratio or simply p/e ratio. A low PE suggest that the stock is cheap. A high PE suggests that the stock is expensive. \n", "However, the PE ratio varies considerably across industries.\n", "\n", "Also, there are quantitative models that can value a stock based on future earnings or \n", "discounted cash\n", " flows (DCF). This approach can be used on other assets in addition to stock. A security analyst can project company earnings and then plug those numbers into a DCF model to get a valuation for the company. Using earnings per share, the valuation \n", " is the estimated current price of the company. If that price is higher than the market price, then the stock is a BUY. If the valuation price is lower than the market price, the stock is a SELL.\n", "\n", "In the homework, you are to write some code that could replace a security analyst." ] }, { "cell_type": "markdown", "id": "177c3287", "metadata": {}, "source": [ "## Model Portfolios\n", "\n", "In addition to analyzing companies, Merrill Lynch also provides sample model portfolios, namely:\n", "\n", "- Large Cap Defensive\n", "- Income\n", "- Income and Growth\n", "- Growth\n", "- Mid-Cap\n", "- International\n", "\n", "Each of these portfolios comprise 30 or so individual stocks across a dozen\n", "industrial sectors. The portfolio specifies the weightings of each holding.\n", "Every few weeks or months, Merrill Lynch will publish changes to weightings or\n", "holdings to recalibrate the portfolio. See the \n", "Research Portfolio Holdings and Primer.\n", "\n", "Merrill Lynch publishes a primer which explains the investment philosophy and \n", "process used to construct these portfolios. The basic premise is that \n", "each portfolio has its own risk profile, with Large Cap Defensive being safer\n", "than Income, and so forth.\n", "\n", "As a machine learning exercise, you could use the holdings of these portfolios \n", "as training data to learn the properties of an income stock versus a growth stock." ] }, { "cell_type": "markdown", "id": "d1f42ae2", "metadata": {}, "source": [ "## Let's go to Python already\n", "\n", "\n", "We get started by loading yfinance and some other useful modules. These are available on the zoo. If you do not have them on you personal machine, you can probably install them with\n", "\n", "> pip install\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "434fdce3", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import yahooquery as yq" ] }, { "cell_type": "code", "execution_count": 2, "id": "d648a760", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.4.1'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "yq.__version__" ] }, { "cell_type": "markdown", "id": "112f63be", "metadata": {}, "source": [ "The documentation for yahooquery is at https://yahooquery.dpguthrie.com/" ] }, { "cell_type": "code", "execution_count": 3, "id": "f06d5333", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.4.1'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy\n", "numpy.version.version" ] }, { "cell_type": "code", "execution_count": 4, "id": "b62866a3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Research',\n", " 'Screener',\n", " 'Ticker',\n", " '__all__',\n", " '__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__path__',\n", " '__spec__',\n", " '__version__',\n", " 'base',\n", " 'constants',\n", " 'get_currencies',\n", " 'get_exchanges',\n", " 'get_market_summary',\n", " 'get_trending',\n", " 'headless',\n", " 'misc',\n", " 'name',\n", " 'research',\n", " 'screener',\n", " 'search',\n", " 'session_management',\n", " 'ticker',\n", " 'utils']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(yq)" ] }, { "cell_type": "code", "execution_count": 5, "id": "434f81e1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Research \t \n", "Screener \t \n", "Ticker \t \n", "base \t \n", "constants \t \n", "get_currencies \t \n", "get_exchanges \t \n", "get_market_summary \t \n", "get_trending \t \n", "headless \t \n", "misc \t \n", "name \t yahooquery\n", "research \t \n", "screener \t \n", "search \t \n", "session_management \t \n", "ticker \t \n", "utils \t \n" ] } ], "source": [ "for x in dir(yq):\n", " if x.startswith('_'): \n", " next\n", " else:\n", " print (x, '\\t', getattr(yq,x))\n", " " ] }, { "cell_type": "code", "execution_count": 6, "id": "049499f7", "metadata": {}, "outputs": [], "source": [ "from yahooquery import Ticker" ] }, { "cell_type": "code", "execution_count": 7, "id": "b89c2b23", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['CHUNK',\n", " '__annotations__',\n", " '__class__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getstate__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " '__weakref__',\n", " '_adjust_ohlc',\n", " '_async_requests',\n", " '_chunk_symbols',\n", " '_construct_data',\n", " '_construct_params',\n", " '_construct_urls',\n", " '_financials',\n", " '_financials_dataframes',\n", " '_format_data',\n", " '_fund_holdings',\n", " '_get_data',\n", " '_get_symbol',\n", " '_historical_data_to_dataframe',\n", " '_history_1m',\n", " '_option_dataframe',\n", " '_quote_summary',\n", " '_quote_summary_dataframe',\n", " '_sync_requests',\n", " '_to_dataframe',\n", " '_validate_response',\n", " 'all_financial_data',\n", " 'all_modules',\n", " 'asset_profile',\n", " 'balance_sheet',\n", " 'calendar_events',\n", " 'cash_flow',\n", " 'company_officers',\n", " 'corporate_events',\n", " 'corporate_guidance',\n", " 'country',\n", " 'default_query_params',\n", " 'dividend_history',\n", " 'earning_history',\n", " 'earnings',\n", " 'earnings_trend',\n", " 'esg_scores',\n", " 'financial_data',\n", " 'fund_bond_holdings',\n", " 'fund_bond_ratings',\n", " 'fund_category_holdings',\n", " 'fund_equity_holdings',\n", " 'fund_holding_info',\n", " 'fund_ownership',\n", " 'fund_performance',\n", " 'fund_profile',\n", " 'fund_sector_weightings',\n", " 'fund_top_holdings',\n", " 'get_financial_data',\n", " 'get_modules',\n", " 'grading_history',\n", " 'history',\n", " 'income_statement',\n", " 'index_trend',\n", " 'industry_trend',\n", " 'insider_holders',\n", " 'insider_transactions',\n", " 'institution_ownership',\n", " 'key_stats',\n", " 'login',\n", " 'major_holders',\n", " 'news',\n", " 'option_chain',\n", " 'p_all_financial_data',\n", " 'p_balance_sheet',\n", " 'p_cash_flow',\n", " 'p_company_360',\n", " 'p_corporate_events',\n", " 'p_fair_value',\n", " 'p_get_financial_data',\n", " 'p_ideas',\n", " 'p_income_statement',\n", " 'p_portal',\n", " 'p_reports',\n", " 'p_technical_events',\n", " 'p_technical_insights',\n", " 'p_valuation_measures',\n", " 'p_value_analyzer',\n", " 'p_value_analyzer_drilldown',\n", " 'page_views',\n", " 'price',\n", " 'quote_type',\n", " 'quotes',\n", " 'recommendation_trend',\n", " 'recommendations',\n", " 'sec_filings',\n", " 'share_purchase_activity',\n", " 'summary_detail',\n", " 'summary_profile',\n", " 'symbols',\n", " 'technical_insights',\n", " 'validate_symbols',\n", " 'valuation_measures']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(Ticker)" ] }, { "cell_type": "code", "execution_count": 8, "id": "5aa28083", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CHUNK \t 1500\n", "all_financial_data \t \n", "all_modules \t \n", "asset_profile \t \n", "balance_sheet \t \n", "calendar_events \t \n", "cash_flow \t \n", "company_officers \t \n", "corporate_events \t \n", "corporate_guidance \t \n", "country \t \n", "default_query_params \t \n", "dividend_history \t \n", "earning_history \t \n", "earnings \t \n", "earnings_trend \t \n", "esg_scores \t \n", "financial_data \t \n", "fund_bond_holdings \t \n", "fund_bond_ratings \t \n", "fund_category_holdings \t \n", "fund_equity_holdings \t \n", "fund_holding_info \t \n", "fund_ownership \t \n", "fund_performance \t \n", "fund_profile \t \n", "fund_sector_weightings \t \n", "fund_top_holdings \t \n", "get_financial_data \t \n", "get_modules \t \n", "grading_history \t \n", "history \t \n", "income_statement \t \n", "index_trend \t \n", "industry_trend \t \n", "insider_holders \t \n", "insider_transactions \t \n", "institution_ownership \t \n", "key_stats \t \n", "login \t \n", "major_holders \t \n", "news \t \n", "option_chain \t \n", "p_all_financial_data \t \n", "p_balance_sheet \t \n", "p_cash_flow \t \n", "p_company_360 \t \n", "p_corporate_events \t \n", "p_fair_value \t \n", "p_get_financial_data \t \n", "p_ideas \t \n", "p_income_statement \t \n", "p_portal \t \n", "p_reports \t \n", "p_technical_events \t \n", "p_technical_insights \t \n", "p_valuation_measures \t \n", "p_value_analyzer \t \n", "p_value_analyzer_drilldown \t \n", "page_views \t \n", "price \t \n", "quote_type \t \n", "quotes \t \n", "recommendation_trend \t \n", "recommendations \t \n", "sec_filings \t \n", "share_purchase_activity \t \n", "summary_detail \t \n", "summary_profile \t \n", "symbols \t \n", "technical_insights \t \n", "validate_symbols \t \n", "valuation_measures \t \n" ] } ], "source": [ "for x in dir(Ticker):\n", " if x.startswith('_'):\n", " next\n", " else:\n", " print (x, '\\t', getattr(Ticker, x))" ] }, { "cell_type": "code", "execution_count": null, "id": "a41b0149", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "1f8b1310", "metadata": {}, "source": [ "## load appl Ticker" ] }, { "cell_type": "code", "execution_count": 9, "id": "71f4d42b", "metadata": {}, "outputs": [], "source": [ "aapl = Ticker('aapl')" ] }, { "cell_type": "code", "execution_count": 10, "id": "adceaa25", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['CHUNK',\n", " '__annotations__',\n", " '__class__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getstate__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " '__weakref__',\n", " '_adjust_ohlc',\n", " '_async_requests',\n", " '_chunk_symbols',\n", " '_construct_data',\n", " '_construct_params',\n", " '_construct_urls',\n", " '_country',\n", " '_country_params',\n", " '_financials',\n", " '_financials_dataframes',\n", " '_format_data',\n", " '_fund_holdings',\n", " '_get_data',\n", " '_get_symbol',\n", " '_historical_data_to_dataframe',\n", " '_history_1m',\n", " '_option_dataframe',\n", " '_quote_summary',\n", " '_quote_summary_dataframe',\n", " '_setup_url',\n", " '_symbols',\n", " '_sync_requests',\n", " '_to_dataframe',\n", " '_validate_response',\n", " 'all_financial_data',\n", " 'all_modules',\n", " 'asset_profile',\n", " 'balance_sheet',\n", " 'calendar_events',\n", " 'cash_flow',\n", " 'company_officers',\n", " 'corporate_events',\n", " 'corporate_guidance',\n", " 'country',\n", " 'crumb',\n", " 'default_query_params',\n", " 'dividend_history',\n", " 'earning_history',\n", " 'earnings',\n", " 'earnings_trend',\n", " 'esg_scores',\n", " 'financial_data',\n", " 'formatted',\n", " 'fund_bond_holdings',\n", " 'fund_bond_ratings',\n", " 'fund_category_holdings',\n", " 'fund_equity_holdings',\n", " 'fund_holding_info',\n", " 'fund_ownership',\n", " 'fund_performance',\n", " 'fund_profile',\n", " 'fund_sector_weightings',\n", " 'fund_top_holdings',\n", " 'get_financial_data',\n", " 'get_modules',\n", " 'grading_history',\n", " 'history',\n", " 'income_statement',\n", " 'index_trend',\n", " 'industry_trend',\n", " 'insider_holders',\n", " 'insider_transactions',\n", " 'institution_ownership',\n", " 'invalid_symbols',\n", " 'key_stats',\n", " 'login',\n", " 'major_holders',\n", " 'news',\n", " 'option_chain',\n", " 'p_all_financial_data',\n", " 'p_balance_sheet',\n", " 'p_cash_flow',\n", " 'p_company_360',\n", " 'p_corporate_events',\n", " 'p_fair_value',\n", " 'p_get_financial_data',\n", " 'p_ideas',\n", " 'p_income_statement',\n", " 'p_portal',\n", " 'p_reports',\n", " 'p_technical_events',\n", " 'p_technical_insights',\n", " 'p_valuation_measures',\n", " 'p_value_analyzer',\n", " 'p_value_analyzer_drilldown',\n", " 'page_views',\n", " 'password',\n", " 'price',\n", " 'progress',\n", " 'quote_type',\n", " 'quotes',\n", " 'recommendation_trend',\n", " 'recommendations',\n", " 'sec_filings',\n", " 'session',\n", " 'share_purchase_activity',\n", " 'summary_detail',\n", " 'summary_profile',\n", " 'symbols',\n", " 'technical_insights',\n", " 'username',\n", " 'validate_symbols',\n", " 'valuation_measures']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(aapl)" ] }, { "cell_type": "code", "execution_count": 11, "id": "b284ee4f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'aapl': {'maxAge': 1,\n", " 'priceHint': 2,\n", " 'previousClose': 269.48,\n", " 'open': 272.31,\n", " 'dayLow': 272.285,\n", " 'dayHigh': 278.81,\n", " 'regularMarketPreviousClose': 269.48,\n", " 'regularMarketOpen': 272.31,\n", " 'regularMarketDayLow': 272.285,\n", " 'regularMarketDayHigh': 278.81,\n", " 'dividendRate': 1.04,\n", " 'dividendYield': 0.0039,\n", " 'exDividendDate': '2026-02-08 19:00:00',\n", " 'payoutRatio': 0.1304,\n", " 'fiveYearAvgDividendYield': 0.52,\n", " 'beta': 1.107,\n", " 'trailingPE': 34.92152,\n", " 'forwardPE': 29.734087,\n", " 'volume': 31333837,\n", " 'regularMarketVolume': 31333837,\n", " 'averageVolume': 47227998,\n", " 'averageVolume10days': 58089390,\n", " 'averageDailyVolume10Day': 58089390,\n", " 'bid': 275.8,\n", " 'ask': 276.06,\n", " 'bidSize': 100,\n", " 'askSize': 100,\n", " 'marketCap': 4054863839232,\n", " 'fiftyTwoWeekLow': 169.21,\n", " 'fiftyTwoWeekHigh': 288.62,\n", " 'allTimeHigh': 288.62,\n", " 'allTimeLow': 0.049107,\n", " 'priceToSalesTrailing12Months': 9.308323,\n", " 'fiftyDayAverage': 268.365,\n", " 'twoHundredDayAverage': 237.36736,\n", " 'trailingAnnualDividendRate': 1.03,\n", " 'trailingAnnualDividendYield': 0.0038221758,\n", " 'currency': 'USD',\n", " 'fromCurrency': None,\n", " 'toCurrency': None,\n", " 'lastMarket': None,\n", " 'coinMarketCapLink': None,\n", " 'algorithm': None,\n", " 'tradeable': False}}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.summary_detail" ] }, { "cell_type": "code", "execution_count": 12, "id": "31b14f27", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'aapl': {'address1': 'One Apple Park Way',\n", " 'city': 'Cupertino',\n", " 'state': 'CA',\n", " 'zip': '95014',\n", " 'country': 'United States',\n", " 'phone': '(408) 996-1010',\n", " 'website': 'https://www.apple.com',\n", " 'industry': 'Consumer Electronics',\n", " 'industryKey': 'consumer-electronics',\n", " 'industryDisp': 'Consumer Electronics',\n", " 'sector': 'Technology',\n", " 'sectorKey': 'technology',\n", " 'sectorDisp': 'Technology',\n", " '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 Vision Pro, Apple TV, Apple Watch, Beats products, and HomePod, as well as Apple branded and third-party accessories. 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, as well as advertising services include third-party licensing arrangements and its own advertising platforms. In addition, the company offers various subscription-based 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 and live sports; 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 and resellers. The company was formerly known as Apple Computer, Inc. and changed its name to Apple Inc. in January 2007. Apple Inc. was founded in 1976 and is headquartered in Cupertino, California.',\n", " 'fullTimeEmployees': 150000,\n", " 'companyOfficers': [{'maxAge': 1,\n", " 'name': 'Mr. Timothy D. Cook',\n", " 'age': 64,\n", " 'title': 'CEO & Director',\n", " 'yearBorn': 1961,\n", " 'fiscalYear': 2025,\n", " 'totalPay': 16759518,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0},\n", " {'maxAge': 1,\n", " 'name': 'Mr. Kevan Parekh',\n", " 'age': 53,\n", " 'title': 'Senior VP & CFO',\n", " 'yearBorn': 1972,\n", " 'fiscalYear': 2025,\n", " 'totalPay': 4034174,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0},\n", " {'maxAge': 1,\n", " 'name': 'Mr. Sabih Khan',\n", " 'age': 58,\n", " 'title': 'Senior VP & Chief Operating Officer',\n", " 'yearBorn': 1967,\n", " 'fiscalYear': 2025,\n", " 'totalPay': 5021905,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0},\n", " {'maxAge': 1,\n", " 'name': 'Ms. Katherine L. Adams',\n", " 'age': 61,\n", " 'title': 'Senior VP, General Counsel & Secretary',\n", " 'yearBorn': 1964,\n", " 'fiscalYear': 2025,\n", " 'totalPay': 5022482,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0},\n", " {'maxAge': 1,\n", " 'name': \"Ms. Deirdre O'Brien\",\n", " 'age': 58,\n", " 'title': 'Senior Vice President of Retail & People',\n", " 'yearBorn': 1967,\n", " 'fiscalYear': 2025,\n", " 'totalPay': 5037867,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0},\n", " {'maxAge': 1,\n", " 'name': 'Mr. Ben Borders',\n", " 'age': 44,\n", " 'title': 'Principal Accounting Officer',\n", " 'yearBorn': 1981,\n", " 'fiscalYear': 2025,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0},\n", " {'maxAge': 1,\n", " 'name': 'Suhasini Chandramouli',\n", " 'title': 'Director of Investor Relations',\n", " 'fiscalYear': 2025,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0},\n", " {'maxAge': 1,\n", " 'name': 'Ms. Kristin Huguet Quayle',\n", " 'title': 'Vice President of Worldwide Communications',\n", " 'fiscalYear': 2025,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0},\n", " {'maxAge': 1,\n", " 'name': 'Mr. Greg Joswiak',\n", " 'title': 'Senior Vice President of Worldwide Marketing',\n", " 'fiscalYear': 2025,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0},\n", " {'maxAge': 1,\n", " 'name': 'Mr. Adrian Perica',\n", " 'age': 51,\n", " 'title': 'Vice President of Corporate Development',\n", " 'yearBorn': 1974,\n", " 'fiscalYear': 2025,\n", " 'exercisedValue': 0,\n", " 'unexercisedValue': 0}],\n", " 'auditRisk': 7,\n", " 'boardRisk': 1,\n", " 'compensationRisk': 3,\n", " 'shareHolderRightsRisk': 1,\n", " 'overallRisk': 1,\n", " 'governanceEpochDate': '2026-01-31 19:00:00',\n", " 'compensationAsOfEpochDate': '2025-12-30 19:00:00',\n", " 'irWebsite': 'http://investor.apple.com/',\n", " 'executiveTeam': [],\n", " 'maxAge': 86400}}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.asset_profile" ] }, { "cell_type": "code", "execution_count": 13, "id": "73209ab7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'aapl': {'maxAge': 1,\n", " 'earnings': {'earningsDate': ['2026-04-30 17:00:S'],\n", " 'earningsCallDate': [1769724000],\n", " 'isEarningsDateEstimate': True,\n", " 'earningsAverage': 1.94877,\n", " 'earningsLow': 1.85,\n", " 'earningsHigh': 2.16,\n", " 'revenueAverage': 109074306590,\n", " 'revenueLow': 105000000000,\n", " 'revenueHigh': 112596000000},\n", " 'exDividendDate': '2026-02-08 19:00:00',\n", " 'dividendDate': '2025-11-12 19:00:00'}}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.calendar_events" ] }, { "cell_type": "code", "execution_count": 14, "id": "f73183ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'aapl': 'No fundamentals data found for symbol: AAPL'}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.esg_scores" ] }, { "cell_type": "code", "execution_count": 15, "id": "cbca3e17", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'aapl': {'maxAge': 1,\n", " 'priceHint': 2,\n", " 'enterpriseValue': 3979875713024,\n", " 'forwardPE': 29.727995,\n", " 'profitMargins': 0.27037,\n", " 'floatShares': 14655594816,\n", " 'sharesOutstanding': 14681140000,\n", " 'sharesShort': 113576032,\n", " 'sharesShortPriorMonth': 122035714,\n", " 'sharesShortPreviousMonthDate': '2025-12-14 19:00:00',\n", " 'dateShortInterest': '2026-01-14 19:00:00',\n", " 'sharesPercentSharesOut': 0.0077,\n", " 'heldPercentInsiders': 0.01702,\n", " 'heldPercentInstitutions': 0.64992994,\n", " 'shortRatio': 2.61,\n", " 'shortPercentOfFloat': 0.0077,\n", " 'beta': 1.107,\n", " 'impliedSharesOutstanding': 14697926000,\n", " 'category': None,\n", " 'bookValue': 5.998,\n", " 'priceToBook': 46.00385,\n", " 'fundFamily': None,\n", " 'legalType': None,\n", " 'lastFiscalYearEnd': '2025-09-26 20:00:00',\n", " 'nextFiscalYearEnd': '2026-09-26 20:00:00',\n", " 'mostRecentQuarter': '2025-12-26 19:00:00',\n", " 'earningsQuarterlyGrowth': 0.159,\n", " 'netIncomeToCommon': 117776998400,\n", " 'trailingEps': 7.9,\n", " 'forwardEps': 9.27824,\n", " 'lastSplitFactor': '4:1',\n", " 'lastSplitDate': '2020-08-30 20:00:00',\n", " 'enterpriseToRevenue': 9.136,\n", " 'enterpriseToEbitda': 26.029,\n", " '52WeekChange': 0.15920329,\n", " 'SandP52WeekChange': 0.1412741,\n", " 'lastDividendValue': 0.26,\n", " 'lastDividendDate': 1762732800,\n", " 'latestShareClass': None,\n", " 'leadInvestor': None}}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.key_stats" ] }, { "cell_type": "code", "execution_count": 16, "id": "01d9e585", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'aapl': {'maxAge': 1,\n", " 'preMarketChangePercent': 0.0107614435,\n", " 'preMarketChange': 2.899994,\n", " 'preMarketTime': '2026-02-04 09:29:59',\n", " 'preMarketPrice': 272.38,\n", " 'preMarketSource': 'FREE_REALTIME',\n", " 'regularMarketChangePercent': 0.023938995,\n", " 'regularMarketChange': 6.4510803,\n", " 'regularMarketTime': '2026-02-04 11:41:52',\n", " 'priceHint': 2,\n", " 'regularMarketPrice': 275.9311,\n", " 'regularMarketDayHigh': 278.81,\n", " 'regularMarketDayLow': 272.285,\n", " 'regularMarketVolume': 31339918,\n", " 'regularMarketPreviousClose': 269.48,\n", " 'regularMarketSource': 'FREE_REALTIME',\n", " 'regularMarketOpen': 272.31,\n", " 'exchange': 'NMS',\n", " 'exchangeName': 'NasdaqGS',\n", " 'exchangeDataDelayedBy': 0,\n", " 'marketState': 'REGULAR',\n", " 'quoteType': 'EQUITY',\n", " 'symbol': 'AAPL',\n", " 'underlyingSymbol': None,\n", " 'shortName': 'Apple Inc.',\n", " 'longName': 'Apple Inc.',\n", " 'currency': 'USD',\n", " 'quoteSourceName': 'Nasdaq Real Time Price',\n", " 'currencySymbol': '$',\n", " 'fromCurrency': None,\n", " 'toCurrency': None,\n", " 'lastMarket': None,\n", " 'marketCap': 4055614619648}}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.price" ] }, { "cell_type": "markdown", "id": "d08e1b9e", "metadata": {}, "source": [ "The recommendation trend shows the change in analyst calls in the past 3 months." ] }, { "cell_type": "code", "execution_count": 17, "id": "50e728b1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "

\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
periodstrongBuybuyholdsellstrongSell
symbolrow
aapl00m6221611
1-1m6221512
2-2m5241513
3-3m5241513
\n", "
" ], "text/plain": [ " period strongBuy buy hold sell strongSell\n", "symbol row \n", "aapl 0 0m 6 22 16 1 1\n", " 1 -1m 6 22 15 1 2\n", " 2 -2m 5 24 15 1 3\n", " 3 -3m 5 24 15 1 3" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.recommendation_trend" ] }, { "cell_type": "code", "execution_count": 18, "id": "7d2fed8e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsignificanceheadlinedescriptionparentTopics
symboldate
aapl2016-06-0233857321Goldman cuts Apple estimates, price targetApple Inc <AAPL.O>: Goldman Sachs cuts price t...Performance
2016-06-0333867811S&P assigns 'AA+' rating to Apple Inc's Austra...S&P: Apple Inc's Australian dollar-denominated...Financing
2016-06-3033988541Nike appoints Tim Cook as lead independent dir...Nike Inc <NKE.N>: Nike, Inc. And Phil Knight c...Restructuring/Reorganization/Related
2016-07-2634119891Apple says Apple music will be global home of ...Apple Inc <AAPL.O>: Host for new series and a ...Performance
2016-07-2834145691Moody's assigns AA1 rating to Apple Inc senior...Moody's on Apple Inc senior unsecured note iss...Performance
..................
2024-10-3049184041Amplifon CEO doesn't see meaningful impact fro...Oct 30 (Reuters) - Amplifon CEO Enrico Vita te...Corporate Guidance
2025-07-3150076921Apple CEO Tim Cook Says Open To M&A To Acceler...July 31 (Reuters) - Apple Inc <AAPL.O>::APPLE ...Corporate Guidance
2025-10-3050383071Apple CEO Cook Says Supply Constrained Today O...Oct 30 (Reuters) - Apple Inc <AAPL.O>::APPLE C...Corporate Guidance
2025-10-3050382921Apple Expects 'Double Digit' Year Over Year Sa...Oct 30 (Reuters) - Apple Inc <AAPL.O>::APPLE E...Corporate Guidance
2025-11-0650423211MP Materials CEO Expects Company To Return To ...Nov 6 (Reuters) - Mp Materials Corp <MP.N>::MP...Corporate Guidance
\n", "

183 rows × 5 columns

\n", "
" ], "text/plain": [ " id significance \\\n", "symbol date \n", "aapl 2016-06-02 3385732 1 \n", " 2016-06-03 3386781 1 \n", " 2016-06-30 3398854 1 \n", " 2016-07-26 3411989 1 \n", " 2016-07-28 3414569 1 \n", "... ... ... \n", " 2024-10-30 4918404 1 \n", " 2025-07-31 5007692 1 \n", " 2025-10-30 5038307 1 \n", " 2025-10-30 5038292 1 \n", " 2025-11-06 5042321 1 \n", "\n", " headline \\\n", "symbol date \n", "aapl 2016-06-02 Goldman cuts Apple estimates, price target \n", " 2016-06-03 S&P assigns 'AA+' rating to Apple Inc's Austra... \n", " 2016-06-30 Nike appoints Tim Cook as lead independent dir... \n", " 2016-07-26 Apple says Apple music will be global home of ... \n", " 2016-07-28 Moody's assigns AA1 rating to Apple Inc senior... \n", "... ... \n", " 2024-10-30 Amplifon CEO doesn't see meaningful impact fro... \n", " 2025-07-31 Apple CEO Tim Cook Says Open To M&A To Acceler... \n", " 2025-10-30 Apple CEO Cook Says Supply Constrained Today O... \n", " 2025-10-30 Apple Expects 'Double Digit' Year Over Year Sa... \n", " 2025-11-06 MP Materials CEO Expects Company To Return To ... \n", "\n", " description \\\n", "symbol date \n", "aapl 2016-06-02 Apple Inc : Goldman Sachs cuts price t... \n", " 2016-06-03 S&P: Apple Inc's Australian dollar-denominated... \n", " 2016-06-30 Nike Inc : Nike, Inc. And Phil Knight c... \n", " 2016-07-26 Apple Inc : Host for new series and a ... \n", " 2016-07-28 Moody's on Apple Inc senior unsecured note iss... \n", "... ... \n", " 2024-10-30 Oct 30 (Reuters) - Amplifon CEO Enrico Vita te... \n", " 2025-07-31 July 31 (Reuters) - Apple Inc ::APPLE ... \n", " 2025-10-30 Oct 30 (Reuters) - Apple Inc ::APPLE C... \n", " 2025-10-30 Oct 30 (Reuters) - Apple Inc ::APPLE E... \n", " 2025-11-06 Nov 6 (Reuters) - Mp Materials Corp ::MP... \n", "\n", " parentTopics \n", "symbol date \n", "aapl 2016-06-02 Performance \n", " 2016-06-03 Financing \n", " 2016-06-30 Restructuring/Reorganization/Related \n", " 2016-07-26 Performance \n", " 2016-07-28 Performance \n", "... ... \n", " 2024-10-30 Corporate Guidance \n", " 2025-07-31 Corporate Guidance \n", " 2025-10-30 Corporate Guidance \n", " 2025-10-30 Corporate Guidance \n", " 2025-11-06 Corporate Guidance \n", "\n", "[183 rows x 5 columns]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.corporate_events" ] }, { "cell_type": "code", "execution_count": null, "id": "0c599bad", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "a36a7f9c", "metadata": {}, "source": [ "### load S and P stock prices for year to date as a data frame" ] }, { "cell_type": "code", "execution_count": 19, "id": "1be99e08", "metadata": {}, "outputs": [ { "ename": "HTTPError", "evalue": "HTTP Error 403: Forbidden", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mHTTPError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[19]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m tables = \u001b[43mpd\u001b[49m\u001b[43m.\u001b[49m\u001b[43mread_html\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mhttps://en.wikipedia.org/wiki/List_of_S\u001b[39;49m\u001b[33;43m%\u001b[39;49m\u001b[33;43m26P_500_companies\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m 2\u001b[39m sp500 = tables[\u001b[32m0\u001b[39m][\u001b[33m'\u001b[39m\u001b[33mSymbol\u001b[39m\u001b[33m'\u001b[39m].tolist()\n\u001b[32m 3\u001b[39m sp500 = [symbol.replace(\u001b[33m\"\u001b[39m\u001b[33m.\u001b[39m\u001b[33m\"\u001b[39m, \u001b[33m\"\u001b[39m\u001b[33m-\u001b[39m\u001b[33m\"\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m symbol \u001b[38;5;129;01min\u001b[39;00m sp500]\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/io/html.py:1226\u001b[39m, in \u001b[36mread_html\u001b[39m\u001b[34m(io, match, flavor, header, index_col, skiprows, attrs, parse_dates, thousands, encoding, decimal, converters, na_values, keep_default_na, displayed_only, extract_links, dtype_backend, storage_options)\u001b[39m\n\u001b[32m 1222\u001b[39m check_dtype_backend(dtype_backend)\n\u001b[32m 1224\u001b[39m io = stringify_path(io)\n\u001b[32m-> \u001b[39m\u001b[32m1226\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_parse\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 1227\u001b[39m \u001b[43m \u001b[49m\u001b[43mflavor\u001b[49m\u001b[43m=\u001b[49m\u001b[43mflavor\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1228\u001b[39m \u001b[43m \u001b[49m\u001b[43mio\u001b[49m\u001b[43m=\u001b[49m\u001b[43mio\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1229\u001b[39m \u001b[43m \u001b[49m\u001b[43mmatch\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmatch\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1230\u001b[39m \u001b[43m \u001b[49m\u001b[43mheader\u001b[49m\u001b[43m=\u001b[49m\u001b[43mheader\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1231\u001b[39m \u001b[43m \u001b[49m\u001b[43mindex_col\u001b[49m\u001b[43m=\u001b[49m\u001b[43mindex_col\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1232\u001b[39m \u001b[43m \u001b[49m\u001b[43mskiprows\u001b[49m\u001b[43m=\u001b[49m\u001b[43mskiprows\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1233\u001b[39m \u001b[43m \u001b[49m\u001b[43mparse_dates\u001b[49m\u001b[43m=\u001b[49m\u001b[43mparse_dates\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1234\u001b[39m \u001b[43m \u001b[49m\u001b[43mthousands\u001b[49m\u001b[43m=\u001b[49m\u001b[43mthousands\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1235\u001b[39m \u001b[43m \u001b[49m\u001b[43mattrs\u001b[49m\u001b[43m=\u001b[49m\u001b[43mattrs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1236\u001b[39m \u001b[43m \u001b[49m\u001b[43mencoding\u001b[49m\u001b[43m=\u001b[49m\u001b[43mencoding\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1237\u001b[39m \u001b[43m \u001b[49m\u001b[43mdecimal\u001b[49m\u001b[43m=\u001b[49m\u001b[43mdecimal\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1238\u001b[39m \u001b[43m \u001b[49m\u001b[43mconverters\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconverters\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1239\u001b[39m \u001b[43m \u001b[49m\u001b[43mna_values\u001b[49m\u001b[43m=\u001b[49m\u001b[43mna_values\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1240\u001b[39m \u001b[43m \u001b[49m\u001b[43mkeep_default_na\u001b[49m\u001b[43m=\u001b[49m\u001b[43mkeep_default_na\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1241\u001b[39m \u001b[43m \u001b[49m\u001b[43mdisplayed_only\u001b[49m\u001b[43m=\u001b[49m\u001b[43mdisplayed_only\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1242\u001b[39m \u001b[43m \u001b[49m\u001b[43mextract_links\u001b[49m\u001b[43m=\u001b[49m\u001b[43mextract_links\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1243\u001b[39m \u001b[43m \u001b[49m\u001b[43mdtype_backend\u001b[49m\u001b[43m=\u001b[49m\u001b[43mdtype_backend\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1244\u001b[39m \u001b[43m \u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1245\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/io/html.py:979\u001b[39m, in \u001b[36m_parse\u001b[39m\u001b[34m(flavor, io, match, attrs, encoding, displayed_only, extract_links, storage_options, **kwargs)\u001b[39m\n\u001b[32m 968\u001b[39m p = parser(\n\u001b[32m 969\u001b[39m io,\n\u001b[32m 970\u001b[39m compiled_match,\n\u001b[32m (...)\u001b[39m\u001b[32m 975\u001b[39m storage_options,\n\u001b[32m 976\u001b[39m )\n\u001b[32m 978\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m--> \u001b[39m\u001b[32m979\u001b[39m tables = \u001b[43mp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mparse_tables\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 980\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m caught:\n\u001b[32m 981\u001b[39m \u001b[38;5;66;03m# if `io` is an io-like object, check if it's seekable\u001b[39;00m\n\u001b[32m 982\u001b[39m \u001b[38;5;66;03m# and try to rewind it before trying the next parser\u001b[39;00m\n\u001b[32m 983\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(io, \u001b[33m\"\u001b[39m\u001b[33mseekable\u001b[39m\u001b[33m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m io.seekable():\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/io/html.py:237\u001b[39m, in \u001b[36m_HtmlFrameParser.parse_tables\u001b[39m\u001b[34m(self)\u001b[39m\n\u001b[32m 229\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mparse_tables\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[32m 230\u001b[39m \u001b[38;5;250m \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 231\u001b[39m \u001b[33;03m Parse and return all tables from the DOM.\u001b[39;00m\n\u001b[32m 232\u001b[39m \n\u001b[32m (...)\u001b[39m\u001b[32m 235\u001b[39m \u001b[33;03m list of parsed (header, body, footer) tuples from tables.\u001b[39;00m\n\u001b[32m 236\u001b[39m \u001b[33;03m \"\"\"\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m237\u001b[39m tables = \u001b[38;5;28mself\u001b[39m._parse_tables(\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_build_doc\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m, \u001b[38;5;28mself\u001b[39m.match, \u001b[38;5;28mself\u001b[39m.attrs)\n\u001b[32m 238\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m (\u001b[38;5;28mself\u001b[39m._parse_thead_tbody_tfoot(table) \u001b[38;5;28;01mfor\u001b[39;00m table \u001b[38;5;129;01min\u001b[39;00m tables)\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/io/html.py:789\u001b[39m, in \u001b[36m_LxmlFrameParser._build_doc\u001b[39m\u001b[34m(self)\u001b[39m\n\u001b[32m 786\u001b[39m parser = HTMLParser(recover=\u001b[38;5;28;01mTrue\u001b[39;00m, encoding=\u001b[38;5;28mself\u001b[39m.encoding)\n\u001b[32m 788\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m is_url(\u001b[38;5;28mself\u001b[39m.io):\n\u001b[32m--> \u001b[39m\u001b[32m789\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[43mget_handle\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mio\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mr\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[32m 790\u001b[39m r = parse(f.handle, parser=parser)\n\u001b[32m 791\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 792\u001b[39m \u001b[38;5;66;03m# try to parse the input in the simplest way\u001b[39;00m\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/io/common.py:772\u001b[39m, in \u001b[36mget_handle\u001b[39m\u001b[34m(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)\u001b[39m\n\u001b[32m 769\u001b[39m codecs.lookup_error(errors)\n\u001b[32m 771\u001b[39m \u001b[38;5;66;03m# open URLs\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m772\u001b[39m ioargs = \u001b[43m_get_filepath_or_buffer\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 773\u001b[39m \u001b[43m \u001b[49m\u001b[43mpath_or_buf\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 774\u001b[39m \u001b[43m \u001b[49m\u001b[43mencoding\u001b[49m\u001b[43m=\u001b[49m\u001b[43mencoding\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 775\u001b[39m \u001b[43m \u001b[49m\u001b[43mcompression\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcompression\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 776\u001b[39m \u001b[43m \u001b[49m\u001b[43mmode\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 777\u001b[39m \u001b[43m \u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 778\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 780\u001b[39m handle = ioargs.filepath_or_buffer\n\u001b[32m 781\u001b[39m handles: \u001b[38;5;28mlist\u001b[39m[BaseBuffer]\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/io/common.py:404\u001b[39m, in \u001b[36m_get_filepath_or_buffer\u001b[39m\u001b[34m(filepath_or_buffer, encoding, compression, mode, storage_options)\u001b[39m\n\u001b[32m 402\u001b[39m \u001b[38;5;66;03m# assuming storage_options is to be interpreted as headers\u001b[39;00m\n\u001b[32m 403\u001b[39m req_info = urllib.request.Request(filepath_or_buffer, headers=storage_options)\n\u001b[32m--> \u001b[39m\u001b[32m404\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[43murlopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreq_info\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m req:\n\u001b[32m 405\u001b[39m content_encoding = req.headers.get(\u001b[33m\"\u001b[39m\u001b[33mContent-Encoding\u001b[39m\u001b[33m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[32m 406\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m content_encoding == \u001b[33m\"\u001b[39m\u001b[33mgzip\u001b[39m\u001b[33m\"\u001b[39m:\n\u001b[32m 407\u001b[39m \u001b[38;5;66;03m# Override compression based on Content-Encoding header\u001b[39;00m\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/io/common.py:281\u001b[39m, in \u001b[36murlopen\u001b[39m\u001b[34m(*args, **kwargs)\u001b[39m\n\u001b[32m 275\u001b[39m \u001b[38;5;250m\u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 276\u001b[39m \u001b[33;03mLazy-import wrapper for stdlib urlopen, as that imports a big chunk of\u001b[39;00m\n\u001b[32m 277\u001b[39m \u001b[33;03mthe stdlib.\u001b[39;00m\n\u001b[32m 278\u001b[39m \u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 279\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01murllib\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mrequest\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m281\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43murllib\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m.\u001b[49m\u001b[43murlopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[36mFile \u001b[39m\u001b[32m/usr/lib/python3.12/urllib/request.py:215\u001b[39m, in \u001b[36murlopen\u001b[39m\u001b[34m(url, data, timeout, cafile, capath, cadefault, context)\u001b[39m\n\u001b[32m 213\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 214\u001b[39m opener = _opener\n\u001b[32m--> \u001b[39m\u001b[32m215\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mopener\u001b[49m\u001b[43m.\u001b[49m\u001b[43mopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[36mFile \u001b[39m\u001b[32m/usr/lib/python3.12/urllib/request.py:521\u001b[39m, in \u001b[36mOpenerDirector.open\u001b[39m\u001b[34m(self, fullurl, data, timeout)\u001b[39m\n\u001b[32m 519\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m processor \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m.process_response.get(protocol, []):\n\u001b[32m 520\u001b[39m meth = \u001b[38;5;28mgetattr\u001b[39m(processor, meth_name)\n\u001b[32m--> \u001b[39m\u001b[32m521\u001b[39m response = \u001b[43mmeth\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreq\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 523\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m response\n", "\u001b[36mFile \u001b[39m\u001b[32m/usr/lib/python3.12/urllib/request.py:630\u001b[39m, in \u001b[36mHTTPErrorProcessor.http_response\u001b[39m\u001b[34m(self, request, response)\u001b[39m\n\u001b[32m 627\u001b[39m \u001b[38;5;66;03m# According to RFC 2616, \"2xx\" code indicates that the client's\u001b[39;00m\n\u001b[32m 628\u001b[39m \u001b[38;5;66;03m# request was successfully received, understood, and accepted.\u001b[39;00m\n\u001b[32m 629\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[32m200\u001b[39m <= code < \u001b[32m300\u001b[39m):\n\u001b[32m--> \u001b[39m\u001b[32m630\u001b[39m response = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mparent\u001b[49m\u001b[43m.\u001b[49m\u001b[43merror\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 631\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mhttp\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmsg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhdrs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 633\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m response\n", "\u001b[36mFile \u001b[39m\u001b[32m/usr/lib/python3.12/urllib/request.py:559\u001b[39m, in \u001b[36mOpenerDirector.error\u001b[39m\u001b[34m(self, proto, *args)\u001b[39m\n\u001b[32m 557\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m http_err:\n\u001b[32m 558\u001b[39m args = (\u001b[38;5;28mdict\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mdefault\u001b[39m\u001b[33m'\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mhttp_error_default\u001b[39m\u001b[33m'\u001b[39m) + orig_args\n\u001b[32m--> \u001b[39m\u001b[32m559\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_call_chain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[36mFile \u001b[39m\u001b[32m/usr/lib/python3.12/urllib/request.py:492\u001b[39m, in \u001b[36mOpenerDirector._call_chain\u001b[39m\u001b[34m(self, chain, kind, meth_name, *args)\u001b[39m\n\u001b[32m 490\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m handler \u001b[38;5;129;01min\u001b[39;00m handlers:\n\u001b[32m 491\u001b[39m func = \u001b[38;5;28mgetattr\u001b[39m(handler, meth_name)\n\u001b[32m--> \u001b[39m\u001b[32m492\u001b[39m result = \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 493\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m result \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[32m 494\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", "\u001b[36mFile \u001b[39m\u001b[32m/usr/lib/python3.12/urllib/request.py:639\u001b[39m, in \u001b[36mHTTPDefaultErrorHandler.http_error_default\u001b[39m\u001b[34m(self, req, fp, code, msg, hdrs)\u001b[39m\n\u001b[32m 638\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mhttp_error_default\u001b[39m(\u001b[38;5;28mself\u001b[39m, req, fp, code, msg, hdrs):\n\u001b[32m--> \u001b[39m\u001b[32m639\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m HTTPError(req.full_url, code, msg, hdrs, fp)\n", "\u001b[31mHTTPError\u001b[39m: HTTP Error 403: Forbidden" ] } ], "source": [ "tables = pd.read_html(\"https://en.wikipedia.org/wiki/List_of_S%26P_500_companies\")\n", "sp500 = tables[0]['Symbol'].tolist()\n", "sp500 = [symbol.replace(\".\", \"-\") for symbol in sp500]" ] }, { "cell_type": "code", "execution_count": 20, "id": "0ecc4b7f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 42.6 ms, sys: 9.37 ms, total: 52 ms\n", "Wall time: 1.07 s\n" ] }, { "ename": "TypeError", "evalue": "'Ticker' object is not iterable", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[20]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mget_ipython\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrun_cell_magic\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mtime\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtickers = Ticker(aapl, asynchronous=True)\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[33;43myq_data = tickers.history(period=\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mytd\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m, interval=\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m1d\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m)\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/IPython/core/interactiveshell.py:2572\u001b[39m, in \u001b[36mInteractiveShell.run_cell_magic\u001b[39m\u001b[34m(self, magic_name, line, cell)\u001b[39m\n\u001b[32m 2570\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m.builtin_trap:\n\u001b[32m 2571\u001b[39m args = (magic_arg_s, cell)\n\u001b[32m-> \u001b[39m\u001b[32m2572\u001b[39m result = \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 2574\u001b[39m \u001b[38;5;66;03m# The code below prevents the output from being displayed\u001b[39;00m\n\u001b[32m 2575\u001b[39m \u001b[38;5;66;03m# when using magics with decorator @output_can_be_silenced\u001b[39;00m\n\u001b[32m 2576\u001b[39m \u001b[38;5;66;03m# when the last Python token in the expression is a ';'.\u001b[39;00m\n\u001b[32m 2577\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, \u001b[38;5;28;01mFalse\u001b[39;00m):\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/IPython/core/magics/execution.py:1447\u001b[39m, in \u001b[36mExecutionMagics.time\u001b[39m\u001b[34m(self, line, cell, local_ns)\u001b[39m\n\u001b[32m 1445\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m interrupt_occured:\n\u001b[32m 1446\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m exit_on_interrupt \u001b[38;5;129;01mand\u001b[39;00m captured_exception:\n\u001b[32m-> \u001b[39m\u001b[32m1447\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m captured_exception\n\u001b[32m 1448\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[32m 1449\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m out\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/IPython/core/magics/execution.py:1411\u001b[39m, in \u001b[36mExecutionMagics.time\u001b[39m\u001b[34m(self, line, cell, local_ns)\u001b[39m\n\u001b[32m 1409\u001b[39m st = clock2()\n\u001b[32m 1410\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m1411\u001b[39m \u001b[43mexec\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mglob\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlocal_ns\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 1412\u001b[39m out = \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m 1413\u001b[39m \u001b[38;5;66;03m# multi-line %%time case\u001b[39;00m\n", "\u001b[36mFile \u001b[39m\u001b[32m:2\u001b[39m\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/yahooquery/ticker.py:1284\u001b[39m, in \u001b[36mTicker.history\u001b[39m\u001b[34m(self, period, interval, start, end, adj_timezone, adj_ohlc)\u001b[39m\n\u001b[32m 1282\u001b[39m df = \u001b[38;5;28mself\u001b[39m._history_1m(adj_timezone, adj_ohlc)\n\u001b[32m 1283\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m1284\u001b[39m data = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_get_data\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mchart\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 1285\u001b[39m df = \u001b[38;5;28mself\u001b[39m._historical_data_to_dataframe(data, params, adj_timezone)\n\u001b[32m 1286\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m adj_ohlc \u001b[38;5;129;01mand\u001b[39;00m \u001b[33m\"\u001b[39m\u001b[33madjclose\u001b[39m\u001b[33m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m df:\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/yahooquery/base.py:181\u001b[39m, in \u001b[36m_YahooFinance._get_data\u001b[39m\u001b[34m(self, key, params, **kwargs)\u001b[39m\n\u001b[32m 179\u001b[39m config = CONFIG[key]\n\u001b[32m 180\u001b[39m params = \u001b[38;5;28mself\u001b[39m._construct_params(config, params)\n\u001b[32m--> \u001b[39m\u001b[32m181\u001b[39m urls = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_construct_urls\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 182\u001b[39m response_field = config[\u001b[33m\"\u001b[39m\u001b[33mresponse_field\u001b[39m\u001b[33m\"\u001b[39m]\n\u001b[32m 183\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/yahooquery/base.py:255\u001b[39m, in \u001b[36m_YahooFinance._construct_urls\u001b[39m\u001b[34m(self, config, params, **kwargs)\u001b[39m\n\u001b[32m 249\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 250\u001b[39m ls = (\n\u001b[32m 251\u001b[39m \u001b[38;5;28mself\u001b[39m._symbols\n\u001b[32m 252\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m.session, FuturesSession)\n\u001b[32m 253\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m tqdm(\u001b[38;5;28mself\u001b[39m._symbols, disable=\u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m.progress)\n\u001b[32m 254\u001b[39m )\n\u001b[32m--> \u001b[39m\u001b[32m255\u001b[39m urls = \u001b[43m[\u001b[49m\n\u001b[32m 256\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43msession\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 257\u001b[39m \u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mpath\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43mformat\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43m{\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43msymbol\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43msymbol\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[43m=\u001b[49m\u001b[43mparams\u001b[49m\n\u001b[32m 258\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 259\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43msymbol\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mls\u001b[49m\n\u001b[32m 260\u001b[39m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\n\u001b[32m 261\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m urls\n", "\u001b[31mTypeError\u001b[39m: 'Ticker' object is not iterable" ] } ], "source": [ "%%time\n", "tickers = Ticker(aapl, asynchronous=True)\n", "yq_data = tickers.history(period='ytd', interval='1d')" ] }, { "cell_type": "code", "execution_count": null, "id": "db607821", "metadata": {}, "outputs": [], "source": [ "aapl_df.tail()" ] }, { "cell_type": "markdown", "id": "0757b18b", "metadata": {}, "source": [ "### plot Apple stock price for the past 5 years" ] }, { "cell_type": "code", "execution_count": 22, "id": "6151cc6f", "metadata": {}, "outputs": [], "source": [ "import matplotlib" ] }, { "cell_type": "code", "execution_count": 23, "id": "cb830b5c", "metadata": { "scrolled": true }, "outputs": [ { "ename": "KeyError", "evalue": "'Close'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/core/indexes/base.py:3641\u001b[39m, in \u001b[36mIndex.get_loc\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 3640\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m3641\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_engine\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcasted_key\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 3642\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n", "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/index.pyx:168\u001b[39m, in \u001b[36mpandas._libs.index.IndexEngine.get_loc\u001b[39m\u001b[34m()\u001b[39m\n", "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/index.pyx:197\u001b[39m, in \u001b[36mpandas._libs.index.IndexEngine.get_loc\u001b[39m\u001b[34m()\u001b[39m\n", "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/hashtable_class_helper.pxi:7668\u001b[39m, in \u001b[36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[39m\u001b[34m()\u001b[39m\n", "\u001b[36mFile \u001b[39m\u001b[32mpandas/_libs/hashtable_class_helper.pxi:7676\u001b[39m, in \u001b[36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[39m\u001b[34m()\u001b[39m\n", "\u001b[31mKeyError\u001b[39m: 'Close'", "\nThe above exception was the direct cause of the following exception:\n", "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[23]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m 1\u001b[39m ticker = Ticker(\u001b[33m'\u001b[39m\u001b[33mAAPL\u001b[39m\u001b[33m'\u001b[39m)\n\u001b[32m 2\u001b[39m aapl_df = ticker.history(period=\u001b[33m\"\u001b[39m\u001b[33m5y\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m \u001b[43maapl_df\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mClose\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m.plot(title=\u001b[33m\"\u001b[39m\u001b[33mAPPLE\u001b[39m\u001b[33m'\u001b[39m\u001b[33ms stock price\u001b[39m\u001b[33m\"\u001b[39m)\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/core/frame.py:4378\u001b[39m, in \u001b[36mDataFrame.__getitem__\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 4376\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m.columns.nlevels > \u001b[32m1\u001b[39m:\n\u001b[32m 4377\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m._getitem_multilevel(key)\n\u001b[32m-> \u001b[39m\u001b[32m4378\u001b[39m indexer = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mcolumns\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 4379\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m is_integer(indexer):\n\u001b[32m 4380\u001b[39m indexer = [indexer]\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/pandas/core/indexes/base.py:3648\u001b[39m, in \u001b[36mIndex.get_loc\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 3643\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(casted_key, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m (\n\u001b[32m 3644\u001b[39m \u001b[38;5;28misinstance\u001b[39m(casted_key, abc.Iterable)\n\u001b[32m 3645\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28many\u001b[39m(\u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m casted_key)\n\u001b[32m 3646\u001b[39m ):\n\u001b[32m 3647\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m InvalidIndexError(key) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01merr\u001b[39;00m\n\u001b[32m-> \u001b[39m\u001b[32m3648\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01merr\u001b[39;00m\n\u001b[32m 3649\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[32m 3650\u001b[39m \u001b[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001b[39;00m\n\u001b[32m 3651\u001b[39m \u001b[38;5;66;03m# InvalidIndexError. Otherwise we fall through and re-raise\u001b[39;00m\n\u001b[32m 3652\u001b[39m \u001b[38;5;66;03m# the TypeError.\u001b[39;00m\n\u001b[32m 3653\u001b[39m \u001b[38;5;28mself\u001b[39m._check_indexing_error(key)\n", "\u001b[31mKeyError\u001b[39m: 'Close'" ] } ], "source": [ "ticker = Ticker('AAPL')\n", "aapl_df = ticker.history(period=\"5y\")\n", "aapl_df['Close'].plot(title=\"APPLE's stock price\")" ] }, { "cell_type": "markdown", "id": "62ff4426", "metadata": {}, "source": [ "## YahooFinancials - Apple\n", "\n" ] }, { "cell_type": "code", "execution_count": 24, "id": "4604f41a", "metadata": {}, "outputs": [], "source": [ "from yahoofinancials import YahooFinancials" ] }, { "cell_type": "code", "execution_count": 25, "id": "81d2bf4d", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'NoneType' object is not iterable", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[25]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m yahoo_financials = YahooFinancials(\u001b[33m'\u001b[39m\u001b[33mAAPL\u001b[39m\u001b[33m'\u001b[39m)\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m data = \u001b[43myahoo_financials\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget_historical_price_data\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstart_date\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m2019-01-01\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[32m 3\u001b[39m \u001b[43m \u001b[49m\u001b[43mend_date\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m2019-12-31\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[32m 4\u001b[39m \u001b[43m \u001b[49m\u001b[43mtime_interval\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mweekly\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m 5\u001b[39m aapl_df = pd.DataFrame(data[\u001b[33m'\u001b[39m\u001b[33mAAPL\u001b[39m\u001b[33m'\u001b[39m][\u001b[33m'\u001b[39m\u001b[33mprices\u001b[39m\u001b[33m'\u001b[39m])\n\u001b[32m 6\u001b[39m aapl_df = aapl_df.drop(\u001b[33m'\u001b[39m\u001b[33mdate\u001b[39m\u001b[33m'\u001b[39m, axis=\u001b[32m1\u001b[39m).set_index(\u001b[33m'\u001b[39m\u001b[33mformatted_date\u001b[39m\u001b[33m'\u001b[39m)\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/yahoofinancials/yf.py:163\u001b[39m, in \u001b[36mYahooFinancials.get_historical_price_data\u001b[39m\u001b[34m(self, start_date, end_date, time_interval)\u001b[39m\n\u001b[32m 161\u001b[39m end = \u001b[38;5;28mself\u001b[39m.format_date(end_date)\n\u001b[32m 162\u001b[39m hist_obj = {\u001b[33m'\u001b[39m\u001b[33mstart\u001b[39m\u001b[33m'\u001b[39m: start, \u001b[33m'\u001b[39m\u001b[33mend\u001b[39m\u001b[33m'\u001b[39m: end, \u001b[33m'\u001b[39m\u001b[33minterval\u001b[39m\u001b[33m'\u001b[39m: interval_code}\n\u001b[32m--> \u001b[39m\u001b[32m163\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mget_stock_data\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mhistory\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhist_obj\u001b[49m\u001b[43m=\u001b[49m\u001b[43mhist_obj\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/yahoofinancials/data.py:631\u001b[39m, in \u001b[36mYahooFinanceData.get_stock_data\u001b[39m\u001b[34m(self, statement_type, tech_type, report_name, hist_obj)\u001b[39m\n\u001b[32m 629\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m.ticker, \u001b[38;5;28mstr\u001b[39m):\n\u001b[32m 630\u001b[39m dict_ent = \u001b[38;5;28mself\u001b[39m._retry_create_dict_ent(\u001b[38;5;28mself\u001b[39m.ticker, statement_type, tech_type, report_name, hist_obj)\n\u001b[32m--> \u001b[39m\u001b[32m631\u001b[39m \u001b[43mdata\u001b[49m\u001b[43m.\u001b[49m\u001b[43mupdate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdict_ent\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 632\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 633\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m.concurrent:\n", "\u001b[31mTypeError\u001b[39m: 'NoneType' object is not iterable" ] } ], "source": [ "yahoo_financials = YahooFinancials('AAPL')\n", "data = yahoo_financials.get_historical_price_data(start_date='2019-01-01', \n", " end_date='2019-12-31', \n", " time_interval='weekly')\n", "aapl_df = pd.DataFrame(data['AAPL']['prices'])\n", "aapl_df = aapl_df.drop('date', axis=1).set_index('formatted_date')\n", "aapl_df.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "f436506d", "metadata": {}, "outputs": [], "source": [ "dir(yahoo_financials)" ] }, { "cell_type": "code", "execution_count": null, "id": "45bc62ee", "metadata": {}, "outputs": [], "source": [ "dir(yahoo_financials.get_clean_data.__func__)" ] }, { "cell_type": "code", "execution_count": null, "id": "c808abf1", "metadata": {}, "outputs": [], "source": [ "hasattr(yahoo_financials.get_beta, '__call__')" ] }, { "cell_type": "code", "execution_count": null, "id": "aa424d7d", "metadata": {}, "outputs": [], "source": [ "for p in dir(yahoo_financials):\n", " print (p, '\\t', getattr(yahoo_financials, p), '\\n')" ] }, { "cell_type": "markdown", "id": "fa9e808b", "metadata": {}, "source": [ "## YahooFinancials - Bitcoin" ] }, { "cell_type": "code", "execution_count": 26, "id": "503452af", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'NoneType' object is not iterable", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[26]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m yahoo_financials = YahooFinancials(\u001b[33m'\u001b[39m\u001b[33mBTC-USD\u001b[39m\u001b[33m'\u001b[39m)\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m data=\u001b[43myahoo_financials\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget_historical_price_data\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43m2019-07-10\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43m2022-01-31\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmonthly\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m 3\u001b[39m btc_df = pd.DataFrame(data[\u001b[33m'\u001b[39m\u001b[33mBTC-USD\u001b[39m\u001b[33m'\u001b[39m][\u001b[33m'\u001b[39m\u001b[33mprices\u001b[39m\u001b[33m'\u001b[39m])\n\u001b[32m 4\u001b[39m btc_df = btc_df.drop(\u001b[33m'\u001b[39m\u001b[33mdate\u001b[39m\u001b[33m'\u001b[39m, axis=\u001b[32m1\u001b[39m).set_index(\u001b[33m'\u001b[39m\u001b[33mformatted_date\u001b[39m\u001b[33m'\u001b[39m)\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/yahoofinancials/yf.py:163\u001b[39m, in \u001b[36mYahooFinancials.get_historical_price_data\u001b[39m\u001b[34m(self, start_date, end_date, time_interval)\u001b[39m\n\u001b[32m 161\u001b[39m end = \u001b[38;5;28mself\u001b[39m.format_date(end_date)\n\u001b[32m 162\u001b[39m hist_obj = {\u001b[33m'\u001b[39m\u001b[33mstart\u001b[39m\u001b[33m'\u001b[39m: start, \u001b[33m'\u001b[39m\u001b[33mend\u001b[39m\u001b[33m'\u001b[39m: end, \u001b[33m'\u001b[39m\u001b[33minterval\u001b[39m\u001b[33m'\u001b[39m: interval_code}\n\u001b[32m--> \u001b[39m\u001b[32m163\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mget_stock_data\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mhistory\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhist_obj\u001b[49m\u001b[43m=\u001b[49m\u001b[43mhist_obj\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[36mFile \u001b[39m\u001b[32m/home/httpd/html/zoo/classes/cs458/lectures/venv/lib/python3.12/site-packages/yahoofinancials/data.py:631\u001b[39m, in \u001b[36mYahooFinanceData.get_stock_data\u001b[39m\u001b[34m(self, statement_type, tech_type, report_name, hist_obj)\u001b[39m\n\u001b[32m 629\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m.ticker, \u001b[38;5;28mstr\u001b[39m):\n\u001b[32m 630\u001b[39m dict_ent = \u001b[38;5;28mself\u001b[39m._retry_create_dict_ent(\u001b[38;5;28mself\u001b[39m.ticker, statement_type, tech_type, report_name, hist_obj)\n\u001b[32m--> \u001b[39m\u001b[32m631\u001b[39m \u001b[43mdata\u001b[49m\u001b[43m.\u001b[49m\u001b[43mupdate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdict_ent\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 632\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 633\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m.concurrent:\n", "\u001b[31mTypeError\u001b[39m: 'NoneType' object is not iterable" ] } ], "source": [ "yahoo_financials = YahooFinancials('BTC-USD')\n", "data=yahoo_financials.get_historical_price_data(\"2019-07-10\", \"2022-01-31\", \"monthly\")\n", "btc_df = pd.DataFrame(data['BTC-USD']['prices'])\n", "btc_df = btc_df.drop('date', axis=1).set_index('formatted_date')\n", "btc_df.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "a381b608", "metadata": {}, "outputs": [], "source": [ "btc_df.tail()" ] }, { "cell_type": "markdown", "id": "1fdbf163", "metadata": {}, "source": [ "## Apple plot" ] }, { "cell_type": "code", "execution_count": null, "id": "8781b1cc-77ad-4bba-b07e-11b20f30a336", "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "id": "2bda1877-c16f-49d3-9811-b3aea075e1ad", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }