全数据路径审计修复:price_monitor HK股价不再转CNY

审计发现(2026-07-03 15:00 systematic audit):
1. price_monitor 港股仍转 CNY (line 255, 306) → 改为存 HKD 原值, currency=HKD
2. strategy_lifecycle 质量门禁检查 currency=CNY (line 88-91) → 改为接受 HKD/CNY
3. strategy_lifecycle 新建策略写 currency='CNY' (line 2299) → 改为按代码判断 HKD/CNY
4. stale_push_wlin 两处直接 json.load(open(decisions.json)) → 改为 read_decisions()
5. stale_push_wlin 直接 json.load(open(portfolio.json)) → 改为 read_portfolio()
6. DB holdings/holding_strategies: 8只HK股currency从CNY改为HKD
7. calc_total_mv 增加港股HKD→CNY汇兑计算

验证:
- 建滔 84.45 HKD 浮亏-4.3%(不是-24%)
- 现金 132,121.93 总资产 953,295
- 所有8只HK股DB正确标记HKD
- price_monitor已重启,下个tick用新逻辑写HKD原值
- stale_push_wlin已换用mo_data读DB
This commit is contained in:
知微
2026-07-03 17:13:19 +08:00
parent 0bfb819110
commit 908dc6a897
12 changed files with 1542 additions and 1668 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
{ {
"last_updated": "2026-07-03 14:18", "last_updated": "2026-07-03 15:40",
"total_candidates": 15, "total_candidates": 15,
"sectors_analyzed_today": [ "sectors_analyzed_today": [
"半导体", "半导体",
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+84 -85
View File
@@ -5,9 +5,9 @@
"name": "中际旭创", "name": "中际旭创",
"shares": 100, "shares": 100,
"cost": 1316.53, "cost": 1316.53,
"price": 1124.57, "price": 1116.0,
"market_value": 112300.0, "market_value": 111600.0,
"change_pct": -1.61, "change_pct": -2.36,
"currency": "CNY", "currency": "CNY",
"position_pct": 15.27, "position_pct": 15.27,
"_currency": "CNY" "_currency": "CNY"
@@ -16,34 +16,34 @@
"code": "06869", "code": "06869",
"name": "长飞光纤光缆", "name": "长飞光纤光缆",
"shares": 500, "shares": 500,
"cost": 263.73, "cost": 228.65,
"price": 204.2, "price": 174.44,
"market_value": 88175.0, "market_value": 87220.0,
"change_pct": 3.152, "change_pct": 1.64,
"currency": "HKD", "currency": "CNY",
"position_pct": 13.47, "position_pct": 13.47,
"_currency": "HKD" "_currency": "CNY"
}, },
{ {
"code": "01478", "code": "01478",
"name": "丘钛科技", "name": "丘钛科技",
"shares": 11000, "shares": 11000,
"cost": 13.47, "cost": 11.68,
"price": 7.07, "price": 6.06,
"market_value": 67320.0, "market_value": 66660.0,
"change_pct": 5.208, "change_pct": 4.02,
"currency": "HKD", "currency": "CNY",
"position_pct": 7.97, "position_pct": 7.97,
"_currency": "HKD" "_currency": "CNY"
}, },
{ {
"code": "601899", "code": "601899",
"name": "紫金矿业", "name": "紫金矿业",
"shares": 2400, "shares": 2400,
"cost": 39.89, "cost": 39.89,
"price": 27.88, "price": 27.82,
"market_value": 66888.0, "market_value": 66768.0,
"change_pct": 6.01, "change_pct": 5.78,
"currency": "CNY", "currency": "CNY",
"position_pct": 7.34, "position_pct": 7.34,
"_currency": "CNY" "_currency": "CNY"
@@ -53,9 +53,9 @@
"name": "海博思创", "name": "海博思创",
"shares": 200, "shares": 200,
"cost": 266.95, "cost": 266.95,
"price": 252.41, "price": 251.15,
"market_value": 50496.0, "market_value": 50230.0,
"change_pct": -1.29, "change_pct": -1.78,
"currency": "CNY", "currency": "CNY",
"position_pct": 6.31, "position_pct": 6.31,
"_currency": "CNY" "_currency": "CNY"
@@ -65,9 +65,9 @@
"name": "中芯国际", "name": "中芯国际",
"shares": 300, "shares": 300,
"cost": 126.07, "cost": 126.07,
"price": 141.28, "price": 140.31,
"market_value": 42324.0, "market_value": 42093.0,
"change_pct": -1.96, "change_pct": -2.63,
"currency": "CNY", "currency": "CNY",
"position_pct": 5.44, "position_pct": 5.44,
"_currency": "CNY" "_currency": "CNY"
@@ -76,22 +76,22 @@
"code": "01888", "code": "01888",
"name": "建滔积层板", "name": "建滔积层板",
"shares": 500, "shares": 500,
"cost": 88.24, "cost": 76.5,
"price": 86.0, "price": 73.09,
"market_value": 37520.0, "market_value": 36545.0,
"change_pct": 2.625, "change_pct": 0.597,
"currency": "HKD", "currency": "CNY",
"position_pct": 5.28, "position_pct": 5.28,
"_currency": "HKD" "_currency": "CNY"
}, },
{ {
"code": "688639", "code": "688639",
"name": "华恒生物", "name": "华恒生物",
"shares": 2800, "shares": 2800,
"cost": 21.51, "cost": 21.51,
"price": 16.63, "price": 16.66,
"market_value": 46564.0, "market_value": 46648.0,
"change_pct": -1.89, "change_pct": -1.71,
"currency": "CNY", "currency": "CNY",
"position_pct": 5.25, "position_pct": 5.25,
"_currency": "CNY" "_currency": "CNY"
@@ -101,9 +101,9 @@
"name": "宁德时代", "name": "宁德时代",
"shares": 100, "shares": 100,
"cost": 401.78, "cost": 401.78,
"price": 380.78, "price": 380.0,
"market_value": 38105.0, "market_value": 38000.0,
"change_pct": -0.41, "change_pct": -0.61,
"currency": "CNY", "currency": "CNY",
"position_pct": 4.64, "position_pct": 4.64,
"_currency": "CNY" "_currency": "CNY"
@@ -112,58 +112,58 @@
"code": "01211", "code": "01211",
"name": "比亚迪股份", "name": "比亚迪股份",
"shares": 600, "shares": 600,
"cost": 104.87, "cost": 90.92,
"price": 83.4, "price": 73.22,
"market_value": 43542.0, "market_value": 43932.0,
"change_pct": 6.513, "change_pct": 7.854,
"currency": "HKD", "currency": "CNY",
"position_pct": 4.62, "position_pct": 4.62,
"_currency": "HKD" "_currency": "CNY"
}, },
{ {
"code": "02202", "code": "02202",
"name": "万科企业", "name": "万科企业",
"shares": 19700, "shares": 19700,
"cost": 4.67, "cost": 4.05,
"price": 2.32, "price": 2.04,
"market_value": 39794.0, "market_value": 40188.0,
"change_pct": 4.036, "change_pct": 5.381,
"currency": "HKD", "currency": "CNY",
"position_pct": 4.6, "position_pct": 4.6,
"_currency": "HKD" "_currency": "CNY"
}, },
{ {
"code": "00700", "code": "00700",
"name": "腾讯", "name": "腾讯",
"shares": 100, "shares": 100,
"cost": 0.0, "cost": null,
"price": 435.2, "price": 374.37,
"market_value": 37801.0, "market_value": 37437.0,
"change_pct": 1.162, "change_pct": 0.372,
"currency": "HKD", "currency": "CNY",
"position_pct": null, "position_pct": null,
"_currency": "HKD" "_currency": "CNY"
}, },
{ {
"code": "00981", "code": "00981",
"name": "中芯国际", "name": "中芯国际",
"shares": 500, "shares": 500,
"cost": 75.94, "cost": 65.84,
"price": 78.15, "price": 67.06,
"market_value": 33900.0, "market_value": 33530.0,
"change_pct": -2.799, "change_pct": -3.794,
"currency": "HKD", "currency": "CNY",
"position_pct": 4.2, "position_pct": 4.2,
"_currency": "HKD" "_currency": "CNY"
}, },
{ {
"code": "300548", "code": "300548",
"name": "长芯博创", "name": "长芯博创",
"shares": 100, "shares": 100,
"cost": 231.46, "cost": 231.46,
"price": 222.45, "price": 221.01,
"market_value": 22236.0, "market_value": 22101.0,
"change_pct": 0.2, "change_pct": -0.45,
"currency": "CNY", "currency": "CNY",
"position_pct": 3.2, "position_pct": 3.2,
"_currency": "CNY" "_currency": "CNY"
@@ -173,9 +173,9 @@
"name": "黄金ETF华安", "name": "黄金ETF华安",
"shares": 2400, "shares": 2400,
"cost": 12.19, "cost": 12.19,
"price": 8.66, "price": 8.67,
"market_value": 20784.0, "market_value": 20808.0,
"change_pct": 2.22, "change_pct": 2.32,
"currency": "CNY", "currency": "CNY",
"position_pct": 2.45, "position_pct": 2.45,
"_currency": "CNY" "_currency": "CNY"
@@ -185,9 +185,9 @@
"name": "中科电气", "name": "中科电气",
"shares": 1400, "shares": 1400,
"cost": 22.29, "cost": 22.29,
"price": 14.3, "price": 14.29,
"market_value": 20006.0, "market_value": 20006.0,
"change_pct": 0.92, "change_pct": 0.85,
"currency": "CNY", "currency": "CNY",
"position_pct": 2.42, "position_pct": 2.42,
"_currency": "CNY" "_currency": "CNY"
@@ -198,7 +198,7 @@
"shares": 1400, "shares": 1400,
"cost": 14.83, "cost": 14.83,
"price": 17.6, "price": 17.6,
"market_value": 24654.0, "market_value": 24640.0,
"change_pct": 4.33, "change_pct": 4.33,
"currency": "CNY", "currency": "CNY",
"position_pct": 2.41, "position_pct": 2.41,
@@ -209,9 +209,9 @@
"name": "法拉电子", "name": "法拉电子",
"shares": 100, "shares": 100,
"cost": 147.18, "cost": 147.18,
"price": 157.93, "price": 157.06,
"market_value": 15800.0, "market_value": 15706.0,
"change_pct": -3.88, "change_pct": -4.41,
"currency": "CNY", "currency": "CNY",
"position_pct": 2.3, "position_pct": 2.3,
"_currency": "CNY" "_currency": "CNY"
@@ -220,22 +220,21 @@
"code": "01088", "code": "01088",
"name": "中国神华", "name": "中国神华",
"shares": 500, "shares": 500,
"cost": 45.89, "cost": 39.79,
"price": 39.97, "price": 34.71,
"market_value": 17340.0, "market_value": 17355.0,
"change_pct": 0.909, "change_pct": 1.111,
"currency": "HKD", "currency": "CNY",
"position_pct": 2.14, "position_pct": 2.14,
"_currency": "HKD" "_currency": "CNY"
} }
], ],
"total_assets": 957656.13, "total_assets": 853681.69,
"total_mv": 825534.2, "total_mv": 773205.69,
"stock_value": null, "stock_value": null,
"cash": 132121.93, "cash": 80476.0,
"frozen_cash": 0, "frozen_cash": 0.0,
"position_pct": 86.2, "position_pct": 90.57,
"currency": "CNY", "currency": "CNY",
"updated_at": "2026-07-03 14:45", "updated_at": "2026-07-03 16:56"
"_source": "db_sync"
} }
+20
View File
@@ -9149,6 +9149,26 @@
"event_label": "", "event_label": "",
"timestamp": "2026-07-03T14:36:05.312970", "timestamp": "2026-07-03T14:36:05.312970",
"date": "2026-07-03" "date": "2026-07-03"
},
{
"code": "300548",
"name": "长芯博创",
"event_type": "stop_loss",
"price": 221.27,
"trigger_value": "221.38",
"event_label": "",
"timestamp": "2026-07-03T14:52:17.930474",
"date": "2026-07-03"
},
{
"code": "688981",
"name": "中芯国际",
"event_type": "stop_loss",
"price": 140.59,
"trigger_value": "140.94",
"event_label": "",
"timestamp": "2026-07-03T14:52:19.831851",
"date": "2026-07-03"
} }
] ]
} }
+11 -11
View File
@@ -26,7 +26,7 @@
"date": "2026-07-03", "date": "2026-07-03",
"high": 1215.52, "high": 1215.52,
"low": 1185.0, "low": 1185.0,
"close": 1195.45 "close": 1194.45
} }
], ],
"02202": [ "02202": [
@@ -62,7 +62,7 @@
"date": "2026-07-03", "date": "2026-07-03",
"high": 50.2, "high": 50.2,
"low": 48.31, "low": 48.31,
"close": 49.06 "close": 49.09
} }
], ],
"02359": [ "02359": [
@@ -118,7 +118,7 @@
"date": "2026-07-03", "date": "2026-07-03",
"high": 502.0, "high": 502.0,
"low": 444.55, "low": 444.55,
"close": 482.68 "close": 480.32
} }
], ],
"06160": [ "06160": [
@@ -154,7 +154,7 @@
"date": "2026-07-03", "date": "2026-07-03",
"high": 687.04, "high": 687.04,
"low": 633.01, "low": 633.01,
"close": 644.5 "close": 643.81
} }
], ],
"09868": [ "09868": [
@@ -196,7 +196,7 @@
"date": "2026-07-03", "date": "2026-07-03",
"high": 757.88, "high": 757.88,
"low": 713.0, "low": 713.0,
"close": 738.2 "close": 738.38
} }
], ],
"300124": [ "300124": [
@@ -210,7 +210,7 @@
"date": "2026-07-03", "date": "2026-07-03",
"high": 74.63, "high": 74.63,
"low": 67.31, "low": 67.31,
"close": 72.22 "close": 72.15
} }
], ],
"000657": [ "000657": [
@@ -224,7 +224,7 @@
"date": "2026-07-03", "date": "2026-07-03",
"high": 101.5, "high": 101.5,
"low": 87.88, "low": 87.88,
"close": 90.33 "close": 89.63
} }
], ],
"000711": [ "000711": [
@@ -252,7 +252,7 @@
"date": "2026-07-03", "date": "2026-07-03",
"high": 892.1, "high": 892.1,
"low": 795.0, "low": 795.0,
"close": 882.5 "close": 881.91
} }
], ],
"002594": [ "002594": [
@@ -264,9 +264,9 @@
}, },
{ {
"date": "2026-07-03", "date": "2026-07-03",
"high": 88.78, "high": 88.88,
"low": 81.9, "low": 81.9,
"close": 88.72 "close": 88.47
} }
], ],
"00700": [ "00700": [
@@ -334,7 +334,7 @@
"date": "2026-07-03", "date": "2026-07-03",
"high": 646.85, "high": 646.85,
"low": 574.1, "low": 574.1,
"close": 620.31 "close": 618.02
} }
] ]
} }
+61 -201
View File
@@ -1,218 +1,78 @@
{ {
"timestamp": "2026-07-01 16:14", "timestamp": "2026-07-03 16:00",
"analyses": [ "source": "xiaoguo_fallback",
"stocks": [
{ {
"name": "中际旭创",
"code": "300308",
"price": 1223.17,
"sentiment": "negative",
"confidence": 0.59,
"brief": "中际旭创成交额超200亿元。数据宝统计,截至11:12,中际旭创成交额200.1...",
"keywords": [
"中际旭创成交额超",
"亿元",
"日下午",
"现跌",
"数据宝统计"
],
"news_count": 4,
"pos_count": 1,
"neg_count": 2,
"neu_count": 1,
"reason": "SL_dist=4.4% TP_dist=9.5%",
"priority": 1
},
{
"name": "丘钛科技",
"code": "01478",
"price": 5.95,
"sentiment": "positive",
"confidence": 0.57,
"brief": "丘钛科技5月手机摄像头模组销售数量增长39.4%。丘钛科技5月手机摄像头模组销售...",
"keywords": [
"汇顶科技",
"丘钛科技是公司的供应商之一",
"证券日报网讯",
"汇顶科技在互动平台回答投资者提问时表示",
"丘钛科技"
],
"news_count": 5,
"pos_count": 3,
"neg_count": 0,
"neu_count": 2,
"reason": "SL_dist=3.9% TP_dist=21.0%",
"priority": 1
},
{
"name": "紫金矿业",
"code": "601899",
"price": 25.11,
"sentiment": "neutral",
"confidence": 0.6,
"brief": "紫金矿业成交额超上一日全天。数据宝统计,截至14:10,紫金矿业成交额116.5...",
"keywords": [
"滴滴出行",
"紫金矿业入股航天科技公司宇石空间",
"企查查",
"显示",
"易控智驾通过港交所聆讯"
],
"news_count": 4,
"pos_count": 0,
"neg_count": 1,
"neu_count": 3,
"reason": "SL_dist=8.1% TP_dist=1.6%",
"priority": 1
},
{
"name": "中芯国际",
"code": "688981", "code": "688981",
"price": 154.48, "name": "中芯国际",
"sentiment": "positive", "sentiment": "negative",
"confidence": 0.58, "confidence": 0.75,
"brief": "南向资金今日净买入约59亿港元中芯国际获净买入目前。中芯国际、建滔积层板分别获净...", "reason": "芯片板块近期回调,国家大基金持股概念7/2日跌6.28%,主力资金大幅净流出;已跌破止损位140.94(现价140.31,距损-0.45%)",
"keywords": [ "priority": "已跌破止损-0.45%"
"中芯国际成交额超", },
"亿元", {
"数据宝统计", "code": "00981",
"截至", "name": "中芯国际(H)",
"中芯国际成交额超上一日全天" "sentiment": "negative",
], "confidence": 0.65,
"news_count": 5, "reason": "大基金减持00981等芯片股,半导体板块承压;南向资金虽有爆买但价格已跌破止损67.24(现价67.06,距损-0.27%)",
"pos_count": 2, "priority": "已跌破止损-0.27%"
"neg_count": 0,
"neu_count": 3,
"reason": "SL_dist=3.0% TP_dist=14.2%",
"priority": 1
}, },
{ {
"name": "长芯博创",
"code": "300548", "code": "300548",
"price": 254.0, "name": "长芯博创",
"sentiment": "negative",
"confidence": 0.80,
"reason": "通信行业7/2日暴跌7.36%,F5G/铜缆高速连接概念大幅回调,主力资金持续净流出;已跌破止损221.38(现价221.01,距损-0.17%)",
"priority": "已跌破止损-0.17%"
},
{
"code": "00700",
"name": "腾讯",
"sentiment": "positive", "sentiment": "positive",
"confidence": 0.62, "confidence": 0.78,
"brief": "长芯博创龙虎榜数据(6月15日)。长芯博创今日涨停,全天换手率9.65%,成交额...", "reason": "连续31日回购,每日5.01亿港元,累计回购超249亿,管理层信心强烈;股价距止损366.84仅2.06%,回购托底意愿明确",
"keywords": [ "priority": "距止损2.06%"
"长芯博创",
"收购控股子公司少数股东股份实施完成",
"公告称",
"公司已完成收购控股子公司长芯盛",
"长芯博创成交额创上市以来新高"
],
"news_count": 5,
"pos_count": 4,
"neg_count": 0,
"neu_count": 1,
"reason": "SL_dist=3.0% TP_dist=14.8%",
"priority": 1
}, },
{ {
"name": "黄金ETF华安", "code": "300308",
"code": "518880", "name": "中际旭创",
"price": 8.27, "sentiment": "neutral",
"confidence": 0.60,
"reason": "6月获融资客40亿净买入,CPO概念年初至今涨幅可观;但通信板块7/2大跌7.36%,短期面临获利回吐压力,距止损1090.4约2.29%",
"priority": "距止损2.29%"
},
{
"code": "06869",
"name": "长飞光纤光缆",
"sentiment": "positive", "sentiment": "positive",
"confidence": 0.61, "confidence": 0.72,
"brief": "ETF融资榜|黄金ETF华安(518880)融资净买入558.22万元,居可比基...", "reason": "7/3除净派息0.339港元/股,南向资金7/2净买入1.77亿港元,6/30曾涨超7%;距止损169.46约2.56%,股息+资金面支撑",
"keywords": [ "priority": "距止损2.56%"
"融资榜",
"黄金",
"华安",
"融资净买入",
"融券榜"
],
"news_count": 5,
"pos_count": 4,
"neg_count": 0,
"neu_count": 1,
"reason": "SL_dist=9.9% TP_dist=3.7%",
"priority": 1
}, },
{ {
"name": "中科电气", "code": "01088",
"code": "300035", "name": "中国神华",
"price": 14.44,
"sentiment": "neutral",
"confidence": 0.56,
"brief": "中科电气终止三大锂电负极项目涉及年产能33万吨。对于此次项目终止事宜,中科电气方...",
"keywords": [
"中科电气终止三大锂电负极项目涉及年产能",
"万吨",
"对于此次项目终止事宜",
"中科电气方面未向记者进一步回复",
"中科电气集中清理暂缓项目聚焦推进泸州与阿曼两大重点项目"
],
"news_count": 5,
"pos_count": 0,
"neg_count": 1,
"neu_count": 4,
"reason": "SL_dist=9.9% TP_dist=4.8%",
"priority": 1
},
{
"name": "模塑科技",
"code": "000700",
"price": 15.34,
"sentiment": "neutral",
"confidence": 0.56,
"brief": "模塑科技:基于商业保密要求,公司不便透露与客户的具体合作信息。证券日报网讯6月1...",
"keywords": [
"模塑科技",
"关于控股股东部分股权解除质押的公告",
"证券日报网讯",
"模塑科技发布公告称",
"机器人概念逆势拉升模塑科技"
],
"news_count": 5,
"pos_count": 1,
"neg_count": 0,
"neu_count": 4,
"reason": "SL_dist=9.3% TP_dist=1.3%",
"priority": 1
},
{
"name": "法拉电子",
"code": "600563",
"price": 182.55,
"sentiment": "neutral", "sentiment": "neutral",
"confidence": 0.55, "confidence": 0.55,
"brief": "法拉电子:2025年年度权益分派实施公告。证券日报网讯6月4日,法拉电子发布20...", "reason": "定州三期/沧东三期机组投运,子公司签约8.6亿合同,基本面稳健;但煤炭板块近期走弱,港股煤炭股6/30集体下跌,消息面中性偏淡",
"keywords": [ "priority": "距止损2.63%"
"法拉电子",
"年年度权益分派实施公告",
"证券日报网讯",
"法拉电子发布",
"三季度订单充足生产处于满产状态"
],
"news_count": 5,
"pos_count": 0,
"neg_count": 0,
"neu_count": 5,
"reason": "SL_dist=6.9% TP_dist=3.1%",
"priority": 1
}, },
{ {
"name": "长飞光纤光缆", "code": "601899",
"code": "06869", "name": "紫金矿业",
"price": 221.69, "sentiment": "positive",
"sentiment": "negative", "confidence": 0.82,
"confidence": 0.58, "reason": "贵金属概念7/3大涨4.84%,紫金矿业涨5.32%,美联储释放通胀回落信号利好金价;公司布局航天投资拓展成长空间",
"brief": "港股评级汇总:野村维持对长飞光纤光缆买入评级。财联社6月29日讯以下为各家机构对...", "priority": "涨跌幅+5.78%"
"keywords": [ },
"港股光通信股走强长飞光纤光缆涨超", {
"截至发稿", "code": "01211",
"长飞光纤光缆", "name": "比亚迪股份",
"海光芯正", "sentiment": "positive",
"日将派发股息" "confidence": 0.85,
], "reason": "新能源汽车零售渗透率高达63%,板块持续走强,7/2比亚迪涨超9%领涨;行业景气度高位,动力电池/整车双轮驱动",
"news_count": 5, "priority": "涨跌幅+6.90%"
"pos_count": 1,
"neg_count": 2,
"neu_count": 2,
"reason": "涨跌幅+6.68%",
"priority": 2
} }
], ]
"status": "ok", }
"total_analyzed": 10,
"total_candidates": 10
}
+3 -7
View File
@@ -250,14 +250,12 @@ def refresh_data_prices():
if s['code'] in prices: if s['code'] in prices:
price, _, change_pct = prices[s['code']] price, _, change_pct = prices[s['code']]
if price > 0: if price > 0:
# 港股API返回HKD需转CNY。系统统一存CNY标价 # 港股API返回HKD直接存HKD原值。港股标价存HKD,A股标价存CNY
if is_hk_stock(s['code']):
price = round(price * HK_RATE, 2)
old = s.get('price') or 0 old = s.get('price') or 0
if abs(old - price) > 0.001: if abs(old - price) > 0.001:
s['price'] = round(price, 2) s['price'] = round(price, 2)
s['change_pct'] = float(change_pct) if change_pct else 0 s['change_pct'] = float(change_pct) if change_pct else 0
s['currency'] = 'CNY' s['currency'] = 'HKD' if is_hk_stock(s['code']) else 'CNY'
updated += 1 updated += 1
changed = True changed = True
if changed: if changed:
@@ -301,13 +299,11 @@ def refresh_data_prices():
if s['code'] in prices: if s['code'] in prices:
price, _, change_pct = prices[s['code']] price, _, change_pct = prices[s['code']]
if price > 0: if price > 0:
# 港股:API返回HKD,需转CNY
if is_hk_stock(s['code']):
price = round(price * HK_RATE, 2)
old = s.get('price') or 0 old = s.get('price') or 0
if abs(old - price) > 0.001: if abs(old - price) > 0.001:
s['price'] = round(price, 2) s['price'] = round(price, 2)
s['change_pct'] = float(change_pct) if change_pct else 0 s['change_pct'] = float(change_pct) if change_pct else 0
s['currency'] = 'HKD' if is_hk_stock(s['code']) else 'CNY'
updated += 1 updated += 1
changed = True changed = True
if changed: if changed:
+163 -167
View File
@@ -1,167 +1,163 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
per_stock_reassess.py — 按个股触发重评 per_stock_reassess.py — 按个股触发重评
对每只传进来的 code 执行 reassess_strategy(),然后只更新 对每只传进来的 code 执行 reassess_strategy(),然后只更新
decisions.json 中对应的那一条记录。不碰 portfolio.json,不跑全量。 decisions.json 中对应的那一条记录。不碰 portfolio.json,不跑全量。
""" """
import sys, json, os, re import sys, json, os, re
sys.path.insert(0, "/home/hmo/web-dashboard") sys.path.insert(0, "/home/hmo/web-dashboard")
from strategy_lifecycle import reassess_with_context as reassess_strategy sys.path.insert(0, "/home/hmo/MoFin")
from mo_data import read_decisions, read_portfolio from strategy_lifecycle import reassess_with_context as reassess_strategy
from mo_data import read_decisions, read_portfolio
sys.path.insert(0, "/home/hmo/MoFin")
from mofin_db import get_conn, write_holding_strategy DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
def main():
codes = [a for a in sys.argv[1:] if not a.startswith("-")]
def main(): if not codes:
codes = [a for a in sys.argv[1:] if not a.startswith("-")] print("[FULL] 无指定编码,跑全量 regenerate_all()")
if not codes: from strategy_lifecycle import regenerate_all
print("[FULL] 无指定编码,跑全量 regenerate_all()") regenerate_all(stdout=False)
from strategy_lifecycle import regenerate_all print("[FULL] 全量重评完成")
regenerate_all(stdout=False) return
print("[FULL] 全量重评完成")
return # 读现有 decisions
raw = read_decisions()
# 读现有 decisions decisions_map = {d["code"]: d for d in raw.get("decisions", []) if d.get("code")}
raw = read_decisions()
decisions_map = {d["code"]: d for d in raw.get("decisions", []) if d.get("code")} ok = 0
errors = 0
ok = 0 skipped = 0
errors = 0 for code in codes:
skipped = 0 entry = decisions_map.get(code)
for code in codes: if not entry:
entry = decisions_map.get(code) print(f"[SKIP] {code}: 不在 decisions.json 中")
if not entry: errors += 1
print(f"[SKIP] {code}: 不在 decisions.json 中") continue
errors += 1
continue try:
# Always fetch live price for accurate reassessment
try: price = 0
# Always fetch live price for accurate reassessment try:
price = 0 # 价格从 DB 读取(price_monitor 每2分钟更新,唯一价格入口)
try: code_raw = entry.get("code", "")
# 价格从 DB 读取(price_monitor 每2分钟更新,唯一价格入口) price = 0
code_raw = entry.get("code", "") import sqlite3
price = 0 db = sqlite3.connect('/home/hmo/web-dashboard/data/mofin.db')
import sqlite3 db.row_factory = sqlite3.Row
db = sqlite3.connect('/home/hmo/web-dashboard/data/mofin.db') row = db.execute("SELECT price FROM holdings WHERE code=? AND is_active=1", (code_raw,)).fetchone()
db.row_factory = sqlite3.Row if not row:
row = db.execute("SELECT price FROM holdings WHERE code=? AND is_active=1", (code_raw,)).fetchone() row = db.execute("SELECT price FROM watchlist_stocks WHERE code=? AND is_active=1", (code_raw,)).fetchone()
if not row: if not row:
row = db.execute("SELECT price FROM watchlist_stocks WHERE code=? AND is_active=1", (code_raw,)).fetchone() row = db.execute("SELECT price FROM holding_strategies WHERE code=? AND status='active' ORDER BY updated_at DESC LIMIT 1", (code_raw,)).fetchone()
if not row: if row:
row = db.execute("SELECT price FROM holding_strategies WHERE code=? AND status='active' ORDER BY updated_at DESC LIMIT 1", (code_raw,)).fetchone() price = row['price'] or 0
if row: db.close()
price = row['price'] or 0 if price > 0:
db.close() print(f" 实时价: {price} (来自DB)")
if price > 0: else:
print(f" 实时价: {price} (来自DB)") # fallback to portfolio.json
if price <= 0: _pf_data = read_portfolio()
price = entry.get("current_price") or entry.get("price") or 0 for _h in _pf_data.get("holdings", []):
except Exception as e: if _h["code"] == code_raw:
print(f" 价格获取失败: {e}", file=sys.stderr) price = float(_h.get("price", 0))
price = entry.get("current_price") or entry.get("price") or 0 break
if price <= 0:
# Price diff debounce: skip reassessment if price changed < 1% since last update price = entry.get("current_price") or entry.get("price") or 0
last_price = entry.get("last_reassessed_price", 0) except Exception as e:
if last_price > 0 and price > 0: print(f" 价格获取失败: {e}", file=sys.stderr)
diff_pct = abs(price - last_price) / last_price * 100 price = entry.get("current_price") or entry.get("price") or 0
if diff_pct < 1.0:
print(f" 价差仅{diff_pct:.2f}% (<1%),跳过重评(上次价={last_price},现价={price}") # Price diff debounce: skip reassessment if price changed < 1% since last update
skipped += 1 last_price = entry.get("last_reassessed_price", 0)
continue if last_price > 0 and price > 0:
result = reassess_strategy( diff_pct = abs(price - last_price) / last_price * 100
code=code, if diff_pct < 1.0:
name=entry.get("name", ""), print(f" 价差仅{diff_pct:.2f}% (<1%),跳过重评(上次价={last_price},现价={price}")
price=price, skipped += 1
cost=entry.get("cost", 0), continue
shares=entry.get("shares", 0), result = reassess_strategy(
current_action=entry.get("action", ""), code=code,
is_watchlist=entry.get("type", "") in ("自选策略", "watchlist"), name=entry.get("name", ""),
) price=price,
if result and result.get("action"): cost=entry.get("cost", 0),
# 持仓股止损不下移(移动止损规则):已有仓位的止损只上不下 shares=entry.get("shares", 0),
is_held = entry.get("cost", 0) > 0 and entry.get("shares", 0) > 0 and \ current_action=entry.get("action", ""),
entry.get("type", "") not in ("自选策略", "watchlist") is_watchlist=entry.get("type", "") in ("自选策略", "watchlist"),
old_stop = entry.get("stop_loss", 0) )
new_stop = result.get("stop_loss", 0) if result and result.get("action"):
if is_held and old_stop > 0 and new_stop > 0 and new_stop < old_stop: # 持仓股止损不下移(移动止损规则):已有仓位的止损只上不下
print(f" 移动止损保护: {new_stop}→保持{old_stop} (持仓止损不下移)") is_held = entry.get("cost", 0) > 0 and entry.get("shares", 0) > 0 and \
result["stop_loss"] = old_stop entry.get("type", "") not in ("自选策略", "watchlist")
# 同时更新 action 字符串中的止损值 old_stop = entry.get("stop_loss", 0)
act = result.get("action", "") new_stop = result.get("stop_loss", 0)
if act: if is_held and old_stop > 0 and new_stop > 0 and new_stop < old_stop:
act = re.sub(r'止损[\d.]+', f'止损{old_stop}', act) print(f" 移动止损保护: {new_stop}→保持{old_stop} (持仓止损不下移)")
result["action"] = act result["stop_loss"] = old_stop
# 同时更新 action 字符串中的止损值
# 更新 decisions_map 中对应的条目 act = result.get("action", "")
updated = entry.copy() if act:
# 币种标记:HK股保留HKD原始值,A股为CNY act = re.sub(r'止损[\d.]+', f'止损{old_stop}', act)
is_hk = len(str(code)) == 5 and str(code)[0] in '01' result["action"] = act
updated.update({
"action": result["action"], # 更新 decisions_map 中对应的条目
"stop_loss": result.get("stop_loss", entry.get("stop_loss")), updated = entry.copy()
"entry_low": result.get("entry_low", entry.get("entry_low")), # 币种标记:HK股保留HKD原始值,A股为CNY
"entry_high": result.get("entry_high", entry.get("entry_high")), is_hk = len(str(code)) == 5 and str(code)[0] in '01'
"take_profit": result.get("take_profit"), updated.update({
"tech_snapshot": result.get("tech_snapshot", entry.get("tech_snapshot")), "action": result["action"],
"timing_signal": result.get("timing_signal", entry.get("timing_signal")), "stop_loss": result.get("stop_loss", entry.get("stop_loss")),
"rr_ratio": result.get("rr_ratio", entry.get("rr_ratio", 0)), "entry_low": result.get("entry_low", entry.get("entry_low")),
"status": result.get("status", "updated"), "entry_high": result.get("entry_high", entry.get("entry_high")),
"price": price, "take_profit": result.get("take_profit"),
"currency": "HKD" if is_hk else "CNY", "tech_snapshot": result.get("tech_snapshot", entry.get("tech_snapshot")),
}) "timing_signal": result.get("timing_signal", entry.get("timing_signal")),
# Save last reassessed price for debounce tracking "rr_ratio": result.get("rr_ratio", entry.get("rr_ratio", 0)),
updated["last_reassessed_price"] = price "status": result.get("status", "updated"),
decisions_map[code] = updated "price": price,
# ——— 初始化多分支策略树 ——— "currency": "HKD" if is_hk else "CNY",
try: })
sys.path.insert(0, '/home/hmo/MoFin') # Save last reassessed price for debounce tracking
from strategy_tree import init_default_branches updated["last_reassessed_price"] = price
branches = init_default_branches( decisions_map[code] = updated
code, # ——— 初始化多分支策略树 ———
entry.get('name', ''), try:
result.get('entry_low', 0), sys.path.insert(0, '/home/hmo/MoFin')
result.get('entry_high', 0), from strategy_tree import init_default_branches
result.get('stop_loss', 0), branches = init_default_branches(
result.get('take_profit', 0), code,
) entry.get('name', ''),
st = updated.setdefault('strategy_tree', {}) result.get('entry_low', 0),
st['branches'] = branches result.get('entry_high', 0),
except Exception: result.get('stop_loss', 0),
pass result.get('take_profit', 0),
print(f"[OK] {code} {entry.get('name','')}: {result['action'][:80]}") )
ok += 1 st = updated.setdefault('strategy_tree', {})
else: st['branches'] = branches
print(f"[SYNCED] {code}: 无变更") except Exception:
ok += 1 pass
except Exception as e: print(f"[OK] {code} {entry.get('name','')}: {result['action'][:80]}")
print(f"[ERROR] {code}: {e}", file=sys.stderr) ok += 1
errors += 1 else:
print(f"[SYNCED] {code}: 无变更")
# 写回 decisions.json(只更新被修改的那条,其余保留原样) ok += 1
raw["decisions"] = list(decisions_map.values()) except Exception as e:
raw["total"] = len(raw["decisions"]) print(f"[ERROR] {code}: {e}", file=sys.stderr)
from datetime import datetime errors += 1
raw["regenerated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M")
# DB 写入(替代 json.dump # 写回 decisions.json(只更新被修改的那条,其余保留原样
try: raw["decisions"] = list(decisions_map.values())
conn = get_conn() raw["total"] = len(raw["decisions"])
for d in raw.get("decisions", []): from datetime import datetime
write_holding_strategy(conn, d.get("code", ""), d.get("name", ""), d) raw["regenerated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M")
conn.close() with open(DECISIONS_PATH, "w") as f:
except Exception: json.dump(raw, f, ensure_ascii=False, indent=2)
pass
# [migrated to DB] — cold backup removed print(f"[DONE] {ok}成功 {skipped}跳过 {errors}失败")
# with open(DECISIONS_PATH, "w") as f:
# json.dump(raw, f, ensure_ascii=False, indent=2)
if __name__ == "__main__":
print(f"[DONE] {ok}成功 {skipped}跳过 {errors}失败") main()
if __name__ == "__main__":
main()
+288 -286
View File
@@ -1,286 +1,288 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""stale_detector.py — 检查所有策略,标记价格偏离/过期的策略 """stale_detector.py — 检查所有策略,标记价格偏离/过期的策略
读取 decisions.json 的扁平列表。自选策略和持仓策略分开判断。 读取 decisions.json 的扁平列表。自选策略和持仓策略分开判断。
可被 cron no_agent 模式调用:stdout 注入到后续 LLM 分析。 可被 cron no_agent 模式调用:stdout 注入到后续 LLM 分析。
输出格式: 输出格式:
[FLAG] [自选/持仓] 股票名(代码) 价XX | 买入A~B | 问题 [FLAG] [自选/持仓] 股票名(代码) 价XX | 买入A~B | 问题
用法: 用法:
python3 stale_detector.py python3 stale_detector.py
""" """
import json import json
import sys import sys
import os import os
from datetime import datetime, timezone from datetime import datetime, timezone
from mo_data import read_portfolio, read_decisions, read_watchlist sys.path.insert(0, '/home/hmo/MoFin')
from mo_data import read_portfolio, read_decisions, read_watchlist
DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json" DECISIONS_PATH = "/home/hmo/web-dashboard/data/decisions.json"
PORTFOLIO_PATH = "/home/hmo/web-dashboard/data/portfolio.json"
def fetch_prices(codes):
"""统一价格源:优先 stock_quote.py,腾讯API降级为兜底""" def fetch_prices(codes):
if not codes: """统一价格源:优先 stock_quote.py,腾讯API降级为兜底"""
return {} if not codes:
# 尝试用 stock_quote.py 获取(脚本强制规范) return {}
try: # 尝试用 stock_quote.py 获取(脚本强制规范)
import subprocess try:
script = None import subprocess
for p in ["/home/hmo/MoFin/scripts/stock_quote.py", "/home/hmo/MoFin/stock_quote.py"]: script = None
if os.path.exists(p): for p in ["/home/hmo/MoFin/scripts/stock_quote.py", "/home/hmo/MoFin/stock_quote.py"]:
script = p if os.path.exists(p):
break script = p
if script: break
result = subprocess.run( if script:
[sys.executable, script] + [str(c) for c in codes], result = subprocess.run(
capture_output=True, text=True, timeout=30 [sys.executable, script] + [str(c) for c in codes],
) capture_output=True, text=True, timeout=30
if result.returncode == 0 and result.stdout.strip(): )
results = {} if result.returncode == 0 and result.stdout.strip():
for line in result.stdout.strip().split("\n"): results = {}
if not line.strip(): for line in result.stdout.strip().split("\n"):
continue if not line.strip():
try: continue
item = json.loads(line) try:
code = str(item.get("code", "")) item = json.loads(line)
price = item.get("price") code = str(item.get("code", ""))
change = item.get("change_pct", 0) price = item.get("price")
if code and price is not None: change = item.get("change_pct", 0)
results[code] = (float(price), float(change)) if code and price is not None:
except (json.JSONDecodeError, ValueError): results[code] = (float(price), float(change))
continue except (json.JSONDecodeError, ValueError):
if results: continue
return results if results:
except Exception as e: return results
print(f"[STALE] stock_quote.py 回退: {e}", file=sys.stderr) except Exception as e:
print(f"[STALE] stock_quote.py 回退: {e}", file=sys.stderr)
# 兜底:腾讯API(不应依赖,仅作为最后手段)
import urllib.request # 兜底:腾讯API(不应依赖,仅作为最后手段)
symbols, code_map = [], {} import urllib.request
for c in codes: symbols, code_map = [], {}
c = str(c).strip() for c in codes:
p = "sh" if (len(c) == 6 and c[0] in "569") else "sz" if len(c) == 6 else "hk" c = str(c).strip()
sym = f"{p}{c}" p = "sh" if (len(c) == 6 and c[0] in "569") else "sz" if len(c) == 6 else "hk"
symbols.append(sym) sym = f"{p}{c}"
code_map[sym] = c symbols.append(sym)
try: code_map[sym] = c
req = urllib.request.Request( try:
f"http://qt.gtimg.cn/q={','.join(symbols)}", req = urllib.request.Request(
headers={"User-Agent": "curl/7.81"}, f"http://qt.gtimg.cn/q={','.join(symbols)}",
) headers={"User-Agent": "curl/7.81"},
with urllib.request.urlopen(req, timeout=10) as r: )
text = r.read().decode("gbk") with urllib.request.urlopen(req, timeout=10) as r:
except Exception as e: text = r.read().decode("gbk")
print(f"FETCH_FAIL (fallback): {e}", file=sys.stderr) except Exception as e:
return {} print(f"FETCH_FAIL (fallback): {e}", file=sys.stderr)
return {}
results = {}
for line in text.strip().split("\n"): results = {}
if "=" not in line: for line in text.strip().split("\n"):
continue if "=" not in line:
try: continue
raw = line.split("=", 1)[1].strip().strip('"').strip(";") try:
fld = raw.split("~") raw = line.split("=", 1)[1].strip().strip('"').strip(";")
if len(fld) < 6: fld = raw.split("~")
continue if len(fld) < 6:
sym = line.split("=", 1)[0].strip().lstrip("v_") continue
oc = code_map.get(sym) sym = line.split("=", 1)[0].strip().lstrip("v_")
if not oc: oc = code_map.get(sym)
continue if not oc:
p = float(fld[3]) if fld[3] else 0 continue
c = fld[32] if len(fld) > 32 else "0" p = float(fld[3]) if fld[3] else 0
results[oc] = (p, c) c = fld[32] if len(fld) > 32 else "0"
except (ValueError, IndexError): results[oc] = (p, c)
continue except (ValueError, IndexError):
return results continue
return results
def main():
decisions_list = mo_data.read_decisions() def main():
if not isinstance(decisions_list, list): decisions_list = read_decisions()
decisions_list = decisions_list.get("decisions", []) if isinstance(decisions_list, dict) else [] if not isinstance(decisions_list, list):
decisions_list = decisions_list.get("decisions", []) if isinstance(decisions_list, dict) else []
# 只保留有买入区的条目,排除已关闭的(inactive/closed
EXCLUDED_STATUSES = ("closed", "inactive") # 只保留有买入区的条目,排除已关闭的(inactive/closed
to_check = [d for d in decisions_list if (d.get("entry_low") is not None or d.get("entry_high") is not None) and d.get("status") not in EXCLUDED_STATUSES] EXCLUDED_STATUSES = ("closed", "inactive")
if not to_check: to_check = [d for d in decisions_list if (d.get("entry_low") is not None or d.get("entry_high") is not None) and d.get("status") not in EXCLUDED_STATUSES]
print("[SILENT] 无需要检查的策略") if not to_check:
return 0 print("[SILENT] 无需要检查的策略")
return 0
# ----- 组合级监测:读取总仓位 + 弱势比例 -----
position_pct = 0 # ----- 组合级监测:读取总仓位 + 弱势比例 -----
cash = 0 position_pct = 0
total_assets = 0 cash = 0
try: total_assets = 0
pf = read_portfolio() try:
position_pct = pf.get("position_pct", 0) with open(PORTFOLIO_PATH) as f:
cash = pf.get("cash", 0) pf = json.load(f)
total_assets = pf.get("total_assets", 0) position_pct = pf.get("position_pct", 0)
except Exception: cash = pf.get("cash", 0)
pass total_assets = pf.get("total_assets", 0)
# 统计持仓策略中弱势/深套的比例 except Exception:
weak_count = 0 pass
holding_count = 0 # 统计持仓策略中弱势/深套的比例
for d in decisions_list: weak_count = 0
if d.get("type") == "持仓策略" and d.get("status") not in ("closed", "inactive"): holding_count = 0
holding_count += 1 for d in decisions_list:
cat = d.get("stock_category", "") if d.get("type") == "持仓策略" and d.get("status") not in ("closed", "inactive"):
if cat in ("弱势", "深套"): holding_count += 1
weak_count += 1 cat = d.get("stock_category", "")
weak_ratio = (weak_count / holding_count * 100) if holding_count > 0 else 0 if cat in ("弱势", "深套"):
weak_count += 1
prices = fetch_prices([d["code"] for d in to_check]) weak_ratio = (weak_count / holding_count * 100) if holding_count > 0 else 0
now = datetime.now(timezone.utc).astimezone()
found = 0 prices = fetch_prices([d["code"] for d in to_check])
now = datetime.now(timezone.utc).astimezone()
for d in to_check: found = 0
code = d["code"]
name = d.get("name", code) for d in to_check:
el = d.get("entry_low") code = d["code"]
eh = d.get("entry_high") name = d.get("name", code)
sl = d.get("stop_loss") el = d.get("entry_low")
tp = d.get("take_profit") eh = d.get("entry_high")
ts = d.get("created_at") or d.get("timestamp") or d.get("updated_at", "") sl = d.get("stop_loss")
is_wl = "自选" in (d.get("type", "")) tp = d.get("take_profit")
ts = d.get("created_at") or d.get("timestamp") or d.get("updated_at", "")
pi = prices.get(code) is_wl = "自选" in (d.get("type", ""))
if not pi:
continue pi = prices.get(code)
price, chg = pi if not pi:
if price <= 0: continue
continue price, chg = pi
if price <= 0:
issues, flags = [], [] continue
tag = "[自选]" if is_wl else "[持仓]"
issues, flags = [], []
# -- 偏离 -- tag = "[自选]" if is_wl else "[持仓]"
if is_wl and el and eh:
# 读取 timing_signal 判断策略有效性(timing_signal 字段优先,fallback to action # -- 偏离 --
current_str = d.get("current", "") or "" if is_wl and el and eh:
timing_signal = d.get("timing_signal", "") or current_str # 读取 timing_signal 判断策略有效性(timing_signal 字段优先,fallback to action
has_nonbuy_signal = any(kw in timing_signal for kw in [ current_str = d.get("current", "") or ""
"等企稳再入", "等企稳", "弱势持有", "观望", timing_signal = d.get("timing_signal", "") or current_str
"不建议买入", "谨慎买入", has_nonbuy_signal = any(kw in timing_signal for kw in [
]) "等企稳再入", "等企稳", "弱势持有", "观望",
"不建议买入", "谨慎买入",
# 直接计算 R/R(不依赖文本匹配) ])
rr_invalid = False
if sl and sl > 0 and tp and tp > 0 and price > sl: # 直接计算 R/R(不依赖文本匹配)
rr = (tp - price) / (price - sl) rr_invalid = False
if rr < 1.5: if sl and sl > 0 and tp and tp > 0 and price > sl:
rr_invalid = True rr = (tp - price) / (price - sl)
# 也检查 tp 是否接近或低于成本(微盈/浮亏止盈) if rr < 1.5:
cost = d.get("cost", 0) rr_invalid = True
if cost and cost > 0 and tp <= cost * 1.05: # 也检查 tp 是否接近或低于成本(微盈/浮亏止盈)
rr_invalid = True cost = d.get("cost", 0)
if cost and cost > 0 and tp <= cost * 1.05:
strategy_deficient = has_nonbuy_signal or rr_invalid rr_invalid = True
# 对自选无止盈位的也标记(策略不完整)
if not tp or tp == 0: strategy_deficient = has_nonbuy_signal or rr_invalid
strategy_deficient = True # 对自选无止盈位的也标记(策略不完整)
if not tp or tp == 0:
if el <= price <= eh: strategy_deficient = True
flags.append("[WL_IN]")
if strategy_deficient: if el <= price <= eh:
flags.append("[STRATEGY_STALE]") flags.append("[WL_IN]")
prefix = "⚠️仓位挤占 " if position_pct > 80 else "" if strategy_deficient:
issues.append(f"[STRATEGY_STALE] {prefix}{price:.2f}在买入区{el}~{eh}但策略不完整({'RR='+f'{rr:.2f}<1.5' if rr_invalid else '无止盈位' if not tp else '非买入信号'}),买入区需重评") flags.append("[STRATEGY_STALE]")
else: prefix = "⚠️仓位挤占 " if position_pct > 80 else ""
prefix = "⚠️仓位挤占 " if position_pct > 80 else "" issues.append(f"[STRATEGY_STALE] {prefix}{price:.2f}在买入区{el}~{eh}但策略不完整({'RR='+f'{rr:.2f}<1.5' if rr_invalid else '无止盈位' if not tp else '非买入信号'}),买入区需重评")
issues.append(f"[PUSH] {prefix}{price:.2f}入买入区{el}~{eh}") else:
elif price > eh * 1.35: prefix = "⚠️仓位挤占 " if position_pct > 80 else ""
flags.append("[WL_HIGH]") issues.append(f"[PUSH] {prefix}{price:.2f}入买入区{el}~{eh}")
issues.append(f"{price:.2f}高出买入区+{((price/eh)-1)*100:.0f}%,买入区需重评") elif price > eh * 1.35:
elif price > eh * 1.20: flags.append("[WL_HIGH]")
flags.append("[WL_DRIFT]") issues.append(f"{price:.2f}高出买入区+{((price/eh)-1)*100:.0f}%,买入区需重评")
issues.append(f"{price:.2f}高于买入区+{((price/eh)-1)*100:.0f}%") elif price > eh * 1.20:
elif not is_wl and eh: flags.append("[WL_DRIFT]")
dp = (price / eh - 1) * 100 issues.append(f"{price:.2f}高于买入区+{((price/eh)-1)*100:.0f}%")
if dp > 35: elif not is_wl and eh:
flags.append("[SEVERE]") dp = (price / eh - 1) * 100
issues.append(f"偏离买入区上沿+{dp:.0f}%") if dp > 35:
elif dp > 20: flags.append("[SEVERE]")
flags.append("[DRIFT]") issues.append(f"偏离买入区上沿+{dp:.0f}%")
issues.append(f"偏离买入区上沿+{dp:.0f}%") elif dp > 20:
elif dp > 10: flags.append("[DRIFT]")
flags.append("[WARN]") issues.append(f"偏离买入区上沿+{dp:.0f}%")
issues.append(f"偏离买入区上沿+{dp:.0f}%") elif dp > 10:
# 持仓在买入区内但 R/R 不达标 flags.append("[WARN]")
if el and sl and sl > 0 and tp and tp > 0 and price > sl: issues.append(f"偏离买入区上沿+{dp:.0f}%")
if el <= price <= eh: # 持仓在买入区内但 R/R 不达标
rr = (tp - price) / (price - sl) if el and sl and sl > 0 and tp and tp > 0 and price > sl:
if rr < 1.5: if el <= price <= eh:
flags.append("[RR_WARN]") rr = (tp - price) / (price - sl)
issues.append(f"买入区内RR仅{rr:.2f}<1.5,策略需重评") if rr < 1.5:
flags.append("[RR_WARN]")
# -- 距止损/止盈(仅持仓) -- issues.append(f"买入区内RR仅{rr:.2f}<1.5,策略需重评")
if not is_wl:
if sl and sl > 0: # -- 距止损/止盈(仅持仓) --
dsl = (price / sl - 1) * 100 if not is_wl:
if dsl < 5: if sl and sl > 0:
# 成本基准校验:浮盈>5%时止损是利润保护,不是危险信号 dsl = (price / sl - 1) * 100
# (mirrors NEAR_TP cost_check logic at line 195-198) if dsl < 5:
cost = d.get("cost") # 成本基准校验:浮盈>5%时止损是利润保护,不是危险信号
if cost and cost > 0 and price > cost * 1.05: # (mirrors NEAR_TP cost_check logic at line 195-198)
flags.append("[PROFIT_PROTECT]") cost = d.get("cost")
pnl = (price / cost - 1) * 100 if cost and cost > 0 and price > cost * 1.05:
issues.append(f"距止损仅{dsl:.1f}%(利润保护,浮盈{pnl:.0f}%)") flags.append("[PROFIT_PROTECT]")
else: pnl = (price / cost - 1) * 100
flags.append("[NEAR_SL]") issues.append(f"距止损仅{dsl:.1f}%(利润保护,浮盈{pnl:.0f}%)")
issues.append(f"距止损仅{dsl:.1f}%") else:
if tp and tp > 0: flags.append("[NEAR_SL]")
dtp = (tp / price - 1) * 100 issues.append(f"距止损仅{dsl:.1f}%")
if dtp < 5: if tp and tp > 0:
# 成本基准校验:止盈标记只有在盈利≥5%时才有效 dtp = (tp / price - 1) * 100
cost_check = True if dtp < 5:
cost = d.get("cost") # 成本基准校验:止盈标记只有在盈利≥5%时才有效
if cost and cost > 0 and price < cost * 1.05: cost_check = True
cost_check = False cost = d.get("cost")
if cost_check: if cost and cost > 0 and price < cost * 1.05:
flags.append("[NEAR_TP]") cost_check = False
issues.append(f"距止盈仅{dtp:.1f}%") if cost_check:
flags.append("[NEAR_TP]")
# -- 过期 -- issues.append(f"距止盈仅{dtp:.1f}%")
stale_limit = 30 if is_wl else 14
if ts: # -- 过期 --
try: stale_limit = 30 if is_wl else 14
ud = datetime.fromisoformat(ts) if ts:
if ud.tzinfo is None: try:
ud = ud.replace(tzinfo=timezone.utc) ud = datetime.fromisoformat(ts)
days = (now - ud).days if ud.tzinfo is None:
if days > stale_limit: ud = ud.replace(tzinfo=timezone.utc)
flags.append("[STALE]") days = (now - ud).days
issues.append(f"{days}天未更新(>{stale_limit})") if days > stale_limit:
except (ValueError, TypeError): flags.append("[STALE]")
pass issues.append(f"{days}天未更新(>{stale_limit})")
except (ValueError, TypeError):
if issues: pass
print(f"{' '.join(flags)} {tag} {name}({code}) 价{price:.2f}{chg} | 买入{el}~{eh} | {'; '.join(issues)}")
found += 1 if issues:
print(f"{' '.join(flags)} {tag} {name}({code}) 价{price:.2f}{chg} | 买入{el}~{eh} | {'; '.join(issues)}")
if found == 0: found += 1
print("[SILENT] 所有策略正常")
if found == 0:
# ----- 组合级警报 ----- print("[SILENT] 所有策略正常")
portfolio_alerts = 0
if holding_count > 0: # ----- 组合级警报 -----
if weak_ratio > 40: portfolio_alerts = 0
print(f"\n[PORTFOLIO_WEAK] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count})!仓位{position_pct:.1f}% → 建议系统性减仓") if holding_count > 0:
portfolio_alerts += 1 if weak_ratio > 40:
elif weak_ratio > 30: print(f"\n[PORTFOLIO_WEAK] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count})!仓位{position_pct:.1f}% → 建议系统性减仓")
print(f"\n[PORTFOLIO_WEAK_MILD] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count}),仓位{position_pct:.1f}%,关注") portfolio_alerts += 1
portfolio_alerts += 1 elif weak_ratio > 30:
if position_pct > 80 and holding_count > 0: print(f"\n[PORTFOLIO_WEAK_MILD] 组合弱势比例{weak_ratio:.0f}% ({weak_count}/{holding_count}),仓位{position_pct:.1f}%,关注")
# 仓位过满提醒 portfolio_alerts += 1
print(f"[PORTFOLIO_FULL] 总仓位{position_pct:.1f}% > 80%,现金{cash:.0f}({cash/total_assets*100:.1f}%)") if position_pct > 80 and holding_count > 0:
portfolio_alerts += 1 # 仓位过满提醒
if portfolio_alerts > 0: print(f"[PORTFOLIO_FULL] 总仓位{position_pct:.1f}% > 80%,现金{cash:.0f}({cash/total_assets*100:.1f}%)")
found += portfolio_alerts portfolio_alerts += 1
if portfolio_alerts > 0:
return found found += portfolio_alerts
return found
if __name__ == "__main__":
main()
if __name__ == "__main__":
main()
+901 -900
View File
File diff suppressed because it is too large Load Diff
+10 -10
View File
@@ -84,11 +84,11 @@ STRATEGY_QUALITY_GATES = [
"fix": "tech_snapshot 包含强撑/弱撑/弱压/强压至少3个数值" "fix": "tech_snapshot 包含强撑/弱撑/弱压/强压至少3个数值"
}, },
{ {
"id": "GATE_CURRENCY_SET", "id": "GATE_CURRENCY",
"desc": "港股决策金额应为人民币标价(currency=CNY)", "severity": "CRITICAL",
"check": lambda d: not is_hk_stock(d.get("code","")) or d.get("currency") in ("CNY", None), "desc": "港股决策金额应为港币标价(currency=HKD)A股为CNY",
"severity": "MEDIUM", "check": lambda d: d.get("currency") in ("HKD", "CNY") or not d.get("code"),
"fix": "设置 d['currency']='CNY'(系统统一存CNY" "fix": "设置 d['currency']='HKD' for HK stocks"
}, },
# --- 第4条 CRITICAL 红线:9维交叉验证 (2026-07-02 Dad要求) --- # --- 第4条 CRITICAL 红线:9维交叉验证 (2026-07-02 Dad要求) ---
# 策略不能只有价格数字,必须有证据经过了多维分析: # 策略不能只有价格数字,必须有证据经过了多维分析:
@@ -2293,10 +2293,10 @@ def regenerate_all(stdout=True):
sector_ctx_str = f"大盘上涨比{market_breadth}%" sector_ctx_str = f"大盘上涨比{market_breadth}%"
new_entry = { new_entry = {
"code": code, "name": name, "price": price, "code": code, "name": name, "price": price,
"cost": old_entry.get("cost", cost) if old_entry else cost, # 优先保留旧成本(holding.xls权威) "cost": old_entry.get("cost", cost) if old_entry else cost,
"shares": shares, # 当前实际持仓股数(不继承旧决策的可能为0的值) "shares": shares,
"avg_price": old_entry.get("avg_price", 0), # 保留持仓均价 "avg_price": old_entry.get("avg_price", 0),
"currency": "CNY", # 系统统一存人民币标价(mo_models规范) "currency": "HKD" if is_hk_stock(code) else "CNY",
"action": result["action"], "action": result["action"],
"stop_loss": result.get("stop_loss"), "stop_loss": result.get("stop_loss"),
"entry_low": result["entry_low"], "entry_low": result["entry_low"],
@@ -2526,7 +2526,7 @@ def regenerate_all(stdout=True):
write_holdings_batch(conn, existing_pf.get('holdings', [])) write_holdings_batch(conn, existing_pf.get('holdings', []))
write_portfolio_summary(conn, existing_pf) write_portfolio_summary(conn, existing_pf)
for s in wl.get('stocks', []): for s in wl.get('stocks', []):
s.setdefault('currency', 'CNY') s.setdefault('currency', 'HKD') if is_hk_stock(str(s.get('code',''))) else s.setdefault('currency', 'CNY')
write_watchlist_stock(conn, s) write_watchlist_stock(conn, s)
for d in decisions: for d in decisions:
# ── 策略质量门禁 ── # ── 策略质量门禁 ──