feat: DB-first architecture with lock-safe writes

- price_monitor: writes live prices to both JSON and mofin.db (holdings + live_prices + portfolio_summary)
- mofin_db: added execute_with_retry/commit_with_retry with exponential backoff on 'database is locked'
- mofin_db: increased timeout 5s->15s, added PRAGMA busy_timeout=15000
- price_monitor retry loop: fixed break-before-if-ok bug (was not retrying on write failure)
- DB connection: WAL mode + retry decorator for all write operations
- cash sync: preserves DB authoritative cash (JSON cash not pushed to DB)

This is the DB-first version. JSON writes remain for dashboard compatibility.
Next step: remove JSON writes entirely for full DB-only architecture.
This commit is contained in:
知微
2026-07-06 12:02:11 +08:00
parent 687155487d
commit e185b4e4dc
30 changed files with 1856 additions and 2042 deletions
+709 -1063
View File
File diff suppressed because it is too large Load Diff
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+147 -221
View File
@@ -1,240 +1,166 @@
{
"holdings": [
{
"code": "300308",
"name": "中际旭创",
"shares": 100,
"cost": 1316.53,
"price": 1116.0,
"market_value": 111600.0,
"change_pct": -2.36,
"currency": "CNY",
"position_pct": 15.27,
"_currency": "CNY"
},
{
"code": "06869",
"name": "长飞光纤光缆",
"shares": 500,
"cost": 263.73,
"price": 201.2,
"market_value": 87220.0,
"change_pct": 1.64,
"currency": "HKD",
"position_pct": 13.47,
"_currency": "HKD"
},
{
"code": "01478",
"name": "丘钛科技",
"shares": 11000,
"cost": 13.47,
"price": 6.99,
"market_value": 66660.0,
"change_pct": 4.02,
"currency": "HKD",
"position_pct": 7.97,
"_currency": "HKD"
},
{
"code": "601899",
"name": "紫金矿业",
"shares": 2400,
"cost": 39.89,
"price": 27.82,
"market_value": 66768.0,
"change_pct": 5.78,
"currency": "CNY",
"position_pct": 7.34,
"_currency": "CNY"
},
{
"code": "688411",
"name": "海博思创",
"shares": 200,
"cost": 266.95,
"price": 251.15,
"market_value": 50230.0,
"change_pct": -1.78,
"currency": "CNY",
"position_pct": 6.31,
"_currency": "CNY"
},
{
"code": "688981",
"name": "中芯国际",
"shares": 300,
"cost": 126.07,
"price": 140.31,
"market_value": 42093.0,
"change_pct": -2.63,
"currency": "CNY",
"position_pct": 5.44,
"_currency": "CNY"
},
{
"code": "01888",
"name": "建滔积层板",
"shares": 500,
"cost": 88.24,
"price": 84.45,
"market_value": 36545.0,
"change_pct": 0.78,
"currency": "HKD",
"position_pct": 5.28,
"_currency": "HKD"
},
{
"code": "688639",
"name": "华恒生物",
"shares": 2800,
"cost": 21.51,
"price": 16.66,
"market_value": 46648.0,
"change_pct": -1.71,
"currency": "CNY",
"position_pct": 5.25,
"_currency": "CNY"
},
{
"code": "300750",
"name": "宁德时代",
"shares": 100,
"cost": 401.78,
"price": 380.0,
"market_value": 38000.0,
"change_pct": -0.61,
"currency": "CNY",
"position_pct": 4.64,
"_currency": "CNY"
},
{
"code": "01211",
"name": "比亚迪股份",
"shares": 600,
"cost": 104.87,
"price": 84.1,
"market_value": 43932.0,
"change_pct": 7.41,
"currency": "HKD",
"position_pct": 4.62,
"_currency": "HKD"
},
{
"code": "02202",
"name": "万科企业",
"shares": 19700,
"cost": 4.67,
"price": 2.34,
"market_value": 40188.0,
"change_pct": 4.93,
"currency": "HKD",
"position_pct": 4.6,
"_currency": "HKD"
},
{
"code": "00700",
"name": "腾讯",
"shares": 100,
"cost": 443.13,
"price": 431.2,
"market_value": 37437.0,
"change_pct": 0.23,
"price": 447.0,
"market_value": 39063.0,
"change_pct": 3.66,
"currency": "HKD",
"position_pct": null,
"_currency": "HKD"
},
{
"code": "00981",
"name": "中芯国际",
"shares": 500,
"cost": 75.94,
"price": 77.6,
"market_value": 33530.0,
"change_pct": -3.48,
"currency": "HKD",
"position_pct": 4.2,
"_currency": "HKD"
},
{
"code": "300548",
"name": "长芯博创",
"shares": 100,
"cost": 231.46,
"price": 221.01,
"market_value": 22101.0,
"change_pct": -0.45,
"currency": "CNY",
"position_pct": 3.2,
"_currency": "CNY"
},
{
"code": "518880",
"name": "黄金ETF华安",
"shares": 2400,
"cost": 12.19,
"price": 8.67,
"market_value": 20808.0,
"change_pct": 2.32,
"currency": "CNY",
"position_pct": 2.45,
"_currency": "CNY"
},
{
"code": "300035",
"name": "中科电气",
"shares": 1400,
"cost": 22.29,
"price": 14.29,
"market_value": 20006.0,
"change_pct": 0.85,
"currency": "CNY",
"position_pct": 2.42,
"_currency": "CNY"
},
{
"code": "000700",
"name": "模塑科技",
"shares": 1400,
"cost": 14.83,
"price": 17.6,
"market_value": 24640.0,
"change_pct": 4.33,
"currency": "CNY",
"position_pct": 2.41,
"_currency": "CNY"
},
{
"code": "600563",
"name": "法拉电子",
"shares": 100,
"cost": 147.18,
"price": 157.06,
"market_value": 15706.0,
"change_pct": -4.41,
"currency": "CNY",
"position_pct": 2.3,
"_currency": "CNY"
"position_pct": null
},
{
"code": "01088",
"name": "中国神华",
"shares": 500,
"cost": 45.89,
"price": 40.0,
"market_value": 17355.0,
"change_pct": 1.01,
"price": 40.68,
"market_value": 17575.0,
"change_pct": 1.7,
"currency": "HKD",
"position_pct": 2.14,
"_currency": "HKD"
"position_pct": 2.14
},
{
"code": "01211",
"name": "比亚迪股份",
"shares": 600,
"cost": 104.87,
"price": 84.2,
"market_value": 44376.0,
"change_pct": 0.12,
"currency": "HKD",
"position_pct": 4.62
},
{
"code": "01478",
"name": "丘钛科技",
"shares": 11000,
"cost": 13.47,
"price": 6.77,
"market_value": 64460.0,
"change_pct": -3.15,
"currency": "HKD",
"position_pct": 7.97
},
{
"code": "02202",
"name": "万科企业",
"shares": 19700,
"cost": 4.67,
"price": 2.33,
"market_value": 40385.0,
"change_pct": -0.43,
"currency": "HKD",
"position_pct": 4.6
},
{
"code": "300035",
"name": "中科电气",
"shares": 1400,
"cost": 22.29,
"price": 14.02,
"market_value": 19712.0,
"change_pct": -1.89,
"currency": "CNY",
"position_pct": 2.42
},
{
"code": "300308",
"name": "中际旭创",
"shares": 100,
"cost": 1316.53,
"price": 1116.03,
"market_value": 106181.0,
"change_pct": 0.0,
"currency": "CNY",
"position_pct": 15.27
},
{
"code": "300750",
"name": "宁德时代",
"shares": 100,
"cost": 401.78,
"price": 377.6,
"market_value": 37726.0,
"change_pct": -0.63,
"currency": "CNY",
"position_pct": 4.64
},
{
"code": "518880",
"name": "黄金ETF华安",
"shares": 2400,
"cost": 12.19,
"price": 8.66,
"market_value": 20832.0,
"change_pct": -0.08,
"currency": "CNY",
"position_pct": 2.45
},
{
"code": "600563",
"name": "法拉电子",
"shares": 100,
"cost": 147.18,
"price": 153.42,
"market_value": 15021.0,
"change_pct": -2.32,
"currency": "CNY",
"position_pct": 2.3
},
{
"code": "601899",
"name": "紫金矿业",
"shares": 2400,
"cost": 39.89,
"price": 28.41,
"market_value": 68784.0,
"change_pct": 2.12,
"currency": "CNY",
"position_pct": 7.34
},
{
"code": "688411",
"name": "海博思创",
"shares": 200,
"cost": 266.95,
"price": 262.98,
"market_value": 52064.0,
"change_pct": 4.71,
"currency": "CNY",
"position_pct": 6.31
},
{
"code": "688639",
"name": "华恒生物",
"shares": 2800,
"cost": 21.51,
"price": 16.61,
"market_value": 46480.0,
"change_pct": -0.3,
"currency": "CNY",
"position_pct": 5.25
},
{
"code": "688981",
"name": "中芯国际",
"shares": 300,
"cost": 126.07,
"price": 147.18,
"market_value": 40599.0,
"change_pct": 4.9,
"currency": "CNY",
"position_pct": 5.44
}
],
"total_assets": 952877.07,
"total_mv": 820755.14,
"stock_value": 820755.14,
"cash": 132121.93,
"frozen_cash": 0,
"position_pct": 86.13,
"cash": 289196.0,
"frozen_cash": 0.0,
"total_mv": 619713.76,
"total_assets": 908909.76,
"position_pct": 68.18,
"currency": "CNY",
"updated_at": "2026-07-04 09:51"
"updated_at": "2026-07-06 11:35:19",
"stock_value": 619713.76
}
+66
View File
@@ -27,6 +27,12 @@
"high": 1215.52,
"low": 1185.0,
"close": 1194.45
},
{
"date": "2026-07-06",
"high": 1215.0,
"low": 1180.0,
"close": 1203.23
}
],
"02202": [
@@ -63,6 +69,12 @@
"high": 50.2,
"low": 48.31,
"close": 49.09
},
{
"date": "2026-07-06",
"high": 49.96,
"low": 48.81,
"close": 49.38
}
],
"02359": [
@@ -119,6 +131,12 @@
"high": 502.0,
"low": 444.55,
"close": 480.32
},
{
"date": "2026-07-06",
"high": 499.0,
"low": 448.0,
"close": 476.72
}
],
"06160": [
@@ -155,6 +173,12 @@
"high": 687.04,
"low": 633.01,
"close": 643.81
},
{
"date": "2026-07-06",
"high": 649.88,
"low": 615.0,
"close": 649.01
}
],
"09868": [
@@ -197,6 +221,12 @@
"high": 757.88,
"low": 713.0,
"close": 738.38
},
{
"date": "2026-07-06",
"high": 745.0,
"low": 690.11,
"close": 743.0
}
],
"300124": [
@@ -211,6 +241,12 @@
"high": 74.63,
"low": 67.31,
"close": 72.15
},
{
"date": "2026-07-06",
"high": 71.94,
"low": 68.6,
"close": 69.55
}
],
"000657": [
@@ -225,6 +261,12 @@
"high": 101.5,
"low": 87.88,
"close": 89.63
},
{
"date": "2026-07-06",
"high": 90.58,
"low": 80.46,
"close": 81.83
}
],
"000711": [
@@ -239,6 +281,12 @@
"high": 5.26,
"low": 4.87,
"close": 5.26
},
{
"date": "2026-07-06",
"high": 5.65,
"low": 5.0,
"close": 5.29
}
],
"001309": [
@@ -253,6 +301,12 @@
"high": 892.1,
"low": 795.0,
"close": 881.91
},
{
"date": "2026-07-06",
"high": 918.98,
"low": 860.0,
"close": 916.6
}
],
"002594": [
@@ -267,6 +321,12 @@
"high": 88.88,
"low": 81.9,
"close": 88.47
},
{
"date": "2026-07-06",
"high": 88.95,
"low": 86.61,
"close": 87.68
}
],
"00700": [
@@ -335,6 +395,12 @@
"high": 646.85,
"low": 574.1,
"close": 618.02
},
{
"date": "2026-07-06",
"high": 710.0,
"low": 659.16,
"close": 695.26
}
]
}
+5
View File
@@ -165,6 +165,11 @@
"time": "2026-07-01T08:55:24.130605",
"content": "**港股(恒指-0.63%):** 中芯国际H(00981) 89.40+5.42%浮盈+17%持有 | 腾讯控股(00700) 429.80+2.28%浮亏-3%距止损3.4% | 丘钛科技(014",
"report_id": "cron_d42f2ce3b479_2026-06-30_20-25-27"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "| **中芯国际双持(688981+00981)** | A+H 双持合计约 9.64% 仓位,两市场同步净多头,若港股中芯继续走弱(注意 00981 止损 ¥77.55 高于成本 ¥75.94 的异",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
}
]
}
+15
View File
@@ -616,6 +616,21 @@
"content": "- **比亚迪(01211)** — 现价72.65,止损63.99(距12%)。港股汽车板块整体承压,持续下跌趋势未改。建议:重新审视持股逻辑——如果中线看多理由充分,可考虑将止损下移至60附近;如",
"report_id": "cron_e02b8bde74f8_2026-06-28_22-11-21"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "3. **0714止盈策略严重过期** — 当前 19 个持仓中有约 13 个(68%)的成本价高于止盈价,说明策略数据已随股价下跌严重偏离,大量止损/止盈位明显陈旧(如万科 02202 成本 ¥4.",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "3. **策略数据翻新** — 针对成本远高于止盈的 13 个持仓(如 万科02202、比亚迪01211、华恒生物688639、中科电气300035、黄金ETF518880 等),建议逐个审核是否需要",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "| **比亚迪(01211)止损临近** | 此前 advice 反复提示止损 ¥81.72、现价距仅 3%。港股比亚迪已是 4.62% 仓位且浮亏 -20.1%,若下周港股继续弱势,止损单需提前挂好",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-06-01T10:25:54.503460",
"content": "比亚迪股份(01211) 仓位4.56% +2.27%→ 持有,连涨",
+10
View File
@@ -456,6 +456,16 @@
"content": "③ 丘钛科技(01478) 距止损6.48仅+7.3%!仓位7.97%",
"report_id": "cron_d42f2ce3b479_2026-07-03_20-03-44"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "2. **收紧输出质量** — 使用\"关注/观望/留意\"时须紧随具体价格、止损位和触发条件。满 3 次模糊表述自动标记为低质量输出。对 01478 和 688795 做一次明确的持仓策略复核:继续持有",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "| **丘钛科技(01478)** | 7.97% 第三大仓位,成本 ¥13.47 远超止盈 ¥7.59(浮亏约 -43%),150 次提及却无追踪结论。无论继续持有还是止损,需 Dad 明确决策 |",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-06-01T10:25:54.503460",
"content": "丘钛科技(01478) 仓位8.58% +4.10%→ 持有,走势健康",
+10
View File
@@ -621,6 +621,16 @@
"content": "- **万科(02202)** — 现价2.20,止损2.00(距9%)。周五港股继续弱,若跌破2.00港币将触发止损。建议:提前制定应对方案,而不是等到触发了再慌。",
"report_id": "cron_e02b8bde74f8_2026-06-28_22-11-21"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "3. **0714止盈策略严重过期** — 当前 19 个持仓中有约 13 个(68%)的成本价高于止盈价,说明策略数据已随股价下跌严重偏离,大量止损/止盈位明显陈旧(如万科 02202 成本 ¥4.",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "3. **策略数据翻新** — 针对成本远高于止盈的 13 个持仓(如 万科02202、比亚迪01211、华恒生物688639、中科电气300035、黄金ETF518880 等),建议逐个审核是否需要",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-06-04T09:55:40.300986",
"content": "- 万科(02202): 补仓区间2.5~2.6,昨收2.70,距+3.8%,未进入。",
+5
View File
@@ -20,6 +20,11 @@
"time": "2026-06-28T08:55:52.587605",
"content": "6. 🟢 **中国人寿** (02628) — 距买入区-2%,保险板块虽弱但估值低",
"report_id": "cron_watchlist_health_weekly_2026-06-27_20-04-22"
},
{
"time": "2026-07-05T08:55:15.087154",
"content": "6. 中国人寿(02628) — 接近止损位28.26,保险板块承压",
"report_id": "cron_watchlist_health_weekly_2026-07-04_20-13-24"
}
]
}
+5
View File
@@ -725,6 +725,11 @@
"time": "2026-06-14T08:55:34.460610",
"content": "- 中国人寿(02628) & 小鹏汽车(09868) — 盈亏比过低,不建议买入",
"report_id": "cron_watchlist_health_weekly_2026-06-13_20-07-41"
},
{
"time": "2026-07-05T08:55:15.087154",
"content": "4. 小鹏汽车(09868) — 跌破止损位,汽车行业竞争格局",
"report_id": "cron_watchlist_health_weekly_2026-07-04_20-13-24"
}
]
}
+5
View File
@@ -861,6 +861,11 @@
"content": "{\"type\":\"周复盘\",\"time\":\"周日\",\"summary\":\"A股先跌后弹分化格局,全市场普跌后暴力反弹\",\"key_holdings\":[{\"code\":\"600110\",\"name\":",
"report_id": "cron_e02b8bde74f8_2026-06-14_22-04-57"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "2. **海博思创(688411)追踪较好** — 该股在持仓中占比 6.31%,成本 ¥266.95 处于买入区内(¥231.32~¥269.87),参与价合理,后续追踪记录清晰,止盈 ¥277.5",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-06-03T09:56:06.723399",
"content": "| 688411 | 海博思创 | 295.00 | 299.17 | -1.39% | 追踪止盈290 | 正常 |",
+5
View File
@@ -91,6 +91,11 @@
"content": "- 华恒生物(688639) ¥21.2,买入区20.78~21.4 ✅ 在区间内,RR:2.28",
"report_id": "cron_e02b8bde74f8_2026-06-14_22-04-57"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "3. **策略数据翻新** — 针对成本远高于止盈的 13 个持仓(如 万科02202、比亚迪01211、华恒生物688639、中科电气300035、黄金ETF518880 等),建议逐个审核是否需要",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-06-01T10:25:54.503460",
"content": "华恒生物(688639) 仓位0.47% -1.71%→ 持有,低位整理",
+5
View File
@@ -26,6 +26,11 @@
"content": "• **摩尔线程(688795) 616.38** +2.73% | 买入区580-600,偏高→等回调至600以下",
"report_id": "cron_d3797d924ddc_2026-06-02_16-33-11"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "2. **收紧输出质量** — 使用\"关注/观望/留意\"时须紧随具体价格、止损位和触发条件。满 3 次模糊表述自动标记为低质量输出。对 01478 和 688795 做一次明确的持仓策略复核:继续持有",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-06-14T08:55:34.460610",
"content": "**🟢 摩尔线程-U(688795) ¥610.55 | 买入区598.34~622.76 ✅ | RR:3.44**",
+5
View File
@@ -151,6 +151,11 @@
"content": "② 中芯国际A(688981) 已跌破止损位!仓位5.44%",
"report_id": "cron_d42f2ce3b479_2026-07-03_20-03-44"
},
{
"time": "2026-07-06T08:55:01.761812",
"content": "| **中芯国际双持(688981+00981)** | A+H 双持合计约 9.64% 仓位,两市场同步净多头,若港股中芯继续走弱(注意 00981 止损 ¥77.55 高于成本 ¥75.94 的异",
"report_id": "cron_e02b8bde74f8_2026-07-05_22-05-42"
},
{
"time": "2026-06-11T08:55:23.441938",
"content": "• 中芯国际(688981) 竞价125.00(-1.81%),策略买入区116~136内",