Skip to main content
Glama
phuihock

TA-Lib MCP Server

by phuihock

calculate_midprice

Calculate the midpoint price for financial market analysis using TA-Lib technical indicators to determine average price levels between high and low values.

Instructions

Calculate Midpoint Price (MIDPRICE).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
kwargsYes

Implementation Reference

  • The MCP tool handler for 'calculate_midprice', decorated with @mcp.tool() for automatic registration. Delegates computation to the 'midprice' indicator from the registry.
    @mcp.tool()
    async def calculate_midprice(high: List[float], low: List[float], timeperiod: int = 14) -> Dict[str, Any]:
        try:
            indicator = registry.get_indicator("midprice")
            if not indicator:
                raise ValueError("MIDPRICE indicator not found")
            market_data = MarketData(close=[0], high=high, low=low)
            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 midprice calculation using TA-Lib's MIDPRICE function. Includes input schema and the calculate method that performs (high + low)/2 averaged over timeperiod.
    class MIDPRICEIndicator(BaseIndicator):
        def __init__(self):
            super().__init__(name="midprice", description="Midpoint Price over period")
    
        @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"}},
                    "timeperiod": {"type": "integer", "default": 14},
                },
                "required": ["high_prices", "low_prices"],
            }
    
        async def calculate(self, market_data: MarketData, options: Dict[str, Any] = None) -> IndicatorResult:
            if options is None:
                options = {}
            timeperiod = options.get("timeperiod", 14)
    
            high = np.asarray(market_data.high or [], dtype=float)
            low = np.asarray(market_data.low or [], dtype=float)
    
            try:
                out = ta.MIDPRICE(high, low, timeperiod=timeperiod)
                return IndicatorResult(indicator_name=self.name, success=True, values={"midprice": out.tolist()}, metadata={"timeperiod": timeperiod, "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))
  • Registration of the MIDPRICEIndicator class in the central indicator registry, allowing it to be retrieved by name in tool handlers.
    registry.register("midprice", MIDPRICEIndicator)
  • JSON schema defining the input parameters for the midprice indicator, matching the tool's high_prices, low_prices, and timeperiod.
    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"}},
                "timeperiod": {"type": "integer", "default": 14},
            },
            "required": ["high_prices", "low_prices"],
        }

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