calculate_ma
Compute moving average values for financial price data to identify trends and support technical analysis in market evaluation.
Instructions
Calculate Moving Average (MA).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| kwargs | Yes |
Implementation Reference
- src/mcp_talib/core/server.py:208-220 (handler)MCP tool handler function for calculate_ma, which delegates to the 'ma' indicator from the registry.@mcp.tool() async def calculate_ma(close: List[float], timeperiod: int = 30, matype: int = 0) -> Dict[str, Any]: try: indicator = registry.get_indicator("ma") if not indicator: raise ValueError("MA indicator not found") market_data = MarketData(close=close) result = await indicator.calculate(market_data, {"timeperiod": timeperiod, "matype": matype}) 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)}
- src/mcp_talib/indicators/ma.py:16-26 (schema)Input schema definition for the MA indicator used by the calculate_ma tool.@property def input_schema(self) -> Dict[str, Any]: return { "type": "object", "properties": { "close_prices": {"type": "array", "items": {"type": "number"}}, "timeperiod": {"type": "integer", "default": 30}, "matype": {"type": "integer", "default": 0}, }, "required": ["close_prices"], }
- src/mcp_talib/indicators/__init__.py:33-33 (registration)Registration of the MAIndicator class in the indicator registry under the key 'ma'.registry.register("ma", MAIndicator)
- src/mcp_talib/indicators/ma.py:28-39 (helper)Core calculation logic for the MA indicator using TA-Lib's MA function.async def calculate(self, market_data: MarketData, options: Dict[str, Any] = None) -> IndicatorResult: if options is None: options = {} timeperiod = options.get("timeperiod", 30) matype = options.get("matype", 0) close = np.asarray(market_data.close, dtype=float) try: out = ta.MA(close, timeperiod=timeperiod, matype=matype) return IndicatorResult(indicator_name=self.name, success=True, values={"ma": out.tolist()}, metadata={"timeperiod": timeperiod, "matype": matype, "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))