jaipreet15/tradingview-mcp

GitHub: jaipreet15/tradingview-mcp

面向 AI 助手的 TradingView MCP 服务器,提供多交易所市场数据、技术分析、情绪聚合与策略回测引擎。

Stars: 158 | Forks: 1084

# TradingView MCP Server 一个面向生产环境的 [Model Context Protocol](https://modelcontextprotocol.io) 服务器,向 AI 助手和自动化工具提供 TradingView 市场数据、多交易所筛选器、技术分析、市场情绪、新闻以及内置的回测引擎。 ## 目录 - [功能亮点](#feature-highlights) - [架构](#architecture) - [工作流](#workflows) - [项目结构](#project-structure) - [安装](#installation) - [配置](#configuration) - [开发](#development) - [测试](#testing) - [故障排除](#troubleshooting) - [贡献](#contributing) - [常见问题](#faq) ## 功能亮点 | 能力 | 详情 | |------------|---------| | **多交易所筛选器** | 通过静态标的列表 + TradingView scanner API 支持 Crypto、美股、EGX、BIST、HKEX、SSE、TWSE 等 | | **技术分析** | 布林带评级、RSI、多时间框架对齐、成交量突破、K线形态 | | **回测** | 9 种策略,支持 Sharpe、Calmar、Walk-forward 过拟合检测 | | **市场情报** | Reddit 情绪分析、RSS 财经新闻、Yahoo Finance 报价 | | **韧性机制** | 重试/退避、内存 + 可选的分布式缓存、结构化错误封装 | | **MCP 原生** | 36 个工具、stdio 传输,兼容 Claude Desktop、Cursor、Copilot 及其他 MCP 客户端 | ## 架构 ``` flowchart TB subgraph clients [MCP Clients] Claude[Claude Desktop] Cursor[Cursor IDE] Other[Other MCP Hosts] end subgraph server [tradingview-mcp-server] Router[MCP Tool Router] Config[Zod Config Loader] Logger[Structured Logger] subgraph services [Service Layer] Screener[Screener Service] Scanner[Volume Scanner] Backtest[Backtest Engine] Yahoo[Yahoo Finance] Sentiment[Reddit Sentiment] News[RSS News] EGX[EGX Tools] Futures[Futures Tools] end subgraph persistence [Persistence] Memory[In-Memory Cache] Redis[(Redis Cache)] end Provider[Screener Provider\nretry + throttle] end subgraph external [External APIs] TV[scanner.tradingview.com] YF[Yahoo Finance Chart API] Reddit[Reddit JSON API] RSS[Financial RSS Feeds] end Claude --> Router Cursor --> Router Other --> Router Router --> services Config --> services Logger --> services Screener --> Provider Scanner --> Provider Provider --> Memory Provider --> Redis Provider --> TV Yahoo --> YF Sentiment --> Reddit News --> RSS Backtest --> YF ``` ### 层级职责 1. **MCP 路由** (`src/server.ts`) — 验证输入,委托给服务,返回 JSON payload。 2. **服务层** — 不依赖 MCP 的领域逻辑;可独立测试。 3. **筛选器提供程序** — 具备重试、抖动、节流和缓存集成的 HTTP 客户端。 4. **持久化层** — 可选的基于 Redis 的缓存,可优雅回退到内存存储。 ## 工作流 ### 筛选器请求流程 ``` sequenceDiagram participant Client as MCP Client participant Server as MCP Server participant Cache as Cache Store participant TV as TradingView Scanner Client->>Server: top_gainers(exchange, timeframe) Server->>Cache: lookup cache key alt cache hit Cache-->>Server: cached rows else cache miss Server->>TV: POST /{market}/scan TV-->>Server: indicator columns Server->>Cache: store result end Server-->>Client: JSON rows or error envelope ``` ### 综合分析流程 ``` sequenceDiagram participant Client as MCP Client participant Server as MCP Server participant TA as Technical Service participant Sent as Sentiment Service participant News as News Service Client->>Server: combined_analysis(symbol) par Parallel fetches Server->>TA: coin_analysis Server->>Sent: market_sentiment Server->>News: financial_news end TA-->>Server: technical payload Sent-->>Server: Reddit score News-->>Server: headlines Server-->>Client: confluence summary ``` ## 项目结构 ``` tradingview-mcp/ ├── src/ │ ├── index.ts # CLI entry point │ ├── server.ts # MCP tool registration │ ├── config/ # Environment validation (Zod) │ ├── core/ # Errors, shared types │ ├── indicators/ # Pure indicator math + metrics │ ├── persistence/ # Redis connection + cache store │ ├── services/ # Domain services │ │ ├── screener/ # TradingView scanner integration │ │ ├── scanner/ # Volume breakout scanners │ │ ├── backtest.ts # Strategy backtesting │ │ ├── yahoo-finance.ts # Price quotes + OHLCV │ │ ├── sentiment.ts # Reddit analysis │ │ ├── news.ts # RSS aggregation │ │ ├── egx.ts # Egyptian Exchange tools │ │ └── futures.ts # Futures overview tools │ ├── utils/ # Validators, HTTP, logging │ └── data/coinlist/ # Per-exchange symbol lists ├── tests/unit/ # Vitest unit tests ├── docs/AUDIT.md # Pre-migration audit notes ├── .env.example # Configuration reference ├── package.json ├── tsconfig.json └── tsup.config.ts ``` **设计决策:** - **严格的 TypeScript** — 启用 `strict`、`noUnusedLocals`、`noUncheckedIndexedAccess`。 - **服务隔离** — MCP 层包含零业务逻辑。 - **缓存抽象** — Redis 是可选的;没有它服务器也能运行。 - **结构化错误** — 稳定的 `ErrorCode` 字符串,便于程序化处理。 ## 安装 ### 环境要求 - Node.js 18+ - npm 9+ - (可选)用于分布式缓存的 Redis 6+ ### 设置 ``` git clone https://github.com/your-org/tradingview-mcp.git cd tradingview-mcp npm install npm run build ``` ### Claude Desktop ``` { "mcpServers": { "tradingview": { "command": "node", "args": ["D:/path/to/tradingview-mcp/dist/index.js"] } } } ``` ### Cursor / VS Code MCP 将你的 MCP 配置指向构建好的 `dist/index.js` 二进制文件。更改后请重启宿主程序。 ### Docker(含 Redis) ``` docker compose up -d ``` ## 配置 将 `.env.example` 复制为 `.env`: | 变量 | 默认值 | 描述 | |----------|---------|-------------| | `LOG_LEVEL` | `info` | 日志详细程度 | | `REDIS_ENABLED` | `false` | 启用分布式缓存 | | `REDIS_URL` | — | 完整的 Redis 连接 URL | | `REDIS_HOST` | `127.0.0.1` | 未设置 URL 时的 Redis 主机 | | `REDIS_PORT` | `6379` | Redis 端口 | | `REDIS_KEY_PREFIX` | `tradingview-mcp:` | Key 命名空间前缀 | | `TRADINGVIEW_MCP_CACHE_TTL` | `60` | 新鲜缓存 TTL(秒) | | `TRADINGVIEW_MCP_STALE_TTL` | `21600` | 过期备选 TTL(秒) | | `TRADINGVIEW_MCP_RETRY_DELAYS` | `1.0,4.0` | 重试退避计划 | | `TRADINGVIEW_MCP_MAX_INFLIGHT` | `2` | 并发 TA 请求上限 | | `PROXY_ENABLED` | `false` | 启用轮换 HTTP 代理 | 查看 `.env.example` 获取完整列表。 ## 开发 ``` # Watch mode npm run dev # Type check npm run typecheck # Lint npm run lint # Full validation pipeline npm run validate ``` ### 添加新的 MCP 工具 1. 在 `src/services/` 中实现逻辑。 2. 在 `src/server.ts` 中使用 Zod 参数 schema 注册该工具。 3. 在 `tests/unit/` 下添加单元测试。 4. 运行 `npm run validate`。 ## 测试 ``` npm test # run once npm run test:watch # watch mode ``` 当前覆盖率主要集中于: - 结构化错误封装 - 时间框架/交易所验证器 - 缓存存储行为 为了保持 CI 的确定性,特意排除了针对实时 TradingView/Yahoo API 的集成测试。 ## 故障排除 ### 空筛选器结果与错误对比 | 症状 | 含义 | 操作 | |---------|---------|--------| | `[]` 空数组 | 今天没有符合过滤条件的标的 | 正常现象 — 调整过滤条件 | | `{"error":{"code":"ALL_BATCHES_FAILED"}}` | 上游故障 | 等待并重试;检查网络 | | 超时信息 | 扫描器缓慢或被限流 | 减小批次大小;启用缓存 | ### Redis 连接失败 服务器会**优雅降级** — 如果在启动时无法连接 Redis,会自动使用内存缓存。请检查 `REDIS_URL`、防火墙规则以及 Redis 是否接受连接。 ### Windows MCP 首次启动超时 在配置你的 MCP 宿主程序之前,请预先构建项目(`npm run build`),以便服务器能瞬间启动: ``` npm install && npm run build ``` ### Node 版本错误 需要 Node 18+。使用 `node --version` 进行验证。 ## 常见问题 **这需要 TradingView 账户吗?** 不需要。该服务器使用公开的扫描器和图表 endpoint。无需登录或 API key。 **我可以在没有 Redis 的情况下运行吗?** 可以。设置 `REDIS_ENABLED=false`(默认)。缓存将仅使用内存存储。 **支持哪些交易所?** `src/data/coinlist/` 中拥有标的列表,且在 `src/utils/validators.ts` 中配置了 TradingView scanner 市场映射的任何交易所。 **回测结果有保证吗?** 没有。回测使用历史 Yahoo Finance 数据及模拟成本。过去的表现不能预测未来的结果。 **如何处理速率限制?** 调整 `TRADINGVIEW_MCP_MAX_INFLIGHT`、`TRADINGVIEW_MCP_MIN_INTERVAL_S`,并启用 Redis 缓存以减少重复的上游调用。 **这是财务建议吗?** 不是。本软件仅为信息工具。在做出财务决定之前,请咨询持有执照的专业人士。 ## 许可证 MIT — 查看 [LICENSE](LICENSE)。
标签:AI助手, MCP协议, MITM代理, 技术分析, 搜索引擎查询, 自动化攻击, 请求拦截, 量化交易, 金融数据