{"id":3404,"date":"2020-06-06T14:59:00","date_gmt":"2020-06-06T14:59:00","guid":{"rendered":"http:\/\/algotrading101.com\/learn\/?p=3404"},"modified":"2021-01-11T15:46:33","modified_gmt":"2021-01-11T15:46:33","slug":"yahoo-finance-api-guide","status":"publish","type":"post","link":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/","title":{"rendered":"Yahoo Finance API &#8211; A Complete Guide"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_3404\" class=\"pvc_stats total_only  \" data-element-id=\"3404\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>\n<h4 class=\"wp-block-heading\">Table of Contents<\/h4>\n\n\n\n<ol><li><a href=\"#What-is-the-Yahoo-Finance-API\">What is the Yahoo Finance API?<\/a><\/li><li><a href=\"#Why-should-I-use-the-Yahoo-Finance-API\">Why should I use the Yahoo Finance API?<\/a><\/li><li><a href=\"#Why-shouldnt-I-use-the-Yahoo-Finance-API\">Why shouldn\u2019t I use the Yahoo Finance API?<\/a><\/li><li><a href=\"#Is-the-Yahoo-Finance-API-free\">Is the Yahoo Finance API free?<\/a><\/li><li><a href=\"#Is-the-Yahoo-Finance-API-usable-with-Python\">Is the Yahoo Finance API usable with Python?<\/a><\/li><li><a href=\"#What-are-some-of-the-ways-to-access-the-Yahoo-Finance-API\">What are some of the ways to access the Yahoo Finance API?<\/a> <ul><li><a href=\"#RapidAPI\">RapidAPI<\/a><\/li><li><a href=\"#yfinance\">yfinance<\/a><\/li><li><a href=\"#Yahoo_fin\">Yahoo_fin<\/a> <\/li><\/ul><\/li><li><a href=\"#How-do-I-get-started-with-the-Yahoo_fin-library\">How do I get started with the Yahoo_fin library?<\/a> <ul><li><a href=\"#Installation\">Installation<\/a><\/li><li><a href=\"#Library-Layout\">Library Layout<\/a> <\/li><\/ul><\/li><li><a href=\"#How-do-I-download-historical-data-using-the-Yahoo-Finance-API\">How do I download historical data using the Yahoo Finance API?<\/a> <ul><li><a href=\"#Demo-with-one-ticker\">Demo with one ticker<\/a><\/li><li><a href=\"#Demo-with-multiple-tickers\">Demo with multiple tickers<\/a> <\/li><\/ul><\/li><li><a href=\"#How-do-I-download-fundamental-data\">How do I download fundamental data?<\/a> <ul><li><a href=\"#Price-to-Earnings-Ratio\">Price to Earnings Ratio<\/a><\/li><li><a href=\"#Dividends\">Dividends<\/a><\/li><li><a href=\"#Fundamentals-data-with-multiple-tickers-at-once\">Fundamentals data with multiple tickers at once<\/a> <ul><li><a href=\"#Comparing-by-a-particular-attribute\">Comparing by a particular attribute<\/a>  <\/li><\/ul><\/li><\/ul><\/li><li><a href=\"#How-do-I-download-trading-data\">How do I download trading data? <\/a> <ul><li><a href=\"#Market-Cap\">Market Cap<\/a><\/li><li><a href=\"#Volume\">Volume<\/a><\/li><li><a href=\"#Highs-and-Lows\">Highs and Lows<\/a> <\/li><\/ul><\/li><li><a href=\"#How-do-I-download-data-from-the-Income-Statement\">How do I download data from the Income Statement?&nbsp;<\/a><\/li><li><a href=\"#How-do-I-download-data-from-the-balance-sheet\">How do I download data from the balance sheet?<\/a><\/li><li><a href=\"#How-do-I-download-data-from-the-cashflow-statement\">How do I download data from the cashflow statement?<\/a><\/li><li><a href=\"#How-do-I-download-options-data\">How do I download options data?<\/a> <ul><li><a href=\"#How-do-I-get-Expiration-dates\">How do I get Expiration dates?<\/a><\/li><li><a href=\"#How-do-I-get-Calls-Data\">How do I get Calls Data?<\/a><\/li><li><a href=\"#How-do-I-get-Puts-Data\">How do I get Puts Data?<\/a> <\/li><\/ul><\/li><li><a href=\"#What-are-some-of-the-alternatives-to-the-Yahoo-Finance-API\">What are some of the alternatives to the Yahoo Finance API?<\/a> <ul><li><a href=\"#Alpha-Vantage\">Alpha Vantage<\/a><\/li><li><a href=\"#Polygon\">Polygon<\/a><\/li><li><a href=\"#IEX\">IEX<\/a> <\/li><\/ul><\/li><li><a href=\"#Why-did-Yahoo-Finance-decommission-their-official-API-in-2017\">Why did Yahoo Finance decommission their official API in 2017?<\/a><\/li><li><a href=\"#Final-thoughts\">Final thoughts<\/a><\/li><li><a href=\"#Link-to-download-code-used\">Link to download code used<\/a><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"What-is-the-Yahoo-Finance-API\">What is the Yahoo Finance API?<\/h2>\n\n\n\n<p>The Yahoo Finance API is a range of libraries\/APIs\/methods to obtain historical and real time data for a variety of financial markets and products, as shown on Yahoo Finance- <a href=\"https:\/\/finance.yahoo.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/finance.yahoo.com\/<\/a>.<\/p>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/05\/yahoo_homepage-2-1024x545.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>Some of the offerings include market data on Cryptocurrencies, regular currencies, stocks and bonds, fundamental and options data, and market analysis and news.<\/p>\n\n\n\n<p>Yahoo used to have its own official API but this was shutdown in 2017- we will go over some possible reasons why briefly at the end of this article.<\/p>\n\n\n\n<p>We will discuss the pros and cons of several of the unofficial APIs\/libraries that now exist to access this data, and then go through how to use one of them in detail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Why-should-I-use-the-Yahoo-Finance-API\">Why should I use the Yahoo Finance API?<\/h2>\n\n\n\n<ul><li>Free<\/li><li>Impressive range of data<\/li><li>Quick and easy to set yourself up<\/li><li>Simple<\/li><\/ul>\n\n\n\n<p>One good reason is because it can be completely <strong>free<\/strong>. However, there are also third-party APIs with more support that do charge for their higher usage plans, but even they tend to have free tier options.<\/p>\n\n\n\n<p><strong>Impressive range of data<\/strong>.<strong> <\/strong>On-top of the core standard data, the Yahoo Finance API offers extras such as options and fundamentals data as well as market news and analysis, which alternatives such as IEX and Alpha Vantage don&#8217;t always have.<\/p>\n\n\n\n<p>It&#8217;s also <strong>easy to set yourself up<\/strong>. Depending on the avenue you pick, setup ranges from a couple of lines of code to install libraries, to creating an account to access personal API keys and then just calling an API with a specific URL (depending on the data you want) and these keys.<\/p>\n\n\n\n<p><strong>It can be simple<\/strong>. Some of the libraries have documentation that fits on a single page whilst retaining functionality that is focused but sufficient for most normal use cases. While compact, it can still help you out by providing functions in the modules that do tedious legwork for you- like automatically getting data into pandas data frames where appropriate.<\/p>\n\n\n\n<p>This makes the Yahoo Finance API perfect if you&#8217;re a relative beginner and just want to dive in and get your feet wet instead of spending hours puzzling over complex documentation.<\/p>\n\n\n\n<div style=\"padding:15px 15px 15px 15px;color: #555555;background-color: #E1FFC1;border: #dddddd 2px solid\"><p>\u00bb Here are some alternative (mostly) free data sources and guides: \n\n<\/p><ul>\n  <li><a href=\"https:\/\/algotrading101.com\/learn\/quandl-guide\/\" rel=\"noopener noreferrer\">Quandl: A Step-by-Step Guide<\/a><\/li>\n  <li><a href=\"https:\/\/algotrading101.com\/learn\/google-finance-api-guide\/\" rel=\"noopener noreferrer\">Google Finance API and 9 Alternatives<\/a><\/li>\n<\/ul>  \n\n<p><\/p>\n<\/div>\n<br>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Why-shouldnt-I-use-the-Yahoo-Finance-API\">Why shouldn\u2019t I use the Yahoo Finance API?<\/h2>\n\n\n\n<ul><li>(Limitingly) simple<\/li><li>Solutions built on-top are fragile<\/li><li>Not officially for data requests<\/li><li>Can get yourself rate limited\/blacklisted<\/li><\/ul>\n\n\n\n<p><strong>It&#8217;s simple.<\/strong> While we can get access to an impressive range of data, the Yahoo Finance API sometimes lacks the bells and whistles of more specialised APIs.<\/p>\n\n\n\n<p>For instance Alpha Vantage offers fantastic ready made modules that calculate common technical analysis indicators for you- which could save an enormous amount of effort if you want to build a trading model that incorporates any of these!<\/p>\n\n\n\n<p><strong>It&#8217;s somewhat fragile.<\/strong> The Yahoo Finance API is no longer a fully official API, meaning that sometimes it does not provide all the information desired. As such, solutions attempting to gather data from Yahoo Finance use a mixture of direct API calls,  HTML data scraping and pandas table scraping depending on the function and library\/API in question.<\/p>\n\n\n\n<p>The methods that HTML scrape gather their data by unofficially scraping the Yahoo Finance website, so their functionality is dependent on Yahoo not changing the layout\/design of any of their pages.<\/p>\n\n\n\n<p>As a quick aside, data scraping works by simply downloading the HTML code of a web page, and searching through all the HTML tags to find the specific elements of a page you want.<\/p>\n\n\n\n<p>For instance below is the Yahoo Finance Amazon (&#8216;AMZN&#8217;) Income Statement page:<\/p>\n\n\n\n<figure><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/05\/data_scraping_example-3-1024x515.png\" alt=\"\" width=\"803\" height=\"403\" \/><\/figure>\n\n\n\n<p>So if a method that data scrapes is supposed to be returning you elements of an income statement, it will be searching through the page structure through many <strong>div, class<\/strong>, and <strong>span <\/strong>tags etc. for various IDs to pick out the exact values that should be returned for each metric from Yahoo&#8217;s web pages.<\/p>\n\n\n\n<p>For instance here the Gross Profit of 74,754 on 12\/31\/2019 has a <em>data-reactid<\/em> of &#8220;114&#8221;. If Yahoo Finance was ever to change the structure\/attribute tag pointing to this value and the method designed to grab this specific piece of data uses data scraping, it might return something completely incorrect, or perhaps nothing at all.<\/p>\n\n\n\n<p>Note that not all the methods for various APIs\/libraries HTML scrape. In fact most don&#8217;t, with the majority of methods using pandas table scraping (much less fragile) or direct API calls, but just bear in mind there are some methods with these vulnerabilities, and some of the libraries\/APIs are already known to have issues with certain functionalities.<\/p>\n\n\n\n<p><strong>Its not for official use<\/strong>.<strong> <\/strong>As an extension to the previous point, since some methods to access this data unofficially scrape the data, there is no guarantee their code will work in the future if Yahoo Finance changes some of it&#8217;s layout.<\/p>\n\n\n\n<p>In particular the free open source libraries have a medium risk of some components not working at some point in the future, even if historically problems have eventually been fixed.<\/p>\n\n\n\n<p>With the paid unofficial APIs, you would hope they have teams of developers that would rectify these issues ASAP, but that&#8217;s still no guarantee any strategies you build on-top of their data would not temporarily go haywire from dodgy (or non-existent) data &#8211; obviously an extreme risk to take if you have money in the game! That said we do know that <a rel=\"noreferrer noopener\" href=\"https:\/\/rapidapi.com\/apidojo\/api\/yahoo-finance1\" target=\"_blank\">RapidAPI <\/a>does use its own internal API completely, so this is perhaps a good choice if you want to use the Yahoo Finance data more securely!<\/p>\n\n\n\n<p><strong>You can get yourself rate limited\/blacklisted. <\/strong>Again because the libraries and unofficial APIs sometimes scrape data, they could get rate limited or blacklisted by Yahoo Finance at any time (for making too many scrapes).<\/p>\n\n\n\n<p>In particular with the completely free unlimited usage libraries you have no idea where this line might be, and accidentally cross it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p>As such on balance we think the Yahoo Finance API is excellent if you are a relative beginner and just want to get your feet wet\/test out some ideas with some data quickly, or need to access something like options data.<\/p>\n\n\n\n<p>However if you are planning to build a more complex and long term system that might be making hundreds to thousands of data requests per day or plan to make use of more specific information like technical analysis indicators in it&#8217;s modelling, we&#8217;d recommend going with another option that provides some of these extra functionalities built in. <\/p>\n\n\n\n<p>Alternatively, you can pay a little extra dollar for one of the official finance data API alternatives like Quandl, <a rel=\"noreferrer noopener\" href=\"https:\/\/algotrading101.com\/learn\/alpha-vantage-guide\/\" target=\"_blank\">Alpha Vantage<\/a> or <a rel=\"noreferrer noopener\" href=\"https:\/\/algotrading101.com\/learn\/iex-api-guide\/\" target=\"_blank\">IEX<\/a> where you can be sure your data won&#8217;t just be randomly cut-off one day.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Is-the-Yahoo-Finance-API-free\">Is the Yahoo Finance API free?<\/h2>\n\n\n\n<p>Yes, as we have already mentioned there are multiple ways to access Yahoo Finance data that are totally free.<\/p>\n\n\n\n<p>If you want a bit more peace of mind that you are using a solution that is supported and kept up to date, there are maintained options you can pay for too.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Is-the-Yahoo-Finance-API-usable-with-Python\">Does the Yahoo Finance API work with Python?<\/h2>\n\n\n\n<p>Yes, almost every library\/unofficial API available to access the Yahoo Finance data supports Python.<\/p>\n\n\n\n<p>Some options support a range of other languages as well, just in case Python is not your thing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"What-are-some-of-the-ways-to-access-the-Yahoo-Finance-API\">What are some of the ways to access the Yahoo Finance API?<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"RapidAPI\">RapidAPI<\/h4>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/rapidapi.com\/apidojo\/api\/yahoo-finance1\/endpoints\" target=\"_blank\">RapidAPI<\/a> is an API marketplace that supports Python, but also lets you pick from 15 different programming languages if you want something else.<\/p>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/05\/rapidapi_15languages.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>It offers a tiered pricing plan depending on your needs. There is a free option but for any serious operations the 500 requests\/month might be a bit limiting:<\/p>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/05\/rapidapi_pricing_plan-1024x406.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>You will have to sign up for an account, and deal with API keys when making requests so RapidAPI is not quite as easy to get started with and utilise as some of the other options.<\/p>\n\n\n\n<p>Also, results are almost always returned as (sometimes quite lengthy) JSONs, so you will often have to do quite a bit of preprocessing to get the data you want in the form you want- Yahoo_fin often helpfully puts the data in pandas dataframes automatically for you.<\/p>\n\n\n\n<p>That said, the range of data available is the most extensive of the options we&#8217;ve found- with RapidAPI you can get fundamentals and options data as well as market news and analysis- the latter two are not available with our other options.<\/p>\n\n\n\n<p>Also, if you head over to the <a rel=\"noreferrer noopener\" href=\"https:\/\/rapidapi.com\/\" target=\"_blank\">main page<\/a>, you&#8217;ll see that RapidAPI offers a huge range of APIs for different purposes, so familiarising how to use the site might have carryover benefit into using another API for a different project in the future.<\/p>\n\n\n\n<p>Overall, RapidAPI is a bit fiddlier to use and get started with, but if you want to make a lot of requests or have access to more detailed data and don&#8217;t mind a bit of extra work, it could be a good bet.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"yfinance\">yfinance<\/h4>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/pypi.org\/project\/yfinance\/\" target=\"_blank\">yfinance <\/a>is a popular open source library developed by <a rel=\"noreferrer noopener\" href=\"https:\/\/pypi.org\/user\/ranaroussi\/\" target=\"_blank\">Ran Aroussi<\/a>.<\/p>\n\n\n\n<p>It&#8217;s completely free and super easy to setup- a single line to install the library:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>$ pip install yfinance --upgrade --no-cache-dir<\/code><\/pre>\n\n\n\n<p>And to use, where you just create a ticker object with a ticker symbol, and then simple method calls on the object return absolutely everything you could want to know.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>import yfinance as yf\nmsft = yf.Ticker(\"MSFT\")\nprint(msft.info)<\/code><\/pre>\n\n\n\n<pre><code>Date\n2003-02-19    0.08\n2003-10-15    0.16\n2004-08-23    0.08\n2004-11-15    3.08\n2005-02-15    0.08\n              ... \n2019-05-15    0.46\n2019-08-14    0.46\n2019-11-20    0.51\n2020-02-19    0.51\n2020-05-20    0.51\nName: Dividends, Length: 66, dtype: float64<\/code><\/pre>\n\n\n\n<p>It offers less of a range of data than RapidAPI (no market news\/analysis) but is much easier to use.<\/p>\n\n\n\n<p>We won&#8217;t elaborate more here as we wrote an entire separate article on yfinance. To learn more: <a href=\"https:\/\/algotrading101.com\/learn\/yfinance-guide\/\">yfinance Library \u2013 A Complete Guide<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Yahoo_fin\">Yahoo_fin<\/h4>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"http:\/\/theautomatic.net\/yahoo_fin-documentation\/\" target=\"_blank\">Yahoo_fin <\/a>is another completely free open source library similar to yfinance, developed by the author of <a href=\"https:\/\/theautomatic.net\/\">https:\/\/theautomatic.net\/<\/a>.<\/p>\n\n\n\n<p>It is also very easy to setup and use, but again like yfinance lacks market analysis\/news- though it offers a good range a good range of fundamentals and options data.<\/p>\n\n\n\n<p>We will focus on it from now on in this article!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"How-do-I-get-started-with-the-Yahoo_fin-library\">How do I get started with the Yahoo_fin library?<\/h2>\n\n\n\n<p>If you are completely new to Python, watch this video first to learn how to set up Python and your IDE (aka coding platform): <a rel=\"noreferrer noopener\" href=\"https:\/\/course.algotrading101.com\/courses\/pt101-practical-python-for-finance-trading-masterclass\/lectures\/8476257\" target=\"_blank\">Preparing Your Python Coding Environment<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Installation\">Installation<\/h4>\n\n\n\n<p>It&#8217;s also super easy. To install the Yahoo_fin library just run the command:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>pip install yahoo_fin<\/code><\/pre>\n\n\n\n<p>If you ever need to upgrade in the future just run:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>pip install yahoo_fin --upgrade<\/code><\/pre>\n\n\n\n<p>Yahoo_fin also has a few dependencies:<\/p>\n\n\n\n<ul><li>ftplib<\/li><li>io<\/li><li>pandas<\/li><li>requests<\/li><li>requests_html<\/li><\/ul>\n\n\n\n<p>Besides requests_html, these should all come pre-installed with Anaconda.<\/p>\n\n\n\n<p>To install requests_html, it&#8217;s as easy as:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>pip install requests_html<\/code><\/pre>\n\n\n\n<p>Note requests_html requires Python 3.6+ to function. You don&#8217;t need it to use the majority of the functionality in Yahoo_fin, but there are a few functions you won&#8217;t be able to use without it. We&#8217;ll highlight which functions depend on it in a sec.<\/p>\n\n\n\n<p>Other than that, that&#8217;s it! You&#8217;re ready to get started!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Library-Layout\"><strong>Library Layout<\/strong><\/h4>\n\n\n\n<p>Just before we start looking at specific useful examples, let&#8217;s quickly go over the layout of the Yahoo_fin library.<\/p>\n\n\n\n<p>Yahoo_fin has two modules- <em>stock_info <\/em>and <em>options.<\/em><\/p>\n\n\n\n<p><em>stock_info <\/em>has the following methods:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>get_analysts_info()\nget_balance_sheet()\nget_cash_flow()\nget_data()\nget_day_gainers()\nget_day_losers()\nget_day_most_active()\nget_holders()\nget_income_statement()\nget_live_price()\nget_quote_table()\nget_top_crypto()\nget_stats()\nget_stats_valuation()\ntickers_dow()\ntickers_nasdaq()\ntickers_other()\ntickers_sp500()<\/code><\/pre>\n\n\n\n<p>And <em>options <\/em>has:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>get_calls()\nget_expiration_dates()\nget_options_chain()\nget_puts()<\/code><\/pre>\n\n\n\n<p>And there are the methods you can&#8217;t use without requests_html are:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>#stock_info module\nget_day_gainers()\nget_day_most_active()\nget_day_losers()\nget_top_crypto()\n#options module\nget_expiration_dates()\n<\/code><\/pre>\n\n\n\n<p>Right, let&#8217;s start playing around a bit with the library!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"How-do-I-download-historical-data-using-the-Yahoo-Finance-API\">How do I download historical data using the Yahoo Finance API?<\/h2>\n\n\n\n<p>Historical price data is the one thing we will probably almost always need.<\/p>\n\n\n\n<p>The method to get this in the Yahoo_fin library is <em>get_data()<\/em>.<\/p>\n\n\n\n<p>We will have to import it from the <em>stock_info<\/em> module, so we do:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>from yahoo_fin.stock_info import get_data<\/code><\/pre>\n\n\n\n<p>It takes the arguments:<\/p>\n\n\n\n<ul><li><strong>ticker<\/strong>: case insensitive ticker of the desired stock\/bond<\/li><li><strong>start_date<\/strong>: date you want the data to start from (mm\/dd\/yyyy)<\/li><li><strong>end_date<\/strong>: date you want the data to end (mm\/dd\/yyyy)<\/li><li><strong>index_as_date<\/strong>: {True, False}. Default is true. If true then the dates of the records are set as the index, else they are returned as a separate column.<\/li><li><strong>interval<\/strong>: {&#8220;1d&#8221;, &#8220;1wk&#8221;, &#8220;1mo&#8221;}. Refers to the interval to sample the data: &#8220;1d&#8221;= daily, &#8220;1wk&#8221;= weekly, &#8220;1mo&#8221;=monthly.<\/li><\/ul>\n\n\n\n<pre data-lang=\"Python\"><code>get_data(ticker, start_date = None, end_date = None, index_as_date = True, interval = \u201c1d\u201d)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Demo-with-one-ticker\">Demo with one ticker<\/h4>\n\n\n\n<p>So for example with a single ticker:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>amazon_weekly= get_data(\"amzn\", start_date=\"12\/04\/2009\", end_date=\"12\/04\/2019\", index_as_date = True, interval=\"1wk\")\namazon_weekly<\/code><\/pre>\n\n\n\n<p>Gets all of the historic data available for Amazon with a weekly interval between the two dates:<\/p>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/get_data_one-4.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>Note that:<\/p>\n\n\n\n<ul><li>The start\/end dates don&#8217;t match exactly- the returned data snaps to some date within a week&#8217;s distance as per the exact data Yahoo Finance holds by the time-frame.<\/li><li>The data is returned in a pandas dataframe.<\/li><li>The date of the records is the index of the dataframe.<\/li><\/ul>\n\n\n\n<p>This is fantastically useful as normally we would have to convert our data from JSON, load it into a pandas dataframe and set the index as the dates, all with separate lines of code.<\/p>\n\n\n\n<p>Yahoo_fin has done all the legwork and has our data ready for modelling in a single line of code!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Demo-with-multiple-tickers\">Demo with multiple tickers<\/h4>\n\n\n\n<p>If we want to collect historical data for many tickers at once, we will want to create a list of the tickers, an empty dictionary, and iterate through the list appending each pandas dataframe to the empty dictionary:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>ticker_list = [\"amzn\", \"aapl\", \"ba\"]\nhistorical_datas = {}\nfor ticker in ticker_list:\n    historical_datas[ticker] = get_data(ticker)<\/code><\/pre>\n\n\n\n<p>Now if we want to look at the historical data for a particular ticker, we just lookup that ticker in the dictionary and we will conveniently have our data in a pandas dataframe as before:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>historical_datas[\"aapl\"]<\/code><\/pre>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/historical_datas_one-1.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>This setup is particularly useful in combination with some of Yahoo_fin&#8217;s other methods if we want to quickly gather data for all the certain markets.<\/p>\n\n\n\n<p>For instance <em>tickers_dow()<\/em> returns a list of all the tickers in the Dow Jones so we can do:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>import yahoo_fin.stock_info as si\ndow_list = si.tickers_dow()<\/code><\/pre>\n\n\n\n<p>Here we are just importing the whole <strong>stock_info <\/strong>module as <strong>si,<\/strong> so we can easily call all the methods without a new import each time.<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>print(\"Tickers in Dow Jones:\", len(dow_list))\ndow_list[0:10]<\/code><\/pre>\n\n\n\n<pre><code>Tickers in Dow Jones: 30\n['AAPL', 'AXP', 'BA', 'CAT', 'CSCO', 'CVX', 'DIS', 'DOW', 'GS', 'HD']<\/code><\/pre>\n\n\n\n<p>Iterate through the list of tickers, appending each time to the dictionary again:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>dow_historical = {}\nfor ticker in dow_list:\n    dow_historical[ticker] = si.get_data(ticker, start_date=\"01\/01\/2020\", end_date=\"01\/03\/2020\", interval=\"1d\")\ndow_historical<\/code><\/pre>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/whole_dow_historical.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>And that&#8217;s it, now you have a dictionary of pandas dataframes of historical data for all the stocks in the Dow Jones!<\/p>\n\n\n\n<p>You can also use the exact same method to quickly acquire all the data for a few other markets with Yahoo_fin&#8217;s inbuilt methods:<\/p>\n\n\n\n<ul><li><strong>Nasdaq<\/strong>: <em>tickers_nasdaq()<\/em><\/li><li><strong>S&amp;P500<\/strong>: <em>tickers_sp500()<\/em><\/li><li><strong>Others<\/strong>: <em>tickers_other()<\/em><\/li><\/ul>\n\n\n\n<p style=\"padding: 15px 15px 15px 15px;color: #555555;background-color: #e1ffc1;border: #dddddd 2px solid\">\u00bb What do you do with the data you got from Yahoo Finance? Backtest your trading ideas with them! Link: <a href=\"https:\/\/algotrading101.com\/learn\/backtrader-for-backtesting\/\" target=\"_blank\" rel=\"noopener noreferrer\">Backtrader for Backtesting (Python) \u2013 A Complete Guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"How-do-I-download-fundamental-data\">How do I download fundamental data?<\/h2>\n\n\n\n<p>With Yahoo_fin there are often a few ways to get the same bit of data with different methods calls.<\/p>\n\n\n\n<p>Let&#8217;s try grabbing a few different bits of fundamentals data.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Price-to-Earnings-Ratio\">Price to Earnings Ratio<\/h4>\n\n\n\n<p>The easiest way to get the Price to Earnings (P\/E) ratio for a stock is with the <em>get_quote_table() <\/em>function.<\/p>\n\n\n\n<p>Note many of the methods that follow automatically return the data in a pandas dataframe.<\/p>\n\n\n\n<p><em>get_quote_table()<\/em> doesn&#8217;t do this by default but takes an optional <strong>dict_result <\/strong>parameter that when set to <strong>False<\/strong> also makes the method return a pandas data-frame (instead of a dictionary).<\/p>\n\n\n\n<p>It returns a really useful summary of some fundamental data:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>quote_table = si.get_quote_table(\"aapl\", dict_result=False)\nquote_table<\/code><\/pre>\n\n\n\n<pre>{'1y Target Est': 314.55,\n '52 Week Range': '174.52 - 327.85',\n 'Ask': '323.97 x 1300',\n 'Avg. Volume': 48563118.0,\n 'Beta (5Y Monthly)': 1.17,\n 'Bid': '322.30 x 1000',\n \"Day's Range\": '317.23 - 322.35',\n 'EPS (TTM)': 12.73,\n 'Earnings Date': 'Jul 28, 2020 - Aug 03, 2020',\n 'Ex-Dividend Date': 'May 08, 2020',\n 'Forward Dividend &amp; Yield': '3.28 (1.02%)',\n 'Market Cap': '1.395T',\n 'Open': 317.75,\n 'PE Ratio (TTM)': 25.29,\n 'Previous Close': 317.94,\n 'Quote Price': 321.8500061035156,\n 'Volume': 20185053.0}<\/pre>\n\n\n\n<p>So to get the P\/E ratio we can simply do:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>quote_table[\"PE Ratio (TTM)\"]<\/code><\/pre>\n\n\n\n<pre><code>25.29<\/code><\/pre>\n\n\n\n<p>We can also get the P\/E ratio with the <em>get_stats()<\/em> or <em>get_stats_valuation()<\/em> functions.<\/p>\n\n\n\n<p><em>get_stats_valuation() <\/em>actually returns the first 9 fields in <em>get_stats()<\/em>, so get_stats() is just a lot more expansive.<\/p>\n\n\n\n<p>This is what get_stats_valuation() looks like:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>si.get_stats_valuation(\"aapl\")<\/code><\/pre>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/get_stats_evalation.png\" alt=\"\" width=\"290\" height=\"301\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Dividends\">Dividends<\/h4>\n\n\n\n<p>To get the dividends expected from holding a stock we can go back to the get_quote_table() function and do:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>quote_table = si.get_quote_table(\"aapl\")\nquote_table[\"Forward Dividend &amp; Yield\"]<\/code><\/pre>\n\n\n\n<pre><code>'3.28 (1.02%)'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Fundamentals-data-with-multiple-tickers-at-once\">Fundamentals data with multiple tickers at once<\/h4>\n\n\n\n<p>Lets have a go at grabbing fundamentals data for a list of tickers at once, and then have a go at picking out a single attribute we want to rank all stocks by.<\/p>\n\n\n\n<p>Lets import the pandas module as we will need it and generate a list of the dow_tickers again.<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>import pandas as pd\n# get list of Dow tickers\ndow_list = si.tickers_dow()<\/code><\/pre>\n\n\n\n<p>This next bit of code is very much like the looping code we used before where we made a dictionary of pandas dataframes- one for each ticker, however here we are just renaming our columns to &#8220;Attribute&#8221; and &#8220;Recent&#8221;:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>dow_stats = {}\nfor ticker in dow_list:\n    temp = si.get_stats_valuation(ticker)\n    temp = temp.iloc[:,:2]\n    temp.columns = [\"Attribute\", \"Recent\"]\n    dow_stats[ticker] = temp\ndow_stats<\/code><\/pre>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/fundamentals_multiple-1024x639.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>Now we combine all of these dataframes into a single dataframe:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>combined_stats = pd.concat(dow_stats)\ncombined_stats = combined_stats.reset_index()\ncombined_stats<\/code><\/pre>\n\n\n\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/fundamentals_multiple_trimmed_1.png\" alt=\"\" width=\"466\" height=\"354\" \/><\/figure>\n\n\n\n<p>And delete the unnecessary &#8220;level_1&#8221; column and clean up the renaming column names:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>del combined_stats[\"level_1\"]\n# update column names\ncombined_stats.columns = [\"Ticker\", \"Attribute\", \"Recent\"]\ncombined_stats<\/code><\/pre>\n\n\n\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/fundamentals_multiple_trimmed_2.png\" alt=\"\" width=\"387\" height=\"388\" \/><\/figure>\n\n\n\n<p>Great, so now we have some fundamental data in the same dataframe for many stocks at once!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Comparing-by-a-particular-attribute\">Comparing by a particular attribute<\/h4>\n\n\n\n<p>We can go one step further and pick out an attribute we would like to compare between all stocks, for instance again P\/E ratio:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>pe_ratios = combined_stats[combined_stats[\"Attribute\"]==\"Trailing P\/E\"].reset_index()\npe_ratios<\/code><\/pre>\n\n\n\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/pe_ratios.png\" alt=\"\" width=\"231\" height=\"469\" \/><\/figure>\n\n\n\n<p>And we can order the &#8216;Recent&#8217; column to find the best\/worst earners:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>pe_ratios_sorted = pe_ratios.sort_values('Recent',ascending=False)\npe_ratios_sorted<\/code><\/pre>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/pe_ratios_ordered.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>How awesome- we now easily analyse a particular attribute for every stock in a market! Let&#8217;s quickly buy us some PG!<\/p>\n\n\n\n<p>Also it&#8217;s worth noting we can use the previous blocks of code to quickly put together a giant dataframe of all attributes for different tickers for most of the methods we will cover going forward.<\/p>\n\n\n\n<p>Now let&#8217;s quickly blast through a bit more of the functionality available to us!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"How-do-I-download-trading-data\">How do I download trading data?<\/h2>\n\n\n\n<p>Remember, you can get the historical open, high, low, close, volume data sampled at regular intervals from the get_data() method:<\/p>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/apple_historical.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>Other than that you can find the data nicely collected for the market cap, volume and current day highs and lows in the get_quote_table() method:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>si.get_quote_table(\"aapl\")<\/code><\/pre>\n\n\n\n<pre>{'1y Target Est': 314.55,\n '52 Week Range': '174.52 - 327.85',\n 'Ask': '321.54 x 1300',\n 'Avg. Volume': 49012080.0,\n 'Beta (5Y Monthly)': 1.17,\n 'Bid': '321.48 x 1000',\n \"Day's Range\": '320.38 - 322.47',\n 'EPS (TTM)': 12.73,\n 'Earnings Date': 'Jul 28, 2020 - Aug 03, 2020',\n 'Ex-Dividend Date': 'May 08, 2020',\n 'Forward Dividend &amp; Yield': '3.28 (1.02%)',\n 'Market Cap': '1.389T',\n 'Open': 320.74,\n 'PE Ratio (TTM)': 25.18,\n 'Previous Close': 321.85,\n 'Quote Price': 320.4599914550781,\n 'Volume': 6657073.0}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Market-Cap\">Market Cap<\/h4>\n\n\n\n<p>So to get the market cap we could do:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>si.get_quote_table(\"aapl\")[\"Market Cap\"]<\/code><\/pre>\n\n\n\n<pre><code>'1.39T'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Volume\">Volume<\/h4>\n\n\n\n<p>We can get either the 52-week average volume or current with volume with:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>si.get_quote_table(\"aapl\")[\"Avg. Volume\"]\nsi.get_quote_table(\"aapl\")[\"Volume\"]<\/code><\/pre>\n\n\n\n<pre><code>49012080.0\n7274811.0<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Highs-and-Lows\">Highs and Lows<\/h4>\n\n\n\n<p>And we can get the daily high and low with:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>si.get_quote_table(\"aapl\")[\"Day's Range\"]<\/code><\/pre>\n\n\n\n<pre><code>'319.32 - 322.47'<\/code><\/pre>\n\n\n\n<p>If we want to actually do something with the highs and lows we can split this string apart with Python&#8217;s split() method and convert the type to float:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>daily_range = si.get_quote_table(\"aapl\")[\"Day's Range\"]\ndaily_low = float(daily_range.split(\" - \")[0])\ndaily_high = float(daily_range.split(\" - \")[1])\nprint(\"daily low:\", daily_low)\nprint(\"daily high:\", daily_high)<\/code><\/pre>\n\n\n\n<pre><code>daily low: 319.32\ndaily high: 322.47<\/code><\/pre>\n\n\n\n<p>You can also find variations of these data from the get_stats() method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"How-do-I-download-data-from-the-Income-Statement\">How do I download data from the Income Statement?<\/h2>\n\n\n\n<p>Yahoo_fin has a fantastically useful <em>get_income_statement()<\/em> function that does this all in one for us:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>income_statement = si.get_income_statement(\"aapl\")\nincome_statement<\/code><\/pre>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/get_income_statement-1-961x1024.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>Remember you can always transpose the indices and columns if you want to perform analysis on the data-frame by time period in a more normal manner:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>transposed = income_statement.transpose()\ntransposed<\/code><\/pre>\n\n\n\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/transposed_income_statement-1024x307.png\" alt=\"\" width=\"783\" height=\"233\" \/><\/figure>\n\n\n\n<p>&#8220;<strong>ttm<\/strong>&#8221; stands for <strong>Trailing Twelve Months<\/strong> and typically shows either the most recent twelve months of a company&#8217;s operations or the last twelve months before a major event- like an acquisition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"How-do-I-download-data-from-the-balance-sheet\">How do I download data from the balance sheet?<\/h2>\n\n\n\n<p>Again, super easy with the <em>get_balance_sheet()<\/em> function:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>balance_sheet = si.get_balance_sheet(\"aapl\")\nbalance_sheet<\/code><\/pre>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/balance_sheet.png\" alt=\"\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"How-do-I-download-data-from-the-cashflow-statement\">How do I download data from the cashflow statement?<\/h2>\n\n\n\n<p>Using the <em>get_cash_flow()<\/em> function if you hadn&#8217;t spotted the pattern yet!<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>cash_flow_statement = si.get_cash_flow(\"aapl\")\ncash_flow_statement<\/code><\/pre>\n\n\n\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/cash_flow_statement.png\" alt=\"\" width=\"595\" height=\"334\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"How-do-I-download-options-data\">How do I download options data?<\/h2>\n\n\n\n<p>So far we&#8217;ve only been using the <em>stock_info <\/em>module, now finally we will have a little play with the <em>options<\/em> module.<\/p>\n\n\n\n<p>Lets import the options module with a reference first:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>import yahoo_fin.options as ops<\/code><\/pre>\n\n\n\n<p>Briefly, options are contracts giving a trader the right, but not the <em>obligation<\/em>, to buy (call) or sell (put) the underlying asset they represent at a specific price on or before a certain date.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"How-do-I-get-Expiration-dates\">How do I get Expiration dates?<\/h4>\n\n\n\n<p>Use the <em>get_expiration_dates()<\/em> method:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>expiration_dates = ops.get_expiration_dates(\"aapl\")\nexpiration_dates<\/code><\/pre>\n\n\n\n<pre><code>['June 5, 2020',\n 'June 12, 2020',\n 'June 19, 2020',\n 'June 26, 2020',\n 'July 2, 2020',\n 'July 10, 2020',\n 'July 17, 2020',\n 'September 18, 2020',\n 'October 16, 2020',\n 'November 20, 2020',\n 'December 18, 2020',\n 'January 15, 2021',\n 'June 18, 2021',\n 'September 17, 2021',\n 'January 21, 2022',\n 'June 17, 2022',\n 'September 16, 2022']<\/code><\/pre>\n\n\n\n<p>Note that the following functions all take an optional <strong>Expiration Date<\/strong> parameter, but it should be given in the form <strong>&#8216;mm\/dd\/yyyy&#8217;<\/strong>, so you will have to do a bit of conversion if you want to use a specific date.<\/p>\n\n\n\n<p>In the absence of a date being passed (ticker only), the functions will return the earliest upcoming expiration date&#8217;s data.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"How-do-I-get-Calls-Data\">How do I get Calls Data?<\/h4>\n\n\n\n<p>You use the <em>get_calls() <\/em>method:<\/p>\n\n\n\n<pre data-lang=\"Python\"><code>ops.get_calls(\"aapl\")<\/code><\/pre>\n\n\n\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/get_calls-1024x334.png\" alt=\"\" width=\"647\" height=\"211\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"How-do-I-get-Puts-Data\">How do I get Puts Data?<\/h4>\n\n\n\n<p>Similarly, you use <em>get_puts():<\/em><\/p>\n\n\n\n<pre data-lang=\"Python\"><code>ops.get_puts(\"aapl\", \"09\/16\/2022\") <\/code><\/pre>\n\n\n\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/get_puts-1024x426.png\" alt=\"\" width=\"697\" height=\"290\" \/><\/figure>\n\n\n\n<p>Finally if you want both the <strong>calls<\/strong> and <strong>puts <\/strong>data together, use the <em>get_options_chain()<\/em> method.<\/p>\n\n\n\n<p>It returns a dictionary with two keys- &#8220;calls&#8221; and &#8220;puts&#8221;, each with their respective tables as data-frames as the values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"What-are-some-of-the-alternatives-to-the-Yahoo-Finance-API\">What are some of the alternatives to the Yahoo Finance API?<\/h2>\n\n\n\n<p>If you aren&#8217;t bothered about using data specifically from Yahoo Finance, then the following alternative APIs are worth considering:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Alpha-Vantage\">Alpha Vantage<\/h4>\n\n\n\n<p>As we&#8217;ve already mentioned, a really big plus is that in addition to price data, Alpha Vantage provides more than 50 common technical indicators- perfect if you&#8217;re looking to build a model that uses some of these.<\/p>\n\n\n\n<p>Their free tier is also very generous- a useful 500 requests per day (with a 5 requests per minute limit)- much better than RapidAPI&#8217;s 500 per month!<\/p>\n\n\n\n<p>We have a guide specifically on Alpha Vantage on this website, check out <a rel=\"noreferrer noopener\" href=\"https:\/\/algotrading101.com\/learn\/alpha-vantage-guide\/\" target=\"_blank\">https:\/\/algotrading101.com\/learn\/alpha-vantage-guide\/<\/a> if you want to learn more!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Polygon\">Polygon<\/h4>\n\n\n\n<p>If you want blazing speed and high performance, <a rel=\"noreferrer noopener\" href=\"https:\/\/polygon.io\/\" target=\"_blank\">Polgon.io<\/a> might just be the way to go.<\/p>\n\n\n\n<p>They connect to data directly from various exchanges, and their Enterprise plan claims a ridiculous &lt; 1 millisecond delay.<\/p>\n\n\n\n<p>This all comes with a hefty price tag though, with their cheapest option being $49\/month (besides a 7-day free trial).<\/p>\n\n\n\n<p>Definitely worth considering if you are building a very serious system.<\/p>\n\n\n\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/polygon_pricing-1024x533.png\" alt=\"\" width=\"732\" height=\"380\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"IEX\">IEX<\/h4>\n\n\n\n<p>IEX offers an absolutely enormous range of data- check out the left hand summary bar of: <a href=\"https:\/\/iexcloud.io\/docs\/api\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/iexcloud.io\/docs\/api\/<\/a><\/p>\n\n\n\n<p>The documentation is very extensive, making it hard to dive into quickly.<\/p>\n\n\n\n<p>It also has complicated rate limits where different data is weighted differently when it comes to working out your usage. So you will need to run some calculations to make sure you stay within your desired limits, but my gosh can you find data for almost anything there!<\/p>\n\n\n\n<p>We have a guide for it on this site as well, check out: <a rel=\"noreferrer noopener\" href=\"https:\/\/algotrading101.com\/learn\/iex-api\/\" target=\"_blank\">https:\/\/algotrading101.com\/learn\/iex-api\/<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Why-did-Yahoo-Finance-decommission-their-official-API-in-2017\">Why did Yahoo Finance decommission their official API in 2017?<\/h2>\n\n\n\n<p>Yahoo decommissioned their official API on May 15th 2017.<\/p>\n\n\n\n<p>An official reason was never given but it is widely thought to be an issue with large scale misuse of data because so many people were using it to build personal applications (there was no requirement to be logged into Yahoo or anything!).<\/p>\n\n\n\n<p>This is the error message that suddenly appeared when people attempted to call the API to download:<\/p>\n\n\n\n<figure><img decoding=\"async\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/yahoo_error_message-1.png\" alt=\"\" \/><\/figure>\n\n\n\n<p>Downloading the data was against its intended use (to view only) and in violation of the terms and conditions of the parties the data was gathered from (exchanges and financial institutions don&#8217;t like giving away their data for free!).<\/p>\n\n\n\n<p>These days, we have a range of unofficial alternatives, some of which we have of course covered in this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Final-thoughts\">Final thoughts<\/h2>\n\n\n\n<p>The Yahoo Finance API offers a decent range of data and can be accessed totally freely.<\/p>\n\n\n\n<p>Some of the libraries developed to access it unofficially are fantastically easy to use, so it&#8217;s perfect to experiment with if you&#8217;re a beginner.<\/p>\n\n\n\n<p>Some solutions built on-top do however rely on unofficial scraping to gather the data. There is also the risk of Yahoo Finance changing it\u2019s API URL or methods, considering it is unofficial after all. <\/p>\n\n\n\n<p>Thus, we absolutely don&#8217;t recommend it to build mission critical systems where you have a lot of money on the line!<\/p>\n\n\n\n<p>Use an official alternative that is connected directly to exchange data instead, like <a rel=\"noreferrer noopener\" href=\"https:\/\/iexcloud.io\/?gclid=CjwKCAjwt-L2BRA_EiwAacX32e8Yyya1WN3JN3nP2fCOgK3QqIbL1pnSTCy1u1zTVCjTnU4vXG87EhoCpNIQAvD_BwE\" target=\"_blank\">IEX <\/a>or <a rel=\"noreferrer noopener\" href=\"https:\/\/polygon.io\/\" target=\"_blank\">Polygon<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Link-to-download-code-used\">Link to download code used<\/h2>\n\n\n\n<p>You can find the code used in this article <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/GregBland\/yahoo_fin_article\" target=\"_blank\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_3404\" class=\"pvc_stats total_only  \" data-element-id=\"3404\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>Table of Contents What is the Yahoo Finance API? Why should I use the Yahoo Finance API? Why shouldn\u2019t I use the Yahoo Finance API? Is the Yahoo Finance API free? Is the Yahoo Finance API usable with Python? What are some of the ways to access the Yahoo Finance API? RapidAPI yfinance Yahoo_fin How [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":3500,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_lmt_disableupdate":"no","_lmt_disable":"no","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[3,2],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Yahoo Finance API - A Complete Guide - AlgoTrading101 Blog<\/title>\n<meta name=\"description\" content=\"The Yahoo Finance API is a range of libraries\/APIs\/methods to obtain historical and real time data for a variety of financial markets and products.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Yahoo Finance API - A Complete Guide - AlgoTrading101 Blog\" \/>\n<meta property=\"og:description\" content=\"The Yahoo Finance API is a range of libraries\/APIs\/methods to obtain historical and real time data for a variety of financial markets and products.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"Quantitative Trading Ideas and Guides - AlgoTrading101 Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-06T14:59:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-11T15:46:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/yahoo-finance-api-guide.png\" \/>\n\t<meta property=\"og:image:width\" content=\"600\" \/>\n\t<meta property=\"og:image:height\" content=\"450\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Greg Bland\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Greg Bland\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"24 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Yahoo Finance API - A Complete Guide - AlgoTrading101 Blog","description":"The Yahoo Finance API is a range of libraries\/APIs\/methods to obtain historical and real time data for a variety of financial markets and products.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/","og_locale":"en_US","og_type":"article","og_title":"Yahoo Finance API - A Complete Guide - AlgoTrading101 Blog","og_description":"The Yahoo Finance API is a range of libraries\/APIs\/methods to obtain historical and real time data for a variety of financial markets and products.","og_url":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/","og_site_name":"Quantitative Trading Ideas and Guides - AlgoTrading101 Blog","article_published_time":"2020-06-06T14:59:00+00:00","article_modified_time":"2021-01-11T15:46:33+00:00","og_image":[{"width":600,"height":450,"url":"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/06\/yahoo-finance-api-guide.png","type":"image\/png"}],"author":"Greg Bland","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Greg Bland","Est. reading time":"24 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/#article","isPartOf":{"@id":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/"},"author":{"name":"Greg Bland","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/person\/8cf8b0040daa7d1114310693ec6f375b"},"headline":"Yahoo Finance API &#8211; A Complete Guide","datePublished":"2020-06-06T14:59:00+00:00","dateModified":"2021-01-11T15:46:33+00:00","mainEntityOfPage":{"@id":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/"},"wordCount":4067,"publisher":{"@id":"https:\/\/algotrading101.com\/learn\/#organization"},"articleSection":["Programming","Trading"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/","url":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/","name":"Yahoo Finance API - A Complete Guide - AlgoTrading101 Blog","isPartOf":{"@id":"https:\/\/algotrading101.com\/learn\/#website"},"datePublished":"2020-06-06T14:59:00+00:00","dateModified":"2021-01-11T15:46:33+00:00","description":"The Yahoo Finance API is a range of libraries\/APIs\/methods to obtain historical and real time data for a variety of financial markets and products.","breadcrumb":{"@id":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/algotrading101.com\/learn\/yahoo-finance-api-guide\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/algotrading101.com\/learn\/"},{"@type":"ListItem","position":2,"name":"Yahoo Finance API &#8211; A Complete Guide"}]},{"@type":"WebSite","@id":"https:\/\/algotrading101.com\/learn\/#website","url":"https:\/\/algotrading101.com\/learn\/","name":"Quantitative Trading Ideas and Guides - AlgoTrading101 Blog","description":"Authentic Stories about Algorithmic trading, coding and life.","publisher":{"@id":"https:\/\/algotrading101.com\/learn\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/algotrading101.com\/learn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/algotrading101.com\/learn\/#organization","name":"AlgoTrading101","url":"https:\/\/algotrading101.com\/learn\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/logo\/image\/","url":"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/11\/AlgoTrading101-Lucas-Liew.jpg","contentUrl":"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/11\/AlgoTrading101-Lucas-Liew.jpg","width":1200,"height":627,"caption":"AlgoTrading101"},"image":{"@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/person\/8cf8b0040daa7d1114310693ec6f375b","name":"Greg Bland","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/abb32ce336d0c5b8e5278a3a38aa7d70?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/abb32ce336d0c5b8e5278a3a38aa7d70?s=96&d=mm&r=g","caption":"Greg Bland"},"url":"https:\/\/algotrading101.com\/learn\/author\/nemesis10192\/"}]}},"modified_by":"Lucas Liew","_links":{"self":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts\/3404"}],"collection":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/comments?post=3404"}],"version-history":[{"count":77,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts\/3404\/revisions"}],"predecessor-version":[{"id":9363,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts\/3404\/revisions\/9363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/media\/3500"}],"wp:attachment":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/media?parent=3404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/categories?post=3404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/tags?post=3404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}