Skip to main content

Polygon Stock Market API Tools

Polygon The Polygon.io Stocks API provides REST endpoints that let you query the latest market data from all US stock exchanges.

This notebook uses tools to get stock market data like the latest quote and news for a ticker from Polygon.

import getpass
import os

os.environ["POLYGON_API_KEY"] = getpass.getpass()
from langchain_community.tools.polygon.last_quote import PolygonLastQuote
from langchain_community.tools.polygon.ticker_news import PolygonTickerNews
from langchain_community.utilities.polygon import PolygonAPIWrapper
api_wrapper = PolygonAPIWrapper()
ticker = "AAPL"

Get latest quote for ticker

# Get the last quote for ticker
last_quote_tool = PolygonLastQuote(api_wrapper=api_wrapper)
last_quote = last_quote_tool.run(ticker)
print(f"Tool output: {last_quote}")
Tool output: {"P": 180.99, "S": 5, "T": "AAPL", "X": 11, "i": [604], "p": 180.98, "q": 31662815, "s": 2, "t": 1708445246516556649, "x": 21, "y": 1708445246516369924, "z": 3}
import json

# Convert the last quote response to JSON
last_quote = last_quote_tool.run(ticker)
last_quote_json = json.loads(last_quote)
# Print the latest price for ticker
latest_price = last_quote_json["p"]
print(f"Latest price for {ticker} is ${latest_price}")
Latest price for AAPL is $180.74

Get latest news for ticker

