calculate_ma
Calculate moving averages for financial price data to identify trends and support technical analysis decisions in market trading.
Instructions
Calculate Moving Average (MA).
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"kwargs": "<kwargs>"
},
"name": "calculate_ma"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| kwargs | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/mcp_talib/core/server.py:208-220 (handler)The primary handler function for the MCP tool 'calculate_ma'. Includes @mcp.tool() decorator for automatic registration and schema inference from type hints. Delegates computation to the 'ma' indicator instance.
@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:28-39 (helper)Core computation logic in MAIndicator.calculate(), invoking TA-Lib's MA function to compute the moving average based on close prices, timeperiod, and matype.
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)) - src/mcp_talib/indicators/ma.py:16-26 (schema)JSON Schema definition for inputs to the MA indicator, aligning with the tool's parameters (close_prices as close, etc.).
@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)Registers the MAIndicator class under the key 'ma' in the central IndicatorRegistry, enabling retrieval by the tool handler.
registry.register("ma", MAIndicator)