Skip to content

Commit 6427254

Browse files
committed
Config-driven architecture: platform-config.json + build_config.py, remove hardcoded platform/account JS
1 parent c198a83 commit 6427254

7 files changed

Lines changed: 841 additions & 116 deletions

File tree

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# QuantRuntimeSettings 配置驱动架构设计规范
2+
3+
## 问题诊断
4+
5+
当前增加一个平台/域/策略需要修改至少 **6 个文件、50+ 处硬编码**,导致 72 次 commit/天。
6+
7+
### 硬编码清单
8+
9+
| 文件 | 硬编码内容 | 类型 |
10+
|------|------|------|
11+
| `index.html` | `platformMeta`(6), `defaultRepositories`(6), `defaultAccountOptions`(6), `state.forms`(6), `defaultStrategyProfiles`(18), `strategyDomains`(4), `platformSupportsMargin`(1), `platformSupportsReservedCash`(1), `platformSupportsDca`(1), `platformDryRunOnly`(1), `dcaProfileDefaults`(2), i18n domain labels(3) | 数据+逻辑 |
12+
| `worker.js` | `SUPPORTED_PLATFORMS`(6), `SUPPORTED_STRATEGY_DOMAINS`(4), `PLATFORM_META`(6), `DEFAULT_PLATFORM_REPOSITORIES`(6), `PLATFORM_REPOSITORY_ENV`(6), `DEFAULT_VARIABLE_SCOPE`(6), `PLATFORM_RESERVED_CASH_*`(4) | 数据 |
13+
| `strategy-profiles.example.json` | 18个策略完整定义 | 数据 |
14+
| `examples/targets/*/` | 每个组合独立的 target JSON | 数据 |
15+
16+
### 核心问题
17+
18+
**数据和逻辑混合**:平台的属性(颜色、名称、域)和业务逻辑(是否支持期权、保证金、DCA)散落在 HTML/JS 各处,每次新增都需要改多份代码。
19+
20+
---
21+
22+
## 方案:配置驱动的三层架构
23+
24+
```
25+
┌─────────────────────────────────────────────────────┐
26+
│ platform-config.json (单一配置源) │
27+
│ ├── platforms: 元数据 + 能力声明 │
28+
│ ├── domains: 域名 → i18n │
29+
│ └── strategies: profile → 特征/域 │
30+
├─────────────────────────────────────────────────────┤
31+
│ sync 脚本 (build-time) │
32+
│ └── 生成: JS bundles / KV seed / target JSONs │
33+
├─────────────────────────────────────────────────────┤
34+
│ Worker API (runtime) │
35+
│ └── GET /api/config → 返回完整配置给前端 │
36+
└─────────────────────────────────────────────────────┘
37+
```
38+
39+
---
40+
41+
## 新增平台/域/策略时只需做的事
42+
43+
### 增加一个平台(如 binance)
44+
45+
**只需改 1 个文件**`platform-config.json`
46+
47+
```json
48+
{
49+
"platforms": {
50+
"binance": {
51+
"label": "Binance",
52+
"code": "BN",
53+
"accent_color": "#f0b90b",
54+
"repository": "QuantStrategyLab/BinancePlatform",
55+
"variable_scope": "repository",
56+
"supported_domains": ["crypto"],
57+
"capabilities": {
58+
"margin_policy": false,
59+
"reserved_cash": false,
60+
"income_layer": false,
61+
"option_overlay": false,
62+
"dca": true,
63+
"dry_run_only": false
64+
},
65+
"default_execution_mode": "live",
66+
"default_account": {
67+
"key": "preview",
68+
"label": "Binance",
69+
"target_name": "crypto_combo",
70+
"supported_domains": ["crypto"]
71+
}
72+
}
73+
}
74+
}
75+
```
76+
77+
前端/后端的 **所有行为自动推导**
78+
- `platformSupportsMargin(binance)` → 读 `capabilities.margin_policy`
79+
- `platformDryRunOnly(binance)` → 读 `capabilities.dry_run_only`
80+
- 收入层/期权层显隐 → 读 `capabilities.income_layer/option_overlay`
81+
- DCA 控件 → 读 `capabilities.dca`
82+
- 帐号域过滤 → 读 `default_account.supported_domains`
83+
- 平台颜色 → 读 `accent_color`
84+
85+
### 增加一个策略
86+
87+
**只需改 1 个文件**`strategy-profiles.json`
88+
89+
```json
90+
{
91+
"profile": "cn_stock_momentum_rotation",
92+
"label": "CN Stock Momentum",
93+
"label_zh": "A股个股动量",
94+
"domain": "cn_equity",
95+
"features": {
96+
"runtime_enabled": true,
97+
"income_layer": false,
98+
"option_overlay": false,
99+
"dca": false,
100+
"combo": false
101+
}
102+
}
103+
```
104+
105+
前端自动渲染:收入层/期权/DCA 控件的显隐由 `features` 决定。
106+
107+
### 增加一个股票市场域(如 crypto)
108+
109+
**只需改 1 个文件**`platform-config.json``domains` 部分
110+
111+
```json
112+
{
113+
"domains": {
114+
"us_equity": { "label_zh": "美股", "label_en": "US Equity" },
115+
"hk_equity": { "label_zh": "港股", "label_en": "HK Equity" },
116+
"cn_equity": { "label_zh": "A股", "label_en": "CN A-share" },
117+
"crypto": { "label_zh": "加密", "label_en": "Crypto" }
118+
}
119+
}
120+
```
121+
122+
---
123+
124+
## 实施路线
125+
126+
### Phase 1 — 提取配置 (1-2h)
127+
128+
1. 创建 `platform-config.json`(包含当前所有平台属性+能力+域)
129+
2. 创建规则:**前端/后端禁止硬编码平台属性,一律读配置**
130+
131+
### Phase 2 — 前端重构 (2-3h)
132+
133+
1. `index.html` 删除所有硬编码的:
134+
- `platformMeta` → 从 API 读
135+
- `defaultAccountOptions` → 从 API 读
136+
- `strategyDomains` → 从 domains 配置推导
137+
- `platformSupports*` 函数 → 从 capabilities 推导
138+
- `defaultRepositories` → 从 API 读
139+
- `dcaProfileDefaults` → 从策略 features.dca 推导
140+
141+
2. `worker.js` 删除所有硬编码,改为读 `platform-config.json`
142+
- `SUPPORTED_PLATFORMS``Object.keys(config.platforms)`
143+
- `PLATFORM_META``config.platforms[id]`
144+
- `DEFAULT_PLATFORM_REPOSITORIES``config.platforms[id].repository`
145+
146+
### Phase 3 — 代码生成 (1h)
147+
148+
1. `sync_strategy_switch_page_asset.py` 扩展为完整构建脚本
149+
2.`platform-config.json` + `strategy-profiles.json` 生成:
150+
- `worker.js` 的配置常量部分
151+
- `strategy_profiles_asset.js`
152+
- `account-options.example.json`
153+
- 各平台的 `target/*.example.json`
154+
155+
---
156+
157+
## 新增操作手册
158+
159+
### 增加新平台
160+
161+
1. 编辑 `platform-config.json` → 加 `platforms.newplatform`
162+
2. 运行 `python3 scripts/build_config.py`
163+
3. 部署 `npx wrangler deploy`
164+
165+
### 增加新策略
166+
167+
1. 编辑 `strategy-profiles.json` → 加 profile entry
168+
2. 运行 `python3 scripts/build_config.py`
169+
3. 部署
170+
171+
### 增加新域
172+
173+
1. 编辑 `platform-config.json` → 加 `domains.newdomain`
174+
2. 把域赋给某个平台的 `supported_domains`
175+
3. 运行 `python3 scripts/build_config.py`
176+
4. 部署
177+
178+
---
179+
180+
## 配置即文档
181+
182+
`platform-config.json` 本身就是架构文档,任何人看一眼就知道:
183+
- 有哪些平台,各自支持什么能力
184+
- 哪些域,哪些策略属于哪个域
185+
- 平台和域的映射关系
186+
187+
不需要再去读 index.html/worker.js 的 JavaScript 代码。

0 commit comments

Comments
 (0)