calculate_sar
Calculate Parabolic SAR (Stop and Reverse) indicator for technical analysis of financial markets to identify potential trend reversals and entry/exit points.
Instructions
Calculate Parabolic SAR.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| kwargs | Yes |
Implementation Reference
- src/mcp_talib/core/server.py:279-291 (handler)The main MCP tool handler function for 'calculate_sar'. It fetches the SAR indicator from the registry, prepares market data from inputs, calls the indicator's calculate method, and returns the result.@mcp.tool() async def calculate_sar(high: List[float], low: List[float], acceleration: float = 0.02, maximum: float = 0.2) -> Dict[str, Any]: try: indicator = registry.get_indicator("sar") if not indicator: raise ValueError("SAR indicator not found") market_data = MarketData(close=[0], high=high, low=low) result = await indicator.calculate(market_data, {"acceleration": acceleration, "maximum": maximum}) if result.success: return {"success": True, "values": result.values, "metadata": result.metadata} return {"success": False, "error": result.error_message} except Exception as e: return {"success": False, "error": str(e)}
- JSON schema defining the input parameters for the SAR indicator, matching the tool's parameters (high/low prices, acceleration, maximum).def input_schema(self) -> Dict[str, Any]: return { "type": "object", "properties": { "high_prices": {"type": "array", "items": {"type": "number"}}, "low_prices": {"type": "array", "items": {"type": "number"}}, "acceleration": {"type": "number", "default": 0.02}, "maximum": {"type": "number", "default": 0.2}, }, "required": ["high_prices", "low_prices"], }
- Core calculation logic in SARIndicator.calculate(), which calls TA-Lib's ta.SAR function with high/low arrays and parameters to compute Parabolic SAR values.async def calculate(self, market_data: MarketData, options: Dict[str, Any] = None) -> IndicatorResult: if options is None: options = {} acceleration = options.get("acceleration", 0.02) maximum = options.get("maximum", 0.2) high = np.asarray(market_data.high or [], dtype=float) low = np.asarray(market_data.low or [], dtype=float) try: out = ta.SAR(high, low, acceleration=acceleration, maximum=maximum) return IndicatorResult(indicator_name=self.name, success=True, values={"sar": out.tolist()}, metadata={"acceleration": acceleration, "maximum": maximum, "input_points": len(high), "output_points": len(out)}) except Exception as e: return IndicatorResult(indicator_name=self.name, success=False, values={}, error_message=str(e))
- src/mcp_talib/indicators/__init__.py:38-38 (registration)Registers the SARIndicator class in the global IndicatorRegistry under the key 'sar', enabling its use by the tool handler.registry.register("sar", SARIndicator)