🧠 MCP_财务代理
一种具备模型上下文协议(MCP)、数值预测和情感融合功能的多智能体金融人工智能系统
______________________________________________________________________
📘 概述
MCP金融代理 是一个智能的, 多智能体金融分析系统 那个融合了 数值预报, 情感推理,和 情境记忆 使用 模型上下文协议(MCP) 并且 LangGraph。
它模拟了一个(物体/系统/行为)的行为 对冲基金级别的AI分析师 — 收集实时数据,进行技术和定量分析,预测市场动态,并提供 买入/卖出/持有建议 (带有)置信水平。
______________________________________________________________________
🚀 主要特点
| 能力(Capability) | 描述(Description) |
|---|---|
| 🧮(计算器) 数值预报 使用ARIMA、Prophet和LSTM模型预测短期价格趋势。 | |
| 🗞️ 情感分析 分析金融新闻和社会信号中的看涨/看跌基调。 | |
| 📊 技术指标 计算相对强弱指数(RSI)、指数移动平均线(EMA)、移动平均收敛散异指标(MACD)和布林带,以检测趋势强度。 | |
| 🧠(大脑图标,无直接对应文字,可理解为“思考中”或“智慧”等抽象概念) MCP上下文内存 | 记忆模型性能,动态调整权重,并存储市场动态。 |
| ⚖️ 混合决策(LLM + 量化模型) 将语言模型的推理能力与数学预测相结合。 | |
| 循环(往返) 自适应学习 随着时间的推移,通过追踪和奖励表现最佳的模型来提高准确性。 | |
| 时钟(表示时间或等待) 多时间框架分析 分析1分钟、15分钟和1小时的K线数据以提高准确性。 | |
| 💬 自然语言报告 生成高层管理备忘录,总结研究结果和建议。 |
______________________________________________________________________
🧩 系统架构
🔹 工作流程图
[Data Collector Agent]
↓
[Quant & Numerical Agents] → ARIMA / LSTM Forecasts
↓
[Sentiment Agent] → News & Tone Analysis
↓
[Quant Analysis Agent] → RSI / EMA / MACD
↓
[Ensemble Agent + MCP Manager] → Combine & Learn
↓
[Report Agent] → Final Memo & Recommendation______________________________________________________________________
🧬 工作流程图
sequenceDiagram
participant U as User
participant T as Telegram
participant F as FastAPI Server
participant Y as yFinance
participant P as Pattern Analyzer
participant L as LSTM Model
participant A as ARIMA Model
participant M as MCP Layer
participant D as Decision Engine
U->>T: "analyze AAPL"
T->>F: Webhook POST /webhook
F->>Y: Fetch OHLCV Data
F->>P: Detect candlestick pattern
F->>L: Predict using LSTM
F->>A: Forecast using ARIMA
F->>M: Adjust weights (self-learning)
M->>D: Combine signals (ensemble)
D->>T: Send summarized result💬 示例互动
用户: analyze TSLA
机器人回复:
📊 TSLA Market Summary:
🕯️ Pattern: Bullish Engulfing
💬 Sentiment: Positive (+0.27)
🔮 Forecasts:
• ARIMA: ↑ +0.41%
• LSTM: ↑ +0.32%
✅ Recommendation: BUY (Confidence: 84%)______________________________________________________________________
🧠 检测到蜡烛图形态
| 模式 | 描述 | |
|---|---|---|
| 🟩 翻译为中文是: | 绿色(表示状态良好或通过) 看涨吞没形态 | 表示向上反转 |
| 🟥(红色) 看跌吞没形态 | 信号表明呈下降趋势 | |
| ⚫ Doji(多吉) | 市场犹豫不决 | |
| 🔨(锤子图标,可理解为“工具”或“手工”等含义,具体根据上下文而定) 锤子 | 看涨反转形态 | |
| 🌠 流星 | 看跌反转信号 |
🌅 晨星 强烈的看涨反转
______________________________________________________________________
🧮 LSTM + ARIMA 融合模型
该系统结合了 ARIMA(统计学中的) 并且 LSTM(神经网络) 使用(某种方法/工具)生成的输出 加权集合(或加权组合)。 权重是 自适应调整的 基于以下(条件/因素)的MCP(多层控制协议/多层感知层等,具体含义需根据上下文确定)层
- 预测准确率
- 时间框架的可靠性
- 近期的波动
final_prediction = w1 * lstm_forecast + w2 * arima_forecast🔹 数据流概要
Trigger → Data Fetch → Numerical Forecast → Sentiment → Ensemble Fusion → Context Memory (MCP) → Final Report______________________________________________________________________
🧠 模型上下文协议(MCP)
这个(或“该”) 模型上下文协议 支持跨模型的自适应推理。 MCP起着……的作用 *智能胶水* 在智能体之间——保持历史准确性,调整集成权重,并在会话之间保留上下文记忆。
MCP职责:
- 跟踪模型性能(ARIMA、LSTM、Prophet)
- 动态更新集成权重
- 存储市场叙事的向量嵌入
- 为未来的决策启用情境回忆功能
______________________________________________________________________
📁 仓库结构
MCP_Financial_Agent/
│
├── main.py # Orchestrator entry point
│
├── config/
│ ├── settings.yaml # Model and API configuration
│ └── secrets.env # API keys and secrets
│
├── core/
│ ├── state.py # Shared AgentState object
│ ├── orchestrator.py # LangGraph orchestration flow
│ └── mcp_manager.py # MCP memory, performance tracking
│
├── agents/
│ ├── data_collector_agent.py # Fetches OHLCV + news
│ ├── quant_analysis_agent.py # Computes indicators (RSI, EMA, etc.)
│ ├── sentiment_agent.py # Analyzes sentiment
│ ├── numerical_agent.py # Runs ARIMA / LSTM / Prophet
│ ├── ensemble_agent.py # Fuses signals + decisions
│ └── report_agent.py # Generates Markdown investment memo
│
├── models/
│ ├── arima_model.py
│ ├── lstm_model.py
│ └── prophet_model.py
│
├── tools/
│ ├── fetch_news.py
│ ├── fetch_ohlcv.py
│ ├── indicators.py
│ └── utils.py
│
└── memory/
├── vector_store.py # FAISS or Chroma vector memory
└── model_performance_db.py # SQLite/Redis performance store______________________________________________________________________
⚙️ 安装
1️⃣ 克隆仓库
git clone https://github.com/yourusername/MCP_Financial_Agent.git
cd MCP_Financial_Agent2️⃣ 创建并激活虚拟环境
python -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windows3️⃣ 安装依赖项
pip install -r requirements.txt______________________________________________________________________
🔑 配置
创建一个 .env 归类于 config/:
ALPHA_VANTAGE_API_KEY=your_api_key
NEWS_API_KEY=your_api_key
OPENAI_API_KEY=your_api_key编辑 config/settings.yaml 进行调整:
model:
reasoning: "gpt-5"
forecasting: ["arima", "lstm", "prophet"]
ensemble_weights: [0.4, 0.3, 0.3]
mcp:
memory_store: "vector"
db_type: "sqlite"______________________________________________________________________
🚀 运行代理
示例命令:
python main.py --ticker "TSLA"预期输出:
[Ticker: TSLA]
Prediction: +1.8% in next 1h
Recommendation: BUY (Confidence 0.82)
Rationale: Positive news sentiment and strong momentum indicators.______________________________________________________________________
📊 示例 JSON 输出
{
"ticker": "AAPL",
"forecast": {
"arima_next_15m": "+1.2%",
"lstm_next_1m": "+0.4%",
"prophet_next_1h": "+2.1%"
},
"sentiment": {
"news_sentiment": 0.65,
"market_sentiment": 0.58
},
"technical": {
"RSI": 48,
"MACD": "bullish crossover"
},
"ensemble_decision": {
"final_score": 0.74,
"recommendation": "BUY",
"confidence": 0.83
},
"rationale": "Positive sentiment and quantitative forecast suggest short-term upside."
}______________________________________________________________________
🧩 集合逻辑(简化版)
score = (
0.4 * forecast_confidence +
0.3 * sentiment_score +
0.3 * indicator_signal
)
if score > 0.6:
decision = "BUY"
elif score < 0.4:
decision = "SELL"
else:
decision = "HOLD"______________________________________________________________________
🧬 基于MCP的自适应学习
MCP不断学习:
- 记录每次预测与实际价格变动
- 更新模型准确率指标
- 自动重新加权模型
- 保持上下文嵌入以确保趋势连续性
示例:
[LSTM Accuracy ↑] → Weight adjusted to 0.5
[ARIMA Drift Detected] → Reduced to 0.2______________________________________________________________________
📈 可视化(可选)
使用 matplotlib 或者 plotly 在里面;在……中 report_agent.py 生成:
- 预测价格与实际价格图表
- 情绪趋势线
- 置信度与准确度曲线
______________________________________________________________________
🧠 技术栈
| 层级 | 技术 |
|---|---|
| 工作流程 | LangGraph |
| 上下文记忆 | 模型上下文协议(MCP) |
| LLM GPT-5 / Gemini 1.5 Pro | |
| 数值模型 | ARIMA、Prophet、LSTM(使用PyTorch) |
| 数据来源 | yFinance,NewsAPI,SEC EDGAR(证券交易委员会电子数据存取检索存档系统) |
| 向量存储 | Chroma / FAISS(注:Chroma和FAISS均为技术或工具名称,在此直接保留原样翻译) |
| 数据库 | SQLite / Redis |
______________________________________________________________________
🤖 未来增强功能
- 集成 强化学习(RLHF) 用于决策反馈循环
- 添加 基于Transformer的预测 (时间融合变换器)
- 实施 模拟交易 使用Alpaca或Binance API
- 通过以下方式部署 CrewAI 运行时 或者 LangGraph Cloud(可译为“LangGraph云平台”或根据具体语境简化为“LangGraph云端”)
______________________________________________________________________
🧾 许可证
麻省理工学院许可证 © 2025 — 您可自由修改并分发,但需注明出处。
