calculate_trima
Calculate Triangular Moving Average (TRIMA) to analyze price trends and smooth market data for technical analysis in financial markets.
Instructions
Calculate Triangular Moving Average (TRIMA).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| kwargs | Yes |
Implementation Reference
- src/mcp_talib/core/server.py:336-348 (handler)The primary MCP tool handler for 'calculate_trima'. Decorated with @mcp.tool() for automatic registration. Implements the tool logic by retrieving the 'trima' indicator from the registry and calling its calculate method.@mcp.tool() async def calculate_trima(close: List[float], timeperiod: int = 30) -> Dict[str, Any]: try: indicator = registry.get_indicator("trima") if not indicator: raise ValueError("TRIMA indicator not found") market_data = MarketData(close=close) result = await indicator.calculate(market_data, {"timeperiod": timeperiod}) 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)}
- Core implementation of the TRIMA calculation using TA-Lib's ta.TRIMA. Includes input schema definition and the calculate method invoked by the tool handler.class TRIMAIndicator(BaseIndicator): def __init__(self): super().__init__(name="trima", description="Triangular Moving Average (TRIMA)") @property def input_schema(self) -> Dict[str, Any]: return {"type": "object", "properties": {"close_prices": {"type": "array", "items": {"type": "number"}}, "timeperiod": {"type": "integer", "default": 30}}, "required": ["close_prices"]} async def calculate(self, market_data: MarketData, options: Dict[str, Any] = None) -> IndicatorResult: if options is None: options = {} timeperiod = options.get("timeperiod", 30) close = np.asarray(market_data.close, dtype=float) try: out = ta.TRIMA(close, timeperiod=timeperiod) return IndicatorResult(indicator_name=self.name, success=True, values={"trima": out.tolist()}, metadata={"timeperiod": timeperiod, "input_points": len(close), "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:42-42 (registration)Registers the TRIMAIndicator class under the 'trima' key in the IndicatorRegistry, making it available to tool handlers via registry.get_indicator('trima').registry.register("trima", TRIMAIndicator)