calculate_sar
Calculate Parabolic SAR (Stop and Reverse) values for technical analysis of financial markets, identifying potential trend reversals in price data.
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 MCP tool handler for 'calculate_sar', decorated with @mcp.tool() for registration. It fetches the 'sar' indicator from the registry and executes the Parabolic SAR calculation using provided high/low prices and parameters.@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)}
- Input schema definition for the SAR indicator, defining parameters high_prices, low_prices, acceleration, and maximum, which match the calculate_sar tool inputs.@property 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"], }
- src/mcp_talib/indicators/__init__.py:38-38 (registration)Registration of the SARIndicator class in the indicator registry under the key 'sar', which is retrieved by the calculate_sar tool handler.registry.register("sar", SARIndicator)
- The calculate method of SARIndicator that performs the actual Parabolic SAR computation using TA-Lib's ta.SAR function on high and low price arrays.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))