calculate_bbands
Calculate Bollinger Bands to analyze price volatility and identify potential overbought or oversold conditions in financial markets.
Instructions
Calculate Bollinger Bands (BBANDS).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| kwargs | Yes |
Implementation Reference
- src/mcp_talib/core/server.py:146-164 (handler)The MCP tool handler function 'calculate_bbands' that executes the tool logic by delegating to the 'bbands' indicator from the registry.@mcp.tool() async def calculate_bbands( close: List[float], timeperiod: int = 20, nbdevup: float = 2.0, nbdevdn: float = 2.0, matype: int = 0, ) -> Dict[str, Any]: try: indicator = registry.get_indicator("bbands") if not indicator: raise ValueError("BBANDS indicator not found") market_data = MarketData(close=close) result = await indicator.calculate(market_data, {"timeperiod": timeperiod, "nbdevup": nbdevup, "nbdevdn": nbdevdn, "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)}
- The BBANDSIndicator class providing the core implementation of Bollinger Bands using TA-Lib's BBANDS function, called by the tool handler.class BBANDSIndicator(BaseIndicator): def __init__(self): super().__init__(name="bbands", description="Bollinger Bands (BBANDS)") @property def input_schema(self) -> Dict[str, Any]: return { "type": "object", "properties": { "close_prices": {"type": "array", "items": {"type": "number"}}, "timeperiod": {"type": "integer", "default": 20}, "nbdevup": {"type": "number", "default": 2.0}, "nbdevdn": {"type": "number", "default": 2.0}, "matype": {"type": "integer", "default": 0}, }, "required": ["close_prices"], } async def calculate(self, market_data: MarketData, options: Dict[str, Any] = None) -> IndicatorResult: if options is None: options = {} timeperiod = options.get("timeperiod", 20) nbdevup = options.get("nbdevup", 2.0) nbdevdn = options.get("nbdevdn", 2.0) matype = options.get("matype", 0) close = np.asarray(market_data.close, dtype=float) try: upper, middle, lower = ta.BBANDS(close, timeperiod=timeperiod, nbdevup=nbdevup, nbdevdn=nbdevdn, matype=matype) return IndicatorResult( indicator_name=self.name, success=True, values={ "upperband": upper.tolist(), "middleband": middle.tolist(), "lowerband": lower.tolist(), }, metadata={ "timeperiod": timeperiod, "nbdevup": nbdevup, "nbdevdn": nbdevdn, "matype": matype, "input_points": len(close), "output_points": len(middle), }, ) except Exception as e: return IndicatorResult(indicator_name=self.name, success=False, values={}, error_message=str(e))
- src/mcp_talib/indicators/__init__.py:29-29 (registration)Registration of the BBANDSIndicator class in the indicator registry under the 'bbands' key.registry.register("bbands", BBANDSIndicator)
- JSON schema defining the input parameters for the BBANDS indicator.@property def input_schema(self) -> Dict[str, Any]: return { "type": "object", "properties": { "close_prices": {"type": "array", "items": {"type": "number"}}, "timeperiod": {"type": "integer", "default": 20}, "nbdevup": {"type": "number", "default": 2.0}, "nbdevdn": {"type": "number", "default": 2.0}, "matype": {"type": "integer", "default": 0}, }, "required": ["close_prices"], }
- Tool specification for 'bbands' in the dynamic MCP server, defining parameters and defaults for the calculate_bbands tool."bbands": { "description": "Bollinger Bands (BBANDS)", "params": {"close": List[float], "timeperiod": int, "nbdevup": float, "nbdevdn": float, "matype": int}, "defaults": {"timeperiod": 20, "nbdevup": 2.0, "nbdevdn": 2.0, "matype": 0}, "market_data_args": {"close": "close"}, },