stock-analysis-plugin 0.2.8 → 0.2.10
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/README.md +34 -26
- package/package.json +15 -8
- package/pi/index.ts +446 -13
- package/scripts/profile-offline.py +127 -0
- package/scripts/profile-workflows.py +87 -0
- package/scripts/setup-python.mjs +11 -4
- package/scripts/venv-python.mjs +7 -0
- package/skills/event-driven/SKILL.md +2 -0
- package/skills/growth-quality/SKILL.md +2 -0
- package/skills/market-review/SKILL.md +1 -1
- package/skills/sector-rotation/SKILL.md +57 -0
- package/skills/sector-rotation/references/computation.md +83 -0
- package/skills/sector-rotation/references/strategy_rules.md +62 -0
- package/skills/sector-rotation/scripts/rotation_metrics.py +11 -0
- package/skills/stock-analysis/SKILL.md +15 -4
- package/skills/stock-analysis/references/host-workflows.md +45 -0
- package/skills/stock-analysis/references/stateless-analysis.md +16 -2
- package/skills/stock-screener/SKILL.md +6 -0
- package/skills/strategy-backtest/SKILL.md +10 -3
- package/tools/_cache.py +93 -21
- package/tools/_subproc.py +43 -8
- package/tools/anomaly_detect.py +11 -6
- package/tools/backtest.py +225 -70
- package/tools/capabilities.py +3 -0
- package/tools/cli_budgets.json +2 -1
- package/tools/gather.py +63 -8
- package/tools/market_regime.py +26 -15
- package/tools/market_review.py +2 -2
- package/tools/position_context.py +5 -2
- package/tools/report_renderer.py +4 -0
- package/tools/requirements.txt +2 -2
- package/tools/risk_screening.py +40 -15
- package/tools/screener.py +44 -18
- package/tools/search_intel.py +94 -32
- package/tools/sec_holders.py +27 -25
- package/tools/sector_rotation.py +466 -0
- package/tools/signal_tracker.py +100 -49
- package/tools/stateless_analysis.py +600 -33
- package/tools/stock_data.py +111 -26
- package/tools/technical.py +42 -40
- package/tools/trading_calendar.py +26 -21
- package/tools/value_data.py +6 -2
- package/tools/watchlist.py +24 -15
- package/tools/watchlist_context.py +63 -49
package/pi/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { withJsonInput } from "../scripts/json-input.mjs";
|
|
2
2
|
import type { ExtensionAPI } from "pi-agent";
|
|
3
|
-
import { existsSync } from "fs";
|
|
4
3
|
import { fileURLToPath } from "url";
|
|
5
4
|
|
|
6
5
|
import { commandTimeout } from "../scripts/cli-timeout.mjs";
|
|
6
|
+
import { resolvePython } from "../scripts/venv-python.mjs";
|
|
7
7
|
|
|
8
8
|
export default (pi: ExtensionAPI) => {
|
|
9
9
|
const tools: Parameters<ExtensionAPI["registerTool"]>[0][] = [];
|
|
@@ -53,11 +53,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
53
53
|
? `${__dirname}/..`
|
|
54
54
|
: fileURLToPath(new URL("..", import.meta.url));
|
|
55
55
|
const toolsDir = `${baseDir}/tools`;
|
|
56
|
-
const
|
|
57
|
-
// Windows venvs ship Scripts/python.exe (there is no python3); POSIX venvs ship bin/python3.
|
|
58
|
-
const venvPython = `${baseDir}/.venv/${isWin ? "Scripts/python.exe" : "bin/python3"}`;
|
|
59
|
-
// Fallback: on Windows "python3" is usually the Microsoft Store stub — prefer "python".
|
|
60
|
-
const python = existsSync(venvPython) ? venvPython : isWin ? "python" : "python3";
|
|
56
|
+
const python = resolvePython(baseDir);
|
|
61
57
|
const py = async (script: string, args: string[]) => {
|
|
62
58
|
const result = await pi.exec(python, [`${toolsDir}/${script}`, ...args], { timeout: commandTimeout(script, args) });
|
|
63
59
|
if (result.killed || !result.stdout.trim()) {
|
|
@@ -313,7 +309,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
313
309
|
|
|
314
310
|
registerTool({
|
|
315
311
|
name: "analyze_portfolio_risk",
|
|
316
|
-
description: "
|
|
312
|
+
description: "无状态现货多头组合风险与压力测试:输入持仓、基准币种现金与汇率,返回集中度、币种/行业敞口、价格盈亏、止损情景及可选市场/行业/个股/汇率冲击损益。缺汇率报错;不保存账户。",
|
|
317
313
|
parameters: {
|
|
318
314
|
"type": "object",
|
|
319
315
|
"properties": {
|
|
@@ -434,6 +430,82 @@ export default (pi: ExtensionAPI) => {
|
|
|
434
430
|
"additionalProperties": false
|
|
435
431
|
},
|
|
436
432
|
"description": "可选历史,共同日期本币价格收益相关性,不计汇率"
|
|
433
|
+
},
|
|
434
|
+
"stress_scenarios": {
|
|
435
|
+
"type": "array",
|
|
436
|
+
"items": {
|
|
437
|
+
"type": "object",
|
|
438
|
+
"properties": {
|
|
439
|
+
"name": {
|
|
440
|
+
"type": "string",
|
|
441
|
+
"description": "情景名称"
|
|
442
|
+
},
|
|
443
|
+
"market_shock": {
|
|
444
|
+
"type": "number",
|
|
445
|
+
"description": "全市场资产价格冲击比例,如-0.1"
|
|
446
|
+
},
|
|
447
|
+
"sector_shocks": {
|
|
448
|
+
"type": "array",
|
|
449
|
+
"items": {
|
|
450
|
+
"type": "object",
|
|
451
|
+
"properties": {
|
|
452
|
+
"sector": {
|
|
453
|
+
"type": "string",
|
|
454
|
+
"description": "与持仓一致的行业名称"
|
|
455
|
+
},
|
|
456
|
+
"shock": {
|
|
457
|
+
"type": "number",
|
|
458
|
+
"description": "行业价格冲击比例"
|
|
459
|
+
}
|
|
460
|
+
},
|
|
461
|
+
"required": ["sector", "shock"],
|
|
462
|
+
"additionalProperties": false
|
|
463
|
+
},
|
|
464
|
+
"description": "可选行业冲击"
|
|
465
|
+
},
|
|
466
|
+
"symbol_shocks": {
|
|
467
|
+
"type": "array",
|
|
468
|
+
"items": {
|
|
469
|
+
"type": "object",
|
|
470
|
+
"properties": {
|
|
471
|
+
"symbol": {
|
|
472
|
+
"type": "string",
|
|
473
|
+
"description": "持仓标的代码"
|
|
474
|
+
},
|
|
475
|
+
"shock": {
|
|
476
|
+
"type": "number",
|
|
477
|
+
"description": "标的价格冲击比例"
|
|
478
|
+
}
|
|
479
|
+
},
|
|
480
|
+
"required": ["symbol", "shock"],
|
|
481
|
+
"additionalProperties": false
|
|
482
|
+
},
|
|
483
|
+
"description": "可选个股冲击"
|
|
484
|
+
},
|
|
485
|
+
"currency_shocks": {
|
|
486
|
+
"type": "array",
|
|
487
|
+
"items": {
|
|
488
|
+
"type": "object",
|
|
489
|
+
"properties": {
|
|
490
|
+
"currency": {
|
|
491
|
+
"type": "string",
|
|
492
|
+
"description": "持仓中的非基准币种;基准币种不可相对自身冲击"
|
|
493
|
+
},
|
|
494
|
+
"shock": {
|
|
495
|
+
"type": "number",
|
|
496
|
+
"description": "相对基准币种的汇率冲击比例"
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
"required": ["currency", "shock"],
|
|
500
|
+
"additionalProperties": false
|
|
501
|
+
},
|
|
502
|
+
"description": "可选汇率冲击"
|
|
503
|
+
}
|
|
504
|
+
},
|
|
505
|
+
"required": ["name"],
|
|
506
|
+
"additionalProperties": false
|
|
507
|
+
},
|
|
508
|
+
"description": "可选压力情景;所有冲击均为比例,由共享核心计算"
|
|
437
509
|
}
|
|
438
510
|
},
|
|
439
511
|
"required": [
|
|
@@ -449,6 +521,156 @@ export default (pi: ExtensionAPI) => {
|
|
|
449
521
|
},
|
|
450
522
|
});
|
|
451
523
|
|
|
524
|
+
registerTool({
|
|
525
|
+
name: "plan_portfolio_rebalance",
|
|
526
|
+
description: "无状态组合调仓计划:按当前持仓、目标权重、现金、汇率、整手、费用和换手约束生成先卖后买清单;不保存账户或提交订单。",
|
|
527
|
+
parameters: {
|
|
528
|
+
"type": "object",
|
|
529
|
+
"properties": {
|
|
530
|
+
"base_currency": {
|
|
531
|
+
"type": "string",
|
|
532
|
+
"description": "基准币种"
|
|
533
|
+
},
|
|
534
|
+
"cash": {
|
|
535
|
+
"type": "number",
|
|
536
|
+
"description": "基准币种现金,非负"
|
|
537
|
+
},
|
|
538
|
+
"positions": {
|
|
539
|
+
"type": "array",
|
|
540
|
+
"items": {
|
|
541
|
+
"type": "object",
|
|
542
|
+
"properties": {
|
|
543
|
+
"symbol": {
|
|
544
|
+
"type": "string",
|
|
545
|
+
"description": "唯一代码,重复持仓先汇总"
|
|
546
|
+
},
|
|
547
|
+
"quantity": {
|
|
548
|
+
"type": "number",
|
|
549
|
+
"description": "正持仓数量"
|
|
550
|
+
},
|
|
551
|
+
"price": {
|
|
552
|
+
"type": "number",
|
|
553
|
+
"description": "正现价"
|
|
554
|
+
},
|
|
555
|
+
"currency": {
|
|
556
|
+
"type": "string",
|
|
557
|
+
"description": "持仓币种,如CNY/USD/HKD"
|
|
558
|
+
},
|
|
559
|
+
"cost": {
|
|
560
|
+
"type": "number",
|
|
561
|
+
"description": "同币种正成本价,可选;调仓计算不使用"
|
|
562
|
+
},
|
|
563
|
+
"stop_price": {
|
|
564
|
+
"type": "number",
|
|
565
|
+
"description": "同币种正止损价,可选;调仓计算不使用"
|
|
566
|
+
},
|
|
567
|
+
"sector": {
|
|
568
|
+
"type": "string",
|
|
569
|
+
"description": "统一行业分类,可选"
|
|
570
|
+
},
|
|
571
|
+
"as_of": {
|
|
572
|
+
"type": "string",
|
|
573
|
+
"description": "实际估值时点,建议ISO8601"
|
|
574
|
+
}
|
|
575
|
+
},
|
|
576
|
+
"required": [
|
|
577
|
+
"symbol",
|
|
578
|
+
"quantity",
|
|
579
|
+
"price",
|
|
580
|
+
"currency"
|
|
581
|
+
],
|
|
582
|
+
"additionalProperties": false
|
|
583
|
+
},
|
|
584
|
+
"description": "0..100个现货多头持仓,空数组表示纯现金建仓"
|
|
585
|
+
},
|
|
586
|
+
"targets": {
|
|
587
|
+
"type": "array",
|
|
588
|
+
"items": {
|
|
589
|
+
"type": "object",
|
|
590
|
+
"properties": {
|
|
591
|
+
"symbol": {
|
|
592
|
+
"type": "string",
|
|
593
|
+
"description": "目标标的代码"
|
|
594
|
+
},
|
|
595
|
+
"weight": {
|
|
596
|
+
"type": "number",
|
|
597
|
+
"description": "目标权重;大于0时lot_size必填"
|
|
598
|
+
},
|
|
599
|
+
"price": {
|
|
600
|
+
"type": "number",
|
|
601
|
+
"description": "正目标价格;当前未持有的新标的必填"
|
|
602
|
+
},
|
|
603
|
+
"currency": {
|
|
604
|
+
"type": "string",
|
|
605
|
+
"description": "目标币种;当前未持有的新标的必填"
|
|
606
|
+
},
|
|
607
|
+
"sector": {
|
|
608
|
+
"type": "string",
|
|
609
|
+
"description": "统一行业分类,可选"
|
|
610
|
+
},
|
|
611
|
+
"as_of": {
|
|
612
|
+
"type": "string",
|
|
613
|
+
"description": "实际估值时点,建议ISO8601"
|
|
614
|
+
},
|
|
615
|
+
"lot_size": {
|
|
616
|
+
"type": "number",
|
|
617
|
+
"description": "正整数交易单位;weight>0时必填"
|
|
618
|
+
}
|
|
619
|
+
},
|
|
620
|
+
"required": ["symbol", "weight"],
|
|
621
|
+
"additionalProperties": false
|
|
622
|
+
},
|
|
623
|
+
"description": "1..100个目标;新标的price/currency必填"
|
|
624
|
+
},
|
|
625
|
+
"fx_rates": {
|
|
626
|
+
"type": "array",
|
|
627
|
+
"items": {
|
|
628
|
+
"type": "object",
|
|
629
|
+
"properties": {
|
|
630
|
+
"currency": {
|
|
631
|
+
"type": "string",
|
|
632
|
+
"description": "唯一非基准币种"
|
|
633
|
+
},
|
|
634
|
+
"rate": {
|
|
635
|
+
"type": "number",
|
|
636
|
+
"description": "一单位该币种对应多少基准币种,正数"
|
|
637
|
+
}
|
|
638
|
+
},
|
|
639
|
+
"required": [
|
|
640
|
+
"currency",
|
|
641
|
+
"rate"
|
|
642
|
+
],
|
|
643
|
+
"additionalProperties": false
|
|
644
|
+
},
|
|
645
|
+
"description": "显式现价汇率;基准币种隐含1,不得重复"
|
|
646
|
+
},
|
|
647
|
+
"fee_rate": {
|
|
648
|
+
"type": "number",
|
|
649
|
+
"description": "每边比例费用0..0.1,默认0;不含未提供税费/最低收费"
|
|
650
|
+
},
|
|
651
|
+
"slippage_rate": {
|
|
652
|
+
"type": "number",
|
|
653
|
+
"description": "每边滑点0..0.1,默认0"
|
|
654
|
+
},
|
|
655
|
+
"max_turnover_fraction": {
|
|
656
|
+
"type": "number",
|
|
657
|
+
"description": "非负最大总换手,按买卖参考金额合计/初始权益;默认不限制,可大于1"
|
|
658
|
+
}
|
|
659
|
+
},
|
|
660
|
+
"required": [
|
|
661
|
+
"base_currency",
|
|
662
|
+
"cash",
|
|
663
|
+
"positions",
|
|
664
|
+
"targets"
|
|
665
|
+
],
|
|
666
|
+
"additionalProperties": false
|
|
667
|
+
},
|
|
668
|
+
async execute(_id, params) {
|
|
669
|
+
return asText(await withJsonInput(params, async (path: string) =>
|
|
670
|
+
(await py("stateless_analysis.py", ["rebalance", "--input", path])).stdout));
|
|
671
|
+
},
|
|
672
|
+
});
|
|
673
|
+
|
|
452
674
|
registerTool({
|
|
453
675
|
name: "calculate_position_size",
|
|
454
676
|
description: "无状态多头仓位预算:同币种金额,按现金/风险预算/最大仓位/交易单位取最小数量,双边费用和滑点参与止损情景;不执行交易。",
|
|
@@ -1605,6 +1827,214 @@ export default (pi: ExtensionAPI) => {
|
|
|
1605
1827
|
},
|
|
1606
1828
|
});
|
|
1607
1829
|
|
|
1830
|
+
registerTool({
|
|
1831
|
+
name: "calculate_rotation_metrics",
|
|
1832
|
+
description: "行业轮动数值复核:输入真实MC-dropout样本和/或历史成交计数,计算集中度和原始配权;不训练模型、不生成买入信号,零历史不补默认值。",
|
|
1833
|
+
parameters: {
|
|
1834
|
+
"type": "object",
|
|
1835
|
+
"properties": {
|
|
1836
|
+
"prediction_samples": {
|
|
1837
|
+
"type": "array",
|
|
1838
|
+
"items": {
|
|
1839
|
+
"type": "object",
|
|
1840
|
+
"properties": {
|
|
1841
|
+
"symbol": {
|
|
1842
|
+
"type": "string",
|
|
1843
|
+
"description": "唯一标的标识"
|
|
1844
|
+
},
|
|
1845
|
+
"samples": {
|
|
1846
|
+
"type": "array",
|
|
1847
|
+
"items": {
|
|
1848
|
+
"type": "number",
|
|
1849
|
+
"description": "同一模型/标的/时间的有限线性输出,不是价格或收益率"
|
|
1850
|
+
},
|
|
1851
|
+
"description": "2..100000个真实MC-dropout预测;不得用随机数或动量代替"
|
|
1852
|
+
}
|
|
1853
|
+
},
|
|
1854
|
+
"required": [
|
|
1855
|
+
"symbol",
|
|
1856
|
+
"samples"
|
|
1857
|
+
],
|
|
1858
|
+
"additionalProperties": false
|
|
1859
|
+
},
|
|
1860
|
+
"description": "1..100组,总样本<=100000;和trade_counts至少提供一项"
|
|
1861
|
+
},
|
|
1862
|
+
"trade_counts": {
|
|
1863
|
+
"type": "array",
|
|
1864
|
+
"items": {
|
|
1865
|
+
"type": "object",
|
|
1866
|
+
"properties": {
|
|
1867
|
+
"symbol": {
|
|
1868
|
+
"type": "string",
|
|
1869
|
+
"description": "唯一标的标识;代码别名须先统一"
|
|
1870
|
+
},
|
|
1871
|
+
"buys": {
|
|
1872
|
+
"type": "number",
|
|
1873
|
+
"description": "此前已平仓买入周数,非负整数"
|
|
1874
|
+
},
|
|
1875
|
+
"wins": {
|
|
1876
|
+
"type": "number",
|
|
1877
|
+
"description": "此前盈利平仓次数,0..buys"
|
|
1878
|
+
},
|
|
1879
|
+
"streak": {
|
|
1880
|
+
"type": "number",
|
|
1881
|
+
"description": "此前连续盈利交易次数,0..wins"
|
|
1882
|
+
}
|
|
1883
|
+
},
|
|
1884
|
+
"required": [
|
|
1885
|
+
"symbol",
|
|
1886
|
+
"buys",
|
|
1887
|
+
"wins",
|
|
1888
|
+
"streak"
|
|
1889
|
+
],
|
|
1890
|
+
"additionalProperties": false
|
|
1891
|
+
},
|
|
1892
|
+
"description": "1..100条已完成历史成交计数;不得使用决策后的交易"
|
|
1893
|
+
}
|
|
1894
|
+
},
|
|
1895
|
+
"required": [],
|
|
1896
|
+
"additionalProperties": false
|
|
1897
|
+
},
|
|
1898
|
+
async execute(_id, params) {
|
|
1899
|
+
return asText(await withJsonInput(params, async (path: string) =>
|
|
1900
|
+
(await py("sector_rotation.py", ["analyze", "--input", path])).stdout));
|
|
1901
|
+
},
|
|
1902
|
+
});
|
|
1903
|
+
|
|
1904
|
+
registerTool({
|
|
1905
|
+
name: "backtest_rotation_allocation",
|
|
1906
|
+
description: "行业轮动配权分项回测:代码自动取行情,或传明确币种/复权OHLCV;相同每周全池买卖下比较等权与历史配权。按市场和报价币种独立资金,支持A/HK/US/JP/KR/TW;不是完整论文深度模型或跨币种共享资金回测。",
|
|
1907
|
+
parameters: {
|
|
1908
|
+
"type": "object",
|
|
1909
|
+
"properties": {
|
|
1910
|
+
"symbols": {
|
|
1911
|
+
"type": "array",
|
|
1912
|
+
"items": {
|
|
1913
|
+
"type": "string",
|
|
1914
|
+
"description": "唯一股票/ETF代码,按插件代码格式"
|
|
1915
|
+
},
|
|
1916
|
+
"description": "1..20只自动取数,与datasets互斥;任一失败整池停止"
|
|
1917
|
+
},
|
|
1918
|
+
"datasets": {
|
|
1919
|
+
"type": "array",
|
|
1920
|
+
"items": {
|
|
1921
|
+
"type": "object",
|
|
1922
|
+
"properties": {
|
|
1923
|
+
"symbol": {
|
|
1924
|
+
"type": "string",
|
|
1925
|
+
"description": "A六位代码/HK如2800.HK/US如XLK/JP如1306.T/KR如069500.KS/TW如0050.TW"
|
|
1926
|
+
},
|
|
1927
|
+
"currency": {
|
|
1928
|
+
"type": "string",
|
|
1929
|
+
"description": "已核实报价币种三字母代码,不按上市地猜测"
|
|
1930
|
+
},
|
|
1931
|
+
"price_basis": {
|
|
1932
|
+
"type": "string",
|
|
1933
|
+
"enum": [
|
|
1934
|
+
"qfq",
|
|
1935
|
+
"total_return",
|
|
1936
|
+
"split_adjusted"
|
|
1937
|
+
],
|
|
1938
|
+
"description": "已核实且一致的复权口径"
|
|
1939
|
+
},
|
|
1940
|
+
"bars": {
|
|
1941
|
+
"type": "array",
|
|
1942
|
+
"items": {
|
|
1943
|
+
"type": "object",
|
|
1944
|
+
"properties": {
|
|
1945
|
+
"date": {
|
|
1946
|
+
"type": "string",
|
|
1947
|
+
"description": "YYYY-MM-DD"
|
|
1948
|
+
},
|
|
1949
|
+
"open": {
|
|
1950
|
+
"type": "number",
|
|
1951
|
+
"description": "有限正OHLC价格;volume非负"
|
|
1952
|
+
},
|
|
1953
|
+
"high": {
|
|
1954
|
+
"type": "number",
|
|
1955
|
+
"description": "有限正OHLC价格;volume非负"
|
|
1956
|
+
},
|
|
1957
|
+
"low": {
|
|
1958
|
+
"type": "number",
|
|
1959
|
+
"description": "有限正OHLC价格;volume非负"
|
|
1960
|
+
},
|
|
1961
|
+
"close": {
|
|
1962
|
+
"type": "number",
|
|
1963
|
+
"description": "有限正OHLC价格;volume非负"
|
|
1964
|
+
},
|
|
1965
|
+
"volume": {
|
|
1966
|
+
"type": "number",
|
|
1967
|
+
"description": "有限正OHLC价格;volume非负"
|
|
1968
|
+
},
|
|
1969
|
+
"adjusted": {
|
|
1970
|
+
"type": "string",
|
|
1971
|
+
"description": "可选逐bar复权标记,存在时必须与price_basis一致"
|
|
1972
|
+
},
|
|
1973
|
+
"source": {
|
|
1974
|
+
"type": "string",
|
|
1975
|
+
"description": "可选行情来源标识,用于数据覆盖报告"
|
|
1976
|
+
}
|
|
1977
|
+
},
|
|
1978
|
+
"required": [
|
|
1979
|
+
"date",
|
|
1980
|
+
"open",
|
|
1981
|
+
"high",
|
|
1982
|
+
"low",
|
|
1983
|
+
"close",
|
|
1984
|
+
"volume"
|
|
1985
|
+
],
|
|
1986
|
+
"additionalProperties": false
|
|
1987
|
+
},
|
|
1988
|
+
"description": "递增唯一daily OHLCV,2..20000行,包含评估前预热;总量<=100000"
|
|
1989
|
+
}
|
|
1990
|
+
},
|
|
1991
|
+
"required": [
|
|
1992
|
+
"symbol",
|
|
1993
|
+
"currency",
|
|
1994
|
+
"price_basis",
|
|
1995
|
+
"bars"
|
|
1996
|
+
],
|
|
1997
|
+
"additionalProperties": false
|
|
1998
|
+
},
|
|
1999
|
+
"description": "1..20个离线数据集,与symbols互斥;8MiB JSON输入上限"
|
|
2000
|
+
},
|
|
2001
|
+
"start_date": {
|
|
2002
|
+
"type": "string",
|
|
2003
|
+
"description": "评估起始YYYY-MM-DD;跨边界周不交易,不能用评估期收益初始化配权"
|
|
2004
|
+
},
|
|
2005
|
+
"end_date": {
|
|
2006
|
+
"type": "string",
|
|
2007
|
+
"description": "已收盘截止YYYY-MM-DD;不完整末周不成交"
|
|
2008
|
+
},
|
|
2009
|
+
"history_start_date": {
|
|
2010
|
+
"type": "string",
|
|
2011
|
+
"description": "仅代码模式;默认评估前一年,必须早于start_date"
|
|
2012
|
+
},
|
|
2013
|
+
"warmup_weeks": {
|
|
2014
|
+
"type": "number",
|
|
2015
|
+
"description": "评估前完整历史交易周1..104整数,默认26;不足停止"
|
|
2016
|
+
},
|
|
2017
|
+
"capital": {
|
|
2018
|
+
"type": "number",
|
|
2019
|
+
"description": "每市场/报价币种分组独立初始资金,默认1000000,不是混合池总额"
|
|
2020
|
+
},
|
|
2021
|
+
"cost_per_side": {
|
|
2022
|
+
"type": "number",
|
|
2023
|
+
"description": "每边比例成本0..0.1,默认0.001;买入预算含费用"
|
|
2024
|
+
}
|
|
2025
|
+
},
|
|
2026
|
+
"required": [
|
|
2027
|
+
"start_date",
|
|
2028
|
+
"end_date"
|
|
2029
|
+
],
|
|
2030
|
+
"additionalProperties": false
|
|
2031
|
+
},
|
|
2032
|
+
async execute(_id, params) {
|
|
2033
|
+
return asText(await withJsonInput(params, async (path: string) =>
|
|
2034
|
+
(await py("sector_rotation.py", ["backtest", "--input", path])).stdout));
|
|
2035
|
+
},
|
|
2036
|
+
});
|
|
2037
|
+
|
|
1608
2038
|
registerTool({
|
|
1609
2039
|
name: "analyze_trend_strategy",
|
|
1610
2040
|
description: "月度右侧趋势策略:直接传同市场A/HK/US股票代码,自动获取日K,输出月度新入/持有/退出与逆波动预算。as_of默认最近已收盘交易日,持仓可选且不保存,不自动下单;跨样本仍not_validated。",
|
|
@@ -1616,8 +2046,6 @@ export default (pi: ExtensionAPI) => {
|
|
|
1616
2046
|
"items": {
|
|
1617
2047
|
"type": "string"
|
|
1618
2048
|
},
|
|
1619
|
-
"minItems": 1,
|
|
1620
|
-
"maxItems": 100,
|
|
1621
2049
|
"description": "同一市场的完整股票池,1..100只;A如600036,HK如0700.HK,US如AAPL"
|
|
1622
2050
|
},
|
|
1623
2051
|
"as_of": {
|
|
@@ -2229,10 +2657,11 @@ export default (pi: ExtensionAPI) => {
|
|
|
2229
2657
|
registerTool({
|
|
2230
2658
|
name: "render_stock_report",
|
|
2231
2659
|
description:
|
|
2232
|
-
"股票分析报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 skill 提供的 report_schema 为准,不要自造字段。template: brief|full
|
|
2660
|
+
"股票分析报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 skill 提供的 report_schema 为准,不要自造字段。template: brief|full。全部分析完成后的最后一步调用。默认仅渲染;final=true表示最终回复,支持直出的宿主可原样交付并结束本轮。report可包含evidence(path/label/value/source/as_of)与data_quality(flags/limitations),brief/full均展示证据和限制。",
|
|
2233
2661
|
parameters: {
|
|
2234
2662
|
type: "object",
|
|
2235
2663
|
properties: {
|
|
2664
|
+
final: { type: "boolean", description: "最终回复标记,默认false;保存附件或继续分析时保持false" },
|
|
2236
2665
|
report: {
|
|
2237
2666
|
type: "object",
|
|
2238
2667
|
description: "结构化股票报告 JSON",
|
|
@@ -2245,7 +2674,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
2245
2674
|
},
|
|
2246
2675
|
required: ["report"],
|
|
2247
2676
|
},
|
|
2248
|
-
async execute(_id, { report, template = "full" }) {
|
|
2677
|
+
async execute(_id, { report, template = "full", final = false }) {
|
|
2249
2678
|
const b64 = Buffer.from(JSON.stringify(report), "utf-8").toString("base64");
|
|
2250
2679
|
const result = await py("report_renderer.py", [
|
|
2251
2680
|
"stock",
|
|
@@ -2253,6 +2682,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
2253
2682
|
template,
|
|
2254
2683
|
"--input-b64",
|
|
2255
2684
|
b64,
|
|
2685
|
+
...(final ? ["--final"] : []),
|
|
2256
2686
|
]);
|
|
2257
2687
|
return asText(result.stdout);
|
|
2258
2688
|
},
|
|
@@ -2261,21 +2691,23 @@ export default (pi: ExtensionAPI) => {
|
|
|
2261
2691
|
registerTool({
|
|
2262
2692
|
name: "render_market_report",
|
|
2263
2693
|
description:
|
|
2264
|
-
"大盘复盘报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 schemas/market_review_schema.json 为准。save=true 时把报告对象追加归档到本地 JSONL(默认不保存不推送),纵向对比用 get_review_history",
|
|
2694
|
+
"大盘复盘报告渲染 — 将结构化报告 JSON 通过 j2 模板渲染为 Markdown。report 字段以 schemas/market_review_schema.json 为准。save=true 时把报告对象追加归档到本地 JSONL(默认不保存不推送),纵向对比用 get_review_history;final=true仅用于最终回复,保存附件或继续分析时保持false",
|
|
2265
2695
|
parameters: {
|
|
2266
2696
|
type: "object",
|
|
2267
2697
|
properties: {
|
|
2698
|
+
final: { type: "boolean", description: "最终回复标记,默认false;保存附件或继续分析时保持false" },
|
|
2268
2699
|
report: { type: "object", description: "结构化市场复盘" },
|
|
2269
2700
|
save: { type: "boolean", description: "是否归档到本地复盘 JSONL 存储,默认 false" },
|
|
2270
2701
|
},
|
|
2271
2702
|
required: ["report"],
|
|
2272
2703
|
},
|
|
2273
|
-
async execute(_id, { report, save }) {
|
|
2704
|
+
async execute(_id, { report, save, final = false }) {
|
|
2274
2705
|
const b64 = Buffer.from(JSON.stringify(report), "utf-8").toString("base64");
|
|
2275
2706
|
const result = await py("report_renderer.py", [
|
|
2276
2707
|
"market",
|
|
2277
2708
|
"--input-b64",
|
|
2278
2709
|
b64,
|
|
2710
|
+
...(final ? ["--final"] : []),
|
|
2279
2711
|
...(save ? ["--save"] : []),
|
|
2280
2712
|
]);
|
|
2281
2713
|
return asText(result.stdout);
|
|
@@ -2499,6 +2931,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
2499
2931
|
`${baseDir}/skills/stock-analysis/SKILL.md`,
|
|
2500
2932
|
`${baseDir}/skills/stock-screener/SKILL.md`,
|
|
2501
2933
|
`${baseDir}/skills/strategy-backtest/SKILL.md`,
|
|
2934
|
+
`${baseDir}/skills/sector-rotation/SKILL.md`,
|
|
2502
2935
|
`${baseDir}/skills/bull-trend/SKILL.md`,
|
|
2503
2936
|
`${baseDir}/skills/shrink-pullback/SKILL.md`,
|
|
2504
2937
|
`${baseDir}/skills/ma-crossover/SKILL.md`,
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Offline baseline: python scripts/profile-offline.py > baseline.json.
|
|
3
|
+
|
|
4
|
+
CLI --help includes interpreter/import/argparse cost, never provider latency.
|
|
5
|
+
Synthetic compute timings exclude imports; this is not a production throughput test.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import cProfile
|
|
9
|
+
import json
|
|
10
|
+
import platform
|
|
11
|
+
import pstats
|
|
12
|
+
import statistics
|
|
13
|
+
import subprocess
|
|
14
|
+
import sys
|
|
15
|
+
import tempfile
|
|
16
|
+
import time
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
20
|
+
sys.path.insert(0, str(ROOT / "tools"))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def measure(operation, repeats=5):
|
|
24
|
+
samples = []
|
|
25
|
+
for _ in range(repeats):
|
|
26
|
+
start = time.perf_counter()
|
|
27
|
+
operation()
|
|
28
|
+
samples.append(round((time.perf_counter() - start) * 1000, 3))
|
|
29
|
+
return {"median_ms": statistics.median(samples), "samples_ms": samples}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def main():
|
|
33
|
+
cli = {}
|
|
34
|
+
for path in sorted((ROOT / "tools").glob("*.py")):
|
|
35
|
+
if '__name__ == "__main__"' not in path.read_text():
|
|
36
|
+
continue
|
|
37
|
+
|
|
38
|
+
def help_call(path=path):
|
|
39
|
+
subprocess.run(
|
|
40
|
+
[sys.executable, str(path), "--help"],
|
|
41
|
+
stdout=subprocess.DEVNULL,
|
|
42
|
+
stderr=subprocess.PIPE,
|
|
43
|
+
timeout=15,
|
|
44
|
+
check=True,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
cli[path.name] = measure(help_call, 3)
|
|
48
|
+
|
|
49
|
+
import _cache
|
|
50
|
+
import numpy as np
|
|
51
|
+
import pandas as pd
|
|
52
|
+
from backtest import load_strategy, simulate
|
|
53
|
+
from stateless_analysis import portfolio
|
|
54
|
+
from technical import analyze_records
|
|
55
|
+
|
|
56
|
+
def bars(count):
|
|
57
|
+
rng = np.random.default_rng(68)
|
|
58
|
+
close = 100 * np.exp(np.cumsum(rng.normal(0.0002, 0.01, count)))
|
|
59
|
+
return pd.DataFrame(
|
|
60
|
+
{
|
|
61
|
+
"date": pd.bdate_range("2000-01-03", periods=count).strftime("%Y-%m-%d"),
|
|
62
|
+
"open": close,
|
|
63
|
+
"close": close,
|
|
64
|
+
"high": close * 1.01,
|
|
65
|
+
"low": close * 0.99,
|
|
66
|
+
"volume": rng.integers(100000, 1000000, count),
|
|
67
|
+
}
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
compute = {}
|
|
71
|
+
strategy = load_strategy(str(ROOT / "strategies/examples/ma_crossover.yaml"))
|
|
72
|
+
for count in (120, 2500):
|
|
73
|
+
frame = bars(count)
|
|
74
|
+
records = frame.to_dict("records")
|
|
75
|
+
assert "error" not in analyze_records("TEST", records)
|
|
76
|
+
compute[f"technical_{count}"] = measure(lambda records=records: analyze_records("TEST", records))
|
|
77
|
+
compute[f"backtest_{count}"] = measure(lambda frame=frame: simulate(frame, strategy, 100000, "TEST"))
|
|
78
|
+
for count in (10, 50, 100):
|
|
79
|
+
histories = []
|
|
80
|
+
for i in range(count):
|
|
81
|
+
records = bars(250).to_dict("records")
|
|
82
|
+
histories.append({"symbol": f"S{i}", "prices": [{"date": r["date"], "close": r["close"]} for r in records]})
|
|
83
|
+
payload = {
|
|
84
|
+
"base_currency": "USD",
|
|
85
|
+
"cash": 0,
|
|
86
|
+
"positions": [{"symbol": f"S{i}", "quantity": 1, "price": 100, "currency": "USD"} for i in range(count)],
|
|
87
|
+
"histories": histories,
|
|
88
|
+
}
|
|
89
|
+
result = portfolio(payload)
|
|
90
|
+
assert len(result["correlations"]) == count * (count - 1) // 2
|
|
91
|
+
compute[f"portfolio_{count}_assets_250_dates"] = measure(lambda payload=payload: portfolio(payload), 3)
|
|
92
|
+
profiler = cProfile.Profile()
|
|
93
|
+
profiler.runcall(simulate, frame, strategy, 100000, "TEST")
|
|
94
|
+
stats = pstats.Stats(profiler)
|
|
95
|
+
hotspots = [
|
|
96
|
+
{"function": f"{Path(file).name}:{line}:{name}", "calls": values[1], "cumulative_s": round(values[3], 4)}
|
|
97
|
+
for (file, line, name), values in sorted(stats.stats.items(), key=lambda item: item[1][3], reverse=True)[:12]
|
|
98
|
+
]
|
|
99
|
+
original_dir = _cache.CACHE_DIR
|
|
100
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
101
|
+
try:
|
|
102
|
+
_cache.CACHE_DIR = Path(directory)
|
|
103
|
+
payload = [{"symbol": str(i), "value": i} for i in range(10000)]
|
|
104
|
+
_cache.cached("profile", 60, lambda: payload)
|
|
105
|
+
|
|
106
|
+
def unexpected_fetch():
|
|
107
|
+
raise AssertionError("warm cache missed")
|
|
108
|
+
|
|
109
|
+
compute["cache_read_10000_rows"] = measure(lambda: _cache.cached("profile", 60, unexpected_fetch))
|
|
110
|
+
finally:
|
|
111
|
+
_cache.CACHE_DIR = original_dir
|
|
112
|
+
print(
|
|
113
|
+
json.dumps(
|
|
114
|
+
{
|
|
115
|
+
"python": platform.python_version(),
|
|
116
|
+
"platform": platform.platform(),
|
|
117
|
+
"cli": cli,
|
|
118
|
+
"compute": compute,
|
|
119
|
+
"backtest_hotspots": hotspots,
|
|
120
|
+
},
|
|
121
|
+
indent=2,
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
if __name__ == "__main__":
|
|
127
|
+
main()
|