-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolygon_tools.py
More file actions
101 lines (84 loc) · 2.74 KB
/
polygon_tools.py
File metadata and controls
101 lines (84 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
from datetime import datetime
from polygon import RESTClient
from pydantic_ai import RunContext
from typing import Dict, Any
import asyncio
import dotenv
dotenv.load_dotenv()
client = RESTClient(api_key=os.getenv("POLYGON_API_KEY"))
TEST_MODE = True
async def get_financial_statements(ticker: RunContext[str]) -> Dict[str, Any]:
"""Get historical financial statements for a company."""
print(f"Getting financial statements for {ticker.deps}")
if not ticker.deps:
return {
"status": "error",
"message": "No ticker symbol provided"
}
try:
statements = client.vx.list_stock_financials(
ticker=ticker.deps,
timeframe="quarterly",
period_of_report_date_gte=f"{datetime.now().year - 2}-01-01",
period_of_report_date_lte=f"{datetime.now().year}-12-31",
limit=100,
)
# Process the generator into a list with rate limit handling
financial_data = []
for statement in statements:
financial_data.append(statement)
await asyncio.sleep(0.2) # Add delay between requests
return {
"status": "success",
"data": financial_data
}
except Exception as e:
return {
"status": "error",
"message": str(e)
}
async def get_historical_prices(ticker: RunContext[str]) -> Dict[str, Any]:
"""Get historical prices for a company."""
print(f"Getting historical prices for {ticker.deps}")
if not ticker.deps:
return {
"status": "error",
"message": "No ticker symbol provided"
}
try:
if TEST_MODE:
historical_prices = client.get_aggs(
ticker=ticker.deps,
multiplier=1,
timespan="day",
# Just current year in test mode
from_=f"{datetime.now().year}-01-01",
to=f"{datetime.now().year}-12-31",
adjusted=True,
sort="desc",
limit=100 # Smaller limit in test mode
)
else:
historical_prices = client.get_aggs(
ticker=ticker.deps,
multiplier=1,
timespan="day",
from_=f"{datetime.now().year - 2}-01-01",
to=f"{datetime.now().year}-12-31",
adjusted=True,
sort="desc",
limit=5000
)
data = []
for price in historical_prices:
data.append(price)
return {
"status": "success",
"data": data
}
except Exception as e:
return {
"status": "error",
"message": str(e)
}