@v587d/capital-generation 2.29.0 → 2.30.1
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 +58 -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/dataset-tools.js +4 -2
- package/lib/data-collector/describe.d.ts +2 -0
- package/lib/data-collector/describe.js +2 -0
- package/lib/data-collector/store.d.ts +11 -0
- package/lib/data-collector/store.js +80 -1
- package/lib/data-collector/tools.d.ts +1 -1
- package/lib/data-collector/tools.js +10 -9
- 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 +517 -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,484 @@
|
|
|
1
|
+
import { buildDataKey } from '../data-collector/hub.js';
|
|
2
|
+
import { getDataTimeContract } from '../time/tools.js';
|
|
3
|
+
import { createRequestThrottle } from '../net/throttle.js';
|
|
4
|
+
const HQ_URL = 'https://hq.sinajs.cn/list=';
|
|
5
|
+
const STOCK_NAME_URL = 'https://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getStockName';
|
|
6
|
+
const A50_SYMBOL = 'hf_CHA50CFD';
|
|
7
|
+
const SINA_REFERER = 'https://stock.finance.sina.com.cn/';
|
|
8
|
+
const USER_AGENT = 'Mozilla/5.0';
|
|
9
|
+
const MIN_INTERVAL_MS = 300;
|
|
10
|
+
const MAX_CODES = 20;
|
|
11
|
+
const TQUOTE_MIN_FIELDS = 45;
|
|
12
|
+
const GREEKS_MIN_FIELDS = 16;
|
|
13
|
+
const A50_MIN_FIELDS = 14;
|
|
14
|
+
const PCT_TOLERANCE = 0.1;
|
|
15
|
+
// One serial chain per upstream host: the contract list is 1 API call plus one hq call per month,
|
|
16
|
+
// and piling those up concurrently gets the IP answered with empty lists.
|
|
17
|
+
const hqThrottle = createRequestThrottle(MIN_INTERVAL_MS);
|
|
18
|
+
const optionApiThrottle = createRequestThrottle(MIN_INTERVAL_MS);
|
|
19
|
+
const OPTION_UNDERLYINGS = { '510050': '50ETF', '510300': '300ETF', '588000': '科创50', '510500': '500ETF' };
|
|
20
|
+
const OPTION_SIDES = { call: 'OP_UP_', put: 'OP_DOWN_' };
|
|
21
|
+
// Literal order: object keys with numeric-looking names iterate numerically, so Object.keys would
|
|
22
|
+
// publish 510050/510300/510500/588000 instead of the documented order.
|
|
23
|
+
const UNDERLYING_LIST = ['510050', '510300', '588000', '510500'];
|
|
24
|
+
const SIDE_LIST = ['call', 'put'];
|
|
25
|
+
function isRecord(value) {
|
|
26
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
27
|
+
}
|
|
28
|
+
function sinaError(message, code = 'sina_source_error') {
|
|
29
|
+
const error = new Error(message);
|
|
30
|
+
error.code = code;
|
|
31
|
+
return error;
|
|
32
|
+
}
|
|
33
|
+
async function sinaGetText(url, throttle, encoding, signal) {
|
|
34
|
+
signal.throwIfAborted();
|
|
35
|
+
return throttle(async () => {
|
|
36
|
+
const response = await fetch(url, { headers: { Referer: SINA_REFERER, 'User-Agent': USER_AGENT }, signal });
|
|
37
|
+
if (!response.ok)
|
|
38
|
+
throw sinaError(`Sina HTTP ${response.status} for ${url}`, response.status === 429 ? 'sina_rate_limit' : 'sina_http_error');
|
|
39
|
+
const bytes = await response.arrayBuffer();
|
|
40
|
+
return new TextDecoder(encoding).decode(bytes);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
const HQ_VARIABLE = /var hq_str_([A-Za-z0-9_.]+)="([^"]*)"/g;
|
|
44
|
+
/** hq.sinajs.cn echoes one `var hq_str_<key>="...";` per requested key; unknown keys come back empty. */
|
|
45
|
+
async function fetchHqLists(keys, signal) {
|
|
46
|
+
const text = await sinaGetText(`${HQ_URL}${keys.join(',')}`, hqThrottle, 'gbk', signal);
|
|
47
|
+
const variables = new Map();
|
|
48
|
+
for (const match of text.matchAll(HQ_VARIABLE))
|
|
49
|
+
variables.set(match[1], match[2]);
|
|
50
|
+
const missing = keys.filter((key) => !variables.has(key));
|
|
51
|
+
if (missing.length > 0)
|
|
52
|
+
throw sinaError(`Sina hq reply has no variable for ${missing.join(',')}: the endpoint format changed`, 'sina_invalid_response');
|
|
53
|
+
return variables;
|
|
54
|
+
}
|
|
55
|
+
/** Empty cell is missing (null); 0 is a real quote value and must survive. */
|
|
56
|
+
function sinaNumber(raw, field, context) {
|
|
57
|
+
const text = (raw ?? '').trim();
|
|
58
|
+
if (text === '' || text === 'NoData.')
|
|
59
|
+
return null;
|
|
60
|
+
const value = Number(text);
|
|
61
|
+
if (!Number.isFinite(value))
|
|
62
|
+
throw sinaError(`Sina ${context} has a non-numeric ${field} (${JSON.stringify(text)})`, 'sina_invalid_response');
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
function round4(value) {
|
|
66
|
+
const rounded = Math.round(value * 10_000) / 10_000;
|
|
67
|
+
return rounded === 0 ? 0 : rounded;
|
|
68
|
+
}
|
|
69
|
+
/** YYMM months of the listed contracts, deduped and sorted near first. */
|
|
70
|
+
async function fetchContractMonths(cate, signal) {
|
|
71
|
+
const url = `${STOCK_NAME_URL}?exchange=null&cate=${encodeURIComponent(cate)}`;
|
|
72
|
+
const text = await sinaGetText(url, optionApiThrottle, 'utf-8', signal);
|
|
73
|
+
let payload;
|
|
74
|
+
try {
|
|
75
|
+
payload = JSON.parse(text);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
throw sinaError(`Sina getStockName returned non-JSON for cate ${cate}`, 'sina_invalid_response');
|
|
79
|
+
}
|
|
80
|
+
if (!isRecord(payload) || !isRecord(payload.result))
|
|
81
|
+
throw sinaError(`Sina getStockName has no result object for cate ${cate}`, 'sina_invalid_response');
|
|
82
|
+
const status = payload.result.status;
|
|
83
|
+
if (!isRecord(status) || Number(status.code) !== 0) {
|
|
84
|
+
const detail = isRecord(status) ? String(status.msg ?? status.code ?? 'unknown') : 'missing status';
|
|
85
|
+
throw sinaError(`Sina getStockName failed for cate ${cate}: ${detail}`, 'sina_upstream_error');
|
|
86
|
+
}
|
|
87
|
+
const data = payload.result.data;
|
|
88
|
+
if (!isRecord(data) || !Array.isArray(data.contractMonth))
|
|
89
|
+
throw sinaError(`Sina getStockName has no contractMonth list for cate ${cate}`, 'sina_invalid_response');
|
|
90
|
+
const months = [];
|
|
91
|
+
for (const entry of data.contractMonth) {
|
|
92
|
+
if (typeof entry !== 'string' || !/^\d{4}-(0[1-9]|1[0-2])$/.test(entry)) {
|
|
93
|
+
throw sinaError(`Sina getStockName returned an unreadable contract month ${JSON.stringify(entry)} for cate ${cate}`, 'sina_invalid_response');
|
|
94
|
+
}
|
|
95
|
+
const month = `${entry.slice(2, 4)}${entry.slice(5, 7)}`;
|
|
96
|
+
if (!months.includes(month))
|
|
97
|
+
months.push(month);
|
|
98
|
+
}
|
|
99
|
+
if (months.length === 0)
|
|
100
|
+
throw sinaError(`Sina getStockName returned no contract month for cate ${cate}`, 'sina_invalid_response');
|
|
101
|
+
months.sort();
|
|
102
|
+
return { months, stockId: typeof data.stockId === 'string' ? data.stockId : '' };
|
|
103
|
+
}
|
|
104
|
+
function assertKnown(values, allowed) {
|
|
105
|
+
for (const key of Object.keys(values))
|
|
106
|
+
if (!allowed.includes(key))
|
|
107
|
+
throw new Error(`unsupported parameter: ${key}`);
|
|
108
|
+
}
|
|
109
|
+
function optionCodes(value) {
|
|
110
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
111
|
+
throw new Error('parameter codes must be a non-empty array of option contract codes');
|
|
112
|
+
if (value.length > MAX_CODES)
|
|
113
|
+
throw new Error(`parameter codes accepts at most ${MAX_CODES} contract codes`);
|
|
114
|
+
const codes = [];
|
|
115
|
+
for (const entry of value) {
|
|
116
|
+
if (typeof entry !== 'string')
|
|
117
|
+
throw new Error('parameter codes must contain strings (for example 10012639 or CON_OP_10012639)');
|
|
118
|
+
const digits = entry.trim().replace(/^CON_OP_/iu, '');
|
|
119
|
+
if (!/^\d{8,12}$/.test(digits))
|
|
120
|
+
throw new Error(`parameter codes has an unreadable option contract code ${JSON.stringify(entry)}: pass the digits (10012639) or CON_OP_<digits>; 6-digit ETF codes such as 510050 are not option contracts`);
|
|
121
|
+
if (!codes.includes(digits))
|
|
122
|
+
codes.push(digits);
|
|
123
|
+
}
|
|
124
|
+
return codes;
|
|
125
|
+
}
|
|
126
|
+
function normalizeCodes(params) {
|
|
127
|
+
if (!isRecord(params))
|
|
128
|
+
throw new Error('params must be an object');
|
|
129
|
+
return { codes: optionCodes(params.codes) };
|
|
130
|
+
}
|
|
131
|
+
function normalizeContracts(params) {
|
|
132
|
+
if (!isRecord(params))
|
|
133
|
+
throw new Error('params must be an object');
|
|
134
|
+
const underlying = String(params.underlying ?? '').trim();
|
|
135
|
+
const side = String(params.side ?? '').trim().toLowerCase();
|
|
136
|
+
if (!UNDERLYING_LIST.includes(underlying))
|
|
137
|
+
throw new Error(`parameter underlying must be one of ${UNDERLYING_LIST.join(', ')}`);
|
|
138
|
+
if (!SIDE_LIST.includes(side))
|
|
139
|
+
throw new Error(`parameter side must be call or put`);
|
|
140
|
+
return { underlying, side };
|
|
141
|
+
}
|
|
142
|
+
const TQUOTE_KEYS = ['name', 'bid_vol', 'bid', 'last', 'ask', 'ask_vol', 'open_interest', 'pct', 'strike', 'prev_close', 'open', 'limit_up', 'limit_down', 'prev_settle', 'amplitude', 'high', 'low', 'volume', 'amount'];
|
|
143
|
+
const GREEKS_KEYS = ['name', 'volume', 'delta', 'gamma', 'theta', 'vega', 'iv', 'high', 'low', 'trade_code', 'strike', 'last', 'theory'];
|
|
144
|
+
const CONTRACT_KEYS = ['underlying', 'side', 'month', 'contract_code', 'contract_symbol'];
|
|
145
|
+
const A50_KEYS = ['name', 'datetime', 'last', 'bid', 'ask', 'high', 'low', 'open', 'prev_settle', 'change', 'change_pct'];
|
|
146
|
+
const numberOrNullSchema = { oneOf: [{ type: 'number' }, { type: 'null' }] };
|
|
147
|
+
function rowSchema(properties, required) {
|
|
148
|
+
return { type: 'object', properties, required, additionalProperties: false };
|
|
149
|
+
}
|
|
150
|
+
const contractsOutput = {
|
|
151
|
+
type: 'array',
|
|
152
|
+
items: rowSchema({
|
|
153
|
+
underlying: { type: 'string', enum: UNDERLYING_LIST },
|
|
154
|
+
side: { type: 'string', enum: SIDE_LIST },
|
|
155
|
+
month: { type: 'string' },
|
|
156
|
+
contract_code: { type: 'string' },
|
|
157
|
+
contract_symbol: { type: 'string' },
|
|
158
|
+
}, CONTRACT_KEYS),
|
|
159
|
+
};
|
|
160
|
+
const tquoteOutput = {
|
|
161
|
+
type: 'array',
|
|
162
|
+
items: rowSchema({
|
|
163
|
+
name: { type: 'string' },
|
|
164
|
+
bid_vol: numberOrNullSchema,
|
|
165
|
+
bid: numberOrNullSchema,
|
|
166
|
+
last: numberOrNullSchema,
|
|
167
|
+
ask: numberOrNullSchema,
|
|
168
|
+
ask_vol: numberOrNullSchema,
|
|
169
|
+
open_interest: numberOrNullSchema,
|
|
170
|
+
pct: numberOrNullSchema,
|
|
171
|
+
strike: numberOrNullSchema,
|
|
172
|
+
prev_close: numberOrNullSchema,
|
|
173
|
+
open: numberOrNullSchema,
|
|
174
|
+
limit_up: numberOrNullSchema,
|
|
175
|
+
limit_down: numberOrNullSchema,
|
|
176
|
+
prev_settle: numberOrNullSchema,
|
|
177
|
+
amplitude: numberOrNullSchema,
|
|
178
|
+
high: numberOrNullSchema,
|
|
179
|
+
low: numberOrNullSchema,
|
|
180
|
+
volume: numberOrNullSchema,
|
|
181
|
+
amount: numberOrNullSchema,
|
|
182
|
+
}, TQUOTE_KEYS),
|
|
183
|
+
};
|
|
184
|
+
const greeksOutput = {
|
|
185
|
+
type: 'array',
|
|
186
|
+
items: rowSchema({
|
|
187
|
+
name: { type: 'string' },
|
|
188
|
+
volume: numberOrNullSchema,
|
|
189
|
+
delta: numberOrNullSchema,
|
|
190
|
+
gamma: numberOrNullSchema,
|
|
191
|
+
theta: numberOrNullSchema,
|
|
192
|
+
vega: numberOrNullSchema,
|
|
193
|
+
iv: numberOrNullSchema,
|
|
194
|
+
high: numberOrNullSchema,
|
|
195
|
+
low: numberOrNullSchema,
|
|
196
|
+
trade_code: { type: 'string' },
|
|
197
|
+
strike: numberOrNullSchema,
|
|
198
|
+
last: numberOrNullSchema,
|
|
199
|
+
theory: numberOrNullSchema,
|
|
200
|
+
}, GREEKS_KEYS),
|
|
201
|
+
};
|
|
202
|
+
const a50Output = {
|
|
203
|
+
type: 'array',
|
|
204
|
+
items: rowSchema({
|
|
205
|
+
name: { type: 'string' },
|
|
206
|
+
datetime: { type: 'string' },
|
|
207
|
+
last: { type: 'number' },
|
|
208
|
+
bid: numberOrNullSchema,
|
|
209
|
+
ask: numberOrNullSchema,
|
|
210
|
+
high: numberOrNullSchema,
|
|
211
|
+
low: numberOrNullSchema,
|
|
212
|
+
open: numberOrNullSchema,
|
|
213
|
+
prev_settle: numberOrNullSchema,
|
|
214
|
+
change: numberOrNullSchema,
|
|
215
|
+
change_pct: numberOrNullSchema,
|
|
216
|
+
}, A50_KEYS),
|
|
217
|
+
};
|
|
218
|
+
function parseTquoteRow(code, body) {
|
|
219
|
+
const context = `CON_OP_${code}`;
|
|
220
|
+
if (body.trim() === '')
|
|
221
|
+
throw sinaError(`Sina ${context} quote is empty: unknown or delisted option contract`, 'sina_empty_quote');
|
|
222
|
+
const v = body.split(',');
|
|
223
|
+
if (v.length < TQUOTE_MIN_FIELDS)
|
|
224
|
+
throw sinaError(`Sina ${context} quote has ${v.length} columns, expected at least ${TQUOTE_MIN_FIELDS}`, 'sina_invalid_response');
|
|
225
|
+
const name = (v[37] ?? '').trim();
|
|
226
|
+
if (name === '')
|
|
227
|
+
throw sinaError(`Sina ${context} quote has an empty contract name at column 38`, 'sina_invalid_response');
|
|
228
|
+
const row = {
|
|
229
|
+
name,
|
|
230
|
+
bid_vol: sinaNumber(v[0], 'bid_vol', context),
|
|
231
|
+
bid: sinaNumber(v[1], 'bid', context),
|
|
232
|
+
last: sinaNumber(v[2], 'last', context),
|
|
233
|
+
ask: sinaNumber(v[3], 'ask', context),
|
|
234
|
+
ask_vol: sinaNumber(v[4], 'ask_vol', context),
|
|
235
|
+
open_interest: sinaNumber(v[5], 'open_interest', context),
|
|
236
|
+
pct: sinaNumber(v[6], 'pct', context),
|
|
237
|
+
strike: sinaNumber(v[7], 'strike', context),
|
|
238
|
+
prev_close: sinaNumber(v[8], 'prev_close', context),
|
|
239
|
+
open: sinaNumber(v[9], 'open', context),
|
|
240
|
+
limit_up: sinaNumber(v[10], 'limit_up', context),
|
|
241
|
+
limit_down: sinaNumber(v[11], 'limit_down', context),
|
|
242
|
+
prev_settle: sinaNumber(v[44], 'prev_settle', context),
|
|
243
|
+
amplitude: sinaNumber(v[38], 'amplitude', context),
|
|
244
|
+
high: sinaNumber(v[39], 'high', context),
|
|
245
|
+
low: sinaNumber(v[40], 'low', context),
|
|
246
|
+
volume: sinaNumber(v[41], 'volume', context),
|
|
247
|
+
amount: sinaNumber(v[42], 'amount', context),
|
|
248
|
+
};
|
|
249
|
+
// pct is quoted against the previous settlement (column 45), not prev_close (column 9); amplitude
|
|
250
|
+
// against prev_close. A mismatch here means the columns moved, so the whole batch fails.
|
|
251
|
+
const { pct, last, prev_settle: prevSettle } = row;
|
|
252
|
+
if (typeof pct === 'number' && typeof last === 'number' && typeof prevSettle === 'number' && last > 0 && prevSettle > 0) {
|
|
253
|
+
const expected = ((last - prevSettle) / prevSettle) * 100;
|
|
254
|
+
if (Math.abs(pct - expected) > PCT_TOLERANCE) {
|
|
255
|
+
throw sinaError(`Sina ${context} pct ${pct} contradicts (last - prev_settle) / prev_settle = ${expected.toFixed(2)}%: the column layout changed`, 'sina_invalid_response');
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return row;
|
|
259
|
+
}
|
|
260
|
+
function parseGreeksRow(code, body) {
|
|
261
|
+
const context = `CON_SO_${code}`;
|
|
262
|
+
if (body.trim() === '')
|
|
263
|
+
throw sinaError(`Sina ${context} quote is empty: unknown or delisted option contract`, 'sina_empty_quote');
|
|
264
|
+
const raw = body.split(',');
|
|
265
|
+
if (raw.length < GREEKS_MIN_FIELDS)
|
|
266
|
+
throw sinaError(`Sina ${context} quote has ${raw.length} columns, expected at least ${GREEKS_MIN_FIELDS}`, 'sina_invalid_response');
|
|
267
|
+
if ((raw[1] ?? '') !== '' || (raw[2] ?? '') !== '' || (raw[3] ?? '') !== '') {
|
|
268
|
+
throw sinaError(`Sina ${context} placeholders raw[1..3] are no longer blank (${JSON.stringify(raw.slice(1, 4))}): the [raw[0]] + raw[4:] shift would misalign delta..iv`, 'sina_invalid_response');
|
|
269
|
+
}
|
|
270
|
+
const v = [raw[0], ...raw.slice(4)];
|
|
271
|
+
const name = (v[0] ?? '').trim();
|
|
272
|
+
if (name === '')
|
|
273
|
+
throw sinaError(`Sina ${context} quote has an empty contract name`, 'sina_invalid_response');
|
|
274
|
+
const tradeCode = (v[9] ?? '').trim();
|
|
275
|
+
if (tradeCode === '')
|
|
276
|
+
throw sinaError(`Sina ${context} quote has an empty trade code at column 10`, 'sina_invalid_response');
|
|
277
|
+
return {
|
|
278
|
+
name,
|
|
279
|
+
volume: sinaNumber(v[1], 'volume', context),
|
|
280
|
+
delta: sinaNumber(v[2], 'delta', context),
|
|
281
|
+
gamma: sinaNumber(v[3], 'gamma', context),
|
|
282
|
+
theta: sinaNumber(v[4], 'theta', context),
|
|
283
|
+
vega: sinaNumber(v[5], 'vega', context),
|
|
284
|
+
iv: sinaNumber(v[6], 'iv', context),
|
|
285
|
+
high: sinaNumber(v[7], 'high', context),
|
|
286
|
+
low: sinaNumber(v[8], 'low', context),
|
|
287
|
+
trade_code: tradeCode,
|
|
288
|
+
strike: sinaNumber(v[10], 'strike', context),
|
|
289
|
+
last: sinaNumber(v[11], 'last', context),
|
|
290
|
+
theory: sinaNumber(v[12], 'theory', context),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function primitive(value) {
|
|
294
|
+
return value === null || typeof value === 'string' || (typeof value === 'number' && Number.isFinite(value));
|
|
295
|
+
}
|
|
296
|
+
function rowGuard(keys, allowEmpty) {
|
|
297
|
+
return (data) => Array.isArray(data)
|
|
298
|
+
&& (allowEmpty || data.length > 0)
|
|
299
|
+
&& data.every((row) => isRecord(row) && Object.keys(row).length === keys.length && keys.every((key) => Object.hasOwn(row, key) && primitive(row[key])));
|
|
300
|
+
}
|
|
301
|
+
function createSource(options) {
|
|
302
|
+
const schema = {
|
|
303
|
+
capability: options.capability,
|
|
304
|
+
time_contract: getDataTimeContract(options.capability),
|
|
305
|
+
name: options.name,
|
|
306
|
+
source: `http:sina.${options.capability}`,
|
|
307
|
+
data_key: buildDataKey('sina', 'http', options.capability),
|
|
308
|
+
source_label: 'sina',
|
|
309
|
+
paginated: false,
|
|
310
|
+
cacheMaxAgeMs: options.cacheMaxAgeMs,
|
|
311
|
+
rowShape: { rootArray: true },
|
|
312
|
+
summary: options.summary,
|
|
313
|
+
description: options.description,
|
|
314
|
+
input_schema: options.inputSchema,
|
|
315
|
+
output_schema: options.outputSchema,
|
|
316
|
+
};
|
|
317
|
+
const normalize = (params) => {
|
|
318
|
+
if (!isRecord(params))
|
|
319
|
+
throw new Error('params must be an object');
|
|
320
|
+
assertKnown(params, options.allowed);
|
|
321
|
+
return options.normalize(params);
|
|
322
|
+
};
|
|
323
|
+
const guard = rowGuard(options.keys, options.allowedEmpty);
|
|
324
|
+
return {
|
|
325
|
+
schema,
|
|
326
|
+
normalizeParams: normalize,
|
|
327
|
+
validateOutput: (data) => guard(data) && (options.singleRow !== true || (Array.isArray(data) && data.length === 1)),
|
|
328
|
+
execute: async (request, signal) => options.execute(normalize(request.params), signal),
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
const codesInput = {
|
|
332
|
+
type: 'object',
|
|
333
|
+
properties: {
|
|
334
|
+
codes: { type: 'array', minItems: 1, maxItems: MAX_CODES, items: { type: 'string', description: '期权合约代码:10012639 或 CON_OP_10012639' } },
|
|
335
|
+
},
|
|
336
|
+
required: ['codes'],
|
|
337
|
+
additionalProperties: false,
|
|
338
|
+
};
|
|
339
|
+
const contractsInput = {
|
|
340
|
+
type: 'object',
|
|
341
|
+
properties: {
|
|
342
|
+
underlying: { type: 'string', enum: UNDERLYING_LIST, description: '期权标的 ETF 代码' },
|
|
343
|
+
side: { type: 'string', enum: SIDE_LIST, description: 'call 认购 / put 认沽' },
|
|
344
|
+
},
|
|
345
|
+
required: ['underlying', 'side'],
|
|
346
|
+
additionalProperties: false,
|
|
347
|
+
};
|
|
348
|
+
async function executeContracts(params, signal) {
|
|
349
|
+
const underlying = String(params.underlying);
|
|
350
|
+
const side = String(params.side);
|
|
351
|
+
const cate = OPTION_UNDERLYINGS[underlying];
|
|
352
|
+
const flag = OPTION_SIDES[side];
|
|
353
|
+
const meta = await fetchContractMonths(cate, signal);
|
|
354
|
+
if (meta.stockId !== '' && meta.stockId !== underlying) {
|
|
355
|
+
throw sinaError(`Sina getStockName cate=${cate} resolved to ${meta.stockId}, expected ${underlying}: the cate table changed`, 'sina_invalid_response');
|
|
356
|
+
}
|
|
357
|
+
const rows = [];
|
|
358
|
+
for (const month of meta.months) {
|
|
359
|
+
const listKey = `${flag}${underlying}${month}`;
|
|
360
|
+
const variables = await fetchHqLists([listKey], signal);
|
|
361
|
+
const body = variables.get(listKey) ?? '';
|
|
362
|
+
if (body.trim() === '')
|
|
363
|
+
continue; // a month with no listed contract is a legal empty, not a failure
|
|
364
|
+
for (const token of body.split(',')) {
|
|
365
|
+
const trimmed = token.trim();
|
|
366
|
+
if (trimmed === '')
|
|
367
|
+
continue;
|
|
368
|
+
if (!/^CON_OP_\d+$/.test(trimmed))
|
|
369
|
+
throw sinaError(`Sina ${listKey} returned an unexpected contract token ${JSON.stringify(trimmed)}`, 'sina_invalid_response');
|
|
370
|
+
rows.push({ underlying, side, month, contract_code: trimmed.slice(7), contract_symbol: listKey });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return { data: rows, schema: contractsOutput };
|
|
374
|
+
}
|
|
375
|
+
async function executeTquote(params, signal) {
|
|
376
|
+
const codes = params.codes;
|
|
377
|
+
const variables = await fetchHqLists(codes.map((code) => `CON_OP_${code}`), signal);
|
|
378
|
+
return { data: codes.map((code) => parseTquoteRow(code, variables.get(`CON_OP_${code}`) ?? '')), schema: tquoteOutput };
|
|
379
|
+
}
|
|
380
|
+
async function executeGreeks(params, signal) {
|
|
381
|
+
const codes = params.codes;
|
|
382
|
+
const variables = await fetchHqLists(codes.map((code) => `CON_SO_${code}`), signal);
|
|
383
|
+
return { data: codes.map((code) => parseGreeksRow(code, variables.get(`CON_SO_${code}`) ?? '')), schema: greeksOutput };
|
|
384
|
+
}
|
|
385
|
+
async function executeA50(_params, signal) {
|
|
386
|
+
const variables = await fetchHqLists([A50_SYMBOL], signal);
|
|
387
|
+
const body = variables.get(A50_SYMBOL) ?? '';
|
|
388
|
+
const context = A50_SYMBOL;
|
|
389
|
+
if (body.trim() === '')
|
|
390
|
+
throw sinaError(`Sina ${context} quote is empty: the A50 CFD symbol probably moved`, 'sina_empty_quote');
|
|
391
|
+
const v = body.split(',');
|
|
392
|
+
if (v.length < A50_MIN_FIELDS)
|
|
393
|
+
throw sinaError(`Sina ${context} quote has ${v.length} columns, expected at least ${A50_MIN_FIELDS}`, 'sina_invalid_response');
|
|
394
|
+
const date = (v[12] ?? '').trim();
|
|
395
|
+
const clock = (v[6] ?? '').trim();
|
|
396
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(date))
|
|
397
|
+
throw sinaError(`Sina ${context} has an unreadable date ${JSON.stringify(date)}`, 'sina_invalid_response');
|
|
398
|
+
if (!/^\d{2}:\d{2}:\d{2}$/.test(clock))
|
|
399
|
+
throw sinaError(`Sina ${context} has an unreadable time ${JSON.stringify(clock)}`, 'sina_invalid_response');
|
|
400
|
+
const name = (v[13] ?? '').trim();
|
|
401
|
+
if (name === '')
|
|
402
|
+
throw sinaError(`Sina ${context} has an empty contract name`, 'sina_invalid_response');
|
|
403
|
+
const last = sinaNumber(v[0], 'last', context);
|
|
404
|
+
if (last === null)
|
|
405
|
+
throw sinaError(`Sina ${context} last price is empty`, 'sina_invalid_response');
|
|
406
|
+
const prevSettle = sinaNumber(v[7], 'prev_settle', context);
|
|
407
|
+
const change = prevSettle === null || prevSettle === 0 ? null : round4(last - prevSettle);
|
|
408
|
+
const changePct = prevSettle === null || prevSettle === 0 ? null : round4(((last - prevSettle) / prevSettle) * 100);
|
|
409
|
+
const row = {
|
|
410
|
+
name,
|
|
411
|
+
datetime: `${date} ${clock}`,
|
|
412
|
+
last,
|
|
413
|
+
bid: sinaNumber(v[2], 'bid', context),
|
|
414
|
+
ask: sinaNumber(v[3], 'ask', context),
|
|
415
|
+
high: sinaNumber(v[4], 'high', context),
|
|
416
|
+
low: sinaNumber(v[5], 'low', context),
|
|
417
|
+
open: sinaNumber(v[8], 'open', context),
|
|
418
|
+
prev_settle: prevSettle,
|
|
419
|
+
change,
|
|
420
|
+
change_pct: changePct,
|
|
421
|
+
};
|
|
422
|
+
return { data: [row], schema: a50Output };
|
|
423
|
+
}
|
|
424
|
+
export function createSinaSources() {
|
|
425
|
+
return [
|
|
426
|
+
createSource({
|
|
427
|
+
capability: 'sina_etf_option_contracts',
|
|
428
|
+
cacheMaxAgeMs: 300_000,
|
|
429
|
+
name: 'get_sina_etf_option_contracts',
|
|
430
|
+
summary: '新浪 ETF 期权合约清单(标的×月份×认购/认沽)',
|
|
431
|
+
description: '列出上交所四只 ETF(510050 上证50ETF、510300 沪深300ETF、588000 科创50ETF、510500 中证500ETF)当前挂牌的期权合约,按标的 × side(call 认购 / put 认沽)× 月份一行一合约;underlying、side 必填。月份取自新浪 StockOptionService.getStockName 的 contractMonth,转 YYMM(2612 = 2026-12)后按值去重、升序输出,即近月在前:实测 2026-11-22 返回 ["2026-11","2026-11","2026-12","2027-03","2027-06"],首个 2026-11 是重复项而不是额外的已到期月(去重后为 2611/2612/2703/2706 四个挂牌月),所以本能力按值去重、不照抄上游「丢掉第一个月」的写法;已到期的 2610/2609 不在该列表内(虽仍能用 OP_UP_<code> 取到历史合约)。month 是 YYMM 字符串,与列表名同串。contract_code 是新浪数字合约号(剥掉 CON_OP_ 前缀,如 10012639,可直接喂给 T 型报价 / 希腊字母能力),contract_symbol 是该行来源的 hq 列表名(如 OP_UP_5100502612),供逐行核对。⛔ 本能力不含合约简称与行权价(中证500ETF 等有分红调整合约:简称带 A 后缀、行权价非整数),要用 T 型报价能力取。某个月列表为空是合法空(该月无挂牌合约),整月跳过;列表非空但出现非 CON_OP_<数字> 元素、或 hq 回包缺少该月变量,都按 sina_invalid_response 失败。',
|
|
432
|
+
inputSchema: contractsInput,
|
|
433
|
+
outputSchema: contractsOutput,
|
|
434
|
+
allowed: ['underlying', 'side'],
|
|
435
|
+
normalize: normalizeContracts,
|
|
436
|
+
allowedEmpty: true,
|
|
437
|
+
keys: CONTRACT_KEYS,
|
|
438
|
+
execute: executeContracts,
|
|
439
|
+
}),
|
|
440
|
+
createSource({
|
|
441
|
+
capability: 'sina_etf_option_tquote',
|
|
442
|
+
cacheMaxAgeMs: 30_000,
|
|
443
|
+
name: 'get_sina_etf_option_tquote',
|
|
444
|
+
summary: '新浪 ETF 期权 T 型报价(盘口/持仓/涨跌/振幅)',
|
|
445
|
+
description: '按合约代码批量取新浪 ETF 期权 T 型报价(hq 的 CON_OP_ 行情),一次 1..20 个代码;codes 接受纯数字 10012639 或带前缀 CON_OP_10012639(归一成纯数字并去重;纯数字须为 8~12 位,510050 这类 6 位 ETF 代码不是期权合约,会被参数层拒绝),输出行顺序 = 请求 codes 顺序,用 name(如 50ETF购12月2650)对回合约。单位:bid_vol/ask_vol/open_interest/volume 是张(1 张 = 10000 份 ETF),amount 是元,价格类是每份元;amount / (volume × 10000) 就是成交均价(实测 2026-11-22 的 300ETF购12月3600:2495304 / (702×10000) = 0.3555,落在当日 high 0.3754 / low 0.3388 之间)。pct 与 amplitude 都是百分数(-15.88 表示 -15.88%,不是比例),⛔ 但两者基准不同:pct = (last − prev_settle) / prev_settle × 100,基准是昨结算价(交易所逐日结算,和 prev_close 是两个数;深实值合约的结算价常贴着内在价值,与昨收能差出十几个百分点,所以 pct 大幅为负不代表昨收也跌了);amplitude = (high − low) / prev_close × 100,基准是 prev_close(昨收)。实测同一行 last 0.3585、prev_close 0.4189、prev_settle 0.4600、pct −22.07、amplitude 8.74:用 prev_close 算涨跌只有 −14.4%,所以不要用 pct 反推 prev_close,也不要拿 prev_close 复核 pct。0 是真实值(无申报/无成交/持仓为 0),空串才返回 null;bid/ask/limit_down 为 0 很常见(无对手价、跌停价为最小变动价位)。⛔ 上游报文 51 列、下标 44 是昨结算价、下标 8 是昨收;列数 < 45、name 为空、或 pct 与 (last−prev_settle)/prev_settle×100 相差超过 0.1 个百分点,一律 sina_invalid_response 响亮失败,不返回空行;请求里的合约不存在(新浪回空内容)报 sina_empty_quote。行内不含行情时间(该列对多数合约是 00:00:00),判断新鲜度用 Dataset 的 captured_at。',
|
|
446
|
+
inputSchema: codesInput,
|
|
447
|
+
outputSchema: tquoteOutput,
|
|
448
|
+
allowed: ['codes'],
|
|
449
|
+
normalize: normalizeCodes,
|
|
450
|
+
allowedEmpty: false,
|
|
451
|
+
keys: TQUOTE_KEYS,
|
|
452
|
+
execute: executeTquote,
|
|
453
|
+
}),
|
|
454
|
+
createSource({
|
|
455
|
+
capability: 'sina_etf_option_greeks',
|
|
456
|
+
cacheMaxAgeMs: 30_000,
|
|
457
|
+
name: 'get_sina_etf_option_greeks',
|
|
458
|
+
summary: '新浪 ETF 期权希腊字母与隐含波动率',
|
|
459
|
+
description: '按合约代码批量取新浪期权扩展行情(hq 的 CON_SO_):希腊字母 + 隐含波动率 + 理论价值,一次 1..20 个代码,codes 规则与行顺序同 T 型报价。字段:name(合约简称)、volume(成交量,张)、delta/gamma/theta/vega(新浪侧算好的原始值,本层不换算,也不反推)、iv(隐含波动率,小数:0.2244 = 22.44%,不是百分数)、high/low(当日权利金最高/最低,与 T 型报价同值)、trade_code(交易所交易代码如 510050C2612M02650,可对回行情软件)、strike(行权价,元/份标的)、last(最新价,元/份)、theory(新浪理论价值,元/份)。⛔ 上游 CON_SO_ 报文名后紧跟 3 个空串(价值状态/内在价值/时间价值,页面自己算,不下发),必须按 [raw[0]] + raw[4:] 取值;实测 2026-11-22 是 17 列:名、3 空串、volume、delta、gamma、theta、vega、iv、high、low、trade_code、strike、last、theory、市场标识。⛔ 列数 < 16 或 raw[1..3] 不再全为空串,都按 sina_invalid_response 响亮失败——后者说明上游改版,照抄错位映射会把 delta/IV 整列搬错。iv 在权利金低于内在价值时被压到下限(实测 0.0007,不是真实波动率);深实值认沽 theta 可以为正,不是符号错误。0 是真实值,空串才是 null。基金有分红调整合约(简称带 A、行权价非整数)时行权价按交易所调整后口径,跨合约比较行权价前先看简称。',
|
|
460
|
+
inputSchema: codesInput,
|
|
461
|
+
outputSchema: greeksOutput,
|
|
462
|
+
allowed: ['codes'],
|
|
463
|
+
normalize: normalizeCodes,
|
|
464
|
+
allowedEmpty: false,
|
|
465
|
+
keys: GREEKS_KEYS,
|
|
466
|
+
execute: executeGreeks,
|
|
467
|
+
}),
|
|
468
|
+
createSource({
|
|
469
|
+
capability: 'sina_a50_futures',
|
|
470
|
+
cacheMaxAgeMs: 30_000,
|
|
471
|
+
name: 'get_sina_a50_futures',
|
|
472
|
+
summary: '富时中国 A50 CFD 连续报价(新浪)',
|
|
473
|
+
description: '富时中国 A50 指数期货 CFD 的连续报价(新浪 hf_CHA50CFD,单行),盘前与夜盘看外资对 A 股的风险偏好用;无参数。⛔ 这是 CFD 连续行情,不是某个具体月份的期货合约:不承诺合约月份、交易所结算价、持仓量、成交额;也不含 CME 的 ES/NQ/YM/RTY(那四个不在本能力范围),不要把它说成 CME 期货或国内股指期货。字段:last/open/high/low/bid/ask(指数点),prev_settle(新浪外盘行情页把 hf_ 行第 8 列标成「昨日结算价」,CFD 没有官方结算价,把它当上一交易日参考价即可,本能力不承诺它是结算价),change/change_pct 由本层按 last − prev_settle 现算(change_pct 是百分数:-0.85 表示 -0.85%,四舍五入到 4 位小数;prev_settle 缺失或为 0 时两者为 null)。⛔ 时间轴:datetime 是 hf_ 行的日期 + 时间,北京时间(UTC+8)墙钟,不是交易所当地时间——实测用 hf_GC 交叉验证:报文 2026-11-21 05:59:57 对应 COMEX 黄金 16:59:57 EST 收盘,即北京 05:59;这只品种的交易时钟就是 UTC+8(与北京同步),所以『北京时间 vs 交易所时间』在它身上无法区分;别把这个结论推广到其他 hf_ 品种。夜盘跨零点:该品种夜盘收在次日 05:15 前后(北京),周五夜盘最后一笔就显示成 2026-11-21 05:14:59(周六),所以 datetime 的日期不是交易日,周末/假期抓到的都是上一场收盘快照。⛔ 没有 hq_str 变量、列数 < 14、日期或时间格式变了、last 为空、name 为空,一律 sina_invalid_response / sina_empty_quote 失败,不返回伪造行;原始报文第 15 列(实测 141730)语义无法从新浪公开栏位确认,已丢弃。',
|
|
474
|
+
inputSchema: { type: 'object', properties: {}, required: [], additionalProperties: false },
|
|
475
|
+
outputSchema: a50Output,
|
|
476
|
+
allowed: [],
|
|
477
|
+
normalize: () => ({}),
|
|
478
|
+
allowedEmpty: false,
|
|
479
|
+
singleRow: true,
|
|
480
|
+
keys: A50_KEYS,
|
|
481
|
+
execute: executeA50,
|
|
482
|
+
}),
|
|
483
|
+
];
|
|
484
|
+
}
|
|
@@ -57,12 +57,23 @@ const klineOutput = {
|
|
|
57
57
|
high: { type: 'number' },
|
|
58
58
|
low: { type: 'number' },
|
|
59
59
|
close: { type: 'number' },
|
|
60
|
-
volume: { type: 'number' },
|
|
60
|
+
volume: { type: 'number', description: '成交量;单位见 volume_unit(科创板是股,其余是手,不是同一量纲)' },
|
|
61
|
+
volume_unit: { enum: ['lot', 'share'], description: '科创板(688/689)为 share,其余(含 ETF 588、指数)为 lot' },
|
|
61
62
|
turnover_rate_pct: { oneOf: [{ type: 'number' }, { type: 'null' }] },
|
|
62
63
|
},
|
|
63
64
|
additionalProperties: true,
|
|
64
65
|
},
|
|
65
66
|
};
|
|
67
|
+
/**
|
|
68
|
+
* Tencent reports K-line volume in 股 for 科创板 (688/689) and in 手 for every other instrument —
|
|
69
|
+
* main board, 创业板, B 股, ETF (including the 科创板 ETFs 588xxx), LOF, REITs and indices. Same rule
|
|
70
|
+
* for daily/weekly/monthly and minute bars, adjusted or not. Upstream v3.10.1 documented this after
|
|
71
|
+
* counting against the TDX post-close package (shares); the K-line docs used to say 手 for all, which
|
|
72
|
+
* makes 科创板 volume come out 100× too small when converted.
|
|
73
|
+
*/
|
|
74
|
+
function volumeUnitOf(canonical) {
|
|
75
|
+
return /^68[89]/u.test(canonical.slice(0, 6)) ? 'share' : 'lot';
|
|
76
|
+
}
|
|
66
77
|
const ticksOutput = {
|
|
67
78
|
type: 'object',
|
|
68
79
|
properties: {
|
|
@@ -248,6 +259,7 @@ function parseKlineRows(data, symbol, canonical, period, adjust, start, end) {
|
|
|
248
259
|
throw sourceError(`Tencent K-line ${symbol} returned empty ${key} beside non-empty raw ${period}`, 'tencent_invalid_response');
|
|
249
260
|
}
|
|
250
261
|
const rows = [];
|
|
262
|
+
const volume_unit = volumeUnitOf(canonical);
|
|
251
263
|
for (const item of items) {
|
|
252
264
|
if (!Array.isArray(item) || item.length < 6)
|
|
253
265
|
throw sourceError(`Tencent K-line ${symbol} row shape changed`, 'tencent_invalid_response');
|
|
@@ -264,6 +276,7 @@ function parseKlineRows(data, symbol, canonical, period, adjust, start, end) {
|
|
|
264
276
|
low: requiredNumber(item[4], 'low'),
|
|
265
277
|
close: requiredNumber(item[2], 'close'),
|
|
266
278
|
volume: requiredNumber(item[5], 'volume'),
|
|
279
|
+
volume_unit,
|
|
267
280
|
});
|
|
268
281
|
}
|
|
269
282
|
return rows;
|
|
@@ -283,6 +296,7 @@ async function executeKline(params, signal) {
|
|
|
283
296
|
throw sourceError(`Tencent minute K-line ${symbol} response has no ${period} array`, 'tencent_invalid_response');
|
|
284
297
|
const rows = [];
|
|
285
298
|
const seen = new Set();
|
|
299
|
+
const volume_unit = volumeUnitOf(code.canonical);
|
|
286
300
|
for (const item of node[period]) {
|
|
287
301
|
if (!Array.isArray(item) || item.length < 6)
|
|
288
302
|
throw sourceError(`Tencent minute K-line ${symbol} row shape changed`, 'tencent_invalid_response');
|
|
@@ -300,6 +314,7 @@ async function executeKline(params, signal) {
|
|
|
300
314
|
low: requiredNumber(item[4], 'low'),
|
|
301
315
|
close: requiredNumber(item[2], 'close'),
|
|
302
316
|
volume: requiredNumber(item[5], 'volume'),
|
|
317
|
+
volume_unit,
|
|
303
318
|
turnover_rate_pct: item.length > 7 ? (numberOrNull(item[7]) === null ? null : numberOrNull(item[7]) / 100) : null,
|
|
304
319
|
});
|
|
305
320
|
}
|
|
@@ -669,7 +684,10 @@ function parseUsPrepostRow(data, symbol, requested) {
|
|
|
669
684
|
reason: 'no_extended_session',
|
|
670
685
|
};
|
|
671
686
|
}
|
|
672
|
-
|
|
687
|
+
// Upstream labels the pre-market window `before`; the public contract is pre/after, so the alias
|
|
688
|
+
// is normalized here instead of leaking an untranslated tag into Dataset rows.
|
|
689
|
+
const rawTag = textOrNull(pandata.tag)?.toLowerCase();
|
|
690
|
+
const tag = rawTag === 'before' ? 'pre' : rawTag;
|
|
673
691
|
if (tag !== 'pre' && tag !== 'after') {
|
|
674
692
|
throw sourceError(`Tencent pre/post ${symbol} returned an unknown session tag ${JSON.stringify(pandata.tag ?? null)}`, 'tencent_invalid_response');
|
|
675
693
|
}
|
|
@@ -936,11 +954,8 @@ export async function fetchDailyBars(input, signal, count = 120) {
|
|
|
936
954
|
return { thscode, adjust: 'qfq', bars: bars(data).slice(-count), volume_unit: 'share' };
|
|
937
955
|
}
|
|
938
956
|
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
957
|
// 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:
|
|
958
|
+
return { thscode, adjust: 'qfq', bars: bars(data).slice(-count), volume_unit: volumeUnitOf(thscode) };
|
|
944
959
|
}
|
|
945
960
|
export function createTencentSources() {
|
|
946
961
|
return [
|
|
@@ -963,7 +978,7 @@ export function createTencentSources() {
|
|
|
963
978
|
cacheMaxAgeMs: 60_000,
|
|
964
979
|
name: 'get_tencent_kline',
|
|
965
980
|
summary: '腾讯日周月复权和分钟 K 线',
|
|
966
|
-
description: '获取单只 SH/SZ 股票、指数或 ETF 的腾讯 K 线。period 为 day/week/month 或 m1/m5/m15/m30/m60;日周月可用 qfq/hfq/none,分钟线只能 none;start/end 仅日周月支持 YYYY-MM-DD,count 日周月最多 640、分钟最多 320
|
|
981
|
+
description: '获取单只 SH/SZ 股票、指数或 ETF 的腾讯 K 线。period 为 day/week/month 或 m1/m5/m15/m30/m60;日周月可用 qfq/hfq/none,分钟线只能 none;start/end 仅日周月支持 YYYY-MM-DD,count 日周月最多 640、分钟最多 320。⛔ **成交量的单位分两套,行里用 `volume_unit` 显式标明**:科创板(688/689)是**股**(`share`),其余——沪深主板、创业板、B 股、ETF(含科创板 ETF 588xxx)、LOF、REITs、指数——是**手**(`lot`,1 手 = 100 股/份;本能力不覆盖可转债的 1 手 = 10 张)。日周月线、分钟线、复权与否都是同一规则;按「手」统一换算科创板会差 100 倍(上游 V3.10.1 用通达信盘后包逐股对账后确认,此前文档一律写「手」)。另:非科创板**最近一个交易日**的分钟量是整手、到下一个交易日才回填成精确股数(回填时点上游未测出),科创板分钟量当天就是精确股数——所以隔日的分钟合计与日线成交量会对不上一点点,那不是丢数据。⛔ **qfq/hfq 日线在盘后可能还没回填当日那根**(实测 2026-11-23 17:35:600519 的 qfqday 只到 11-20,同票 adjust=none 已有 11-23;688981 正常返回当日)——要当日收盘价请用 adjust:"none" 或 tencent_quote 快照,少一根是上游复权序列未回填,不是数据损坏。腾讯 K 线不提供成交额;分钟返回 turnover_rate_pct,腾讯原始字段为基点后除以 100。内部会按参考 skill 的三个腾讯入口轮换,但不把入口拆成 capability。BJ 代码明确拒绝。',
|
|
967
982
|
inputSchema: { type: 'object', properties: { code: { type: 'string', description: '单只完整证券代码,如 600519.SH' }, period: { type: 'string', enum: ['day', 'week', 'month', 'm1', 'm5', 'm15', 'm30', 'm60'] }, adjust: { type: 'string', enum: ['qfq', 'hfq', 'none'] }, start: { type: 'string', description: '日/周/月起始日期 YYYY-MM-DD' }, end: { type: 'string', description: '日/周/月结束日期 YYYY-MM-DD' }, count: { type: 'integer', minimum: 1, maximum: 640 } }, required: ['code'], additionalProperties: false },
|
|
968
983
|
outputSchema: klineOutput,
|
|
969
984
|
paginated: false,
|
|
@@ -1074,7 +1089,7 @@ export function createTencentSources() {
|
|
|
1074
1089
|
cacheMaxAgeMs: 30_000,
|
|
1075
1090
|
name: 'get_tencent_us_prepost_quote',
|
|
1076
1091
|
summary: '腾讯美股盘前/盘后最近成交(美东时间,含延迟标记)',
|
|
1077
|
-
description: '批量获取美股/ETF/ADR 的**盘前或盘后最近一笔成交**(一次最多 20 只;数据来自腾讯美股 K 线响应内嵌的 `pandata` 节点,普通的 `tencent_us_quote` 快照不透出延长时段)。返回 `session
|
|
1092
|
+
description: '批量获取美股/ETF/ADR 的**盘前或盘后最近一笔成交**(一次最多 20 只;数据来自腾讯美股 K 线响应内嵌的 `pandata` 节点,普通的 `tencent_us_quote` 快照不透出延长时段)。返回 `session`(上游盘前标签是 `before`、本层归一为 `pre`,盘后为 `after`;输出只有 pre / after 两值)、`price`、`change_amt`、`change_pct`、`volume_shares`、`session_time`(美东墙钟)与 `quote_time_utc`。⛔ **`prev_close` 不是昨收**:它是 `last − netchange` 得到的涨跌基准,盘后时等于当日正规收盘;昨收另在 `regular_prev_close`,正规时段最后价在 `regular_price`。⛔ **只保留最近一段延长时段**:上游一次只给一个 `pandata`,不能同时承诺盘前和盘后;没有延长成交(例如盘中)时返回 `available=false` + `reason=no_extended_session`,**不拿正规时段价格冒充**。⛔ 没有 high/low、没有盘前盘后成交量以外的明细;`pct` 是百分数原值(-0.24 表示 -0.24%),不是比例。`delay` 直接取腾讯快照第 0 位(`delay`),免费公开源**不承诺实时**。未知 ticker 返回单行 `available=false` + `reason=ticker_not_found`,不静默丢行;网络/形状错误仍整批失败。交易所后缀先由快照解析,再打 K 线,`tencent_symbol` 回显解析结果。',
|
|
1078
1093
|
inputSchema: { type: 'object', properties: { codes: { type: 'array', minItems: 1, maxItems: 20, items: { type: 'string', description: '美股 ticker,如 AAPL、BRK.B 或 BABA.N' } } }, required: ['codes'], additionalProperties: false },
|
|
1079
1094
|
outputSchema: usPrepostOutput,
|
|
1080
1095
|
paginated: false,
|