ticker_news_tool = PolygonTickerNews(api_wrapper=api_wrapper)
ticker_news = ticker_news_tool.run(ticker)
print(f"Tool output: {ticker_news}")
Tool output: [{"id": "RckckqTS-K2zI6WRyycBB0HonfGLc2MPTQROWsbpKSA", "publisher": {"name": "The Motley Fool", "homepage_url": "https://www.fool.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/themotleyfool.svg", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/themotleyfool.ico"}, "title": "Should You Worry About Apple's Slowdown in China?", "author": "newsfeedback@fool.com (Adria Cimino)", "published_utc": "2024-02-20T15:10:00Z", "article_url": "https://www.fool.com/investing/2024/02/20/should-you-worry-about-apples-slowdown-in-china/", "tickers": ["AAPL"], "image_url": "https://g.foolcdn.com/editorial/images/765910/aapl.png", "description": "A local smartphone giant is gaining market share.", "keywords": ["investing"]}, {"id": "5goYCKw3ZsHlJYwN1XX8AJCyybZuvfJeoUz38SzlYBM", "publisher": {"name": "The Motley Fool", "homepage_url": "https://www.fool.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/themotleyfool.svg", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/themotleyfool.ico"}, "title": "Vanguard Total Stock Market ETF: Buy, Sell, or Hold?", "author": "newsfeedback@fool.com (Justin Pope)", "published_utc": "2024-02-20T12:30:00Z", "article_url": "https://www.fool.com/investing/2024/02/20/vanguard-total-stock-market-etf-buy-sell-or-hold/", "tickers": ["VTI", "AAPL", "MSFT", "GOOGL", "AMZN", "NVDA", "META", "TSLA", "GOOG"], "image_url": "https://g.foolcdn.com/editorial/images/765208/getty-buy-sell-hold-stocks-decide-ratings-analysts.jpg", "description": "This is the ultimate tool to diversify your portfolio.", "keywords": ["investing"]}, {"id": "F2nshszd6rBX-JJMHtbPuA325ZBizcq7iIJ7PBK_114", "publisher": {"name": "MarketWatch", "homepage_url": "https://www.marketwatch.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/marketwatch.svg", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/marketwatch.ico"}, "title": "Wall Street\u2019s \u2018mob psychology\u2019 could fuel a dangerous stock meltup, warns top strategist", "author": "MarketWatch", "published_utc": "2024-02-20T12:20:00Z", "article_url": "https://www.marketwatch.com/story/wall-streets-mob-psychology-could-fuel-a-dangerous-stock-meltup-warns-top-strategist-fc8647c6", "tickers": ["NVDA", "GOOGL", "AMZN", "AAPL", "META", "MSFT", "NFLX", "TSLA", "COF", "DFS", "WMT", "VZIO", "HD", "PANW", "CZR", "SMCI", "OCGN", "NIO", "MARA", "AMD", "COIN"], "amp_url": "https://www.marketwatch.com/amp/story/wall-streets-mob-psychology-could-fuel-a-dangerous-stock-meltup-warns-top-strategist-fc8647c6", "image_url": "https://images.mktw.net/im-59050122/social", "description": "Ed Yardeni says giddy Wall Street analysts could be the undoing of this stock market."}, {"id": "uvUtqCvuVV-wyCECL0DTIbHdqAtEgviJqSrPurvrqEQ", "publisher": {"name": "Zacks Investment Research", "homepage_url": "https://www.zacks.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/zacks.png", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/zacks.ico"}, "title": "Should Vanguard Mega Cap ETF (MGC) Be on Your Investing Radar?", "author": "Zacks Equity Research", "published_utc": "2024-02-20T11:20:07Z", "article_url": "https://www.zacks.com/stock/news/2228257/should-vanguard-mega-cap-etf-mgc-be-on-your-investing-radar", "tickers": ["MGC", "AMZN", "AAPL", "MSFT", "SPY", "IVV"], "amp_url": "https://www.zacks.com/amp/stock/news/2228257/should-vanguard-mega-cap-etf-mgc-be-on-your-investing-radar", "image_url": "https://staticx-tuner.zacks.com/images/default_article_images/default17.jpg", "description": "Style Box ETF report for MGC"}, {"id": "RRxHgtvw7_7Ql0QhUphMlzXdUjWhJbpZVjHqR7N5TCg", "publisher": {"name": "Zacks Investment Research", "homepage_url": "https://www.zacks.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/zacks.png", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/zacks.ico"}, "title": "Should Vanguard S&P 500 ETF (VOO) Be on Your Investing Radar?", "author": "Zacks Equity Research", "published_utc": "2024-02-20T11:20:06Z", "article_url": "https://www.zacks.com/stock/news/2228264/should-vanguard-sp-500-etf-voo-be-on-your-investing-radar", "tickers": ["VOO", "AMZN", "AAPL", "MSFT", "SPY", "IVV"], "amp_url": "https://www.zacks.com/amp/stock/news/2228264/should-vanguard-sp-500-etf-voo-be-on-your-investing-radar", "image_url": "https://staticx-tuner.zacks.com/images/default_article_images/default24.jpg", "description": "Style Box ETF report for VOO"}, {"id": "QBaNxnCVlXTHlVAChWRdM5RrWCD9f20qp6aUxfdxrEI", "publisher": {"name": "Zacks Investment Research", "homepage_url": "https://www.zacks.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/zacks.png", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/zacks.ico"}, "title": "Should Fidelity Nasdaq Composite Index ETF (ONEQ) Be on Your Investing Radar?", "author": "Zacks Equity Research", "published_utc": "2024-02-20T11:20:05Z", "article_url": "https://www.zacks.com/stock/news/2228269/should-fidelity-nasdaq-composite-index-etf-oneq-be-on-your-investing-radar", "tickers": ["ONEQ", "AMZN", "AAPL", "MSFT", "QQQ", "VUG"], "amp_url": "https://www.zacks.com/amp/stock/news/2228269/should-fidelity-nasdaq-composite-index-etf-oneq-be-on-your-investing-radar", "image_url": "https://staticx-tuner.zacks.com/images/default_article_images/default29.jpg", "description": "Style Box ETF report for ONEQ"}, {"id": "aVYhWKzOHm1Y8k8z9ka0xrCCI-tiUWYDdsrPuDFzG_g", "publisher": {"name": "The Motley Fool", "homepage_url": "https://www.fool.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/themotleyfool.svg", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/themotleyfool.ico"}, "title": "Apple Is Synonymous With iPhones, But Its Largest Gross Margin Comes From Somewhere Else Entirely", "author": "newsfeedback@fool.com (Neil Patel)", "published_utc": "2024-02-20T11:07:00Z", "article_url": "https://www.fool.com/investing/2024/02/20/apple-synonymous-iphones-devices-gross-margin/", "tickers": ["AAPL"], "image_url": "https://g.foolcdn.com/editorial/images/765917/aapl_revenue_bar.png", "description": "Investors need to pay attention to a budding segment.", "keywords": ["investing"]}, {"id": "6uW_52HjWOj3sKeCdPrqN4ZHmIbJAZxXV5naPVzjaHw", "publisher": {"name": "The Motley Fool", "homepage_url": "https://www.fool.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/themotleyfool.svg", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/themotleyfool.ico"}, "title": "Warren Buffett Has 51% of Berkshire Hathaway's $370 Billion Portfolio Invested in 2 Stocks", "author": "newsfeedback@fool.com (Trevor Jennewine)", "published_utc": "2024-02-20T10:45:00Z", "article_url": "https://www.fool.com/investing/2024/02/20/warren-buffett-51-portfolio-invested-in-2-stocks/", "tickers": ["AAPL", "KO", "BRK.A", "BRK.B"], "image_url": "https://g.foolcdn.com/editorial/images/765625/market-3.jpg", "description": "Warren Buffett's Berkshire Hathaway has more than half of its stock portfolio invested in Apple and Coca-Cola.", "keywords": ["investing"]}, {"id": "aE6I7jULkhZEsKWnVsRNUaPYNfsHBC89wARk4mf6iPQ", "publisher": {"name": "Investing.com", "homepage_url": "https://www.investing.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/investing.png", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/investing.ico"}, "title": "20-Year High Bullish Sentiment Meets Record Tech Allocation: Recipe for Disaster?", "author": "Investing.com", "published_utc": "2024-02-20T10:24:00Z", "article_url": "https://www.investing.com/analysis/20year-high-bullish-sentiment-meets-record-tech-allocation-recipe-for-disaster-200646188", "tickers": ["MSFT", "AAPL", "NVDA", "AMZN", "GOOGL", "META"], "amp_url": "https://m.investing.com/analysis/20year-high-bullish-sentiment-meets-record-tech-allocation-recipe-for-disaster-200646188?ampMode=1", "image_url": "https://i-invdn-com.investing.com/redesign/images/seo/investingcom_analysis_og.jpg"}, {"id": "EJIIij7T9dEbvogmzWS_P_1cLvVskocSSLrM3p2V_qE", "publisher": {"name": "Investing.com", "homepage_url": "https://www.investing.com/", "logo_url": "https://s3.polygon.io/public/assets/news/logos/investing.png", "favicon_url": "https://s3.polygon.io/public/assets/news/favicons/investing.ico"}, "title": "3 Wide-Moat Stocks Worth Buying in 2024", "author": "The Tokenist", "published_utc": "2024-02-20T07:09:00Z", "article_url": "https://www.investing.com/analysis/3-widemoat-stocks-worth-buying-in-2024-200646181", "tickers": ["KO", "GOOGL", "AAPL", "META", "AMZN", "MSFT", "NVDA", "AX", "QTWO", "ADBE", "CRM"], "amp_url": "https://m.investing.com/analysis/3-widemoat-stocks-worth-buying-in-2024-200646181?ampMode=1", "image_url": "https://i-invdn-com.investing.com/redesign/images/seo/investingcom_analysis_og.jpg"}]
# Convert the news response to JSON array
ticker_news_json = json.loads(ticker_news)
print(f"Total news items: {len(ticker_news_json)}")
Total news items: 10
# Inspect the first news item
news_item = ticker_news_json[0]
print(f"Title: {news_item['title']}")
print(f"Description: {news_item['description']}")
print(f"Publisher: {news_item['publisher']['name']}")
print(f"URL: {news_item['article_url']}")
Title: Should You Worry About Apple's Slowdown in China?
Description: A local smartphone giant is gaining market share.
Publisher: The Motley Fool
URL: https://www.fool.com/investing/2024/02/20/should-you-worry-about-apples-slowdown-in-china/