Skip to main content
Glama
aahl

MCP Server for stock and crypto

by aahl

获取股票历史价格

stock_prices

Retrieve historical stock prices and technical indicators for stocks listed on Shanghai, Shenzhen, Hong Kong, and US markets. Supports daily and weekly periods.

Instructions

根据股票代码和市场获取股票历史价格及技术指标, 不支持加密货币

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYes股票代码
marketNo股票市场,仅支持: sh(上证), sz(深证), hk(港股), us(美股), 不支持加密货币sh
periodNo周期,如: daily(日线), weekly(周线,不支持美股)daily
limitNo返回数量(int)

Implementation Reference

  • Registration of 'stock_prices' tool via @mcp.tool decorator with title '获取股票历史价格' and description about getting historical prices and technical indicators
    @mcp.tool(
        title="获取股票历史价格",
        description="根据股票代码和市场获取股票历史价格及技术指标, 不支持加密货币",
    )
  • Main handler function for stock_prices tool. Fetches historical stock prices from akshare for different markets (SH/SZ/HK/US/ETF), adds technical indicators (MACD, KDJ, RSI, Bollinger Bands), and returns CSV-formatted data limited to specified number of rows.
    def stock_prices(
        symbol: str = field_symbol,
        market: str = field_market,
        period: str = Field("daily", description="周期,如: daily(日线), weekly(周线,不支持美股)"),
        limit: int = Field(30, description="返回数量(int)", strict=False),
    ):
        if period == "weekly":
            delta = {"weeks": limit + 62}
        else:
            delta = {"days": limit + 62}
        start_date = (datetime.now() - timedelta(**delta)).strftime("%Y%m%d")
        markets = [
            ["sh", ak.stock_zh_a_hist, {}],
            ["sz", ak.stock_zh_a_hist, {}],
            ["hk", ak.stock_hk_hist, {}],
            ["us", stock_us_daily, {}],
            ["sh", fund_etf_hist_sina, {"market": "sh"}],
            ["sz", fund_etf_hist_sina, {"market": "sz"}],
        ]
        for m in markets:
            if m[0] != market:
                continue
            kws = {"period": period, "start_date": start_date, **m[2]}
            dfs = ak_cache(m[1], symbol=symbol, ttl=3600, **kws)
            if dfs is None or dfs.empty:
                continue
            add_technical_indicators(dfs, dfs["收盘"], dfs["最低"], dfs["最高"])
            columns = [
                "日期", "开盘", "收盘", "最高", "最低", "成交量", "换手率",
                "MACD", "DIF", "DEA", "KDJ.K", "KDJ.D", "KDJ.J", "RSI", "BOLL.U", "BOLL.M", "BOLL.L",
            ]
            all = dfs.to_csv(columns=columns, index=False, float_format="%.2f").strip().split("\n")
            return "\n".join([all[0], *all[-limit:]])
        return f"Not Found for {symbol}.{market}"
  • Helper function for fetching US stock daily data via akshare, with column renaming for consistency with other market data
    def stock_us_daily(symbol, start_date="2025-01-01", period="daily"):
        dfs = ak.stock_us_daily(symbol=symbol)
        if dfs is None or dfs.empty:
            return None
        dfs.rename(columns={"date": "日期", "open": "开盘", "close": "收盘", "high": "最高", "low": "最低", "volume": "成交量"}, inplace=True)
        dfs["换手率"] = None
        dfs.index = pd.to_datetime(dfs["日期"], errors="coerce")
        return dfs[start_date:"2222-01-01"]
  • Helper function for fetching ETF fund historical data via akshare/sina, used by stock_prices for ETF market types
    def fund_etf_hist_sina(symbol, market="sh", start_date="2025-01-01", period="daily"):
        dfs = ak.fund_etf_hist_sina(symbol=f"{market}{symbol}")
        if dfs is None or dfs.empty:
            return None
        dfs.rename(columns={"date": "日期", "open": "开盘", "close": "收盘", "high": "最高", "low": "最低", "volume": "成交量"}, inplace=True)
        dfs["换手率"] = None
        dfs.index = pd.to_datetime(dfs["日期"], errors="coerce")
        return dfs[start_date:"2222-01-01"]
  • Helper function that adds MACD, KDJ, RSI, and Bollinger Bands technical indicators to the dataframe, used by stock_prices and okx_prices
    def add_technical_indicators(df, clos, lows, high):
        # 计算MACD指标
        ema12 = clos.ewm(span=12, adjust=False).mean()
        ema26 = clos.ewm(span=26, adjust=False).mean()
        df["DIF"] = ema12 - ema26
        df["DEA"] = df["DIF"].ewm(span=9, adjust=False).mean()
        df["MACD"] = (df["DIF"] - df["DEA"]) * 2
    
        # 计算KDJ指标
        low_min  = lows.rolling(window=9, min_periods=1).min()
        high_max = high.rolling(window=9, min_periods=1).max()
        rsv = (clos - low_min) / (high_max - low_min) * 100
        df["KDJ.K"] = rsv.ewm(com=2, adjust=False).mean()
        df["KDJ.D"] = df["KDJ.K"].ewm(com=2, adjust=False).mean()
        df["KDJ.J"] = 3 * df["KDJ.K"] - 2 * df["KDJ.D"]
    
        # 计算RSI指标
        delta = clos.diff()
        gain = delta.where(delta > 0, 0)
        loss = -delta.where(delta < 0, 0)
        avg_gain = gain.rolling(window=14).mean()
        avg_loss = loss.rolling(window=14).mean()
        rs = avg_gain / avg_loss
        df["RSI"] = 100 - (100 / (1 + rs))
    
        # 计算布林带指标
        df["BOLL.M"] = clos.rolling(window=20).mean()
        std = clos.rolling(window=20).std()
        df["BOLL.U"] = df["BOLL.M"] + 2 * std
        df["BOLL.L"] = df["BOLL.M"] - 2 * std
  • Generic caching helper used by stock_prices to cache akshare API results with dual-layer cache (in-memory TTL cache + disk cache)
    def ak_cache(fun, *args, **kwargs) -> pd.DataFrame | None:
        key = kwargs.pop("key", None)
        if not key:
            key = f"{fun.__name__}-{args}-{kwargs}"
        ttl1 = kwargs.pop("ttl", 86400)
        ttl2 = kwargs.pop("ttl2", None)
        cache = CacheKey.init(key, ttl1, ttl2)
        all = cache.get()
        if all is None:
            try:
                _LOGGER.info("Request akshare: %s", [key, args, kwargs])
                all = fun(*args, **kwargs)
                cache.set(all)
            except Exception as exc:
                _LOGGER.exception(str(exc))
        return all

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed1 schema field changedv1.0.0
    • changedInput schema / properties / market / description
      Previous value: -"股票市场,如: sh(上证), sz(深证), hk(港股), us(美股) 等"New value: +"股票市场,仅支持: sh(上证), sz(深证), hk(港股), us(美股), 不支持加密货币"
  2. First observed

TDQS

B3.3/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It discloses only one limitation (no crypto support) and does not describe return format, error behavior, or required permissions. This is minimal behavioral disclosure.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single sentence that is front-loaded with the core purpose. It contains no waste and is easily parsed.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the lack of output schema and annotations, the description should clarify return structure. It only mentions historical prices and technical indicators, but not the format or specifics. Still, for a straightforward price-fetching tool, this is minimally adequate.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema provides 100% coverage of all 4 parameters with clear descriptions. The tool description adds little beyond referencing symbol and market, so it does not enhance parameter semantics.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states it retrieves stock historical prices and technical indicators based on symbol and market. It also notes crypto is not supported, which distinguishes it from cryptocurrency tools. However, it does not differentiate from sibling stock indicator tools, so it lacks sibling differentiation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies the primary use case (fetching historical prices/indicators) and explicitly excludes cryptocurrency. It does not mention alternatives or when to use other sibling tools like stock_indicators_a. This leaves some ambiguity about tool selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.