Skip to main content
Glama
accelizero

Stock Market Data and Technical Indicators MCP Server

by accelizero

股票市场数据和技术指标计算 MCP 服务

这是一个结合了 Go 项目优点和 Python 股市数据获取的 MCP 服务,提供:

  1. 高效的指标计算:使用序列增量计算(O(n)时间复杂度)

  2. 清晰的数据结构:参考 Go 项目的 market.Data 结构

  3. 多时间框架支持:支持从1分钟到1年的完整周期(1m/5m/15m/30m/1h/4h/1d/1w/1M/1Q/1Y)

  4. MCP 协议:标准的 MCP 服务,可被任何 MCP 客户端调用

特点

1. Go 项目的优点

  • 序列增量计算:避免重复计算,O(n) 时间复杂度

  • 清晰的数据结构MarketData 包含当前指标和历史序列

  • 多时间框架分析:支持同时分析多个周期

2. Python 的优点

  • LongPort API:获取真实的股市数据

  • TA-Lib:成熟的技术指标库

  • 易于扩展:Python 生态丰富

Related MCP server: TA-Lib MCP Server

安装

1. 安装依赖

# 安装 Python 依赖
pip install pandas numpy talib longport python-dotenv

# 安装 MCP SDK
pip install mcp

2. 配置环境变量

创建 .env 文件(或使用现有的):

# LongPort API 配置
LONGPORT_APP_KEY=your_app_key
LONGPORT_APP_SECRET=your_app_secret
LONGPORT_ACCESS_TOKEN=your_access_token

使用方法

方法 1: 独立测试(不使用 MCP)

# 运行测试脚本
python test_stock_indicators.py

这会测试:

  • 单个股票的指标计算(AAPL.US, NVDA.US, TSLA.US)

  • 多时间框架分析(1d, 4h, 1h, 15m)

方法 2: 作为 MCP 服务运行

# 启动 MCP 服务器
python stock_indicators_mcp.py

方法 3: 在 Claude Desktop 中使用

在 Claude Desktop 配置文件中添加:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json Linux: ~/.config/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "stock-indicators": {
      "command": "python",
      "args": ["/path/to/nofx/backend/stock_indicators_mcp.py"],
      "env": {
        "LONGPORT_APP_KEY": "your_app_key",
        "LONGPORT_APP_SECRET": "your_app_secret",
        "LONGPORT_ACCESS_TOKEN": "your_access_token"
      }
    }
  }
}

然后在 Claude Desktop 中使用:

请使用 get_stock_indicators 工具获取 AAPL.US 的 1小时 技术指标

方法 4: 使用 MCP Inspector 测试

# 安装 MCP Inspector
npx @modelcontextprotocol/inspector python stock_indicators_mcp.py

这会打开一个 Web 界面,可以可视化地测试 MCP 工具。

API 文档

工具 1: get_stock_indicators

获取指定股票和周期的技术指标数据。

