@openfinclaw/findoo-datahub-plugin 2026.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/DESIGN.md ADDED
@@ -0,0 +1,234 @@
1
+ # Findoo DataHub Plugin — 设计哲学与使用指南
2
+
3
+ ## 一句话定位
4
+
5
+ **一个 DataHub 端点,覆盖全部金融市场** — 插件不生产数据,只做 DataHub 的智能前端。
6
+
7
+ ---
8
+
9
+ ## 设计哲学
10
+
11
+ ### 1. 单一数据源 (Single Source of Truth)
12
+
13
+ 所有金融数据(A 股、港股、美股、加密货币、宏观经济、衍生品、指数、ETF)统一走 DataHub REST API。
14
+ 插件本身不调用 Tushare、CoinGecko、DefiLlama、Yahoo Finance 的 API —— 这些 provider 的编排和路由在 DataHub 服务端完成。
15
+
16
+ ```
17
+ 用户请求 → AI Tool → DataHubClient → DataHub REST → 38+ provider
18
+ ├── Tushare (A/HK)
19
+ ├── yfinance (US)
20
+ ├── CCXT (Crypto)
21
+ ├── CoinGecko
22
+ ├── DefiLlama
23
+ ├── WorldBank
24
+ └── ...
25
+ ```
26
+
27
+ 好处:
28
+
29
+ - 插件零外部依赖(不需要 ccxt、yahoo-finance2 等 npm 包)
30
+ - 新增 provider 只需 DataHub 端升级,插件无需改动
31
+ - 统一认证模型(一组 Basic Auth 凭据覆盖 172 个端点)
32
+
33
+ ### 2. 零配置即用 (Zero-Config)
34
+
35
+ 内置默认凭据(公共 DataHub 实例),安装后立刻可用,无需注册 API key:
36
+
37
+ ```typescript
38
+ // 默认配置 — 开箱即用
39
+ const DEFAULT_DATAHUB_URL = "http://43.134.61.136:8088";
40
+ const DEFAULT_DATAHUB_USERNAME = "admin";
41
+ const DEFAULT_DATAHUB_PASSWORD = "98ffa5c5-1ec6-4735-8e0c-715a5eca1a8d";
42
+ ```
43
+
44
+ 三种覆盖方式(优先级从高到低):
45
+
46
+ 1. 插件配置(`openclaw config set plugins.findoo-datahub-plugin.datahubApiUrl "..."`)
47
+ 2. 环境变量(`DATAHUB_API_URL`、`DATAHUB_USERNAME`、`DATAHUB_PASSWORD`)
48
+ 3. 内置默认值
49
+
50
+ ### 3. 薄客户端 (Thin Client)
51
+
52
+ `DataHubClient` 只有 ~200 行代码:
53
+
54
+ - 一个通用 `query(path, params)` 方法
55
+ - 8 个 category helper(`equity()`, `crypto()`, `economy()` 等)— 纯路径前缀封装
56
+ - 2 个 typed method(`getOHLCV()`, `getTicker()`)— 标准化 OHLCV/Ticker 结构
57
+ - 智能 provider 选择(`detectEquityProvider`):A 股/港股 → tushare,美股 → yfinance
58
+
59
+ ### 4. 本地缓存加速 (Cache-First OHLCV)
60
+
61
+ OHLCV 数据使用本地 SQLite 缓存(`~/.openfinclaw/state/findoo-ohlcv-cache.sqlite`):
62
+
63
+ ```
64
+ 请求 OHLCV → 命中缓存?→ Yes → 直接返回
65
+ → No → DataHub 取数 → 写入缓存 → 返回
66
+ ```
67
+
68
+ 增量更新策略:缓存记录每个 symbol/market/timeframe 的时间范围,只请求缺失的部分。
69
+
70
+ ### 5. 服务注册 (Service Registration)
71
+
72
+ 暴露 2 个服务供其他 `fin-*` 扩展使用:
73
+
74
+ | Service ID | 接口 | 用途 |
75
+ | --------------------- | ---------------------------------------------------------------------- | ---------------------------------- |
76
+ | `fin-data-provider` | `getOHLCV()`, `getTicker()`, `detectRegime()`, `getSupportedMarkets()` | 策略引擎、模拟盘、基金经理获取行情 |
77
+ | `fin-regime-detector` | `detect(ohlcv[])` | 进化引擎判断市场状态 |
78
+
79
+ 这意味着插件不仅是用户对话的工具,也是整个量化交易系统的数据层基础设施。
80
+
81
+ ---
82
+
83
+ ## 架构总览
84
+
85
+ ```
86
+ findoo-datahub-plugin/
87
+ ├── index.ts # 插件入口:注册 10 个 AI tools + 2 个 services
88
+ ├── openclaw.plugin.json # 插件元数据 + 配置 schema
89
+ ├── package.json
90
+ ├── skills/ # 6 个 Claude 技能文件
91
+ │ ├── equity/ # 股票研究
92
+ │ ├── crypto-defi/ # 加密货币 + DeFi
93
+ │ ├── derivatives/ # 衍生品(期货/期权/可转债)
94
+ │ ├── macro/ # 宏观经济
95
+ │ ├── market-radar/ # 市场雷达
96
+ │ └── data-query/ # 原始数据查询
97
+ └── src/
98
+ ├── config.ts # 配置解析(48 LOC)
99
+ ├── datahub-client.ts # DataHub REST 客户端(208 LOC)
100
+ ├── ohlcv-cache.ts # SQLite OHLCV 缓存
101
+ ├── regime-detector.ts # 市场趋势检测(SMA/ATR)
102
+ ├── types.ts # 类型定义(re-export from fin-shared-types)
103
+ └── datahub-client.test.ts # 46 个测试
104
+ ```
105
+
106
+ ---
107
+
108
+ ## 10 个 AI Tools
109
+
110
+ ### 按市场分类
111
+
112
+ | # | Tool | 覆盖范围 | 典型问题 |
113
+ | --- | ----------------- | -------------------------------- | ------------------------------- |
114
+ | 1 | `fin_stock` | A 股/港股/美股行情、财务、资金流 | "茅台最新股价"、"AAPL 利润表" |
115
+ | 2 | `fin_index` | 指数/ETF/基金 | "沪深 300 成分股"、"50ETF 净值" |
116
+ | 3 | `fin_macro` | GDP/CPI/PMI/利率/汇率/世行 | "中国 CPI 趋势"、"美债收益率" |
117
+ | 4 | `fin_derivatives` | 期货/期权/可转债 | "螺纹钢持仓"、"AAPL 期权链" |
118
+ | 5 | `fin_crypto` | 加密货币行情 + DeFi 协议 | "BTC 行情"、"DeFi TVL 排名" |
119
+ | 6 | `fin_market` | 龙虎榜/涨跌停/融资融券/北向资金 | "今日龙虎榜"、"北向资金流向" |
120
+
121
+ ### 基础设施类
122
+
123
+ | # | Tool | 功能 | 场景 |
124
+ | --- | ------------------ | ----------------------- | ---------------------- |
125
+ | 7 | `fin_query` | 直通 172 端点的原始查询 | 其他 tool 未覆盖的端点 |
126
+ | 8 | `fin_data_ohlcv` | 带缓存的 OHLCV K 线 | 策略回测、技术分析 |
127
+ | 9 | `fin_data_regime` | 市场趋势检测 | 进化引擎判断当前行情 |
128
+ | 10 | `fin_data_markets` | 支持的市场列表 | 能力发现 |
129
+
130
+ ---
131
+
132
+ ## 使用方式
133
+
134
+ ### 1. 安装(零配置)
135
+
136
+ 插件随 OpenFinClaw 自动加载。默认使用公共 DataHub,无需任何配置。
137
+
138
+ ### 2. 自定义 DataHub 实例
139
+
140
+ 如果你自建了 OpenBB DataHub:
141
+
142
+ ```bash
143
+ # 方式 A: 配置文件
144
+ openclaw config set plugins.findoo-datahub-plugin.datahubApiUrl "http://localhost:8088"
145
+ openclaw config set plugins.findoo-datahub-plugin.datahubUsername "myuser"
146
+ openclaw config set plugins.findoo-datahub-plugin.datahubPassword "mypass"
147
+
148
+ # 方式 B: 环境变量
149
+ export DATAHUB_API_URL="http://localhost:8088"
150
+ export DATAHUB_USERNAME="myuser"
151
+ export DATAHUB_PASSWORD="mypass"
152
+ ```
153
+
154
+ ### 3. 对话示例
155
+
156
+ ```
157
+ 用户: 茅台最近一个月行情
158
+ AI: (调用 fin_stock, symbol=600519.SH, endpoint=price/historical)
159
+
160
+ 用户: 比特币 DeFi 协议 TVL 排名
161
+ AI: (调用 fin_crypto, endpoint=defi/protocols)
162
+
163
+ 用户: 中国 CPI 和 PPI 趋势对比
164
+ AI: (调用 fin_macro, endpoint=cpi) + (调用 fin_macro, endpoint=ppi)
165
+
166
+ 用户: 螺纹钢期货持仓分析
167
+ AI: (调用 fin_derivatives, symbol=RB2501.SHF, endpoint=futures/holding)
168
+ ```
169
+
170
+ ### 4. 作为数据层供其他扩展使用
171
+
172
+ 其他 `fin-*` 扩展通过服务注册表访问数据:
173
+
174
+ ```typescript
175
+ // 在 fin-strategy-engine 或 fin-fund-manager 中
176
+ const dp = api.getService("fin-data-provider");
177
+ const ohlcv = await dp.instance.getOHLCV({
178
+ symbol: "BTC/USDT",
179
+ market: "crypto",
180
+ timeframe: "1h",
181
+ limit: 300,
182
+ });
183
+ ```
184
+
185
+ ---
186
+
187
+ ## DataHub 端点分类 (172 总计)
188
+
189
+ | 分类 | 端点数 | 主要 Provider | 代表性 API |
190
+ | ----------- | ------ | -------------------------- | ----------------------------------------------- |
191
+ | Equity | 83 | Tushare, yfinance, Polygon | price/historical, fundamental/income, moneyflow |
192
+ | Crypto | 23 | CCXT, CoinGecko, DefiLlama | market/ticker, defi/protocols, coin/market |
193
+ | Economy | 21 | Tushare, WorldBank | gdp/real, cpi, shibor, treasury |
194
+ | Derivatives | 13 | Tushare | futures/holding, options/chains |
195
+ | Index | 12 | Tushare | price/historical, constituents, thematic |
196
+ | ETF/Fund | 9 | Tushare, yfinance | etf/historical, fund/portfolio |
197
+ | Currency | 6 | Polygon, fixer | fx/historical, fx/snapshots |
198
+ | Coverage | 5 | (meta) | providers, endpoints |
199
+
200
+ ---
201
+
202
+ ## 测试
203
+
204
+ ```bash
205
+ # 全量测试(含 live DataHub 连接)
206
+ pnpm test extensions/findoo-datahub-plugin
207
+
208
+ # 跳过 live 测试
209
+ DATAHUB_SKIP_LIVE=1 pnpm test extensions/findoo-datahub-plugin
210
+ ```
211
+
212
+ 46 个测试:DataHubClient API、OHLCV 缓存、provider 检测、配置解析、Live E2E。
213
+
214
+ ---
215
+
216
+ ## 与旧架构的对比
217
+
218
+ | 维度 | 旧 (fin-data-bus + fin-data-hub) | 新 (findoo-datahub-plugin) |
219
+ | ---------------- | ----------------------------------- | -------------------------- |
220
+ | 代码量 | ~1200 LOC (两个扩展) | ~700 LOC (单扩展) |
221
+ | 外部 npm 依赖 | ccxt, yahoo-finance2, coingecko-api | 无(仅 DataHub REST) |
222
+ | API key 管理 | 每个 provider 单独配置 | 一组凭据覆盖全部 |
223
+ | 新 provider 接入 | 写 adapter → 改路由 → 测试 | DataHub 端增加,插件不动 |
224
+ | AI Tools | 分散在两个扩展 | 统一 10 个 tool |
225
+ | 缓存 | 有(OHLCVCache) | 保留(SQLite) |
226
+ | 市场趋势 | RegimeDetector | 保留(SMA/ATR) |
227
+
228
+ ---
229
+
230
+ ## 未来计划
231
+
232
+ - **Lite 版本**: 无需 DataHub 的轻量模式,直接调用免费 API(CCXT + Yahoo + CoinGecko)
233
+ - **WebSocket 实时**: DataHub 支持后接入实时行情推送
234
+ - **自定义 Provider 路由**: 允许用户指定特定 symbol 走特定 provider