calculate_midprice
Calculate the midpoint price (MIDPRICE) for financial market analysis by averaging high and low values over a specified period to identify price equilibrium levels.
Instructions
Calculate Midpoint Price (MIDPRICE).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| kwargs | Yes |
Implementation Reference
- src/mcp_talib/core/server.py:265-277 (handler)The primary MCP tool handler for 'calculate_midprice', registered via @mcp.tool() decorator. 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)}
- The MIDPRICEIndicator class containing the core computation logic using TA-Lib's MIDPRICE function, along with input schema definition.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))
- src/mcp_talib/indicators/__init__.py:37-37 (registration)Registers the MIDPRICEIndicator class in the global indicator registry under the 'midprice' key, making it available to tool handlers.registry.register("midprice", MIDPRICEIndicator)