get_stock_ohlc
Retrieve Open, High, Low, Close stock price data for a specific symbol on a given date or the last trading day.
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 |
|---|---|---|---|
| symbol | Yes | ||
| date | No |
Implementation Reference
- server.py:25-29 (handler)MCP tool handler for 'get_stock_ohlc', registered via @mcp.tool() decorator. Executes by calling get_stock_data from polygon_api.@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-120 (helper)Core helper method in PolygonStockAPI class that performs the actual API call to Polygon.io to retrieve OHLC stock data.def 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-164 (helper)Wrapper function imported by server.py that instantiates the API client, fetches data, formats it, and returns the string result.def 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:28-45 (helper)Helper method to determine the last working day (weekday) if no date is specified.def 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
- polygon_api.py:121-144 (helper)Helper method to format the raw stock data dictionary into a human-readable string.def 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']:,} """