community: Add PolygonAggregates tool (#18882)

**Description:**
In this PR, I am adding a `PolygonAggregates` tool, which can be used to
get historical stock price data (called aggregates by Polygon) for a
given ticker.

Polygon
[docs](https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to)
for this endpoint.

**Twitter**: 
[@virattt](https://twitter.com/virattt)
This commit is contained in:
Virat Singh
2024-03-11 14:58:10 -04:00
committed by GitHub
parent 2d172181e0
commit cafffe8a21
8 changed files with 197 additions and 7 deletions

View File

@@ -41,7 +41,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 2,
"id": "ac4910f8",
"metadata": {
"id": "ac4910f8",
@@ -49,6 +49,7 @@
},
"outputs": [],
"source": [
"from langchain_community.tools.polygon.aggregates import PolygonAggregates\n",
"from langchain_community.tools.polygon.financials import PolygonFinancials\n",
"from langchain_community.tools.polygon.last_quote import PolygonLastQuote\n",
"from langchain_community.tools.polygon.ticker_news import PolygonTickerNews\n",
@@ -86,7 +87,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Tool output: {\"P\": 181.18, \"S\": 7, \"T\": \"AAPL\", \"X\": 11, \"i\": [604], \"p\": 181.13, \"q\": 704855, \"s\": 3, \"t\": 1709213573791533513, \"x\": 8, \"y\": 1709213573791191178, \"z\": 3}\n"
"Tool output: {\"P\": 170.5, \"S\": 2, \"T\": \"AAPL\", \"X\": 11, \"i\": [604], \"p\": 170.48, \"q\": 106666224, \"s\": 1, \"t\": 1709945992614283138, \"x\": 12, \"y\": 1709945992614268948, \"z\": 3}\n"
]
}
],
@@ -116,7 +117,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"id": "174e2556-eb3e-48a4-bde6-9a3309fae9c9",
"metadata": {},
"outputs": [
@@ -124,7 +125,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Latest price for AAPL is $181.13\n"
"Latest price for AAPL is $170.48\n"
]
}
],
@@ -134,6 +135,57 @@
"print(f\"Latest price for {ticker} is ${latest_price}\")"
]
},
{
"cell_type": "markdown",
"id": "1f478364-f41b-47f2-ac4b-d3154f1c7faa",
"metadata": {},
"source": [
"### Get aggregates (historical prices) for ticker"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "5e14e091-3150-4bd5-bfd3-de17caa75ee1",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.tools.polygon.aggregates import PolygonAggregatesSchema\n",
"\n",
"# Define param\n",
"params = PolygonAggregatesSchema(\n",
" ticker=ticker,\n",
" timespan=\"day\",\n",
" timespan_multiplier=1,\n",
" from_date=\"2024-03-01\",\n",
" to_date=\"2024-03-08\",\n",
")\n",
"# Get aggregates for ticker\n",
"aggregates_tool = PolygonAggregates(api_wrapper=api_wrapper)\n",
"aggregates = aggregates_tool.run(tool_input=params.dict())\n",
"aggregates_json = json.loads(aggregates)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "a01f3888-d233-400d-b8c4-298d27c8f793",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total aggregates: 6\n",
"Aggregates: [{'v': 73450582.0, 'vw': 179.0322, 'o': 179.55, 'c': 179.66, 'h': 180.53, 'l': 177.38, 't': 1709269200000, 'n': 911077}, {'v': 81505451.0, 'vw': 174.8938, 'o': 176.15, 'c': 175.1, 'h': 176.9, 'l': 173.79, 't': 1709528400000, 'n': 1167166}, {'v': 94702355.0, 'vw': 170.3234, 'o': 170.76, 'c': 170.12, 'h': 172.04, 'l': 169.62, 't': 1709614800000, 'n': 1108820}, {'v': 68568907.0, 'vw': 169.5506, 'o': 171.06, 'c': 169.12, 'h': 171.24, 'l': 168.68, 't': 1709701200000, 'n': 896297}, {'v': 71763761.0, 'vw': 169.3619, 'o': 169.15, 'c': 169, 'h': 170.73, 'l': 168.49, 't': 1709787600000, 'n': 825405}, {'v': 76267041.0, 'vw': 171.5322, 'o': 169, 'c': 170.73, 'h': 173.7, 'l': 168.94, 't': 1709874000000, 'n': 925213}]\n"
]
}
],
"source": [
"print(f\"Total aggregates: {len(aggregates_json)}\")\n",
"print(f\"Aggregates: {aggregates_json}\")"
]
},
{
"cell_type": "markdown",
"id": "04f1b612-f91f-471c-8264-9cc8c14bdaef",