get_stock_ohlc
Retrieve Open, High, Low, Close (OHLC) stock price data for any symbol on the last trading day or specific date to analyze market performance.
Instructions
Get Open, High, Low, Close (OHLC) data for a stock symbol for the last working day or specified date
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | No | ||
| symbol | Yes |
Implementation Reference
- server.py:25-29 (registration)MCP tool registration and entry-point handler that calls the polygon_api implementation@mcp.tool() def get_stock_ohlc(symbol: str, date: str = None) -> str: """Get Open, High, Low, Close (OHLC) data for a stock symbol for the last working day or specified date""" return get_stock_data(symbol, date)
- polygon_api.py:46-119 (handler)Core handler logic in PolygonStockAPI class that fetches and returns OHLC stock data from Polygon.io APIdef get_stock_ohlc(self, symbol: str, date: Optional[str] = None) -> Dict: """ Fetch Open, High, Low, Close (OHLC) data for a stock symbol. Args: symbol: Stock symbol (e.g., 'AAPL', 'MSFT', 'GOOGL') date: Date in YYYY-MM-DD format. If not provided, uses last working day. Returns: Dictionary containing OHLC data and metadata """ if not date: date = self.get_last_working_day() # Validate symbol format symbol = symbol.upper().strip() # Polygon.io API endpoint for daily bars url = f"{self.base_url}/v2/aggs/ticker/{symbol}/prev" params = { "adjusted": "true", "apikey": self.api_key } try: response = requests.get(url, params=params, timeout=10) response.raise_for_status() data = response.json() if data.get("status") != "OK": return { "error": f"API Error: {data.get('status', 'Unknown error')}", "symbol": symbol, "date": date } results = data.get("results", []) if not results: return { "error": f"No data found for symbol {symbol}", "symbol": symbol, "date": date } # Extract OHLC data from the first result result = results[0] return { "symbol": symbol, "date": date, "open": result.get("o"), "high": result.get("h"), "low": result.get("l"), "close": result.get("c"), "volume": result.get("v"), "volume_weighted_average_price": result.get("vw"), "number_of_transactions": result.get("n"), "status": "success" } except requests.exceptions.RequestException as e: return { "error": f"Network error: {str(e)}", "symbol": symbol, "date": date } except Exception as e: return { "error": f"Unexpected error: {str(e)}", "symbol": symbol, "date": date }
- polygon_api.py:147-163 (helper)Top-level helper function called by server.py handler, which instantiates the API, calls get_stock_ohlc, and formats the outputdef get_stock_data(symbol: str, date: Optional[str] = None) -> str: """ Convenience function to get formatted stock data. Args: symbol: Stock symbol (e.g., 'AAPL', 'MSFT', 'GOOGL') date: Date in YYYY-MM-DD format. If not provided, uses last working day. Returns: Formatted string with stock OHLC data """ try: api = PolygonStockAPI() data = api.get_stock_ohlc(symbol, date) return api.format_stock_data(data) except Exception as e: return f"Error initializing API: {str(e)}"
- polygon_api.py:121-144 (helper)Helper method to format the raw OHLC data into a human-readable stringdef format_stock_data(self, data: Dict) -> str: """ Format stock data into a readable string. Args: data: Stock data dictionary from get_stock_ohlc Returns: Formatted string with stock information """ if "error" in data: return f"Error fetching data for {data.get('symbol', 'Unknown')}: {data['error']}" return f""" Stock Symbol: {data['symbol']} Date: {data['date']} Open: ${data['open']:.2f} High: ${data['high']:.2f} Low: ${data['low']:.2f} Close: ${data['close']:.2f} Volume: {data['volume']:,} VWAP: ${data['volume_weighted_average_price']:.2f} Transactions: {data['number_of_transactions']:,} """
- polygon_api.py:28-45 (helper)Helper method to determine the last working day (weekday) if no date provideddef get_last_working_day(self) -> str: """ Get the last working day (excluding weekends). Returns: Date string in YYYY-MM-DD format """ today = datetime.now() # Go back to find the last working day days_back = 1 while True: check_date = today - timedelta(days=days_back) # Monday = 0, Sunday = 6 if check_date.weekday() < 5: # Monday to Friday return check_date.strftime("%Y-%m-%d") days_back += 1