get_option_chain
Retrieve detailed option chain data for a specific stock, including calls and puts, by providing the ticker symbol, expiration date, and option type. Use this to analyze and strategize financial options trading.
Instructions
根据股票代码、到期日和期权类型获取期权链数据。
参数说明: ticker: str 股票代码,例如 "AAPL" expiration_date: str 期权到期日,格式为 'YYYY-MM-DD' option_type: str 期权类型:'calls' 或 'puts'
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expiration_date | Yes | ||
| option_type | Yes | ||
| ticker | Yes |
Implementation Reference
- server.py:622-647 (handler)The core handler function for the 'get_option_chain' tool. It retrieves option chain data from the Financial Modeling Prep API for a given ticker, filters by expiration date and option type (calls/puts), handles errors, and returns filtered JSON data.async def get_option_chain(ticker: str, expiration_date: str, option_type: str) -> str: """Fetch the option chain for a given ticker symbol, expiration date, and option type.""" api_key = os.environ.get("FMP_API_KEY") if not api_key: return "Error: FMP_API_KEY environment variable not set." base = "https://financialmodelingprep.com/api/v3" try: resp = requests.get( f"{base}/options/chain/{ticker}", params={"expiration": expiration_date, "apikey": api_key}, timeout=10, ) resp.raise_for_status() data = resp.json() except Exception as e: return f"Error: getting option chain for {ticker}: {e}" filtered = [ item for item in data if item.get("expirationDate") == expiration_date and item.get("optionType", "").lower() == option_type.lower() ] return json.dumps(filtered)
- server.py:609-621 (registration)The @fmp_server.tool decorator registers the 'get_option_chain' tool, specifying its name and detailed description including input parameters (schema). This is how the tool is exposed in the MCP server.@fmp_server.tool( name="get_option_chain", description="""根据股票代码、到期日和期权类型获取期权链数据。 参数说明: ticker: str 股票代码,例如 "AAPL" expiration_date: str 期权到期日,格式为 'YYYY-MM-DD' option_type: str 期权类型:'calls' 或 'puts' """, )
- server.py:622-622 (schema)Type hints in the function signature define the input schema: ticker (str), expiration_date (str), option_type (str), returning str (JSON).async def get_option_chain(ticker: str, expiration_date: str, option_type: str) -> str: