@mzzsfy/dsh-stock-assistant 0.8.0 → 0.9.0
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/CHANGELOG.md +23 -1
- package/README.md +10 -0
- package/package.json +1 -1
- package/src/application.mjs +400 -400
- package/src/catalog.mjs +193 -193
- package/src/client.js +2680 -2642
- package/src/collector.mjs +265 -265
- package/src/executor.mjs +149 -148
- package/src/index.js +45 -45
- package/src/provider-router.mjs +2 -1
- package/src/providers/eastmoney.mjs +226 -217
- package/src/providers/tencent.mjs +168 -168
- package/src/providers/tushare.mjs +92 -92
- package//345/205/274/345/256/271/346/200/247/346/265/213/350/257/225.md +5 -1
|
@@ -1,217 +1,226 @@
|
|
|
1
|
-
// 东财公开行情接口适配器:quotes/catalog/search/kline。非官方端点,无SLA;延迟未知,f124为来源秒级时间戳。
|
|
2
|
-
import { normalizeQuote, makeSecurity, securityId, StockError, MARKET_META, sharesFromLots, zonedUnixSeconds } from '../market-model.mjs'
|
|
3
|
-
// 官方默认端点表(key 供 instance.endpoints 覆盖, 反代/镜像场景由 ctx.endpoints 提供同名 key)
|
|
4
|
-
const DEFAULT_ENDPOINTS = {
|
|
5
|
-
quotes: 'https://push2.eastmoney.com/api/qt/ulist.np/get',
|
|
6
|
-
list: 'https://push2.eastmoney.com/api/qt/clist/get',
|
|
7
|
-
search: 'https://searchapi.eastmoney.com/api/suggest/get',
|
|
8
|
-
kline: 'https://push2his.eastmoney.com/api/qt/stock/kline/get',
|
|
9
|
-
trend: 'https://push2his.eastmoney.com/api/qt/stock/trends2/get',
|
|
10
|
-
}
|
|
11
|
-
const endpointOf = (ctx, key) => ctx.endpoints?.[key] || DEFAULT_ENDPOINTS[key]
|
|
12
|
-
// 沪深市场代码(其余市场的 secid 前缀见 SECID_PREFIX)
|
|
13
|
-
const MARKET = { XSHG: '1', XSHE: '0' }
|
|
14
|
-
// 搜索联想 MktNum→交易所全集(实测: 1/0 沪深, 116 港, 105/106/107 美三大, 176/177/155 日韩英)
|
|
15
|
-
const SEARCH_EXCHANGE = { 1: 'XSHG', 0: 'XSHE', 116: 'XHKG', 105: 'XNAS', 106: 'XNYS', 107: 'XNYS', 176: 'XJPX', 177: 'XKRX', 155: 'XLON' }
|
|
16
|
-
const SECID_PREFIX = { XSHG: '1', XSHE: '0', XHKG: '116', XNAS: '105', XNYS: '106', XJPX: '176', XKRX: '177', XLON: '155' }
|
|
17
|
-
// 美股三大市场号: 105 纳斯达克 / 106 纽约 / 107 美交所。secid 前缀必须精确匹配, 探针期据此逐个试探。
|
|
18
|
-
const US_MKTNUMS = ['105', '106', '107']
|
|
19
|
-
const isUSExchange = exchange => exchange === 'XNAS' || exchange === 'XNYS'
|
|
20
|
-
// 美股 secid 前缀候选序: 搜索缓存 > 交易所静态前缀 > 其余美股市场号。XNYS 实横跨 106 与 107, 静态前缀只取其一。
|
|
21
|
-
// 缓存只加速重复查询, 缺失或过期不改变正确性: 进程重启后 usSymbols 为空, 取报价不得依赖此前的搜索行为。
|
|
22
|
-
const usSecidPrefixes = (security, ctx) => [...new Set([ctx.usSymbols?.get(`US:${security.code}`)?.mktNum, SECID_PREFIX[security.exchange], ...US_MKTNUMS].filter(mktNum => US_MKTNUMS.includes(mktNum)))]
|
|
23
|
-
const secidFor = (security, ctx) => isUSExchange(security.exchange) ? `${usSecidPrefixes(security, ctx)[0]}.${security.code}` : `${SECID_PREFIX[security.exchange]}.${security.code}`
|
|
24
|
-
// klt 周期映射(实测): 日101/周102/月103; trends2 无 klt。
|
|
25
|
-
const KLT = { '1d': 101, '1w': 102, '1M': 103 }
|
|
26
|
-
// 目录 fs 按市场给出; 英股 m:155 含债券与结构性产品, 加 t:1 只取股票; 日韩不支持 t: 细分, 全量即股票
|
|
27
|
-
const LIST_FS = { XSHG: 'm:1+t:2,m:1+t:23', XSHE: 'm:0+t:6,m:0+t:80', XHKG: 'm:116+t:3,m:116+t:4', XNAS: 'm:105+t:1', XNYS: 'm:106+t:1,m:107+t:1', XJPX: 'm:176', XKRX: 'm:177', XLON: 'm:155+t:1' }
|
|
28
|
-
const SYMBOL_PREFIX = { 1: 'XSHG', 0: 'XSHE', 116: 'XHKG', 105: 'XNAS', 106: 'XNYS', 107: 'XNYS', 176: 'XJPX', 177: 'XKRX', 155: 'XLON' }
|
|
29
|
-
// 探针代表证券(实测可用的各市场流动性代表),quotes/kline 探针按市场取用。
|
|
30
|
-
const PROBE_SECURITY = { XSHG: '600519', XSHE: '000001', XHKG: '00700', XNAS: 'AAPL', XNYS: 'BABA', XJPX: '7203', XKRX: '005930', XLON: 'SHEL' }
|
|
31
|
-
// klines CSV 前 7 列固定语义(实测 fields2=f51-f57); f53=close 在 f54=high 前为东财原生序。
|
|
32
|
-
const KLINE_FIELDS = 'f51,f52,f53,f54,f55,f56,f57'
|
|
33
|
-
const TREND_FIELDS = 'f51,f52,f53,f54,f55,f56,f57,f58'
|
|
34
|
-
// 分时时间戳的来源时区: 东财统一用北京时间, 与证券所属市场无关
|
|
35
|
-
const SOURCE_ZONE = 'Asia/Shanghai'
|
|
36
|
-
const num = value => value == null || typeof value === 'number' && !Number.isFinite(value) ? null : value === '-' ? null : String(value)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const result = await this[capability](
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
//
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
if (total
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
1
|
+
// 东财公开行情接口适配器:quotes/catalog/search/kline。非官方端点,无SLA;延迟未知,f124为来源秒级时间戳。
|
|
2
|
+
import { normalizeQuote, makeSecurity, securityId, StockError, MARKET_META, sharesFromLots, zonedUnixSeconds } from '../market-model.mjs'
|
|
3
|
+
// 官方默认端点表(key 供 instance.endpoints 覆盖, 反代/镜像场景由 ctx.endpoints 提供同名 key)
|
|
4
|
+
const DEFAULT_ENDPOINTS = {
|
|
5
|
+
quotes: 'https://push2.eastmoney.com/api/qt/ulist.np/get',
|
|
6
|
+
list: 'https://push2.eastmoney.com/api/qt/clist/get',
|
|
7
|
+
search: 'https://searchapi.eastmoney.com/api/suggest/get',
|
|
8
|
+
kline: 'https://push2his.eastmoney.com/api/qt/stock/kline/get',
|
|
9
|
+
trend: 'https://push2his.eastmoney.com/api/qt/stock/trends2/get',
|
|
10
|
+
}
|
|
11
|
+
const endpointOf = (ctx, key) => ctx.endpoints?.[key] || DEFAULT_ENDPOINTS[key]
|
|
12
|
+
// 沪深市场代码(其余市场的 secid 前缀见 SECID_PREFIX)
|
|
13
|
+
const MARKET = { XSHG: '1', XSHE: '0' }
|
|
14
|
+
// 搜索联想 MktNum→交易所全集(实测: 1/0 沪深, 116 港, 105/106/107 美三大, 176/177/155 日韩英)
|
|
15
|
+
const SEARCH_EXCHANGE = { 1: 'XSHG', 0: 'XSHE', 116: 'XHKG', 105: 'XNAS', 106: 'XNYS', 107: 'XNYS', 176: 'XJPX', 177: 'XKRX', 155: 'XLON' }
|
|
16
|
+
const SECID_PREFIX = { XSHG: '1', XSHE: '0', XHKG: '116', XNAS: '105', XNYS: '106', XJPX: '176', XKRX: '177', XLON: '155' }
|
|
17
|
+
// 美股三大市场号: 105 纳斯达克 / 106 纽约 / 107 美交所。secid 前缀必须精确匹配, 探针期据此逐个试探。
|
|
18
|
+
const US_MKTNUMS = ['105', '106', '107']
|
|
19
|
+
const isUSExchange = exchange => exchange === 'XNAS' || exchange === 'XNYS'
|
|
20
|
+
// 美股 secid 前缀候选序: 搜索缓存 > 交易所静态前缀 > 其余美股市场号。XNYS 实横跨 106 与 107, 静态前缀只取其一。
|
|
21
|
+
// 缓存只加速重复查询, 缺失或过期不改变正确性: 进程重启后 usSymbols 为空, 取报价不得依赖此前的搜索行为。
|
|
22
|
+
const usSecidPrefixes = (security, ctx) => [...new Set([ctx.usSymbols?.get(`US:${security.code}`)?.mktNum, SECID_PREFIX[security.exchange], ...US_MKTNUMS].filter(mktNum => US_MKTNUMS.includes(mktNum)))]
|
|
23
|
+
const secidFor = (security, ctx) => isUSExchange(security.exchange) ? `${usSecidPrefixes(security, ctx)[0]}.${security.code}` : `${SECID_PREFIX[security.exchange]}.${security.code}`
|
|
24
|
+
// klt 周期映射(实测): 日101/周102/月103; trends2 无 klt。
|
|
25
|
+
const KLT = { '1d': 101, '1w': 102, '1M': 103 }
|
|
26
|
+
// 目录 fs 按市场给出; 英股 m:155 含债券与结构性产品, 加 t:1 只取股票; 日韩不支持 t: 细分, 全量即股票
|
|
27
|
+
const LIST_FS = { XSHG: 'm:1+t:2,m:1+t:23', XSHE: 'm:0+t:6,m:0+t:80', XHKG: 'm:116+t:3,m:116+t:4', XNAS: 'm:105+t:1', XNYS: 'm:106+t:1,m:107+t:1', XJPX: 'm:176', XKRX: 'm:177', XLON: 'm:155+t:1' }
|
|
28
|
+
const SYMBOL_PREFIX = { 1: 'XSHG', 0: 'XSHE', 116: 'XHKG', 105: 'XNAS', 106: 'XNYS', 107: 'XNYS', 176: 'XJPX', 177: 'XKRX', 155: 'XLON' }
|
|
29
|
+
// 探针代表证券(实测可用的各市场流动性代表),quotes/kline 探针按市场取用。
|
|
30
|
+
const PROBE_SECURITY = { XSHG: '600519', XSHE: '000001', XHKG: '00700', XNAS: 'AAPL', XNYS: 'BABA', XJPX: '7203', XKRX: '005930', XLON: 'SHEL' }
|
|
31
|
+
// klines CSV 前 7 列固定语义(实测 fields2=f51-f57); f53=close 在 f54=high 前为东财原生序。
|
|
32
|
+
const KLINE_FIELDS = 'f51,f52,f53,f54,f55,f56,f57'
|
|
33
|
+
const TREND_FIELDS = 'f51,f52,f53,f54,f55,f56,f57,f58'
|
|
34
|
+
// 分时时间戳的来源时区: 东财统一用北京时间, 与证券所属市场无关
|
|
35
|
+
const SOURCE_ZONE = 'Asia/Shanghai'
|
|
36
|
+
const num = value => value == null || typeof value === 'number' && !Number.isFinite(value) ? null : value === '-' ? null : String(value)
|
|
37
|
+
// 风控页是 HTML: 不嗅探就会被当成「响应不是JSON」, 用户看不到「去浏览器过滑块」这条唯一出路
|
|
38
|
+
const CHALLENGE_HINT = /验证码|人机验证|安全验证|滑块|拖动|geetest|tcaptcha|captcha/i
|
|
39
|
+
const challenged = async response => {
|
|
40
|
+
try {
|
|
41
|
+
const text = await response.clone().text()
|
|
42
|
+
return /^\s*</.test(text) || CHALLENGE_HINT.test(text)
|
|
43
|
+
} catch { return false }
|
|
44
|
+
}
|
|
45
|
+
async function getJson(ctx, url) {
|
|
46
|
+
let response
|
|
47
|
+
try {
|
|
48
|
+
response = await ctx.fetch(url, { headers: { Referer: 'https://quote.eastmoney.com/', 'User-Agent': 'Mozilla/5.0' }, signal: ctx.signal, redirect: 'error' })
|
|
49
|
+
} catch (e) {
|
|
50
|
+
if (ctx.signal.aborted) throw new StockError('CANCELLED', '请求已取消', 503)
|
|
51
|
+
if (e instanceof StockError) throw e
|
|
52
|
+
throw new StockError('NETWORK', '无法连接行情来源', 502, { retryable: true })
|
|
53
|
+
}
|
|
54
|
+
if (await challenged(response)) throw new StockError('CHALLENGE', '东财要求人机验证:用浏览器打开 www.eastmoney.com 完成验证后自动恢复', 502)
|
|
55
|
+
if (!response.ok) throw new StockError('NETWORK', '来源服务暂不可用', 502, { retryable: true })
|
|
56
|
+
return response.json().catch(() => { throw new StockError('BAD_PAYLOAD', '来源响应不是JSON', 502) })
|
|
57
|
+
}
|
|
58
|
+
export const eastmoney = {
|
|
59
|
+
descriptor: { adapterId: 'eastmoney', capabilities: ['quotes', 'catalog', 'search', 'kline', 'trend'], markets: ['XSHG', 'XSHE', 'XHKG', 'XNAS', 'XNYS', 'XJPX', 'XKRX', 'XLON'], instruments: ['equity'], maxQuoteBatch: 60, credential: 'none', description: '东方财富公开接口:免凭据,八市场目录,行情/K线/分时/搜索,非官方无SLA', endpointKeys: [{ key: 'quotes', label: '行情批量' }, { key: 'list', label: '证券目录' }, { key: 'search', label: '搜索联想' }, { key: 'kline', label: 'K线历史' }, { key: 'trend', label: '分时' }] },
|
|
60
|
+
async probe(request, ctx) {
|
|
61
|
+
if (request.capability === 'catalog' || request.capability === 'search') {
|
|
62
|
+
// 探针词按市场用代表证券代码(茅台/平安只对沪深有效, 其他市场返回 0 项会误判 denied)。
|
|
63
|
+
const result = await this[request.capability]({ ...request, probe: true, q: PROBE_SECURITY[request.market] || '平安', limit: 5 }, ctx)
|
|
64
|
+
if (!result.items.length) throw new StockError('NO_DATA', '来源未返回可校验数据,暂不能确认能力就绪', 422)
|
|
65
|
+
return { status: 'ready', capability: request.capability, market: request.market, instrument: 'equity' }
|
|
66
|
+
}
|
|
67
|
+
// kline 探针直接拉代表证券 3 根日K(实测列义/端点全链), trend 探针拉当日分时, quotes 探针沿用行情行。
|
|
68
|
+
// 各能力必须探各自的上游端点: 拿行情接口冒充分时, 「分时就绪」只会是假阳性
|
|
69
|
+
if (request.capability === 'kline' || request.capability === 'trend') {
|
|
70
|
+
const capability = request.capability
|
|
71
|
+
const code = PROBE_SECURITY[request.market] || '000001'
|
|
72
|
+
const result = await this[capability](capability === 'kline'
|
|
73
|
+
? { ...request, securityId: `${request.market}:equity:${code}`, code, period: '1d', adjust: 'qfq', limit: 3 }
|
|
74
|
+
: { ...request, securityId: `${request.market}:equity:${code}`, code, period: 'trend', limit: 50 }, ctx)
|
|
75
|
+
if (!result.candles.length) throw new StockError('NO_DATA', '来源未返回可校验数据,暂不能确认能力就绪', 422)
|
|
76
|
+
return { status: 'ready', capability, market: request.market, instrument: 'equity' }
|
|
77
|
+
}
|
|
78
|
+
// 美股前缀的逐个试探已收敛进 quotes,探针只需一次结果,避免探针与真实取价走两套路径
|
|
79
|
+
const security = request.securities?.[0] || { exchange: request.market, code: PROBE_SECURITY[request.market] || '000001' }
|
|
80
|
+
const rows = await this.quotes({ ...request, securities: [security] }, ctx)
|
|
81
|
+
if (!rows.items.length) throw new StockError('NO_DATA', '来源未返回可校验数据,暂不能确认能力就绪', 422)
|
|
82
|
+
return { status: 'ready', capability: 'quotes', market: request.market, instrument: 'equity' }
|
|
83
|
+
},
|
|
84
|
+
async quotes(request, ctx) {
|
|
85
|
+
const fields = 'f2,f3,f4,f5,f6,f12,f13,f14,f15,f16,f17,f18,f124'
|
|
86
|
+
const seen = new Set(), items = []
|
|
87
|
+
// 一次请求取一批 secid;data:null 表示整批 secid 均无效(如整批退市),降级为逐证券缺失。
|
|
88
|
+
const ingest = async symbols => {
|
|
89
|
+
const body = await getJson(ctx, `${endpointOf(ctx, 'quotes')}?secids=${[...symbols.keys()].join(',')}&fields=${fields}&fltt=2&invt=2`)
|
|
90
|
+
const rows = body?.data?.diff
|
|
91
|
+
if (!Array.isArray(rows)) return
|
|
92
|
+
for (const row of rows) {
|
|
93
|
+
const security = symbols.get(`${row.f13}.${row.f12}`)
|
|
94
|
+
if (!security) continue
|
|
95
|
+
if (seen.has(security.id)) throw new StockError('BAD_PAYLOAD', '报价身份冲突', 502)
|
|
96
|
+
seen.add(security.id)
|
|
97
|
+
// fltt=2时价格为已缩放数值, "-"表示停牌等无值;f124为来源Unix秒;成交额币种按市场派生(单一事实源 MARKET_META.currency)。
|
|
98
|
+
const quoteAt = Number.isFinite(row.f124) ? new Date(row.f124 * 1000).toISOString() : null
|
|
99
|
+
const currency = MARKET_META[security.exchange]?.currency || 'CNY'
|
|
100
|
+
items.push(normalizeQuote({
|
|
101
|
+
securityId: security.id, providerInstanceId: request.providerInstanceId, adapterId: 'eastmoney', providerSymbol: `${row.f13}.${row.f12}`,
|
|
102
|
+
last: num(row.f2), previousClose: num(row.f18), open: num(row.f17), high: num(row.f15), low: num(row.f16),
|
|
103
|
+
volume: sharesFromLots(num(row.f5), security.exchange), volumeUnit: 'share', amount: num(row.f6), amountUnit: currency,
|
|
104
|
+
quoteAt, timestampKind: 'trade',
|
|
105
|
+
receivedAt: ctx.now(), delayKind: 'unknown', delaySeconds: null, securityState: 'unknown',
|
|
106
|
+
}))
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
await ingest(new Map(request.securities.map(s => [secidFor(s, ctx), s])))
|
|
110
|
+
// 美股前缀兜底: 静态前缀只对应一个市场号,而交易所归属未必精确(实测 NYSE 的票分属 106/107)。
|
|
111
|
+
// 只对首轮落空的美股补齐剩余候选,一次批量发完,不逐证券重试。
|
|
112
|
+
const orphanUS = request.securities.filter(s => !seen.has(s.id) && isUSExchange(s.exchange))
|
|
113
|
+
const altSymbols = new Map()
|
|
114
|
+
for (const security of orphanUS) for (const mktNum of usSecidPrefixes(security, ctx).slice(1)) altSymbols.set(`${mktNum}.${security.code}`, security)
|
|
115
|
+
if (altSymbols.size) await ingest(altSymbols)
|
|
116
|
+
return { providerInstanceId: request.providerInstanceId, fetchedAt: ctx.now(), items, errors: request.securities.filter(s => !seen.has(s.id)).map(s => ({ securityId: s.id, code: 'NO_DATA', message: '来源未返回此证券报价' })) }
|
|
117
|
+
},
|
|
118
|
+
async catalog(request, ctx) {
|
|
119
|
+
const items = [], seen = new Set()
|
|
120
|
+
const PAGE_SIZE = 100, MAX_PAGES = 50
|
|
121
|
+
// 探针只拉一页: executor 单请求 8 秒预算内完成, 全量翻页是同步通道的职责
|
|
122
|
+
const pages = request.probe ? 1 : MAX_PAGES
|
|
123
|
+
let page = 1, complete = true
|
|
124
|
+
while (page <= pages) {
|
|
125
|
+
const url = `${endpointOf(ctx, 'list')}?pn=${page}&pz=${PAGE_SIZE}&po=1&np=1&fltt=2&invt=2&fid=f12&fs=${LIST_FS[request.market]}&fields=f12,f13,f14`
|
|
126
|
+
const body = await getJson(ctx, url)
|
|
127
|
+
const diff = body?.data?.diff
|
|
128
|
+
if (!Array.isArray(diff)) throw new StockError('BAD_PAYLOAD', '目录字段结构异常', 502)
|
|
129
|
+
const total = Number(body.data.total)
|
|
130
|
+
if (!Number.isFinite(total)) throw new StockError('BAD_PAYLOAD', '目录总数缺失', 502)
|
|
131
|
+
for (const row of diff) {
|
|
132
|
+
const exchange = SYMBOL_PREFIX[row.f13]
|
|
133
|
+
if (exchange !== request.market || typeof row.f12 !== 'string' || !MARKET_META[request.market].codePattern.test(row.f12) || typeof row.f14 !== 'string' || !row.f14) continue
|
|
134
|
+
const id = securityId(exchange, row.f12)
|
|
135
|
+
if (seen.has(id)) throw new StockError('BAD_PAYLOAD', '目录身份冲突', 502)
|
|
136
|
+
seen.add(id)
|
|
137
|
+
items.push(makeSecurity(exchange, row.f12, row.f14, { cnspell: '', catalogSource: request.providerInstanceId }))
|
|
138
|
+
}
|
|
139
|
+
if (total > MAX_PAGES * PAGE_SIZE) complete = false
|
|
140
|
+
if (page * PAGE_SIZE >= total || diff.length === 0) break
|
|
141
|
+
page++
|
|
142
|
+
}
|
|
143
|
+
return { providerInstanceId: request.providerInstanceId, fetchedAt: ctx.now(), items, errors: [], complete }
|
|
144
|
+
},
|
|
145
|
+
async search(request, ctx) {
|
|
146
|
+
// 不限市场时扩大候选量: 单次响应要覆盖调用方负责的多个市场
|
|
147
|
+
const count = request.market ? Math.min(20, request.limit || 10) : Math.min(50, (request.limit || 10) * 3)
|
|
148
|
+
const url = `${endpointOf(ctx, 'search')}?input=${encodeURIComponent(request.q)}&type=14&token=D43BF722C8E33BDC906FB84D85E326E8&count=${count}`
|
|
149
|
+
const body = await getJson(ctx, url)
|
|
150
|
+
const rows = body?.QuotationCodeTable?.Data
|
|
151
|
+
if (!Array.isArray(rows)) return { providerInstanceId: request.providerInstanceId, fetchedAt: ctx.now(), items: [], errors: [] }
|
|
152
|
+
const items = []
|
|
153
|
+
for (const row of rows) {
|
|
154
|
+
const exchange = SEARCH_EXCHANGE[row.MktNum]
|
|
155
|
+
if (!exchange || (request.market && exchange !== request.market)) continue
|
|
156
|
+
// 美股候选写 usSymbols(代码→MktNum/JYS), kline 美股动态归属与腾讯 suffix 唯一数据来源
|
|
157
|
+
if (isUSExchange(exchange) && ctx.usSymbols && US_MKTNUMS.includes(String(row.MktNum))) ctx.usSymbols.set(`US:${row.Code}`, { mktNum: row.MktNum, jys: row.JYS || null, at: ctx.now() })
|
|
158
|
+
const meta = MARKET_META[exchange]
|
|
159
|
+
if (!meta || !meta.codePattern.test(row.Code)) continue
|
|
160
|
+
items.push(makeSecurity(exchange, row.Code, row.Name, { cnspell: typeof row.PinYin === 'string' ? row.PinYin.toLowerCase() : '', catalogSource: request.providerInstanceId }))
|
|
161
|
+
}
|
|
162
|
+
return { providerInstanceId: request.providerInstanceId, fetchedAt: ctx.now(), items, errors: [] }
|
|
163
|
+
},
|
|
164
|
+
// 与 quotes 同源的 secid;猜错时调用方走 suggest 重试路径(搜索结果是市场号的权威来源)。
|
|
165
|
+
secidOf(request, ctx) {
|
|
166
|
+
return secidFor({ exchange: request.market, code: request.code }, ctx)
|
|
167
|
+
},
|
|
168
|
+
// 美股默认前缀猜错时经搜索联想解析真实 MktNum, 重试一次并缓存映射; K线与分时共用同一条兜底
|
|
169
|
+
async withUsFallback(request, ctx, buildUrl, hasData, label) {
|
|
170
|
+
let body = await getJson(ctx, buildUrl(this.secidOf(request, ctx)))
|
|
171
|
+
if (hasData(body)) return body
|
|
172
|
+
if (!isUSExchange(request.market)) throw new StockError('NO_DATA', `来源未返回此证券${label}`, 422)
|
|
173
|
+
const resolved = await this.resolveUsSecid(request, ctx)
|
|
174
|
+
if (!resolved) throw new StockError('NO_DATA', `来源未返回此证券${label}`, 422)
|
|
175
|
+
ctx.usSymbols?.set(`US:${request.code}`, resolved)
|
|
176
|
+
body = await getJson(ctx, buildUrl(`${resolved.mktNum}.${request.code}`))
|
|
177
|
+
if (!hasData(body)) throw new StockError('NO_DATA', `来源未返回此证券${label}`, 422)
|
|
178
|
+
return body
|
|
179
|
+
},
|
|
180
|
+
async kline(request, ctx) {
|
|
181
|
+
const klt = KLT[request.period]
|
|
182
|
+
if (!klt) throw new StockError('INVALID_PARAM', '东财 K 线只支持日/周/月,分时请走分时能力', 400)
|
|
183
|
+
const body = await this.withUsFallback(request, ctx, secid => `${endpointOf(ctx, 'kline')}?secid=${secid}&fields1=f1,f2,f3&fields2=${KLINE_FIELDS}&klt=${klt}&fqt=1&end=20500101&lmt=${request.limit}`, body => !!body?.data?.klines, 'K线')
|
|
184
|
+
return buildKlineResponse(request, body.data, ctx)
|
|
185
|
+
},
|
|
186
|
+
async resolveUsSecid(request, ctx) {
|
|
187
|
+
const url = `${endpointOf(ctx, 'search')}?input=${encodeURIComponent(request.code)}&type=14&token=D43BF722C8E33BDC906FB84D85E326E8&count=5`
|
|
188
|
+
const body = await getJson(ctx, url)
|
|
189
|
+
const row = body?.QuotationCodeTable?.Data?.find(r => r.Code === request.code && /^10[567]$/.test(r.MktNum || ''))
|
|
190
|
+
return row ? { mktNum: row.MktNum, jys: row.JYS || null, at: ctx.now() } : null
|
|
191
|
+
},
|
|
192
|
+
async trend(request, ctx) {
|
|
193
|
+
const body = await this.withUsFallback(request, ctx, secid => `${endpointOf(ctx, 'trend')}?secid=${secid}&fields1=f1,f2,f3,f4,f5,f6,f7,f8&fields2=${TREND_FIELDS}&iscr=0&ndays=1`, body => !!body?.data?.trends, '分时')
|
|
194
|
+
// 东财分时的时间戳一律按北京时间给出(八市场实测: 日股 08:00 对应东京 09:00 开盘, 英股 16:00 对应伦敦 08:00),
|
|
195
|
+
// 不是市场本地时间; 之前按市场时区解释, 日韩英的横轴整体偏了几小时。
|
|
196
|
+
const candles = body.data.trends.map(row => {
|
|
197
|
+
const cols = row.split(',')
|
|
198
|
+
if (cols.length < 8) throw new StockError('BAD_PAYLOAD', '分时列数不足', 502)
|
|
199
|
+
const [, time] = cols[0].split(' ')
|
|
200
|
+
const num = v => { const n = parseFloat(v); return Number.isFinite(n) ? n : null }
|
|
201
|
+
// at 是来源时刻的 Unix 秒:只留 HH:MM 会让客户端按索引自造时间轴,横轴整体平移
|
|
202
|
+
// f56 与日K 同为手: 沪深要按 100 股换算, 否则分时图例的量和日K/详情差两个数量级
|
|
203
|
+
return { time, at: zonedUnixSeconds(cols[0], SOURCE_ZONE), open: num(cols[1]), close: num(cols[2]), high: num(cols[3]), low: num(cols[4]), volume: sharesFromLots(num(cols[5]), request.market), amount: num(cols[6]), avg: num(cols[7]) }
|
|
204
|
+
})
|
|
205
|
+
return { providerInstanceId: request.providerInstanceId, fetchedAt: ctx.now(), complete: true, preClose: num_or_null(body.data.preClose), candles }
|
|
206
|
+
},
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function num_or_null(v) { const n = parseFloat(v); return Number.isFinite(n) ? n : null }
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
// klines CSV → 合同响应: 逐行解析/升序/重复拒绝; complete 由 dktotal 与请求量判定(源截断标志)。
|
|
213
|
+
function buildKlineResponse(request, data, ctx) {
|
|
214
|
+
const candles = []
|
|
215
|
+
let prevTime = ''
|
|
216
|
+
for (const row of data.klines) {
|
|
217
|
+
const cols = row.split(',')
|
|
218
|
+
if (cols.length < 7) throw new StockError('BAD_PAYLOAD', 'K线列数不足', 502)
|
|
219
|
+
if (cols[0] <= prevTime) throw new StockError('BAD_PAYLOAD', 'K线时间戳重复或乱序', 502)
|
|
220
|
+
prevTime = cols[0]
|
|
221
|
+
const num = v => { const n = parseFloat(v); return Number.isFinite(n) ? n : null }
|
|
222
|
+
candles.push({ time: cols[0], open: num(cols[1]), close: num(cols[2]), high: num(cols[3]), low: num(cols[4]), volume: sharesFromLots(num(cols[5]), request.market), amount: num(cols[6]) })
|
|
223
|
+
}
|
|
224
|
+
const total = Number(data.dktotal)
|
|
225
|
+
return { providerInstanceId: request.providerInstanceId, fetchedAt: ctx.now(), complete: Number.isFinite(total) ? request.limit <= total : true, preClose: num_or_null(data.preKPrice), candles }
|
|
226
|
+
}
|