参数:

  • symbol (必需): 股票代码(例如:AAPL.US, NVDA.US, TSLA.US

  • timeframe (可选): 时间周期

    • 分钟级: 1m, 2m, 3m, 5m, 10m, 15m, 20m, 30m, 45m

    • 小时级: 1h, 2h, 3h, 4h

    • 日周月季年: 1d, 1w, 1M(月), 1Q(季), 1Y(年)

    • 默认: 1h

  • limit (可选): 获取K线数量

    • 短周期(分钟/小时): 建议 200-500

    • 中周期(日/周): 建议 100-200

    • 长周期(月/季/年): 建议 60-120

    • 默认: 200

返回:

{
  "symbol": "AAPL.US",
  "timeframe": "1h",
  "current_price": 252.29,
  "price_change_pct": 2.15,
  "current_ema20": 248.50,
  "current_ema60": 245.10,
  "current_macd_hist": 0.523,
  "current_rsi7": 65.2,
  "current_rsi14": 58.3,
  "current_atr14": 4.85,
  "intraday_series": {
    "mid_prices": [250.1, 251.2, 252.0, ...],
    "ema20_values": [247.5, 248.0, 248.5, ...],
    "macd_hist": [0.45, 0.50, 0.52, ...],
    "rsi7_values": [62.5, 64.0, 65.2, ...],
    ...
  },
  "timestamp": "2025-01-17T10:30:00+08:00"
}

工具 2: get_multi_timeframe_analysis

获取指定股票的多时间框架分析。

参数:

  • symbol (必需): 股票代码

  • timeframes (可选): 时间周期列表,默认 ["1M", "1w", "1d", "4h"]

    • 支持任意组合:从 1m1Y 的所有周期

返回: 多个时间框架的数据汇总(Markdown 格式)。

数据结构

MarketData

@dataclass
class MarketData:
    symbol: str                    # 股票代码
    timeframe: str                 # 时间周期
    current_price: float           # 当前价格
    price_change_pct: float        # 价格变化百分比
    current_ema20: float           # 当前EMA20
    current_ema60: float           # 当前EMA60
    current_macd_hist: float       # 当前MACD柱
    current_rsi7: float            # 当前RSI(7)
    current_rsi14: float           # 当前RSI(14)
    current_atr14: float           # 当前ATR(14)
    intraday_series: IntradaySeriesData  # 日内序列数据
    timestamp: str                 # 时间戳

IntradaySeriesData

@dataclass
class IntradaySeriesData:
    mid_prices: List[float]        # 收盘价序列(最近10个)
    volume_values: List[float]     # 成交量序列
    ema20_values: List[float]      # EMA20序列
    ema60_values: List[float]      # EMA60序列
    macd_dif: List[float]          # MACD DIF序列
    macd_dea: List[float]          # MACD DEA序列
    macd_hist: List[float]         # MACD HIST序列
    rsi7_values: List[float]       # RSI7序列
    rsi14_values: List[float]      # RSI14序列
    atr14_values: List[float]      # ATR14序列
    bb_upper: List[float]          # 布林带上轨
    bb_middle: List[float]         # 布林带中轨
    bb_lower: List[float]          # 布林带下轨

支持的技术指标

  1. 趋势指标:

    • EMA20: 20周期指数移动平均线

    • EMA60: 60周期指数移动平均线

  2. 动量指标:

    • MACD: 包括 DIF、DEA、HIST(柱状图 = (DIF-DEA)×2,与交易所显示一致)

    • RSI7: 7周期相对强弱指标

    • RSI14: 14周期相对强弱指标

  3. 波动性指标:

    • ATR14: 14周期平均真实波动幅度

    • 布林带: 上轨、中轨、下轨

  4. 价格数据:

    • 当前价格

    • 价格变化百分比

    • 历史价格序列

示例

示例 1: 获取 AAPL 的1小时指标

from stock_indicators_mcp import StockDataProvider

provider = StockDataProvider()
data = provider.get_market_data("AAPL.US", "1h", 200)

print(f"当前价格: {data.current_price}")
print(f"EMA20: {data.current_ema20}")
print(f"RSI7: {data.current_rsi7}")

示例 2: 多时间框架分析(包含月K线)

symbols = ["AAPL.US"]
timeframes = ["1Y", "1M", "1w", "1d", "4h", "1h"]  # 从年线到小时线

for symbol in symbols:
    for tf in timeframes:
        data = provider.get_market_data(symbol, tf)
        print(f"{symbol} ({tf}): 价格={data.current_price}, RSI7={data.current_rsi7}")

示例 3: 获取长周期数据(月/季/年线)

# 月K线 - 适合长期趋势分析
monthly_data = provider.get_market_data("AAPL.US", "1M", limit=60)  # 最近5年月K
print(f"月线趋势: EMA20={monthly_data.current_ema20:.2f}, RSI={monthly_data.current_rsi7:.2f}")

# 季K线 - 适合宏观分析
quarterly_data = provider.get_market_data("AAPL.US", "1Q", limit=40)  # 最近10年季K
print(f"季度趋势: MACD={quarterly_data.current_macd_hist:.3f}")

# 年K线 - 适合超长期分析
yearly_data = provider.get_market_data("AAPL.US", "1Y", limit=20)  # 最近20年年K
print(f"年度表现: 价格变化={yearly_data.price_change_pct:.2f}%")

性能特点

  1. 序列增量计算: 所有指标使用 TA-Lib 的优化算法,O(n) 时间复杂度

  2. 并发支持: 多时间框架分析支持异步并发获取

  3. 数据缓存: 可选的缓存机制(待实现)

与 Go 项目的对比

特性

Go 项目

Python 版本

数据源

Aster API (加密货币)

LongPort API (股票)

指标计算

手动实现

TA-Lib

时间复杂度

O(n)

O(n)

数据结构

market.Data

MarketData

多时间框架

MCP 支持

开发计划

  • 基础指标计算

  • 多时间框架支持

  • MCP 服务封装

  • 缓存机制

  • 更多技术指标(KDJ、CCI等)

  • WebSocket 实时数据

  • 性能优化

许可证

MIT License

相关文档

Related MCP Connectors

Related MCP Servers

  • A
    license
    C
    quality
    D
    maintenance
    The MCP provides comprehensive financial data and analytical tool support for AI large language models, specifically including the following five core data capabilities: Stock Analysis/ETF Analysis/Public Funds/News & Information/General Tools More Info: https://github.com/shenqingtech/deepq-finan
    44
    30 npm
    7
    ISC
  • A
    license
    C
    quality
    D
    maintenance
    Provides technical analysis indicators like SMA, EMA, RSI, MACD, Bollinger Bands, and Stochastic through MCP, enabling AI assistants to perform financial market analysis and calculations on price data.
    18
    7
    MIT
  • F
    license
    Not graded
    quality
    C
    maintenance
    Provides foundational trading utilities by fetching market data through Akshare and computing technical indicators like RSI and MACD using TA-Lib. It enables users to retrieve candlestick data and technical analysis series via a standardized MCP interface.
    9
    -
  • A
    license
    Not graded
    quality
    F
    maintenance
    Provides comprehensive stock market data and technical analysis tools via the MCP protocol, enabling real-time quotes, historical data, and professional indicators like RSI and MACD for Claude Desktop and other clients.
    5
    Apache 2.0