Skip to main content
Glama
phuihock

TA-Lib MCP Server

by phuihock

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
NameRequiredDescriptionDefault
kwargsYes

Implementation Reference

  • 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))
  • 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"}, },

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