@v587d/capital-generation 2.25.0 → 2.27.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 +69 -0
- package/README.md +8 -6
- package/automation/brief.js +21 -24
- package/automation/builtins.js +55 -54
- package/automation/client.js +11 -3
- package/automation/client.src.cjs +70 -8
- package/automation/package.json +1 -1
- package/chart-ui/package.json +1 -1
- package/config/package.json +1 -1
- package/lib/sources/tencent-http.d.ts +20 -0
- package/lib/sources/tencent-http.js +37 -58
- package/lib/sources/tencent-public-core.d.ts +31 -0
- package/lib/sources/tencent-public-core.js +63 -0
- package/lib/sources/trend-series.d.ts +53 -0
- package/lib/sources/trend-series.js +382 -0
- package/lib/web-retriever/local-fetch.d.ts +1 -1
- package/lib/web-retriever/local-fetch.js +1 -1
- package/lib/web-retriever/wind-client.d.ts +1 -1
- package/lib/web-retriever/wind-client.js +1 -1
- package/package.json +2 -1
- package/watchlist/client.js +124 -3
- package/watchlist/client.src.cjs +1229 -1
- package/watchlist/index.js +146 -0
- package/watchlist/package.json +1 -1
|
@@ -24,6 +24,7 @@ const {
|
|
|
24
24
|
IconRefreshOutlineRegular,
|
|
25
25
|
IconRightUpOutlineRegular,
|
|
26
26
|
IconSearchOutlineRegular,
|
|
27
|
+
IconSlidersTwoOutlineRegular,
|
|
27
28
|
IconSparkleRegular,
|
|
28
29
|
IconTrashOutlineRegular,
|
|
29
30
|
IconWarningOutlineRegular,
|
|
@@ -845,6 +846,14 @@ function installStyles() {
|
|
|
845
846
|
.capital-automation-select:disabled { opacity: .5; cursor: not-allowed; }
|
|
846
847
|
.capital-automation-selectvalue { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
847
848
|
.capital-automation-select > svg { flex: none; color: var(--ca-muted); }
|
|
849
|
+
.capital-automation-select.capital-automation-taskfilter { width: auto; min-width: 112px; max-width: 170px; height: 30px; padding: 0 9px 0 11px; border: .5px solid var(--ca-line-soft); border-radius: 999px; background: var(--ca-card); font-size: 12px; }
|
|
850
|
+
.capital-automation-select.capital-automation-taskfilter:hover:not(:disabled) { border-color: var(--ca-line); background: var(--ca-card); }
|
|
851
|
+
.capital-automation-taskfiltervalue { display: inline-flex; align-items: center; gap: 6px; min-width: 0; }
|
|
852
|
+
.capital-automation-taskfiltervalue > svg { flex: none; color: var(--ca-muted); }
|
|
853
|
+
.capital-automation-taskfilter .capital-automation-filtercount { background: color-mix(in srgb, var(--ca-business) 12%, transparent); color: var(--ca-business-text); }
|
|
854
|
+
.capital-automation-taskfiltermenu { min-width: 176px; }
|
|
855
|
+
.capital-automation-taskfilteroption { display: flex; align-items: center; justify-content: flex-start; gap: 7px; min-width: 0; }
|
|
856
|
+
.capital-automation-taskfilteroptionlabel { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
848
857
|
.capital-automation-formactions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px; padding-top: 14px; border-top: .5px solid var(--ca-line); }
|
|
849
858
|
.capital-automation-formactions[data-sticky="true"] { position: sticky; bottom: -1px; z-index: 2; margin-top: 2px; padding: 12px 0 4px; background: var(--dsw-alias-bg-base); }
|
|
850
859
|
.capital-automation-formstatus { margin-right: auto; color: var(--ca-caption); font-size: 11.5px; line-height: 18px; }
|
|
@@ -2331,6 +2340,56 @@ function FilterButton(props) {
|
|
|
2331
2340
|
count > 0 ? h('span', { className: 'capital-automation-filtercount' }, String(count)) : null)
|
|
2332
2341
|
}
|
|
2333
2342
|
|
|
2343
|
+
function TaskFilterOption(props) {
|
|
2344
|
+
const { label, count } = props
|
|
2345
|
+
return h('span', { className: 'capital-automation-taskfilteroption' },
|
|
2346
|
+
h('span', { className: 'capital-automation-taskfilteroptionlabel' }, label),
|
|
2347
|
+
count > 0 ? h('span', { className: 'capital-automation-filtercount' }, String(count)) : null)
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
// items[].selectedId only covers `items`, so counted rows mark `selected` themselves.
|
|
2351
|
+
function TaskFilterMenu(props) {
|
|
2352
|
+
const { value, options, counts, label, onChange } = props
|
|
2353
|
+
const [open, setOpen] = useState(false)
|
|
2354
|
+
const triggerRef = useRef(null)
|
|
2355
|
+
const current = options.find((option) => option.value === value) ?? options[0]
|
|
2356
|
+
const currentCount = counts[value] ?? 0
|
|
2357
|
+
const closeMenu = () => setOpen(false)
|
|
2358
|
+
const selectValue = (id) => {
|
|
2359
|
+
setOpen(false)
|
|
2360
|
+
triggerRef.current?.focus?.({ preventScroll: true })
|
|
2361
|
+
if (id !== value) onChange(id)
|
|
2362
|
+
}
|
|
2363
|
+
return h(Menu, {
|
|
2364
|
+
open,
|
|
2365
|
+
anchor: h('button', {
|
|
2366
|
+
ref: triggerRef,
|
|
2367
|
+
type: 'button',
|
|
2368
|
+
className: 'capital-automation-select capital-automation-taskfilter',
|
|
2369
|
+
'aria-label': currentCount > 0 ? `${label}:${current.label},${currentCount} 个` : `${label}:${current.label}`,
|
|
2370
|
+
'aria-haspopup': 'menu',
|
|
2371
|
+
'aria-expanded': open ? 'true' : 'false',
|
|
2372
|
+
title: `${label}:${current.label}`,
|
|
2373
|
+
onClick: () => setOpen((currentOpen) => !currentOpen),
|
|
2374
|
+
},
|
|
2375
|
+
h('span', { className: 'capital-automation-taskfiltervalue' },
|
|
2376
|
+
h(IconSlidersTwoOutlineRegular, { size: 13 }),
|
|
2377
|
+
h('span', { className: 'capital-automation-selectvalue' }, current.label),
|
|
2378
|
+
currentCount > 0 ? h('span', { className: 'capital-automation-filtercount' }, String(currentCount)) : null),
|
|
2379
|
+
h(IconChevronDownOutlineRegular, { size: 14 })),
|
|
2380
|
+
items: [],
|
|
2381
|
+
onClose: closeMenu,
|
|
2382
|
+
align: 'end',
|
|
2383
|
+
portal: true,
|
|
2384
|
+
listClassName: 'capital-automation-taskfiltermenu',
|
|
2385
|
+
children: options.map((option) => h(MenuItemButton, {
|
|
2386
|
+
key: option.value,
|
|
2387
|
+
selected: option.value === value,
|
|
2388
|
+
onSelect: () => selectValue(option.value),
|
|
2389
|
+
}, h(TaskFilterOption, { label: option.label, count: counts[option.value] }))),
|
|
2390
|
+
})
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2334
2393
|
function TaskTags(props) {
|
|
2335
2394
|
const { task } = props
|
|
2336
2395
|
const tags = [[categoryLabel(task.category), toneOf(task)]]
|
|
@@ -2674,20 +2733,23 @@ function TaskDashboard(props) {
|
|
|
2674
2733
|
monitor: roster.filter((task) => task.category === 'monitor').length,
|
|
2675
2734
|
}
|
|
2676
2735
|
const filters = ['all', 'custom', 'news', 'market', 'review', 'research', 'portfolio', 'monitor']
|
|
2736
|
+
const filterOptions = filters.map((value) => ({
|
|
2737
|
+
value,
|
|
2738
|
+
label: value === 'all' ? zh.filterAll : value === 'custom' ? zh.filterCustom : categoryLabel(value),
|
|
2739
|
+
}))
|
|
2677
2740
|
return h('div', null,
|
|
2678
2741
|
h('section', { className: 'capital-automation-section', style: { marginTop: 0 } },
|
|
2679
2742
|
h('div', { className: 'capital-automation-sectionhead' },
|
|
2680
2743
|
h('h2', null, zh.taskSection),
|
|
2681
2744
|
h('span', null, zh.taskSectionHint),
|
|
2682
2745
|
h('div', { className: 'capital-automation-sectiontools' },
|
|
2683
|
-
h(
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
}))),
|
|
2746
|
+
h(TaskFilterMenu, {
|
|
2747
|
+
value: filter,
|
|
2748
|
+
options: filterOptions,
|
|
2749
|
+
counts: filterCounts,
|
|
2750
|
+
label: zh.filterLabel,
|
|
2751
|
+
onChange: setFilter,
|
|
2752
|
+
}),
|
|
2691
2753
|
h('label', { className: 'capital-automation-search' },
|
|
2692
2754
|
h(IconSearchOutlineRegular, { size: 13 }),
|
|
2693
2755
|
h('input', {
|
package/automation/package.json
CHANGED
package/chart-ui/package.json
CHANGED
package/config/package.json
CHANGED
|
@@ -1,2 +1,22 @@
|
|
|
1
1
|
import type { DataSource } from '../data-collector/hub.js';
|
|
2
|
+
/**
|
|
3
|
+
* One daily series for a UI pane: always day/qfq, one request per symbol, no paging.
|
|
4
|
+
* It runs the same executors and row parsers as `get_kline` (a second parser would drift), and it is
|
|
5
|
+
* the only place that knows the desk shows 万股 for 科创板 while the main board reports 手.
|
|
6
|
+
*/
|
|
7
|
+
export declare function fetchDailyBars(input: {
|
|
8
|
+
thscode: string;
|
|
9
|
+
}, signal: AbortSignal, count?: number): Promise<{
|
|
10
|
+
thscode: string;
|
|
11
|
+
adjust: 'qfq';
|
|
12
|
+
bars: Array<{
|
|
13
|
+
date: string;
|
|
14
|
+
open: number;
|
|
15
|
+
high: number;
|
|
16
|
+
low: number;
|
|
17
|
+
close: number;
|
|
18
|
+
volume: number;
|
|
19
|
+
}>;
|
|
20
|
+
volume_unit: 'lot' | 'share';
|
|
21
|
+
}>;
|
|
2
22
|
export declare function createTencentSources(): DataSource[];
|
|
@@ -3,20 +3,13 @@ import { getDataTimeContract } from '../time/tools.js';
|
|
|
3
3
|
import { isLikelyIndex, normalizeSecurityCodes, parseSecurityCode, requireTencentSecurity } from './security-code.js';
|
|
4
4
|
// Shared core holds HK/US snapshots, smartbox and the one throttle chain; this file only defines
|
|
5
5
|
// capabilities and K-line/tick orchestration. Two throttles = no throttle.
|
|
6
|
-
import { QUOTE_URL, aShareQuoteTime, fetchOffshoreIndexQuotes, fetchOffshoreQuotes, isRecord, normalizeHkCode, normalizeOffshoreIndexCode, normalizeUsCode, numberOrNull, offshoreQuoteTime, requiredNumber, resolveUsKlineSymbol, sourceError, US_QUOTE_FIELDS, tencentGetText as getText, } from './tencent-public-core.js';
|
|
6
|
+
import { QUOTE_URL, aShareQuoteTime, callIfzqHosts, fetchOffshoreIndexQuotes, fetchOffshoreQuotes, isRecord, normalizeHkCode, normalizeOffshoreIndexCode, normalizeUsCode, numberOrNull, offshoreQuoteTime, requiredNumber, resolveUsKlineSymbol, sourceError, US_QUOTE_FIELDS, tencentGetText as getText, } from './tencent-public-core.js';
|
|
7
7
|
const TICK_URL = 'https://stock.gtimg.cn/data/index.php';
|
|
8
|
-
const KLINE_HOSTS = [
|
|
9
|
-
'https://web.ifzq.gtimg.cn',
|
|
10
|
-
'https://proxy.finance.qq.com/ifzqgtimg',
|
|
11
|
-
'https://ifzq.gtimg.cn',
|
|
12
|
-
];
|
|
13
|
-
const HOST_COOLDOWN_MS = 120_000;
|
|
14
8
|
const KLINE_MINUTES = new Set(['m1', 'm5', 'm15', 'm30', 'm60']);
|
|
15
9
|
const KLINE_PERIODS = new Set(['day', 'week', 'month', 'm1', 'm5', 'm15', 'm30', 'm60']);
|
|
16
10
|
const KLINE_SPAN_DAYS = { day: 700, week: 3650, month: 18250 };
|
|
17
11
|
const MAX_TICK_PAGES = 300;
|
|
18
12
|
const TICK_SESSION_END = '15:00:59';
|
|
19
|
-
const hostDownUntil = new Map();
|
|
20
13
|
const quoteOutput = {
|
|
21
14
|
type: 'array',
|
|
22
15
|
items: {
|
|
@@ -115,20 +108,6 @@ function integer(value, name, min, max) {
|
|
|
115
108
|
throw new Error(`parameter ${name} must be an integer from ${min} to ${max}`);
|
|
116
109
|
return value;
|
|
117
110
|
}
|
|
118
|
-
function parseJson(text, context) {
|
|
119
|
-
if (!text.trim())
|
|
120
|
-
throw sourceError(`Tencent ${context} returned an empty response`, 'tencent_empty_response');
|
|
121
|
-
let parsed;
|
|
122
|
-
try {
|
|
123
|
-
parsed = JSON.parse(text);
|
|
124
|
-
}
|
|
125
|
-
catch (error) {
|
|
126
|
-
throw sourceError(`Tencent ${context} returned invalid JSON: ${error instanceof Error ? error.message : String(error)}`, 'tencent_invalid_response');
|
|
127
|
-
}
|
|
128
|
-
if (!isRecord(parsed))
|
|
129
|
-
throw sourceError(`Tencent ${context} returned ${Array.isArray(parsed) ? 'an array' : typeof parsed}, expected an object`, 'tencent_invalid_response');
|
|
130
|
-
return parsed;
|
|
131
|
-
}
|
|
132
111
|
function parseCodeParams(params, name = 'code') {
|
|
133
112
|
const code = requireTencentSecurity(parseSecurityCode(params[name], name), name);
|
|
134
113
|
return { code, canonical: code.canonical };
|
|
@@ -246,41 +225,8 @@ async function executeQuote(params, signal) {
|
|
|
246
225
|
}
|
|
247
226
|
return { data: rows, schema: quoteOutput };
|
|
248
227
|
}
|
|
249
|
-
function defaultParamRejection(payload) {
|
|
250
|
-
return payload.msg === 'param error';
|
|
251
|
-
}
|
|
252
|
-
async function callKlineHosts(transport, signal) {
|
|
253
|
-
const errors = [];
|
|
254
|
-
const rejected = transport.isParamRejection ?? defaultParamRejection;
|
|
255
|
-
for (const host of KLINE_HOSTS) {
|
|
256
|
-
if ((hostDownUntil.get(host) ?? 0) > Date.now())
|
|
257
|
-
continue;
|
|
258
|
-
try {
|
|
259
|
-
const url = `${host}${transport.path}?param=${encodeURIComponent(transport.param)}`;
|
|
260
|
-
const payload = parseJson(await getText(url, signal), `${transport.label} ${host}`);
|
|
261
|
-
if (rejected(payload)) {
|
|
262
|
-
throw sourceError(`Tencent rejected the ${transport.label} parameter (${String(payload.msg ?? '') || `code ${String(payload.code)}`}): ${transport.param}`, 'tencent_invalid_param');
|
|
263
|
-
}
|
|
264
|
-
if (isRecord(payload.data) && !transport.isEmpty(payload.data))
|
|
265
|
-
return payload.data;
|
|
266
|
-
errors.push(`${host}: empty data`);
|
|
267
|
-
hostDownUntil.set(host, Date.now() + HOST_COOLDOWN_MS);
|
|
268
|
-
}
|
|
269
|
-
catch (error) {
|
|
270
|
-
// Cancellation and param failures rethrow before blacklist/rotation; see
|
|
271
|
-
// `docs/dev/web-retriever.md` §4.2.
|
|
272
|
-
if (error instanceof Error && error.name === 'AbortError')
|
|
273
|
-
throw error;
|
|
274
|
-
if (error.code === 'tencent_invalid_param')
|
|
275
|
-
throw error;
|
|
276
|
-
errors.push(`${host}: ${error instanceof Error ? error.message : String(error)}`);
|
|
277
|
-
hostDownUntil.set(host, Date.now() + HOST_COOLDOWN_MS);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
throw sourceError(`Tencent ${transport.label} endpoints unavailable: ${errors.join('; ')}`, 'tencent_kline_unavailable');
|
|
281
|
-
}
|
|
282
228
|
async function callKline(path, param, signal) {
|
|
283
|
-
return { data: await
|
|
229
|
+
return { data: await callIfzqHosts({ path, param, label: 'K-line', isEmpty: (data) => Object.keys(data).length === 0 }, signal) };
|
|
284
230
|
}
|
|
285
231
|
function addDays(date, days) {
|
|
286
232
|
const value = new Date(`${date}T00:00:00Z`);
|
|
@@ -498,7 +444,7 @@ const offshoreKlinePaths = {
|
|
|
498
444
|
us: { qfq: '/appstock/app/usfqkline/get', none: '/appstock/app/kline/kline' },
|
|
499
445
|
};
|
|
500
446
|
async function callOffshoreKline(path, param, symbol, signal) {
|
|
501
|
-
const data = await
|
|
447
|
+
const data = await callIfzqHosts({
|
|
502
448
|
path,
|
|
503
449
|
param,
|
|
504
450
|
label: `${symbol} K-line`,
|
|
@@ -761,7 +707,7 @@ function parseUsPrepostRow(data, symbol, requested) {
|
|
|
761
707
|
}
|
|
762
708
|
async function fetchUsPrepostRow(ticker, signal) {
|
|
763
709
|
const symbol = await resolveUsKlineSymbol(ticker, signal);
|
|
764
|
-
const data = await
|
|
710
|
+
const data = await callIfzqHosts({
|
|
765
711
|
path: '/appstock/app/usfqkline/get',
|
|
766
712
|
param: `${symbol},day,,,${US_PREPOST_BARS},qfq`,
|
|
767
713
|
label: `${symbol} pre/post`,
|
|
@@ -963,6 +909,39 @@ function createSource(options) {
|
|
|
963
909
|
execute: async (request, signal) => options.execute(normalize(request.params), signal),
|
|
964
910
|
};
|
|
965
911
|
}
|
|
912
|
+
/**
|
|
913
|
+
* One daily series for a UI pane: always day/qfq, one request per symbol, no paging.
|
|
914
|
+
* It runs the same executors and row parsers as `get_kline` (a second parser would drift), and it is
|
|
915
|
+
* the only place that knows the desk shows 万股 for 科创板 while the main board reports 手.
|
|
916
|
+
*/
|
|
917
|
+
export async function fetchDailyBars(input, signal, count = 120) {
|
|
918
|
+
const thscode = typeof input.thscode === 'string' ? input.thscode.trim() : '';
|
|
919
|
+
const upper = thscode.toUpperCase();
|
|
920
|
+
const bars = (rows) => rows.map((row) => ({
|
|
921
|
+
date: String(row.date),
|
|
922
|
+
open: Number(row.open),
|
|
923
|
+
high: Number(row.high),
|
|
924
|
+
low: Number(row.low),
|
|
925
|
+
close: Number(row.close),
|
|
926
|
+
volume: Number(row.volume),
|
|
927
|
+
}));
|
|
928
|
+
if (upper.endsWith('.HK')) {
|
|
929
|
+
const params = normalizeOffshoreKline({ code: thscode, period: 'day', adjust: 'qfq', count }, 'hk');
|
|
930
|
+
const { data } = await executeOffshoreKline(params, signal, 'hk');
|
|
931
|
+
return { thscode, adjust: 'qfq', bars: bars(data).slice(-count), volume_unit: 'share' };
|
|
932
|
+
}
|
|
933
|
+
if (/^[A-Z][A-Z0-9.]{0,9}\.[A-Z]{1,3}$/u.test(upper) && !/\.(SH|SZ|BJ)$/u.test(upper)) {
|
|
934
|
+
const params = normalizeOffshoreKline({ code: thscode, period: 'day', adjust: 'qfq', count }, 'us');
|
|
935
|
+
const { data } = await executeOffshoreKline(params, signal, 'us');
|
|
936
|
+
return { thscode, adjust: 'qfq', bars: bars(data).slice(-count), volume_unit: 'share' };
|
|
937
|
+
}
|
|
938
|
+
const { data } = await executeKline({ code: thscode, period: 'day', adjust: 'qfq', count }, signal);
|
|
939
|
+
// 科创板 reports daily volume in shares (688599: 27-29M) while the main board reports 手 (600519: 35,116)
|
|
940
|
+
// — the same split as the minute endpoint, and daily rows carry no turnover column to cross-check.
|
|
941
|
+
const digits = thscode.slice(0, 6);
|
|
942
|
+
// The endpoint answers count+1 rows; the pane asks for a window, so hand it exactly that.
|
|
943
|
+
return { thscode, adjust: 'qfq', bars: bars(data).slice(-count), volume_unit: /^68[89]/u.test(digits) ? 'share' : 'lot' };
|
|
944
|
+
}
|
|
966
945
|
export function createTencentSources() {
|
|
967
946
|
return [
|
|
968
947
|
createSource({
|
|
@@ -2,6 +2,14 @@ export declare const QUOTE_URL = "https://qt.gtimg.cn/q=";
|
|
|
2
2
|
export declare const SMARTBOX_URL = "https://smartbox.gtimg.cn/s3/";
|
|
3
3
|
export declare const TENCENT_MIN_INTERVAL_MS = 120;
|
|
4
4
|
export declare const tencentThrottle: <T>(task: () => Promise<T>) => Promise<T>;
|
|
5
|
+
/**
|
|
6
|
+
* ifzq JSON hosts (K-line + intraday trend). One rotation and one cooldown table for both
|
|
7
|
+
* callers: a host that failed for K-lines must not be retried by the trend path 200ms later.
|
|
8
|
+
*/
|
|
9
|
+
export declare const TENCENT_IFZQ_HOSTS: string[];
|
|
10
|
+
export declare const TENCENT_HOST_COOLDOWN_MS = 120000;
|
|
11
|
+
/** Shared by every ifzq caller; keyed by host URL. */
|
|
12
|
+
export declare const tencentHostDownUntil: Map<string, number>;
|
|
5
13
|
export type JsonRecord = Record<string, unknown>;
|
|
6
14
|
export declare function isRecord(value: unknown): value is JsonRecord;
|
|
7
15
|
export declare function numberOrNull(value: unknown): number | null;
|
|
@@ -48,3 +56,26 @@ export interface SmartboxCandidate {
|
|
|
48
56
|
export declare function parseSmartbox(text: string): SmartboxCandidate[];
|
|
49
57
|
export declare const SMARTBOX_MARKETS: string[];
|
|
50
58
|
export declare function fetchSmartbox(query: string, market: string, signal: AbortSignal): Promise<SmartboxCandidate[]>;
|
|
59
|
+
/** ifzq JSON body: empty / non-JSON / non-object must fail loudly with the host named. */
|
|
60
|
+
export declare function parseIfzqPayload(text: string, context: string): JsonRecord;
|
|
61
|
+
/** One ifzq request; `param=` is the K-line shape, `query` is for endpoints with their own names. */
|
|
62
|
+
export interface IfzqRequest {
|
|
63
|
+
path: string;
|
|
64
|
+
param?: string;
|
|
65
|
+
query?: string;
|
|
66
|
+
label: string;
|
|
67
|
+
/** Error code when every host is exhausted; K-line keeps its historical code. */
|
|
68
|
+
code?: string;
|
|
69
|
+
/** "Host gave nothing": whole data object for A-shares, symbol series for HK/US. */
|
|
70
|
+
isEmpty: (data: JsonRecord) => boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Stop on first param rejection: another host will not fix it, and blacklisting all hosts for
|
|
73
|
+
* 120s spreads one bad call process-wide. HK/US returns `{"code":1,"msg":"bad params"}`.
|
|
74
|
+
*/
|
|
75
|
+
isParamRejection?: (payload: JsonRecord) => boolean;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Rotate the shared ifzq hosts and return the first non-empty `data` object. Cancellation and
|
|
79
|
+
* param failures rethrow before blacklist/rotation; see `docs/dev/web-retriever.md` §4.2.
|
|
80
|
+
*/
|
|
81
|
+
export declare function callIfzqHosts(request: IfzqRequest, signal: AbortSignal): Promise<JsonRecord>;
|
|
@@ -7,6 +7,18 @@ export const QUOTE_URL = 'https://qt.gtimg.cn/q=';
|
|
|
7
7
|
export const SMARTBOX_URL = 'https://smartbox.gtimg.cn/s3/';
|
|
8
8
|
export const TENCENT_MIN_INTERVAL_MS = 120;
|
|
9
9
|
export const tencentThrottle = createRequestThrottle(TENCENT_MIN_INTERVAL_MS);
|
|
10
|
+
/**
|
|
11
|
+
* ifzq JSON hosts (K-line + intraday trend). One rotation and one cooldown table for both
|
|
12
|
+
* callers: a host that failed for K-lines must not be retried by the trend path 200ms later.
|
|
13
|
+
*/
|
|
14
|
+
export const TENCENT_IFZQ_HOSTS = [
|
|
15
|
+
'https://web.ifzq.gtimg.cn',
|
|
16
|
+
'https://proxy.finance.qq.com/ifzqgtimg',
|
|
17
|
+
'https://ifzq.gtimg.cn',
|
|
18
|
+
];
|
|
19
|
+
export const TENCENT_HOST_COOLDOWN_MS = 120_000;
|
|
20
|
+
/** Shared by every ifzq caller; keyed by host URL. */
|
|
21
|
+
export const tencentHostDownUntil = new Map();
|
|
10
22
|
export function isRecord(value) {
|
|
11
23
|
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
12
24
|
}
|
|
@@ -324,3 +336,54 @@ export async function fetchSmartbox(query, market, signal) {
|
|
|
324
336
|
const text = await tencentGetText(url, signal);
|
|
325
337
|
return parseSmartbox(text);
|
|
326
338
|
}
|
|
339
|
+
/** ifzq JSON body: empty / non-JSON / non-object must fail loudly with the host named. */
|
|
340
|
+
export function parseIfzqPayload(text, context) {
|
|
341
|
+
if (!text.trim())
|
|
342
|
+
throw sourceError(`Tencent ${context} returned an empty response`, 'tencent_empty_response');
|
|
343
|
+
let parsed;
|
|
344
|
+
try {
|
|
345
|
+
parsed = JSON.parse(text);
|
|
346
|
+
}
|
|
347
|
+
catch (error) {
|
|
348
|
+
throw sourceError(`Tencent ${context} returned invalid JSON: ${error instanceof Error ? error.message : String(error)}`, 'tencent_invalid_response');
|
|
349
|
+
}
|
|
350
|
+
if (!isRecord(parsed))
|
|
351
|
+
throw sourceError(`Tencent ${context} returned ${Array.isArray(parsed) ? 'an array' : typeof parsed}, expected an object`, 'tencent_invalid_response');
|
|
352
|
+
return parsed;
|
|
353
|
+
}
|
|
354
|
+
function ifzqParamRejected(payload) {
|
|
355
|
+
return payload.msg === 'param error';
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Rotate the shared ifzq hosts and return the first non-empty `data` object. Cancellation and
|
|
359
|
+
* param failures rethrow before blacklist/rotation; see `docs/dev/web-retriever.md` §4.2.
|
|
360
|
+
*/
|
|
361
|
+
export async function callIfzqHosts(request, signal) {
|
|
362
|
+
const errors = [];
|
|
363
|
+
const rejected = request.isParamRejection ?? ifzqParamRejected;
|
|
364
|
+
const link = request.query ?? `param=${encodeURIComponent(request.param ?? '')}`;
|
|
365
|
+
for (const host of TENCENT_IFZQ_HOSTS) {
|
|
366
|
+
if ((tencentHostDownUntil.get(host) ?? 0) > Date.now())
|
|
367
|
+
continue;
|
|
368
|
+
try {
|
|
369
|
+
const payload = parseIfzqPayload(await tencentGetText(`${host}${request.path}?${link}`, signal), `${request.label} ${host}`);
|
|
370
|
+
if (rejected(payload)) {
|
|
371
|
+
const detail = String(payload.msg ?? '') || `code ${String(payload.code)}`;
|
|
372
|
+
throw sourceError(`Tencent rejected the ${request.label} parameter (${detail})`, 'tencent_invalid_param');
|
|
373
|
+
}
|
|
374
|
+
if (isRecord(payload.data) && !request.isEmpty(payload.data))
|
|
375
|
+
return payload.data;
|
|
376
|
+
errors.push(`${host}: empty data`);
|
|
377
|
+
tencentHostDownUntil.set(host, Date.now() + TENCENT_HOST_COOLDOWN_MS);
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
if (error instanceof Error && error.name === 'AbortError')
|
|
381
|
+
throw error;
|
|
382
|
+
if (error.code === 'tencent_invalid_param')
|
|
383
|
+
throw error;
|
|
384
|
+
errors.push(`${host}: ${error instanceof Error ? error.message : String(error)}`);
|
|
385
|
+
tencentHostDownUntil.set(host, Date.now() + TENCENT_HOST_COOLDOWN_MS);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
throw sourceError(`Tencent ${request.label} endpoints unavailable: ${errors.join('; ')}`, request.code ?? 'tencent_kline_unavailable');
|
|
389
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type TrendMarket = 'a-share' | 'hk' | 'us';
|
|
2
|
+
export type TrendSource = 'tencent' | 'eastmoney';
|
|
3
|
+
export type TrendStatus = 'live' | 'closed' | 'previous';
|
|
4
|
+
export type TrendPoint = [time: string, price: number, avg: number | null, volume: number | null, amount: number | null];
|
|
5
|
+
export interface TrendTarget {
|
|
6
|
+
thscode: string;
|
|
7
|
+
source: TrendSource;
|
|
8
|
+
market: TrendMarket;
|
|
9
|
+
/** Tencent symbol (`sh600519`, `hk00700`), set on the Tencent branch. */
|
|
10
|
+
symbol?: string;
|
|
11
|
+
/** Eastmoney secid (`105.AAPL`), set on the US branch. */
|
|
12
|
+
secid?: string;
|
|
13
|
+
isIndex: boolean;
|
|
14
|
+
volumeUnit: 'lot' | 'share' | null;
|
|
15
|
+
currency: string | null;
|
|
16
|
+
}
|
|
17
|
+
export interface TrendSeries {
|
|
18
|
+
thscode: string;
|
|
19
|
+
source: TrendSource;
|
|
20
|
+
market: TrendMarket;
|
|
21
|
+
tz: string;
|
|
22
|
+
session_date: string | null;
|
|
23
|
+
session_status: TrendStatus;
|
|
24
|
+
/** Live but no point for >10 trading minutes: the desk says so instead of faking realtime. */
|
|
25
|
+
stale: boolean;
|
|
26
|
+
prev_close: number;
|
|
27
|
+
open: number | null;
|
|
28
|
+
high: number | null;
|
|
29
|
+
low: number | null;
|
|
30
|
+
last: number | null;
|
|
31
|
+
/** Day VWAP; null for indices (an index level has no traded price). */
|
|
32
|
+
avg: number | null;
|
|
33
|
+
volume: number | null;
|
|
34
|
+
amount: number | null;
|
|
35
|
+
volume_unit: 'lot' | 'share' | null;
|
|
36
|
+
currency: string | null;
|
|
37
|
+
points: TrendPoint[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Storage identity → upstream request. `undefined` means the chart cannot be drawn at all
|
|
41
|
+
* (US OTC pink and exotic US indices): the caller must not spend a request to find out.
|
|
42
|
+
*/
|
|
43
|
+
export declare function resolveTrendTarget(item: {
|
|
44
|
+
thscode?: unknown;
|
|
45
|
+
asset_type?: unknown;
|
|
46
|
+
}): TrendTarget | undefined;
|
|
47
|
+
/** Trading minutes elapsed up to a wall clock; lunch breaks never count as elapsed time. */
|
|
48
|
+
export declare function elapsedTradingMinutes(market: TrendMarket, minutes: number): number;
|
|
49
|
+
/** One entry point for both branches; `thscode` is filled by the caller, which owns identity. */
|
|
50
|
+
export declare function fetchTrendSeries(target: TrendTarget, options: {
|
|
51
|
+
signal: AbortSignal;
|
|
52
|
+
now?: number;
|
|
53
|
+
}): Promise<TrendSeries>;
|