@v587d/capital-generation 2.29.0 → 2.30.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 +38 -0
- package/README.md +3 -3
- package/THIRD_PARTY_NOTICES.md +16 -0
- package/automation/package.json +1 -1
- package/chart-ui/package.json +1 -1
- package/config/package.json +1 -1
- package/lib/collector-runtime.js +56 -1
- package/lib/data-collector/catalog-taxonomy.d.ts +1 -1
- package/lib/data-collector/catalog-taxonomy.js +33 -0
- package/lib/data-collector/tools.js +7 -7
- package/lib/python-runtime/errors.d.ts +1 -1
- package/lib/python-runtime/errors.js +2 -0
- package/lib/python-runtime/manager.d.ts +28 -1
- package/lib/python-runtime/manager.js +141 -5
- package/lib/python-runtime/probe.d.ts +24 -0
- package/lib/python-runtime/probe.js +66 -50
- package/lib/sources/csindex-http.d.ts +4 -0
- package/lib/sources/csindex-http.js +233 -0
- package/lib/sources/eastmoney-http.js +1504 -1
- package/lib/sources/fuyao-rest.js +168 -8
- package/lib/sources/official-http.d.ts +4 -0
- package/lib/sources/official-http.js +477 -0
- package/lib/sources/python-remote.d.ts +25 -0
- package/lib/sources/python-remote.js +461 -0
- package/lib/sources/sina-http.d.ts +2 -0
- package/lib/sources/sina-http.js +484 -0
- package/lib/sources/tencent-http.js +23 -8
- package/lib/sources/ths-http.d.ts +2 -0
- package/lib/sources/ths-http.js +514 -0
- package/lib/time/tools.js +19 -0
- package/lib/web-retriever/local-fetch.d.ts +1 -1
- package/lib/web-retriever/local-fetch.js +1 -1
- package/lib/web-retriever/sources.d.ts +7 -0
- package/lib/web-retriever/sources.js +46 -21
- package/lib/web-retriever/throttle.d.ts +8 -0
- package/lib/web-retriever/throttle.js +13 -0
- package/lib/web-retriever/tools.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 +1 -1
- package/preset/capital-generation/agent.patch.yml +7 -5
- package/preset/capital-generation/skills/capital-data-protocol/SKILL.md +16 -16
- package/python/scripts/baostock_stock_basic.py +129 -0
- package/python/scripts/baostock_valuation_history.py +141 -0
- package/python/scripts/pbc_social_financing.py +291 -0
- package/python/vendor/baostock/LICENSE +19 -0
- package/python/vendor/baostock/__init__.py +42 -0
- package/python/vendor/baostock/common/__init__.py +0 -0
- package/python/vendor/baostock/common/contants.py +189 -0
- package/python/vendor/baostock/common/context.py +4 -0
- package/python/vendor/baostock/corpreport/__init__.py +0 -0
- package/python/vendor/baostock/corpreport/corp_performance.py +202 -0
- package/python/vendor/baostock/data/__init__.py +0 -0
- package/python/vendor/baostock/data/messageheader.py +17 -0
- package/python/vendor/baostock/data/msg.py +21 -0
- package/python/vendor/baostock/data/resultset.py +200 -0
- package/python/vendor/baostock/demo/__init__.py +0 -0
- package/python/vendor/baostock/demo/demo_A_stock_5mk-data.py +34 -0
- package/python/vendor/baostock/demo/demo_A_stock_k_data.py +36 -0
- package/python/vendor/baostock/demo/demo_AllStock.py +27 -0
- package/python/vendor/baostock/demo/demo_Balance_data.py +22 -0
- package/python/vendor/baostock/demo/demo_CashFlow_data.py +22 -0
- package/python/vendor/baostock/demo/demo_Dividend_data.py +35 -0
- package/python/vendor/baostock/demo/demo_Dupont_data.py +25 -0
- package/python/vendor/baostock/demo/demo_Forecast_report.py +24 -0
- package/python/vendor/baostock/demo/demo_Growth_data.py +22 -0
- package/python/vendor/baostock/demo/demo_Index_k_data.py +39 -0
- package/python/vendor/baostock/demo/demo_Operation_data.py +22 -0
- package/python/vendor/baostock/demo/demo_PerformanceExpress_report.py +25 -0
- package/python/vendor/baostock/demo/demo_Profit_data.py +22 -0
- package/python/vendor/baostock/demo/demo_TradeDates.py +27 -0
- package/python/vendor/baostock/demo/demo_adjust_factor_data.py +23 -0
- package/python/vendor/baostock/demo/demo_assignDayData.py +23 -0
- package/python/vendor/baostock/demo/demo_daily_adjust_factor_data.py +28 -0
- package/python/vendor/baostock/demo/demo_daily_k_4_AStock.py +28 -0
- package/python/vendor/baostock/demo/demo_daily_k_4_ETF.py +28 -0
- package/python/vendor/baostock/demo/demo_depositRate_data.py +26 -0
- package/python/vendor/baostock/demo/demo_hs300_stocks.py +26 -0
- package/python/vendor/baostock/demo/demo_loanRate_data.py +26 -0
- package/python/vendor/baostock/demo/demo_moneySupplyDataMonth_data.py +26 -0
- package/python/vendor/baostock/demo/demo_moneySupplyDataYear_data.py +26 -0
- package/python/vendor/baostock/demo/demo_requiredReserveRatio_data.py +26 -0
- package/python/vendor/baostock/demo/demo_set_API_key.py +37 -0
- package/python/vendor/baostock/demo/demo_stockBasic_data.py +27 -0
- package/python/vendor/baostock/demo/demo_stock_Valuation_indicator_data.py +34 -0
- package/python/vendor/baostock/demo/demo_stock_industry.py +26 -0
- package/python/vendor/baostock/demo/demo_sz50stocks.py +26 -0
- package/python/vendor/baostock/demo/demo_zz500_stock.py +26 -0
- package/python/vendor/baostock/evaluation/__init__.py +0 -0
- package/python/vendor/baostock/evaluation/season_index.py +758 -0
- package/python/vendor/baostock/login/__init__.py +0 -0
- package/python/vendor/baostock/login/loginout.py +230 -0
- package/python/vendor/baostock/macroscopic/__init__.py +0 -0
- package/python/vendor/baostock/macroscopic/economic_data.py +723 -0
- package/python/vendor/baostock/metadata/__init__.py +0 -0
- package/python/vendor/baostock/metadata/stock_metadata.py +207 -0
- package/python/vendor/baostock/security/__init__.py +0 -0
- package/python/vendor/baostock/security/history.py +265 -0
- package/python/vendor/baostock/security/sectorinfo.py +1024 -0
- package/python/vendor/baostock/util/__init__.py +0 -0
- package/python/vendor/baostock/util/socketutil.py +97 -0
- package/python/vendor/baostock/util/stringutil.py +83 -0
- package/python/vendor/et_xmlfile/LICENSE +23 -0
- package/python/vendor/et_xmlfile/LICENSE.python +298 -0
- package/python/vendor/et_xmlfile/__init__.py +8 -0
- package/python/vendor/et_xmlfile/incremental_tree.py +917 -0
- package/python/vendor/et_xmlfile/xmlfile.py +158 -0
- package/python/vendor/openpyxl/LICENSE +23 -0
- package/python/vendor/openpyxl/__init__.py +19 -0
- package/python/vendor/openpyxl/_constants.py +13 -0
- package/python/vendor/openpyxl/cell/__init__.py +4 -0
- package/python/vendor/openpyxl/cell/_writer.py +136 -0
- package/python/vendor/openpyxl/cell/cell.py +332 -0
- package/python/vendor/openpyxl/cell/read_only.py +136 -0
- package/python/vendor/openpyxl/cell/rich_text.py +202 -0
- package/python/vendor/openpyxl/cell/text.py +184 -0
- package/python/vendor/openpyxl/chart/_3d.py +105 -0
- package/python/vendor/openpyxl/chart/__init__.py +19 -0
- package/python/vendor/openpyxl/chart/_chart.py +199 -0
- package/python/vendor/openpyxl/chart/area_chart.py +106 -0
- package/python/vendor/openpyxl/chart/axis.py +401 -0
- package/python/vendor/openpyxl/chart/bar_chart.py +144 -0
- package/python/vendor/openpyxl/chart/bubble_chart.py +67 -0
- package/python/vendor/openpyxl/chart/chartspace.py +195 -0
- package/python/vendor/openpyxl/chart/data_source.py +246 -0
- package/python/vendor/openpyxl/chart/descriptors.py +43 -0
- package/python/vendor/openpyxl/chart/error_bar.py +62 -0
- package/python/vendor/openpyxl/chart/label.py +127 -0
- package/python/vendor/openpyxl/chart/layout.py +74 -0
- package/python/vendor/openpyxl/chart/legend.py +75 -0
- package/python/vendor/openpyxl/chart/line_chart.py +129 -0
- package/python/vendor/openpyxl/chart/marker.py +90 -0
- package/python/vendor/openpyxl/chart/picture.py +35 -0
- package/python/vendor/openpyxl/chart/pie_chart.py +177 -0
- package/python/vendor/openpyxl/chart/pivot.py +65 -0
- package/python/vendor/openpyxl/chart/plotarea.py +162 -0
- package/python/vendor/openpyxl/chart/print_settings.py +57 -0
- package/python/vendor/openpyxl/chart/radar_chart.py +55 -0
- package/python/vendor/openpyxl/chart/reader.py +32 -0
- package/python/vendor/openpyxl/chart/reference.py +124 -0
- package/python/vendor/openpyxl/chart/scatter_chart.py +53 -0
- package/python/vendor/openpyxl/chart/series.py +197 -0
- package/python/vendor/openpyxl/chart/series_factory.py +41 -0
- package/python/vendor/openpyxl/chart/shapes.py +89 -0
- package/python/vendor/openpyxl/chart/stock_chart.py +54 -0
- package/python/vendor/openpyxl/chart/surface_chart.py +119 -0
- package/python/vendor/openpyxl/chart/text.py +78 -0
- package/python/vendor/openpyxl/chart/title.py +76 -0
- package/python/vendor/openpyxl/chart/trendline.py +98 -0
- package/python/vendor/openpyxl/chart/updown_bars.py +31 -0
- package/python/vendor/openpyxl/chartsheet/__init__.py +3 -0
- package/python/vendor/openpyxl/chartsheet/chartsheet.py +107 -0
- package/python/vendor/openpyxl/chartsheet/custom.py +61 -0
- package/python/vendor/openpyxl/chartsheet/properties.py +28 -0
- package/python/vendor/openpyxl/chartsheet/protection.py +41 -0
- package/python/vendor/openpyxl/chartsheet/publish.py +58 -0
- package/python/vendor/openpyxl/chartsheet/relation.py +97 -0
- package/python/vendor/openpyxl/chartsheet/views.py +51 -0
- package/python/vendor/openpyxl/comments/__init__.py +4 -0
- package/python/vendor/openpyxl/comments/author.py +21 -0
- package/python/vendor/openpyxl/comments/comment_sheet.py +211 -0
- package/python/vendor/openpyxl/comments/comments.py +62 -0
- package/python/vendor/openpyxl/comments/shape_writer.py +112 -0
- package/python/vendor/openpyxl/compat/__init__.py +54 -0
- package/python/vendor/openpyxl/compat/abc.py +8 -0
- package/python/vendor/openpyxl/compat/numbers.py +43 -0
- package/python/vendor/openpyxl/compat/product.py +17 -0
- package/python/vendor/openpyxl/compat/singleton.py +40 -0
- package/python/vendor/openpyxl/compat/strings.py +25 -0
- package/python/vendor/openpyxl/descriptors/__init__.py +58 -0
- package/python/vendor/openpyxl/descriptors/base.py +272 -0
- package/python/vendor/openpyxl/descriptors/container.py +41 -0
- package/python/vendor/openpyxl/descriptors/excel.py +112 -0
- package/python/vendor/openpyxl/descriptors/namespace.py +12 -0
- package/python/vendor/openpyxl/descriptors/nested.py +129 -0
- package/python/vendor/openpyxl/descriptors/sequence.py +136 -0
- package/python/vendor/openpyxl/descriptors/serialisable.py +240 -0
- package/python/vendor/openpyxl/descriptors/slots.py +18 -0
- package/python/vendor/openpyxl/drawing/__init__.py +4 -0
- package/python/vendor/openpyxl/drawing/colors.py +435 -0
- package/python/vendor/openpyxl/drawing/connector.py +144 -0
- package/python/vendor/openpyxl/drawing/drawing.py +92 -0
- package/python/vendor/openpyxl/drawing/effect.py +407 -0
- package/python/vendor/openpyxl/drawing/fill.py +425 -0
- package/python/vendor/openpyxl/drawing/geometry.py +584 -0
- package/python/vendor/openpyxl/drawing/graphic.py +177 -0
- package/python/vendor/openpyxl/drawing/image.py +65 -0
- package/python/vendor/openpyxl/drawing/line.py +144 -0
- package/python/vendor/openpyxl/drawing/picture.py +144 -0
- package/python/vendor/openpyxl/drawing/properties.py +174 -0
- package/python/vendor/openpyxl/drawing/relation.py +17 -0
- package/python/vendor/openpyxl/drawing/spreadsheet_drawing.py +382 -0
- package/python/vendor/openpyxl/drawing/text.py +717 -0
- package/python/vendor/openpyxl/drawing/xdr.py +33 -0
- package/python/vendor/openpyxl/formatting/__init__.py +3 -0
- package/python/vendor/openpyxl/formatting/formatting.py +114 -0
- package/python/vendor/openpyxl/formatting/rule.py +291 -0
- package/python/vendor/openpyxl/formula/__init__.py +3 -0
- package/python/vendor/openpyxl/formula/tokenizer.py +446 -0
- package/python/vendor/openpyxl/formula/translate.py +166 -0
- package/python/vendor/openpyxl/packaging/__init__.py +3 -0
- package/python/vendor/openpyxl/packaging/core.py +115 -0
- package/python/vendor/openpyxl/packaging/custom.py +289 -0
- package/python/vendor/openpyxl/packaging/extended.py +137 -0
- package/python/vendor/openpyxl/packaging/interface.py +56 -0
- package/python/vendor/openpyxl/packaging/manifest.py +194 -0
- package/python/vendor/openpyxl/packaging/relationship.py +158 -0
- package/python/vendor/openpyxl/packaging/workbook.py +185 -0
- package/python/vendor/openpyxl/pivot/__init__.py +1 -0
- package/python/vendor/openpyxl/pivot/cache.py +965 -0
- package/python/vendor/openpyxl/pivot/fields.py +326 -0
- package/python/vendor/openpyxl/pivot/record.py +111 -0
- package/python/vendor/openpyxl/pivot/table.py +1261 -0
- package/python/vendor/openpyxl/reader/__init__.py +1 -0
- package/python/vendor/openpyxl/reader/drawings.py +71 -0
- package/python/vendor/openpyxl/reader/excel.py +349 -0
- package/python/vendor/openpyxl/reader/strings.py +44 -0
- package/python/vendor/openpyxl/reader/workbook.py +133 -0
- package/python/vendor/openpyxl/styles/__init__.py +11 -0
- package/python/vendor/openpyxl/styles/alignment.py +62 -0
- package/python/vendor/openpyxl/styles/borders.py +103 -0
- package/python/vendor/openpyxl/styles/builtins.py +1397 -0
- package/python/vendor/openpyxl/styles/cell_style.py +206 -0
- package/python/vendor/openpyxl/styles/colors.py +172 -0
- package/python/vendor/openpyxl/styles/differential.py +95 -0
- package/python/vendor/openpyxl/styles/fills.py +224 -0
- package/python/vendor/openpyxl/styles/fonts.py +113 -0
- package/python/vendor/openpyxl/styles/named_styles.py +282 -0
- package/python/vendor/openpyxl/styles/numbers.py +200 -0
- package/python/vendor/openpyxl/styles/protection.py +17 -0
- package/python/vendor/openpyxl/styles/proxy.py +62 -0
- package/python/vendor/openpyxl/styles/styleable.py +151 -0
- package/python/vendor/openpyxl/styles/stylesheet.py +274 -0
- package/python/vendor/openpyxl/styles/table.py +94 -0
- package/python/vendor/openpyxl/utils/__init__.py +17 -0
- package/python/vendor/openpyxl/utils/bound_dictionary.py +26 -0
- package/python/vendor/openpyxl/utils/cell.py +240 -0
- package/python/vendor/openpyxl/utils/dataframe.py +87 -0
- package/python/vendor/openpyxl/utils/datetime.py +140 -0
- package/python/vendor/openpyxl/utils/escape.py +43 -0
- package/python/vendor/openpyxl/utils/exceptions.py +34 -0
- package/python/vendor/openpyxl/utils/formulas.py +24 -0
- package/python/vendor/openpyxl/utils/indexed_list.py +49 -0
- package/python/vendor/openpyxl/utils/inference.py +60 -0
- package/python/vendor/openpyxl/utils/protection.py +22 -0
- package/python/vendor/openpyxl/utils/units.py +108 -0
- package/python/vendor/openpyxl/workbook/__init__.py +4 -0
- package/python/vendor/openpyxl/workbook/_writer.py +197 -0
- package/python/vendor/openpyxl/workbook/child.py +166 -0
- package/python/vendor/openpyxl/workbook/defined_name.py +189 -0
- package/python/vendor/openpyxl/workbook/external_link/__init__.py +3 -0
- package/python/vendor/openpyxl/workbook/external_link/external.py +190 -0
- package/python/vendor/openpyxl/workbook/external_reference.py +18 -0
- package/python/vendor/openpyxl/workbook/function_group.py +36 -0
- package/python/vendor/openpyxl/workbook/properties.py +151 -0
- package/python/vendor/openpyxl/workbook/protection.py +163 -0
- package/python/vendor/openpyxl/workbook/smart_tags.py +56 -0
- package/python/vendor/openpyxl/workbook/views.py +155 -0
- package/python/vendor/openpyxl/workbook/web.py +98 -0
- package/python/vendor/openpyxl/workbook/workbook.py +438 -0
- package/python/vendor/openpyxl/worksheet/__init__.py +1 -0
- package/python/vendor/openpyxl/worksheet/_read_only.py +190 -0
- package/python/vendor/openpyxl/worksheet/_reader.py +472 -0
- package/python/vendor/openpyxl/worksheet/_write_only.py +160 -0
- package/python/vendor/openpyxl/worksheet/_writer.py +390 -0
- package/python/vendor/openpyxl/worksheet/cell_range.py +512 -0
- package/python/vendor/openpyxl/worksheet/cell_watch.py +34 -0
- package/python/vendor/openpyxl/worksheet/controls.py +107 -0
- package/python/vendor/openpyxl/worksheet/copier.py +70 -0
- package/python/vendor/openpyxl/worksheet/custom.py +35 -0
- package/python/vendor/openpyxl/worksheet/datavalidation.py +202 -0
- package/python/vendor/openpyxl/worksheet/dimensions.py +306 -0
- package/python/vendor/openpyxl/worksheet/drawing.py +14 -0
- package/python/vendor/openpyxl/worksheet/errors.py +93 -0
- package/python/vendor/openpyxl/worksheet/filters.py +486 -0
- package/python/vendor/openpyxl/worksheet/formula.py +51 -0
- package/python/vendor/openpyxl/worksheet/header_footer.py +270 -0
- package/python/vendor/openpyxl/worksheet/hyperlink.py +46 -0
- package/python/vendor/openpyxl/worksheet/merge.py +141 -0
- package/python/vendor/openpyxl/worksheet/ole.py +133 -0
- package/python/vendor/openpyxl/worksheet/page.py +174 -0
- package/python/vendor/openpyxl/worksheet/pagebreak.py +94 -0
- package/python/vendor/openpyxl/worksheet/picture.py +8 -0
- package/python/vendor/openpyxl/worksheet/print_settings.py +184 -0
- package/python/vendor/openpyxl/worksheet/properties.py +97 -0
- package/python/vendor/openpyxl/worksheet/protection.py +120 -0
- package/python/vendor/openpyxl/worksheet/related.py +17 -0
- package/python/vendor/openpyxl/worksheet/scenario.py +105 -0
- package/python/vendor/openpyxl/worksheet/smart_tag.py +78 -0
- package/python/vendor/openpyxl/worksheet/table.py +385 -0
- package/python/vendor/openpyxl/worksheet/views.py +155 -0
- package/python/vendor/openpyxl/worksheet/worksheet.py +907 -0
- package/python/vendor/openpyxl/writer/__init__.py +1 -0
- package/python/vendor/openpyxl/writer/excel.py +295 -0
- package/python/vendor/openpyxl/writer/theme.py +291 -0
- package/python/vendor/openpyxl/xml/__init__.py +42 -0
- package/python/vendor/openpyxl/xml/constants.py +129 -0
- package/python/vendor/openpyxl/xml/functions.py +87 -0
- package/watchlist/package.json +1 -1
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
/** Official venue HTTP sources: SSE/SZSE ETF shares and CFETS repo fixings. One throttle chain per
|
|
2
|
+
* upstream; row keys are remapped explicitly (§10.6) and 万份 is converted to 份 at this boundary. */
|
|
3
|
+
import { buildDataKey } from '../data-collector/hub.js';
|
|
4
|
+
import { getDataTimeContract } from '../time/tools.js';
|
|
5
|
+
import { createRequestThrottle } from '../net/throttle.js';
|
|
6
|
+
const SSE_QUERY_URL = 'https://query.sse.com.cn/commonQuery.do';
|
|
7
|
+
const SSE_ETF_SHARES_SQL_ID = 'COMMON_SSE_ZQPZ_ETFZL_XXPL_ETFGM_SEARCH_L';
|
|
8
|
+
const SZSE_REPORT_URL = 'https://fund.szse.cn/api/report/ShowReport/data';
|
|
9
|
+
const CHINAMONEY_CSV_URL = 'https://www.chinamoney.com.cn/r/cms/www/chinamoney/data/currency';
|
|
10
|
+
const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
|
11
|
+
/** Every upstream here republishes once per trading day, so 1h staleness is below the cadence. */
|
|
12
|
+
const DAILY_CACHE_MS = 3_600_000;
|
|
13
|
+
// One chain per upstream: the two exchanges and CFETS have independent rate limits, and SZSE needs
|
|
14
|
+
// ~39 sequential page fetches per call.
|
|
15
|
+
const sseThrottle = createRequestThrottle(300);
|
|
16
|
+
const szseThrottle = createRequestThrottle(300);
|
|
17
|
+
const chinamoneyThrottle = createRequestThrottle(500);
|
|
18
|
+
function isRecord(value) {
|
|
19
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
20
|
+
}
|
|
21
|
+
function sourceError(message, code = 'official_invalid_response') {
|
|
22
|
+
const error = new Error(message);
|
|
23
|
+
error.code = code;
|
|
24
|
+
return error;
|
|
25
|
+
}
|
|
26
|
+
function assertKnown(params, allowed) {
|
|
27
|
+
for (const key of Object.keys(params)) {
|
|
28
|
+
if (!allowed.includes(key))
|
|
29
|
+
throw new Error(`unsupported parameter: ${key} (allowed: ${allowed.join(', ')})`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function isCalendarDate(value) {
|
|
33
|
+
if (!DATE_PATTERN.test(value))
|
|
34
|
+
return false;
|
|
35
|
+
const parsed = new Date(`${value}T00:00:00Z`);
|
|
36
|
+
return !Number.isNaN(parsed.getTime()) && parsed.toISOString().slice(0, 10) === value;
|
|
37
|
+
}
|
|
38
|
+
function parameterDate(value, name) {
|
|
39
|
+
if (typeof value !== 'string' || !isCalendarDate(value.trim()))
|
|
40
|
+
throw new Error(`parameter ${name} must be a valid YYYY-MM-DD date`);
|
|
41
|
+
return value.trim();
|
|
42
|
+
}
|
|
43
|
+
function upstreamDate(value, label) {
|
|
44
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
45
|
+
if (!isCalendarDate(text))
|
|
46
|
+
throw sourceError(`${label} is not a YYYY-MM-DD date: ${JSON.stringify(String(value ?? ''))}`, 'official_invalid_response');
|
|
47
|
+
return text;
|
|
48
|
+
}
|
|
49
|
+
function textOrNull(value) {
|
|
50
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
51
|
+
return text.length === 0 ? null : text;
|
|
52
|
+
}
|
|
53
|
+
function requiredText(value, label) {
|
|
54
|
+
const text = textOrNull(value);
|
|
55
|
+
if (text === null)
|
|
56
|
+
throw sourceError(`${label} is empty`, 'official_invalid_response');
|
|
57
|
+
return text;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Upstream reports 万份 with at most two decimals; convert through strings because
|
|
61
|
+
* `5518488.77 * 1e4 === 55184887699.99999` in binary floating point.
|
|
62
|
+
*/
|
|
63
|
+
function sharesFromWan(value, label) {
|
|
64
|
+
const raw = String(value ?? '').trim().replaceAll(',', '');
|
|
65
|
+
const match = /^(\d+)(?:\.(\d{1,4}))?$/.exec(raw);
|
|
66
|
+
if (match === null)
|
|
67
|
+
throw sourceError(`${label} is not a non-negative 万份 value: ${JSON.stringify(String(value ?? ''))}`, 'official_invalid_response');
|
|
68
|
+
const shares = Number(match[1] + (match[2] ?? '').padEnd(4, '0'));
|
|
69
|
+
if (!Number.isSafeInteger(shares))
|
|
70
|
+
throw sourceError(`${label} ${raw} 万份 exceeds the exact integer range`, 'official_invalid_response');
|
|
71
|
+
return shares;
|
|
72
|
+
}
|
|
73
|
+
/** Blank cells are never a fixing result (measured 748/748 FR and 249/249 FDR are non-empty). */
|
|
74
|
+
function percentFromCsv(value, label) {
|
|
75
|
+
const raw = String(value ?? '').trim();
|
|
76
|
+
const parsed = /^-?\d+(?:\.\d+)?$/.test(raw) ? Number(raw) : Number.NaN;
|
|
77
|
+
if (!Number.isFinite(parsed))
|
|
78
|
+
throw sourceError(`${label} is not a numeric percent value: ${JSON.stringify(raw)}`, 'official_invalid_response');
|
|
79
|
+
return parsed;
|
|
80
|
+
}
|
|
81
|
+
function parseJson(text, label) {
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(text);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
throw sourceError(`${label} returned non-JSON: ${JSON.stringify(text.slice(0, 120))}`, 'official_invalid_response');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async function requestText(throttle, url, signal, referer, label) {
|
|
90
|
+
signal.throwIfAborted();
|
|
91
|
+
return throttle(async () => {
|
|
92
|
+
let response;
|
|
93
|
+
try {
|
|
94
|
+
response = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0', Referer: referer }, signal });
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
if (error instanceof Error && error.name === 'AbortError')
|
|
98
|
+
throw error;
|
|
99
|
+
throw sourceError(`${label} request failed: ${error instanceof Error ? error.message : String(error)}`, 'official_http_error');
|
|
100
|
+
}
|
|
101
|
+
const text = await response.text();
|
|
102
|
+
if (!response.ok)
|
|
103
|
+
throw sourceError(`${label} returned HTTP ${response.status}`, 'official_http_error');
|
|
104
|
+
return text;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const tradeDateInput = (description) => ({
|
|
108
|
+
type: 'object',
|
|
109
|
+
properties: { trade_date: { type: 'string', description } },
|
|
110
|
+
required: [],
|
|
111
|
+
additionalProperties: false,
|
|
112
|
+
});
|
|
113
|
+
const nullableNumber = (description) => ({ oneOf: [{ type: 'number' }, { type: 'null' }], ...(description === undefined ? {} : { description }) });
|
|
114
|
+
const nullableString = (description) => ({ oneOf: [{ type: 'string' }, { type: 'null' }], ...(description === undefined ? {} : { description }) });
|
|
115
|
+
const sseEtfSharesOutput = {
|
|
116
|
+
type: 'array',
|
|
117
|
+
items: {
|
|
118
|
+
type: 'object',
|
|
119
|
+
properties: {
|
|
120
|
+
trade_date: { type: 'string', description: '上交所披露的交易日(北京时间日期),不是抓取时刻' },
|
|
121
|
+
fund_code: { type: 'string', description: '6 位基金代码' },
|
|
122
|
+
fund_name: { type: 'string', description: '基金简称(上游原文)' },
|
|
123
|
+
shares: { type: 'number', description: '份额(份)= 上游 TOT_VOL(万份)× 10000' },
|
|
124
|
+
etf_type: nullableString('上交所分类:单市/跨市/跨境/其他;⛔ 与深交所 fund_category 口径不同,不要并成一列'),
|
|
125
|
+
},
|
|
126
|
+
required: ['trade_date', 'fund_code', 'fund_name', 'shares', 'etf_type'],
|
|
127
|
+
additionalProperties: true,
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
const szseEtfSharesOutput = {
|
|
131
|
+
type: 'array',
|
|
132
|
+
items: {
|
|
133
|
+
type: 'object',
|
|
134
|
+
properties: {
|
|
135
|
+
trade_date: { type: 'string', description: '快照数据日期(北京时间日期),即响应 metadata.subname;深市只有最新一天' },
|
|
136
|
+
fund_code: { type: 'string', description: '6 位基金代码' },
|
|
137
|
+
fund_name: { type: 'string', description: '基金简称(从内嵌 HTML 的 <u>…</u> 提取)' },
|
|
138
|
+
shares: { type: 'number', description: '份额(份)= 上游 dqgm(万份,带千分位逗号)× 10000' },
|
|
139
|
+
fund_category: nullableString('深交所投资类别:股票基金/债券基金/货币市场基金/其它基金;⛔ 与上交所 etf_type 口径不同'),
|
|
140
|
+
manager: nullableString('基金管理人全称'),
|
|
141
|
+
listing_date: nullableString('上市日期 YYYY-MM-DD'),
|
|
142
|
+
},
|
|
143
|
+
required: ['trade_date', 'fund_code', 'fund_name', 'shares', 'fund_category', 'manager', 'listing_date'],
|
|
144
|
+
additionalProperties: true,
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
const repoFixingOutput = {
|
|
148
|
+
type: 'array',
|
|
149
|
+
items: {
|
|
150
|
+
type: 'object',
|
|
151
|
+
properties: {
|
|
152
|
+
date: { type: 'string', description: '定盘日 YYYY-MM-DD(北京时间日期)' },
|
|
153
|
+
fr001: nullableNumber('FR001 隔夜,百分数原值(1.37 表示 1.37%)'),
|
|
154
|
+
fr007: nullableNumber('FR007 7 天,百分数原值'),
|
|
155
|
+
fr014: nullableNumber('FR014 14 天,百分数原值'),
|
|
156
|
+
fdr001: nullableNumber('FDR001 隔夜,百分数原值;2025-11-20 之前为 null(不是 0)'),
|
|
157
|
+
fdr007: nullableNumber('FDR007 7 天,百分数原值;2025-11-20 之前为 null(不是 0)'),
|
|
158
|
+
fdr014: nullableNumber('FDR014 14 天,百分数原值;2025-11-20 之前为 null(不是 0)'),
|
|
159
|
+
},
|
|
160
|
+
required: ['date', 'fr001', 'fr007', 'fr014', 'fdr001', 'fdr007', 'fdr014'],
|
|
161
|
+
additionalProperties: true,
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
const isSseEtfSharesRow = (row) => isRecord(row)
|
|
165
|
+
&& typeof row.trade_date === 'string' && isCalendarDate(row.trade_date)
|
|
166
|
+
&& typeof row.fund_code === 'string' && /^\d{6}$/.test(row.fund_code)
|
|
167
|
+
&& typeof row.fund_name === 'string' && row.fund_name.length > 0
|
|
168
|
+
&& Number.isSafeInteger(row.shares) && Number(row.shares) >= 0;
|
|
169
|
+
const isSzseEtfSharesRow = (row) => isSseEtfSharesRow(row)
|
|
170
|
+
&& row.fund_category !== undefined
|
|
171
|
+
&& row.manager !== undefined
|
|
172
|
+
&& row.listing_date !== undefined;
|
|
173
|
+
const isFixingValue = (value) => value === null || (typeof value === 'number' && Number.isFinite(value));
|
|
174
|
+
const isRepoFixingRow = (row) => isRecord(row)
|
|
175
|
+
&& typeof row.date === 'string' && isCalendarDate(row.date)
|
|
176
|
+
&& ['fr001', 'fr007', 'fr014', 'fdr001', 'fdr007', 'fdr014'].every((key) => isFixingValue(row[key]));
|
|
177
|
+
function normalizeSseEtfShares(params) {
|
|
178
|
+
if (!isRecord(params))
|
|
179
|
+
throw new Error('params must be an object');
|
|
180
|
+
assertKnown(params, ['trade_date']);
|
|
181
|
+
// Omitted trade_date means "the latest published day": measured 2026-11-22 (Sun) -> 2026-11-20.
|
|
182
|
+
return params.trade_date === undefined ? {} : { trade_date: parameterDate(params.trade_date, 'trade_date') };
|
|
183
|
+
}
|
|
184
|
+
function normalizeSzseEtfShares(params) {
|
|
185
|
+
if (!isRecord(params))
|
|
186
|
+
throw new Error('params must be an object');
|
|
187
|
+
assertKnown(params, ['trade_date']);
|
|
188
|
+
return params.trade_date === undefined ? {} : { trade_date: parameterDate(params.trade_date, 'trade_date') };
|
|
189
|
+
}
|
|
190
|
+
function normalizeRepoFixing(params) {
|
|
191
|
+
if (!isRecord(params))
|
|
192
|
+
throw new Error('params must be an object');
|
|
193
|
+
assertKnown(params, ['start_date', 'end_date']);
|
|
194
|
+
const start = params.start_date === undefined ? undefined : parameterDate(params.start_date, 'start_date');
|
|
195
|
+
const end = params.end_date === undefined ? undefined : parameterDate(params.end_date, 'end_date');
|
|
196
|
+
if (start !== undefined && end !== undefined && start > end)
|
|
197
|
+
throw new Error('parameter start_date must not be later than end_date');
|
|
198
|
+
return { ...(start === undefined ? {} : { start_date: start }), ...(end === undefined ? {} : { end_date: end }) };
|
|
199
|
+
}
|
|
200
|
+
async function executeSseEtfShares(params, signal) {
|
|
201
|
+
const requested = typeof params.trade_date === 'string' ? params.trade_date : undefined;
|
|
202
|
+
const query = new URLSearchParams({
|
|
203
|
+
sqlId: SSE_ETF_SHARES_SQL_ID,
|
|
204
|
+
// Not optional: without isPagination=true the endpoint answers total=0 and no rows (measured).
|
|
205
|
+
isPagination: 'true',
|
|
206
|
+
'pageHelp.pageSize': '10000',
|
|
207
|
+
'pageHelp.pageNo': '1',
|
|
208
|
+
'pageHelp.beginPage': '1',
|
|
209
|
+
'pageHelp.cacheSize': '1',
|
|
210
|
+
'pageHelp.endPage': '1',
|
|
211
|
+
});
|
|
212
|
+
if (requested !== undefined)
|
|
213
|
+
query.set('STAT_DATE', requested);
|
|
214
|
+
const payload = parseJson(await requestText(sseThrottle, `${SSE_QUERY_URL}?${query.toString()}`, signal, 'https://www.sse.com.cn/', 'SSE ETF shares'), 'SSE ETF shares');
|
|
215
|
+
if (!isRecord(payload) || !isRecord(payload.pageHelp))
|
|
216
|
+
throw sourceError('SSE ETF shares response lost pageHelp', 'official_invalid_response');
|
|
217
|
+
const pageHelp = payload.pageHelp;
|
|
218
|
+
const total = pageHelp.total;
|
|
219
|
+
if (!Number.isSafeInteger(total) || Number(total) < 0)
|
|
220
|
+
throw sourceError(`SSE ETF shares pageHelp.total is not a count: ${JSON.stringify(total)}`, 'official_invalid_response');
|
|
221
|
+
const listed = Array.isArray(pageHelp.data) ? pageHelp.data : Array.isArray(payload.result) ? payload.result : [];
|
|
222
|
+
// pageSize is capped at 2000 and pageHelp.pageNo is ignored (measured: pageNo=2 re-serves page 1),
|
|
223
|
+
// so a day larger than one page cannot be paged — refuse the partial snapshot instead of shrinking it.
|
|
224
|
+
if (listed.length !== total)
|
|
225
|
+
throw sourceError(`SSE ETF shares returned ${listed.length} rows but pageHelp.total=${String(total)}; one page is not the whole snapshot`, 'official_incomplete');
|
|
226
|
+
if (listed.length === 0) {
|
|
227
|
+
// A non-trading day / future date / unpublished day is a real answer only when it was asked for.
|
|
228
|
+
if (requested === undefined)
|
|
229
|
+
throw sourceError('SSE ETF shares latest-snapshot query returned 0 rows; the no-date form always resolves to a published day', 'official_empty_response');
|
|
230
|
+
return { data: [], schema: sseEtfSharesOutput };
|
|
231
|
+
}
|
|
232
|
+
const rows = [];
|
|
233
|
+
const seen = new Set();
|
|
234
|
+
let snapshotDate;
|
|
235
|
+
for (const record of listed) {
|
|
236
|
+
if (!isRecord(record))
|
|
237
|
+
throw sourceError('SSE ETF shares row is not an object', 'official_invalid_response');
|
|
238
|
+
const tradeDate = upstreamDate(record.STAT_DATE, 'SSE STAT_DATE');
|
|
239
|
+
snapshotDate ??= tradeDate;
|
|
240
|
+
if (tradeDate !== snapshotDate)
|
|
241
|
+
throw sourceError(`SSE ETF shares mixed ${snapshotDate} and ${tradeDate} in one snapshot`, 'official_invalid_response');
|
|
242
|
+
if (requested !== undefined && tradeDate !== requested)
|
|
243
|
+
throw sourceError(`SSE returned ${tradeDate} for requested ${requested}`, 'official_invalid_response');
|
|
244
|
+
const fundCode = requiredText(record.SEC_CODE, 'SSE SEC_CODE');
|
|
245
|
+
if (!/^\d{6}$/.test(fundCode))
|
|
246
|
+
throw sourceError(`SSE SEC_CODE is not a six-digit fund code: ${JSON.stringify(fundCode)}`, 'official_invalid_response');
|
|
247
|
+
if (seen.has(fundCode))
|
|
248
|
+
throw sourceError(`SSE ETF shares repeats ${fundCode}; a duplicated code is not a complete snapshot`, 'official_invalid_response');
|
|
249
|
+
seen.add(fundCode);
|
|
250
|
+
rows.push({
|
|
251
|
+
trade_date: tradeDate,
|
|
252
|
+
fund_code: fundCode,
|
|
253
|
+
fund_name: requiredText(record.SEC_NAME, `SSE SEC_NAME ${fundCode}`),
|
|
254
|
+
shares: sharesFromWan(record.TOT_VOL, `SSE TOT_VOL ${fundCode}`),
|
|
255
|
+
etf_type: textOrNull(record.ETF_TYPE),
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return { data: rows, schema: sseEtfSharesOutput };
|
|
259
|
+
}
|
|
260
|
+
async function szsePage(pageNo, signal) {
|
|
261
|
+
const query = new URLSearchParams({ SHOWTYPE: 'JSON', CATALOGID: '1000_lf', TABKEY: 'tab1', selectJjlb: 'ETF', PAGENO: String(pageNo) });
|
|
262
|
+
const label = `SZSE fund list page ${pageNo}`;
|
|
263
|
+
const payload = parseJson(await requestText(szseThrottle, `${SZSE_REPORT_URL}?${query.toString()}`, signal, 'https://fund.szse.cn/', label), label);
|
|
264
|
+
if (!Array.isArray(payload))
|
|
265
|
+
throw sourceError(`${label} is not an array of tables`, 'official_invalid_response');
|
|
266
|
+
// The report error sits on the table, not in metadata: an unknown CATALOGID answers one table with
|
|
267
|
+
// metadata.tabkey=null and error="noalert@显示报表数据失败" (measured).
|
|
268
|
+
const tables = payload.filter((entry) => isRecord(entry));
|
|
269
|
+
for (const entry of tables) {
|
|
270
|
+
const message = textOrNull(entry.error);
|
|
271
|
+
if (message !== null)
|
|
272
|
+
throw sourceError(`${label} reported ${JSON.stringify(message)}`, 'official_invalid_response');
|
|
273
|
+
}
|
|
274
|
+
const table = tables.find((entry) => isRecord(entry.metadata) && entry.metadata.tabkey === 'tab1');
|
|
275
|
+
if (table === undefined || !isRecord(table.metadata) || !Array.isArray(table.data))
|
|
276
|
+
throw sourceError(`${label} lost its tab1 metadata/data`, 'official_invalid_response');
|
|
277
|
+
return { metadata: table.metadata, rows: table.data };
|
|
278
|
+
}
|
|
279
|
+
function szseCount(value, label) {
|
|
280
|
+
if (!Number.isSafeInteger(value) || Number(value) < 0)
|
|
281
|
+
throw sourceError(`SZSE metadata.${label} is not a count: ${JSON.stringify(value)}`, 'official_invalid_response');
|
|
282
|
+
return Number(value);
|
|
283
|
+
}
|
|
284
|
+
function embeddedTagText(value, label, pattern) {
|
|
285
|
+
const match = pattern.exec(String(value ?? ''));
|
|
286
|
+
if (match === null)
|
|
287
|
+
throw sourceError(`SZSE fund list ${label} HTML shape changed: ${JSON.stringify(String(value ?? '').slice(0, 80))}`, 'official_invalid_response');
|
|
288
|
+
return match[1].trim();
|
|
289
|
+
}
|
|
290
|
+
async function executeSzseEtfShares(params, signal) {
|
|
291
|
+
const requested = typeof params.trade_date === 'string' ? params.trade_date : undefined;
|
|
292
|
+
const first = await szsePage(1, signal);
|
|
293
|
+
const snapshotDate = upstreamDate(first.metadata.subname, 'SZSE metadata.subname');
|
|
294
|
+
if (requested !== undefined && requested !== snapshotDate) {
|
|
295
|
+
throw sourceError(`SZSE publishes only the latest snapshot (${snapshotDate}), not ${requested}; Shenzhen ETF shares have no history, omit trade_date to take the current snapshot`, 'official_snapshot_mismatch');
|
|
296
|
+
}
|
|
297
|
+
const pageCount = szseCount(first.metadata.pagecount, 'pagecount');
|
|
298
|
+
const recordCount = szseCount(first.metadata.recordcount, 'recordcount');
|
|
299
|
+
if (pageCount < 1 || recordCount < 1)
|
|
300
|
+
throw sourceError(`SZSE fund list reports pagecount=${pageCount} recordcount=${recordCount}`, 'official_invalid_response');
|
|
301
|
+
if (first.rows.length === 0)
|
|
302
|
+
throw sourceError('SZSE fund list page 1 is empty while recordcount>0', 'official_incomplete');
|
|
303
|
+
const listed = [...first.rows];
|
|
304
|
+
for (let pageNo = 2; pageNo <= pageCount; pageNo += 1) {
|
|
305
|
+
const page = await szsePage(pageNo, signal);
|
|
306
|
+
// The report is re-rendered live; a mid-run date or count change means two snapshots were stitched.
|
|
307
|
+
if (page.metadata.subname !== first.metadata.subname || szseCount(page.metadata.pagecount, 'pagecount') !== pageCount || szseCount(page.metadata.recordcount, 'recordcount') !== recordCount) {
|
|
308
|
+
throw sourceError(`SZSE fund list snapshot changed while paging (${String(first.metadata.subname)} -> ${String(page.metadata.subname)}); retry the request`, 'official_incomplete');
|
|
309
|
+
}
|
|
310
|
+
if (page.rows.length === 0)
|
|
311
|
+
throw sourceError(`SZSE fund list page ${pageNo}/${pageCount} is empty`, 'official_incomplete');
|
|
312
|
+
listed.push(...page.rows);
|
|
313
|
+
}
|
|
314
|
+
if (listed.length !== recordCount)
|
|
315
|
+
throw sourceError(`SZSE fund list returned ${listed.length} rows but recordcount=${recordCount}`, 'official_incomplete');
|
|
316
|
+
const rows = [];
|
|
317
|
+
const seen = new Set();
|
|
318
|
+
for (const record of listed) {
|
|
319
|
+
if (!isRecord(record))
|
|
320
|
+
throw sourceError('SZSE fund list row is not an object', 'official_invalid_response');
|
|
321
|
+
// selectJjlb=ETF is ours; if it stops filtering, this table silently becomes every fund (measured 1088 rows).
|
|
322
|
+
if (record.jjlb !== 'ETF')
|
|
323
|
+
throw sourceError(`SZSE fund list returned jjlb=${JSON.stringify(record.jjlb)}; the ETF filter is no longer applied`, 'official_invalid_response');
|
|
324
|
+
const fundCode = embeddedTagText(record.sys_key, 'sys_key', /<u>(\d{6})<\/u>/);
|
|
325
|
+
if (seen.has(fundCode))
|
|
326
|
+
throw sourceError(`SZSE fund list repeats ${fundCode}; a duplicated code is not a complete snapshot`, 'official_invalid_response');
|
|
327
|
+
seen.add(fundCode);
|
|
328
|
+
const listingDate = textOrNull(record.ssrq);
|
|
329
|
+
rows.push({
|
|
330
|
+
trade_date: snapshotDate,
|
|
331
|
+
fund_code: fundCode,
|
|
332
|
+
fund_name: embeddedTagText(record.jjjcurl, 'jjjcurl', /<u>([^<]*)<\/u>/),
|
|
333
|
+
shares: sharesFromWan(embeddedTagText(record.dqgm, 'dqgm', /<a[^>]*>([^<]*)<\/a>/), `SZSE dqgm ${fundCode}`),
|
|
334
|
+
fund_category: textOrNull(record.tzlb),
|
|
335
|
+
manager: textOrNull(record.glrmc),
|
|
336
|
+
listing_date: listingDate === null ? null : upstreamDate(listingDate, `SZSE listing date ${fundCode}`),
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
return { data: rows, schema: szseEtfSharesOutput };
|
|
340
|
+
}
|
|
341
|
+
function parseFixingCsv(text, kind) {
|
|
342
|
+
const byDate = new Map();
|
|
343
|
+
for (const line of text.split(/\r?\n/)) {
|
|
344
|
+
if (line.trim().length === 0)
|
|
345
|
+
continue;
|
|
346
|
+
const cells = line.split(',');
|
|
347
|
+
// Fixed layout `date,,,,,,001,007,014`. The five middle cells are always empty upstream; any
|
|
348
|
+
// other shape means the column order moved and these positions would mislabel the tenors.
|
|
349
|
+
if (cells.length !== 9 || cells.slice(1, 6).some((cell) => cell.trim().length !== 0)) {
|
|
350
|
+
throw sourceError(`CFETS ${kind} CSV layout changed: ${JSON.stringify(line.slice(0, 60))}`, 'official_invalid_response');
|
|
351
|
+
}
|
|
352
|
+
const date = upstreamDate(cells[0], `CFETS ${kind} CSV date`);
|
|
353
|
+
if (byDate.has(date))
|
|
354
|
+
throw sourceError(`CFETS ${kind} CSV repeats ${date}`, 'official_invalid_response');
|
|
355
|
+
byDate.set(date, [
|
|
356
|
+
percentFromCsv(cells[6], `CFETS ${kind}001 ${date}`),
|
|
357
|
+
percentFromCsv(cells[7], `CFETS ${kind}007 ${date}`),
|
|
358
|
+
percentFromCsv(cells[8], `CFETS ${kind}014 ${date}`),
|
|
359
|
+
]);
|
|
360
|
+
}
|
|
361
|
+
if (byDate.size === 0)
|
|
362
|
+
throw sourceError(`CFETS ${kind} CSV returned no rows`, 'official_empty_response');
|
|
363
|
+
return byDate;
|
|
364
|
+
}
|
|
365
|
+
async function fetchFixingCsv(file, kind, signal) {
|
|
366
|
+
// Encoding is UTF-8 (pure ASCII in practice, CRLF, no BOM); GBK would be a wrong guess, so only a
|
|
367
|
+
// defensive BOM strip is applied.
|
|
368
|
+
const text = await requestText(chinamoneyThrottle, `${CHINAMONEY_CSV_URL}/${file}-chrt.csv`, signal, 'https://www.chinamoney.com.cn/chinese/bkfrr/', `CFETS ${kind} CSV`);
|
|
369
|
+
return parseFixingCsv(text.replace(/^\uFEFF/, ''), kind);
|
|
370
|
+
}
|
|
371
|
+
async function executeRepoFixing(params, signal) {
|
|
372
|
+
const start = typeof params.start_date === 'string' ? params.start_date : undefined;
|
|
373
|
+
const end = typeof params.end_date === 'string' ? params.end_date : undefined;
|
|
374
|
+
const fr = await fetchFixingCsv('frr', 'FR', signal);
|
|
375
|
+
const fdr = await fetchFixingCsv('fdr', 'FDR', signal);
|
|
376
|
+
const dates = [...new Set([...fr.keys(), ...fdr.keys()])].sort();
|
|
377
|
+
const rows = [];
|
|
378
|
+
for (const date of dates) {
|
|
379
|
+
// Local crop: the endpoint ignores startDate/endDate (measured) and only ever serves the full file.
|
|
380
|
+
if (start !== undefined && date < start)
|
|
381
|
+
continue;
|
|
382
|
+
if (end !== undefined && date > end)
|
|
383
|
+
continue;
|
|
384
|
+
const a = fr.get(date);
|
|
385
|
+
const b = fdr.get(date);
|
|
386
|
+
rows.push({
|
|
387
|
+
date,
|
|
388
|
+
fr001: a?.[0] ?? null,
|
|
389
|
+
fr007: a?.[1] ?? null,
|
|
390
|
+
fr014: a?.[2] ?? null,
|
|
391
|
+
// FDR starts 2025-11-20; older days have no FDR fixing at all, which is null, never 0.
|
|
392
|
+
fdr001: b?.[0] ?? null,
|
|
393
|
+
fdr007: b?.[1] ?? null,
|
|
394
|
+
fdr014: b?.[2] ?? null,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
return { data: rows, schema: repoFixingOutput };
|
|
398
|
+
}
|
|
399
|
+
function createSource(options) {
|
|
400
|
+
const schema = {
|
|
401
|
+
capability: options.capability,
|
|
402
|
+
time_contract: getDataTimeContract(options.capability),
|
|
403
|
+
name: options.name,
|
|
404
|
+
source: `http:official.${options.capability}`,
|
|
405
|
+
data_key: buildDataKey('official', 'http', options.capability),
|
|
406
|
+
source_label: 'official',
|
|
407
|
+
paginated: false,
|
|
408
|
+
cacheMaxAgeMs: options.cacheMaxAgeMs,
|
|
409
|
+
rowShape: { rootArray: true },
|
|
410
|
+
summary: options.summary,
|
|
411
|
+
description: options.description,
|
|
412
|
+
input_schema: options.inputSchema,
|
|
413
|
+
output_schema: options.outputSchema,
|
|
414
|
+
};
|
|
415
|
+
const normalize = (params) => {
|
|
416
|
+
if (!isRecord(params))
|
|
417
|
+
throw new Error('params must be an object');
|
|
418
|
+
return options.normalize(params);
|
|
419
|
+
};
|
|
420
|
+
return {
|
|
421
|
+
schema,
|
|
422
|
+
normalizeParams: normalize,
|
|
423
|
+
validateOutput: options.validateOutput,
|
|
424
|
+
execute: async (request, signal) => options.execute(normalize(request.params), signal),
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
export function createOfficialSources() {
|
|
428
|
+
return [
|
|
429
|
+
createSource({
|
|
430
|
+
capability: 'sse_etf_shares',
|
|
431
|
+
cacheMaxAgeMs: DAILY_CACHE_MS,
|
|
432
|
+
name: 'get_sse_etf_shares',
|
|
433
|
+
summary: '上交所 ETF 份额(按日归档,可查历史)',
|
|
434
|
+
description: '获取上交所 ETF 份额(官方「ETF 规模信息披露」按日归档,query.sse.com.cn/commonQuery.do)。一行一只 ETF。`trade_date` 可选:**省略时返回最近已发布的交易日快照**(实测 2026-11-22 周日取到 2026-11-20 周五 927 行)——默认不是「今天」,回答「今天」要用行内的 trade_date 核对;传 YYYY-MM-DD 取该日(实测 2023-01-03 有 446 行、2012-01-04 有 23 行;2011-12-01 及更早为 0 行,归档起点在 2012 年初)。⛔ `shares` 单位是**份**:上游 TOT_VOL 是**万份**,本层用字符串换算 ×10000 换成份(浮点乘法会出错:5518488.77×1e4 在 float64 里是 55184887699.99999)。单位对账实测(2026-11-20 收盘):510300 上游 5518488.77 万份 → 55,184,887,700 份,× 收盘价 4.018 = 2217.4 亿元,与腾讯同日总市值 2217.33 亿元比值 0.999999;510500(0.999999)、513100(0.999994)、510050、588000 同样逐票对上——⛔ 别再把这列当份的原始值乘净值、也别当亿份。⛔ **非交易日 / 未来日期 / 尚未发布返回成功空数组**(实测 2026-11-21 周六、2026-12-01、2011-12-01 都是 HTTP 200 + total=0 + 空 data),空不是失败、不要重试;但**省略 trade_date(latest 模式)回 0 行按上游异常响亮失败**(无日期查询总能解析到一个已发布日)。`etf_type` 是上交所自己的分类(实测单市/跨市/跨境/其他);⛔ 深交所的 fund_category 是另一套口径,不要并成一列。时区:trade_date 是上海交易所交易日(北京时间日期),不是抓取时刻。分页:本层内部一次取满——上游 pageSize 硬上限 2000 且 pageHelp.pageNo 被忽略(实测 pageSize=100 仍回 500 行、pageNo=2 仍回第一页),所以按 pageHelp.total 核对行数,行数 < total 即快照被截断并响亮失败,绝不返回不完整快照;同日代码重复同样失败。未收录:上游 NUM(当日序号,无分析价值)、净值与规模列(上游不提供,别用别的来源冒充)、份额变化列(要变化请取两个交易日自行相减,本能力支持历史日期)。',
|
|
435
|
+
inputSchema: tradeDateInput('交易日 YYYY-MM-DD(北京时间);省略则取最近已发布交易日'),
|
|
436
|
+
outputSchema: sseEtfSharesOutput,
|
|
437
|
+
allowed: ['trade_date'],
|
|
438
|
+
normalize: normalizeSseEtfShares,
|
|
439
|
+
validateOutput: (data) => Array.isArray(data) && data.every(isSseEtfSharesRow),
|
|
440
|
+
execute: executeSseEtfShares,
|
|
441
|
+
}),
|
|
442
|
+
createSource({
|
|
443
|
+
capability: 'szse_etf_shares',
|
|
444
|
+
cacheMaxAgeMs: DAILY_CACHE_MS,
|
|
445
|
+
name: 'get_szse_etf_shares',
|
|
446
|
+
summary: '深交所 ETF 份额(仅最新一天快照)',
|
|
447
|
+
description: '获取深交所 ETF 份额(官方「基金列表」,fund.szse.cn ShowReport,CATALOGID=1000_lf / TABKEY=tab1 / selectJjlb=ETF)。⛔ **只有最新一天,没有历史**:端点不接受任何日期参数(实测 tradeDate/date 被忽略),快照日期由响应 metadata.subname 给出(实测 2026-11-22 周日快照 = 2026-11-20,772 只)。`trade_date` 可选:省略 = 取当前快照并用行内 trade_date 标明是哪天;**给出时必须等于快照日期**,否则响亮失败(不拿最新快照冒充历史;深市要历史只能自己按日留存)。⛔ `shares` 单位是**份**:上游 dqgm 是**万份**且带千分位逗号(如 2,156,781.66),本层去逗号后用字符串 ×10000。单位对账实测:159919 的 2026-11-20 份额 21,567,816,600 份 × 收盘价 4.191 = 903.9 亿元,而腾讯同日总市值 678.39 亿 ÷ 4.191 = 161.87 亿份 = **前一交易日(11-19)的 1,618,671.66 万份**——腾讯总市值列用的是滞后一天的份额(该 ETF 当日有大额申购,份额 161.87 亿 → 215.68 亿),⛔ 不要反过来判定本表错。解析:sys_key / jjjcurl / dqgm 三列是**内嵌 HTML 片段**,本层从 `<u>…</u>` 取代码与简称、从 `<a>…</a>` 取规模(实测 772/772 行三处都能取到、代码唯一、无 HTML 实体);结构不符即响亮失败,不静默丢行。`fund_category` 是深交所投资类别(实测股票基金 702 / 其它基金 48 / 债券基金 19 / 货币市场基金 3),`manager` 管理人全称,`listing_date` 上市日期。分页:上游每页固定 20 条(PAGESIZE/pageSize 实测无效),772 只需 39 页,本层翻满并核对行数 = metadata.recordcount;翻页途中快照日期或总数变化、某页为空、行数不符、`jjlb` 不再是 ETF(过滤失效会混进 LOF/封闭式,实测不过滤是 1088 行)一律响亮失败——绝不返回混合或不完整快照。上游不提供净值与份额变化列。更新时点:上游注明 T 日晚为预估值、T+1 早为确认值(本轮未复测)。时区:trade_date 为北京时间日期。',
|
|
448
|
+
inputSchema: tradeDateInput('快照数据日期 YYYY-MM-DD(北京时间);省略取当前快照,给出时必须与快照日期一致'),
|
|
449
|
+
outputSchema: szseEtfSharesOutput,
|
|
450
|
+
allowed: ['trade_date'],
|
|
451
|
+
normalize: normalizeSzseEtfShares,
|
|
452
|
+
validateOutput: (data) => Array.isArray(data) && data.length > 0 && data.every(isSzseEtfSharesRow),
|
|
453
|
+
execute: executeSzseEtfShares,
|
|
454
|
+
}),
|
|
455
|
+
createSource({
|
|
456
|
+
capability: 'chinamoney_repo_fixing',
|
|
457
|
+
cacheMaxAgeMs: DAILY_CACHE_MS,
|
|
458
|
+
name: 'get_chinamoney_repo_fixing',
|
|
459
|
+
summary: '银行间回购定盘利率 FR/FDR 全历史',
|
|
460
|
+
description: '获取银行间回购定盘利率 FR / FDR(中国货币网 = 外汇交易中心官方 CSV,一次取两个文件:frr-chrt.csv 与 fdr-chrt.csv),一行一个自然日,宽表 6 个期限列:`fr001`/`fr007`/`fr014`(全市场回购定盘利率,实测 748 行、最早 2023-11-20)与 `fdr001`/`fdr007`/`fdr014`(银银间,实测 249 行、最早 2025-11-20)。⛔ 单位是 **%** 且为**百分数原值**(1.37 读作 1.37%):实测全样本区间 FR001 1.22~2.69、FR007 1.33~4.35、FR014 1.35~4.10(FDR 四列 1.22~2.00),最新 2026-11-20 为 1.37/1.40/1.42——与 7 天逆回购政策利率 1.40% 同一量级;若按比例读会是 0.0137 这种量级、差 100 倍,⛔ 不要和 turnover_pct 那类比例值混用。⛔ **上游不支持日期区间**:URL 上加 startDate/endDate 实测被忽略、照样回整份 CSV;本能力的 `start_date`/`end_date` 是**本地裁剪**(含两端),省略即全量。⛔ CSV **没有表头**,列序固定为「日期 + 5 个空列 + 001/007/014」;列数不为 9 或中间 5 列非空即响亮失败(别猜别的列序)。编码实测 **UTF-8(纯 ASCII,无 BOM,CRLF)**——不是 GBK;本层按 UTF-8 解码并防御性剥 BOM。⛔ 空值语义:上游两个文件实测各利率列零空串、零 0;**FDR 只有 2025-11-20 之后**,更早日期的 fdr* 为 **null(不是 0)**——实测 FDR 日期集合是 FR 的子集,且 FR 在 ≥2025-11-20 恰好 249 行 = FDR 行数。利率出现空串或非数字时响亮失败,⛔ 绝不把「−」/空当 0。更新时点:实测该文件 Last-Modified = 2026-11-20 11:32:07 北京时间(周五),周末不追加(2026-11-21/22 无行),故按 1 小时缓存。输出的行按 date **升序**(上游 CSV 是降序),利率原值照抄、不四舍五入。`start_date`/`end_date` 落在周末或无数据日的区间返回**成功空数组**(合法空,不是失败,不要换日期重试);整份 CSV 解析出 0 行才是失败。',
|
|
461
|
+
inputSchema: {
|
|
462
|
+
type: 'object',
|
|
463
|
+
properties: {
|
|
464
|
+
start_date: { type: 'string', description: '区间起点 YYYY-MM-DD(含端点,本地裁剪;上游只给整份 CSV)' },
|
|
465
|
+
end_date: { type: 'string', description: '区间终点 YYYY-MM-DD(含端点,本地裁剪;上游只给整份 CSV)' },
|
|
466
|
+
},
|
|
467
|
+
required: [],
|
|
468
|
+
additionalProperties: false,
|
|
469
|
+
},
|
|
470
|
+
outputSchema: repoFixingOutput,
|
|
471
|
+
allowed: ['start_date', 'end_date'],
|
|
472
|
+
normalize: normalizeRepoFixing,
|
|
473
|
+
validateOutput: (data) => Array.isArray(data) && data.every(isRepoFixingRow),
|
|
474
|
+
execute: executeRepoFixing,
|
|
475
|
+
}),
|
|
476
|
+
];
|
|
477
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Python-only data sources (private runtime + repo-vendored packages).
|
|
3
|
+
*
|
|
4
|
+
* Every capability is fixed to one reviewed script under `python/scripts/`; the runner is injected at
|
|
5
|
+
* assembly time and only touches the runtime when a capability is called (§9.7: external state stays
|
|
6
|
+
* call-time, failures are not memoized). Rows coming back from Python are re-validated and mapped
|
|
7
|
+
* field by field here — raw dicts never reach a Dataset as-is.
|
|
8
|
+
*/
|
|
9
|
+
import type { DataSource } from '../data-collector/hub.js';
|
|
10
|
+
export type PythonDataScript = 'baostock_valuation_history' | 'baostock_stock_basic' | 'pbc_social_financing';
|
|
11
|
+
export interface PythonScriptRunRequest {
|
|
12
|
+
script: PythonDataScript;
|
|
13
|
+
params: Record<string, unknown>;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
}
|
|
16
|
+
export interface PythonScriptRunResult {
|
|
17
|
+
rows: unknown[];
|
|
18
|
+
pythonVersion?: string;
|
|
19
|
+
durationMs?: number;
|
|
20
|
+
}
|
|
21
|
+
/** Injected by the host: `PythonRuntimeManager.runDataScript` fits this shape. */
|
|
22
|
+
export type PythonScriptRunner = (request: PythonScriptRunRequest) => Promise<PythonScriptRunResult>;
|
|
23
|
+
export declare const MAX_BASIC_CODES = 50;
|
|
24
|
+
export declare const MAX_SOCIAL_FINANCING_MONTHS = 120;
|
|
25
|
+
export declare function createPythonSources(runner: PythonScriptRunner): DataSource[];
|