Skip to main content
Glama
phuihock

TA-Lib MCP Server

by phuihock

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).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
kwargsYes

Implementation Reference

  • 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)}
  • 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))
  • 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"], }
  • Registers the MAIndicator class under the key 'ma' in the central IndicatorRegistry, enabling retrieval by the tool handler.
    registry.register("ma", MAIndicator)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/phuihock/mcp-talib'

If you have feedback or need assistance with the MCP directory API, please join our Discord server