Spaces:
Running
Running
File size: 3,129 Bytes
332f271 | 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 | import requests
from config import POLYGON_API_KEY
BASE_URL = "https://api.polygon.io"
class PolygonAPI:
def __init__(self):
self.api_key = POLYGON_API_KEY
def get_ticker_details(self, ticker):
"""Get detailed information about a ticker"""
url = f"{BASE_URL}/v3/reference/tickers/{ticker}"
params = {"apiKey": self.api_key}
response = requests.get(url, params=params)
return response.json()
def get_previous_close(self, ticker):
"""Get previous day's close data"""
url = f"{BASE_URL}/v2/aggs/ticker/{ticker}/prev"
params = {"adjusted": "true", "apiKey": self.api_key}
response = requests.get(url, params=params)
return response.json()
def get_aggregates(self, ticker, timespan="day", from_date=None, to_date=None):
"""Get aggregate bars for a ticker over a given date range"""
url = f"{BASE_URL}/v2/aggs/ticker/{ticker}/range/1/{timespan}/{from_date}/{to_date}"
params = {"adjusted": "true", "sort": "asc", "apiKey": self.api_key}
response = requests.get(url, params=params)
return response.json()
def get_ticker_news(self, ticker, limit=10):
"""Get news articles for a ticker"""
url = f"{BASE_URL}/v2/reference/news"
params = {
"ticker": ticker,
"limit": limit,
"apiKey": self.api_key
}
response = requests.get(url, params=params)
return response.json()
def get_financials(self, ticker):
"""Get financial data for a ticker"""
url = f"{BASE_URL}/vX/reference/financials"
params = {
"ticker": ticker,
"limit": 4,
"apiKey": self.api_key
}
response = requests.get(url, params=params)
return response.json()
def get_snapshot(self, ticker):
"""Get current snapshot of a ticker"""
url = f"{BASE_URL}/v2/snapshot/locale/us/markets/stocks/tickers/{ticker}"
params = {"apiKey": self.api_key}
response = requests.get(url, params=params)
return response.json()
def get_dividends(self, ticker, limit=10):
"""Get dividend history for a ticker"""
url = f"{BASE_URL}/v3/reference/dividends"
params = {
"ticker": ticker,
"limit": limit,
"order": "desc",
"apiKey": self.api_key
}
response = requests.get(url, params=params)
return response.json()
def get_splits(self, ticker, limit=10):
"""Get stock split history for a ticker"""
url = f"{BASE_URL}/v3/reference/splits"
params = {
"ticker": ticker,
"limit": limit,
"order": "desc",
"apiKey": self.api_key
}
response = requests.get(url, params=params)
return response.json()
def get_market_status(self):
"""Get current market status"""
url = f"{BASE_URL}/v1/marketstatus/now"
params = {"apiKey": self.api_key}
response = requests.get(url, params=params)
return response.json()
|