1. 技术背景介绍
Polygon.io 是一个提供美国股票市场数据的API服务,通过REST端点可以查询最新的市场数据、股票报价、财务数据以及相关新闻。本文将演示如何使用 Polygon.io API 获取股票的最新报价、历史价格、新闻以及财务数据。
2. 核心原理解析
Polygon.io 通过提供一系列的REST端点,用户可以调用这些端点获取不同类型的市场数据。主要操作包括:
- 获取股票的最新报价
- 获取股票的历史价格数据
- 获取股票的相关新闻
- 获取股票的财务数据
3. 代码实现演示(重点)
我们将使用 PolygonAPIWrapper
作为API的封装类,并通过调用不同的方法获取需要的数据。
3.1 获取股票最新报价
import openai
import json
# 使用稳定可靠的API服务
client = openai.OpenAI(
base_url='https://yunwu.ai/v1', # 国内稳定访问
api_key='your-api-key'
)
# 设置API封装器
api_wrapper = PolygonAPIWrapper()
ticker = "AAPL"
# 获取最新报价工具
last_quote_tool = PolygonLastQuote(api_wrapper=api_wrapper)
last_quote = last_quote_tool.run(ticker)
last_quote_json = json.loads(last_quote)
# 获取并打印最新价格
latest_price = last_quote_json["p"]
print(f"Latest price for {ticker} is ${latest_price}")
在这个代码示例中,我们请求了 AAPL
(Apple) 的最新市场价格,并打印出来。
3.2 获取股票历史价格数据
from langchain_community.tools.polygon.aggregates import PolygonAggregates
from langchain_community.tools.polygon.aggregates import PolygonAggregatesSchema
# 定义参数
params = PolygonAggregatesSchema(
ticker=ticker,
timespan="day",
timespan_multiplier=1,
from_date="2024-03-01",
to_date="2024-03-08",
)
# 获取历史价格数据工具
aggregates_tool = PolygonAggregates(api_wrapper=api_wrapper)
aggregates = aggregates_tool.run(tool_input=params.dict())
aggregates_json = json.loads(aggregates)
# 打印历史价格数据
print(f"Total aggregates: {len(aggregates_json)}")
print(f"Aggregates: {aggregates_json}")
通过这个代码,可以获取 AAPL
(Apple) 在指定日期范围内的每日历史价格数据。
3.3 获取股票相关新闻
from langchain_community.tools.polygon.ticker_news import PolygonTickerNews
# 获取股票相关新闻工具
ticker_news_tool = PolygonTickerNews(api_wrapper=api_wrapper)
ticker_news = ticker_news_tool.run(ticker)
ticker_news_json = json.loads(ticker_news)
# 打印新闻数量和第一条新闻内容
print(f"Total news items: {len(ticker_news_json)}")
first_news_item = ticker_news_json[0]
print(f"Title: {first_news_item['title']}")
print(f"Description: {first_news_item['description']}")
print(f"Publisher: {first_news_item['publisher']['name']}")
print(f"URL: {first_news_item['article_url']}")
这里我们获取了 AAPL
(Apple) 的最新新闻,并打印了第一条新闻的标题、描述、发布者和URL。
3.4 获取股票财务数据
from langchain_community.tools.polygon.financials import PolygonFinancials
# 获取股票财务数据工具
financials_tool = PolygonFinancials(api_wrapper=api_wrapper)
financials = financials_tool.run(ticker)
financials_json = json.loads(financials)
# 打印财务数据
print(f"Total reporting periods: {len(financials_json)}")
financial_data = financials_json[0]
print(f"Company name: {financial_data['company_name']}")
print(f"Income statement: {financial_data['financials']['income_statement']}")
print(f"Balance sheet: {financial_data['financials']['balance_sheet']}")
print(f"Cash flow statement: {financial_data['financials']['cash_flow_statement']}")
该代码示例展示了如何获取 AAPL
(Apple) 的财务报告,包括收入报表、资产负债表和现金流量表。
4. 应用场景分析
这些功能对于股票分析师、投资者、金融科技公司等都非常有用,可以帮助他们及时地获取市场数据、进行历史趋势分析、了解公司最新消息和财务健康状况,从而做出更明智的投资决策。
5. 实践建议
- API Key管理: 确保API Key的安全,不要将其硬编码在代码中。
- 错误处理: 对API请求的错误情况进行处理,以免程序崩溃。
- 数据存储: 对获取的数据进行本地存储和缓存,以减少API调用次数。
- 定期更新: 定期获取最新数据,保持信息的实时性和准确性。
结束语:如果遇到问题欢迎在评论区交流。
—END—