](https://mseep.ai/app/hasan-syed25-mcp-server-alphavantage)
用于股票市场分析的MCP服务器

概述
该项目实现了一个为股票市场分析设计的模型上下文协议(MCP)服务器。它提供实时股价数据,计算移动平均线和RSI等关键技术指标,并使用AlphaVantage API实现趋势检测。该服务器可以与Claude等LLM集成,以增强财务洞察力。
我为什么建造它
我开发了这个MCP服务器,通过自动化数据获取和技术指标计算来简化股市分析。目标是通过利用人工智能辅助的见解,帮助交易员和投资者做出数据驱动的决策。通过使用MCP,该服务器可以与LLM无缝连接,使他们能够检索财务数据并实时进行分析。
特性
- 获取实时股票数据 使用AlphaVantage API
- 计算移动平均线 (短期和长期)分析趋势
- 检测趋势交叉 (金十字架和死亡十字架)
- 计算相对强弱指数(RSI) 确定超买/超卖情况
- 公开MCP工具和资源 实现无缝的人工智能集成
- 未来的可扩展性 (例如,回溯测试策略、交易平台集成)
先决条件
在开始之前,请确保您具备以下条件:
- Python 3.10+ 安装
- MCP SDK 1.2.0+
- AlphaVantage API密钥 (注册地址: AlphaVantage)
- 对Python和股市指标有基本了解
安装
要设置项目,请执行以下步骤:
1.安装依赖项
使用 uv (推荐):
uv add mcp[cli] httpx使用 pip:
pip install mcp httpx2.获取AlphaVantage API密钥
- 注册地址: AlphaVantage
- 记下你的 API密钥 供以后使用
3.克隆存储库
git clone https://github.com/your-username/mcp-stock-analysis.git
cd mcp-stock-analysis4.设置环境变量
创建一个 .env 文件并添加您的AlphaVantage API密钥:
ALPHA_VANTAGE_API_KEY=your_api_key_here运行MCP服务器
要启动服务器,请运行:
python server.py或使用MCP:
mcp install server.py开发模式:
mcp dev server.py运作原理
获取日内库存数据
服务器从AlphaVantage获取实时股价数据:
@mcp.tool()
def fetch_intraday_data(symbol: str, interval: str = "5min") -> dict:
"""Fetches intraday stock price data."""
url = f"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval={interval}&apikey={API_KEY}"
response = httpx.get(url)
return response.json()移动平均线(短期与长期)
移动平均线有助于平滑价格数据以识别趋势:
@mcp.tool()
def calculate_moving_averages(prices: list[float], short_window: int = 50, long_window: int = 200) -> dict:
"""Calculates short-term and long-term moving averages."""
short_ma = sum(prices[-short_window:]) / short_window
long_ma = sum(prices[-long_window:]) / long_window
return {"short_ma": short_ma, "long_ma": long_ma}检测趋势交叉(金十字和死亡十字)
- 黄金交叉:短期MA超过长期MA→ 看涨信号
- 死亡交叉:短期均线低于长期均线→ 熊市信号
@mcp.tool()
def detect_trend_crossover(short_ma: float, long_ma: float) -> str:
"""Detects if a Golden Cross or Death Cross has occurred."""
if short_ma > long_ma:
return "Golden Cross - Bullish Trend"
elif short_ma float:
"""Calculates RSI indicator."""
gains = [max(0, prices[i] - prices[i-1]) for i in range(1, len(prices))]
losses = [max(0, prices[i-1] - prices[i]) for i in range(1, len(prices))]
avg_gain = sum(gains[-period:]) / period
avg_loss = sum(losses[-period:]) / period
rs = avg_gain / avg_loss if avg_loss != 0 else float('inf')
return 100 - (100 / (1 + rs))超买/超卖情况
- RSI>70 → 过度购买(潜在卖出信号)
- **RSI\ str:
"""Determines whether the RSI indicates overbought or oversold conditions.""" if rsi > 70: return "Overbought - Consider Selling" elif rsi < 30: return "Oversold - Consider Buying" return "Neutral"
## 未来的增强功能
此MCP服务器可以扩展其他功能:
- **更多技术指标**:MACD、布林带等。
- **回溯测试**:根据策略规则模拟过去的交易
- **交易平台集成**:与经纪人联系进行实时交易
## 贡献
您可以通过提交pull请求或报告问题来做出贡献。让我们让股市分析更容易获得,并由人工智能驱动!
## 与我联系
如果您有任何问题或建议,请随时联系!
🔗 **领英**: [赛义德·哈桑](https://www.linkedin.com/in/syedhasan)\
🤗 **拥抱脸**: [Syed-Hasan-8503](https://huggingface.co/Syed-Hasan-8503)