Skip to content

Commit 2f95a29

Browse files
authored
Add unified market regime control plugin (#5)
* Add unified market regime control plugin * Tune macro volatility confirmation defaults * Add versioned market regime plugin contract * Broaden market regime control compatibility * Document market regime control plan * Route SOXL market regime signals to notification only * Add plugin consumption policy registry * Add watch-only macro indicator coverage * Add macro external context builder * Fix external stress signals to watch-only by default * Add localized plugin notification messages
1 parent 7ee7c27 commit 2f95a29

14 files changed

Lines changed: 3814 additions & 50 deletions

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ send notifications; plugin research and signal generation live here.
3030
gaps only, never overrides the deterministic route, places orders, or changes
3131
allocations. Local Codex is tried first when enabled; OpenAI-compatible and
3232
Anthropic fallback endpoints can be configured.
33+
- `macro_risk_governor`: deterministic macro de-leveraging governor for TQQQ.
34+
It scores price trend, realized volatility, VIX, and credit-pair stress. The
35+
artifact can expose
36+
`leverage_scalar` and `risk_asset_scalar` to strategy runtimes that explicitly
37+
opt in through mounted metadata. External hard-data fields such as HY OAS and
38+
financial-stress indices, plus OSINT-style, sentiment, options-volatility,
39+
rates, breadth, funding, and liquidity fields such as a Pentagon pizza index,
40+
Fear & Greed, put/call, VVIX, SKEW, MOVE, yield curves, dollar stress, and
41+
safe-haven demand, are kept as watch-only evidence by default. They do not
42+
contribute to the actionable trading score unless explicitly enabled for
43+
research.
44+
- `market_regime_control`: unified deterministic facade for crisis, macro, and
45+
TACO signals. Only strategies with positive backtest evidence should mount
46+
position controls for automated consumption; SOXL/SOXX currently receives
47+
broad macro/crisis signals as general notifications only. Stock/ETF rotation
48+
strategies should consume the same artifact through their local risk-scaling
49+
policy and keep TACO as notification-only.
3350
- `taco_rebound_shadow`: TQQQ-only event-rebound context notifier. It writes
3451
manual-review artifacts and never recommends position size or changes
3552
allocations. Softening/de-escalation events stay watch-only until post-event
@@ -56,6 +73,23 @@ qsp-build-crisis-response-shadow-signal \
5673
--output-dir data/output/tqqq_growth_income/plugins/crisis_response_shadow
5774
```
5875

76+
Build the public hard-data `external_context` CSV used by macro and unified
77+
market-regime plugins:
78+
79+
```bash
80+
qsp-build-macro-external-context \
81+
--start 1999-01-01 \
82+
--output data/output/market_regime_control/input/external_context.csv
83+
```
84+
85+
The builder downloads public FRED/CBOE fields when available: VIX, VIX3M,
86+
VVIX, SKEW, Cboe put/call ratios, HY/IG OAS, financial-stress indices, yield
87+
curves, trade-weighted dollar stress, and TED/funding stress. Fields without a
88+
stable no-login historical feed, such as CNN Fear & Greed, AAII, NAAIM,
89+
Pentagon pizza, MOVE, and breadth, can be supplied with `--manual-context`.
90+
OAS coverage follows what the public FRED graph endpoint returns; archived
91+
local OAS history can be injected with the same manual context path.
92+
5993
AI audit reads API settings from environment variables:
6094

6195
- `QSP_STRATEGY_PLUGIN_AI_AUDIT_CODEX_ENABLED`, default `true`
@@ -84,6 +118,24 @@ Generated artifacts include `latest_signal.json`, dated JSON, dated CSV, and
84118
an evidence CSV. `latest_signal.json` is the file platform runtimes mount via
85119
`*_STRATEGY_PLUGIN_MOUNTS_JSON`.
86120

121+
## Notification and Log i18n
122+
123+
Runner-managed artifacts add display-only i18n fields for notifications and
124+
logs:
125+
126+
- `localized_messages.schema_version = strategy_plugin_messages.v1`
127+
- `localized_messages.notification.en-US` / `localized_messages.notification.zh-CN`
128+
- `localized_messages.log.en-US` / `localized_messages.log.zh-CN`
129+
- `log_record.schema_version = strategy_plugin_log.v1`
130+
- `market_regime_control.notification.localized_message_schema_version`
131+
132+
Strategy and broker runtimes should keep trading logic on machine fields such
133+
as `schema_version`, `canonical_route`, `suggested_action`, `reason_codes`, and
134+
`position_control`. Localized strings are for human notification surfaces and
135+
logs only. `market_regime_control.notification` mirrors the localized
136+
notification text and reason labels so existing notification code can render a
137+
message without translating route/action codes itself.
138+
87139
## Local Checks
88140

89141
```bash

README.zh-CN.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Brokers、Schwab、LongBridge、Firstrade 等平台仓库只负责加载 artifac
2525

2626
- `crisis_response_shadow`:面向杠杆美股策略的黑天鹅防守观察插件。它只写入 shadow-mode artifact,不调用券商接口。
2727
可选启用 AI shadow audit:AI 只审计证据一致性和数据缺口,不改写确定性路线、不下单、不改仓位;默认优先尝试本机 Codex,失败后可走 OpenAI-compatible 或 Anthropic fallback endpoint。
28+
- `macro_risk_governor`:面向 TQQQ 的确定性宏观降杠杆插件。它按价格趋势、实现波动、VIX 和信用 ETF 相对压力打分,输出 `leverage_scalar` / `risk_asset_scalar` 给显式 opt-in 的策略运行时消费。HY OAS、金融压力指数、五角大楼比萨指数、Fear & Greed、put/call、VVIX、SKEW、MOVE、收益率曲线、美元压力、safe-haven demand 等外部硬数据、OSINT、情绪或跨资产字段默认只作为 watch-only 证据,不进入可执行分数;只有显式研究开关开启后才允许外部压力字段参与自动分数。
29+
- `market_regime_control`:统一确定性 facade,汇总 crisis、macro 和 TACO 信号,输出版本化的 `notification``position_control`。只有经过回测证明自动消费有效的策略才挂载仓位控制;SOXL/SOXX 这类未通过统一宏观插件复核的高波动行业杠杆策略只接收通用通知,人工决定是否干预。股票/ETF 轮动策略通过本地风险缩放策略消费;TACO 在统一插件里保持通知-only,并会被危机和宏观降风险路线 veto。设计说明见 [Market Regime Control 统一插件方案](docs/market-regime-control-plan.zh-CN.md)
2830
- `taco_rebound_shadow`:仅适用于 TQQQ 的事件反弹上下文通知插件。它只写入人工复核 artifact,不给仓位大小建议,也不改动配置或账户分配。缓和/降温事件会先保持 watch-only,只有事件后价格反弹确认通过后才触发人工复核通知,以减少过早抄底提醒。
2931
该插件也可选启用同样的 shadow-only AI audit,但 AI 只复核事件来源和反弹证据质量。
3032
- TACO panic-rebound 研究、组合回测和 overlay 对比也归属本仓库;snapshot pipeline 仓库只保留兼容入口。
@@ -47,6 +49,21 @@ qsp-build-crisis-response-shadow-signal \
4749
--output-dir data/output/tqqq_growth_income/plugins/crisis_response_shadow
4850
```
4951

52+
生成宏观和统一市场状态插件使用的公开硬数据 `external_context` CSV:
53+
54+
```bash
55+
qsp-build-macro-external-context \
56+
--start 1999-01-01 \
57+
--output data/output/market_regime_control/input/external_context.csv
58+
```
59+
60+
构建器会尽量下载公开 FRED/CBOE 字段:VIX、VIX3M、VVIX、SKEW、Cboe
61+
put/call、HY/IG OAS、金融压力指数、收益率曲线、贸易加权美元压力和
62+
TED/funding stress。CNN Fear & Greed、AAII、NAAIM、五角大楼比萨指数、
63+
MOVE、市场宽度等没有稳定免登录历史源的字段,可以通过 `--manual-context`
64+
提供。OAS 覆盖范围以 FRED 公开 graph endpoint 实际返回为准;如果需要更早的
65+
本地归档 OAS 历史,也通过同一个 manual context 注入。
66+
5067
AI audit 使用环境变量读取 API 配置:
5168

5269
- `QSP_STRATEGY_PLUGIN_AI_AUDIT_CODEX_ENABLED`,默认 `true`
@@ -73,6 +90,20 @@ qsp-build-taco-rebound-shadow-signal \
7390

7491
输出包括 `latest_signal.json`、按日期归档的 JSON、按日期归档的 CSV,以及 evidence CSV。平台运行时通过 `*_STRATEGY_PLUGIN_MOUNTS_JSON` 挂载的就是 `latest_signal.json`
7592

93+
## 通知和日志 i18n
94+
95+
通过 strategy plugin runner 生成的 artifact 会附带展示层 i18n 字段:
96+
97+
- `localized_messages.schema_version = strategy_plugin_messages.v1`
98+
- `localized_messages.notification.en-US` / `localized_messages.notification.zh-CN`
99+
- `localized_messages.log.en-US` / `localized_messages.log.zh-CN`
100+
- `log_record.schema_version = strategy_plugin_log.v1`
101+
- `market_regime_control.notification.localized_message_schema_version`
102+
103+
策略和券商运行时的交易逻辑仍应只读取 `schema_version``canonical_route`
104+
`suggested_action``reason_codes``position_control` 等机器字段。中英文文案只用于通知界面和日志展示,不参与策略判断。`market_regime_control.notification`
105+
会同步包含本地化通知文案和原因标签,方便现有通知代码直接渲染,不需要在策略仓库里重复翻译 route/action code。
106+
76107
## 本地检查
77108

78109
```bash

docs/examples/strategy_plugins.example.toml

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,84 @@ default_mode = "shadow"
33

44
[[strategy_plugins]]
55
strategy = "tqqq_growth_income"
6-
plugin = "crisis_response_shadow"
6+
plugin = "market_regime_control"
77
enabled = true
8-
# The runner enforces plugin/strategy compatibility. This plugin is scoped to
9-
# TQQQ/SOXL leveraged equity black-swan defense strategies.
10-
# mode is optional when it matches default_mode; only shadow notification mode is supported.
8+
# Default runtime contract. This facade arbitrates crisis_response_shadow,
9+
# macro_risk_governor, and taco_rebound_shadow into one deterministic signal.
10+
# Strategy code consumes only notification and position_control from this
11+
# artifact; broker writes and live allocation mutation remain disabled.
12+
# Runner artifacts include en-US/zh-CN localized_messages and log_record for
13+
# display/logging. Strategy logic should still read machine route/action codes.
14+
15+
[strategy_plugins.inputs]
16+
prices = "data/output/market_regime_control/input/tqqq_price_history.csv"
17+
# Build with:
18+
# qsp-build-macro-external-context --start 1999-01-01 --output data/output/market_regime_control/input/external_context.csv
19+
external_context = "data/output/market_regime_control/input/external_context.csv"
20+
event_set = "geopolitical-deescalation"
21+
benchmark_symbol = "QQQ"
22+
attack_symbol = "TQQQ"
23+
vix_symbols = ["VIX", "^VIX", "VIXCLS"]
24+
vix3m_symbols = ["VIX3M", "^VIX3M", "VXV", "^VXV"]
25+
credit_pairs = ["HYG:IEF", "LQD:IEF"]
26+
financial_symbols = ["XLF", "KRE"]
27+
rate_symbols = ["IEF", "TLT"]
28+
strategy_policy = "levered_growth_income_v1"
29+
realized_vol_threshold = 0.30
30+
realized_vol_requires_confirmation = true
31+
# Fixed production default after external-context backtest: external hard-data
32+
# stress fields are notification/watch evidence unless this research switch is
33+
# explicitly enabled.
34+
external_stress_actionable = false
35+
delever_risk_asset_scalar = 0.0
36+
taco_opportunity_size_scalar = 0.0
37+
crisis_enabled = true
38+
macro_enabled = true
39+
taco_enabled = true
40+
41+
[strategy_plugins.outputs]
42+
output_dir = "data/output/tqqq_growth_income/plugins/market_regime_control"
43+
44+
[[strategy_plugins]]
45+
strategy = "market_regime_notification"
46+
plugin = "market_regime_control"
47+
enabled = true
48+
# General market-regime notification. This artifact is not mounted into the
49+
# SOXL/SOXX strategy runtime; sector-levered SOXL keeps its own validated SOXX
50+
# volatility gate and humans decide whether broad macro/crisis notices matter.
51+
52+
[strategy_plugins.inputs]
53+
prices = "data/output/market_regime_control/input/soxl_price_history.csv"
54+
external_context = "data/output/market_regime_control/input/external_context.csv"
55+
event_set = "full"
56+
benchmark_symbol = "SOXX"
57+
attack_symbol = "SOXL"
58+
vix_symbols = ["VIX", "^VIX", "VIXCLS"]
59+
vix3m_symbols = ["VIX3M", "^VIX3M", "VXV", "^VXV"]
60+
credit_pairs = ["HYG:IEF", "LQD:IEF"]
61+
financial_symbols = ["XLF", "KRE"]
62+
rate_symbols = ["IEF", "TLT"]
63+
strategy_policy = "levered_growth_income_v1"
64+
realized_vol_threshold = 0.30
65+
realized_vol_requires_confirmation = true
66+
external_stress_actionable = false
67+
delever_risk_asset_scalar = 0.0
68+
taco_enabled = false
69+
crisis_enabled = true
70+
macro_enabled = true
71+
72+
[strategy_plugins.outputs]
73+
output_dir = "data/output/market_regime_notification/plugins/market_regime_control"
74+
75+
# Deprecated compatibility mounts. They remain runnable for historical
76+
# backtests and downstream consumers that have not migrated to
77+
# market_regime_control yet. New strategy integrations should not read them
78+
# directly.
79+
80+
[[strategy_plugins]]
81+
strategy = "tqqq_growth_income"
82+
plugin = "crisis_response_shadow"
83+
enabled = false
1184

1285
[strategy_plugins.inputs]
1386
prices = "data/output/crisis_response_shadow/input/price_history.csv"
@@ -16,41 +89,37 @@ event_set = "full"
1689
financial_symbols = ["XLF", "KRE"]
1790
credit_pairs = ["HYG:IEF", "LQD:IEF"]
1891
rate_symbols = ["IEF", "TLT"]
19-
# Optional shadow-only AI audit. API keys are read from env vars, not TOML:
20-
# QSP_STRATEGY_PLUGIN_AI_AUDIT_API_KEY / QSP_STRATEGY_PLUGIN_AI_AUDIT_FALLBACK_API_KEY
21-
# Legacy QSP_CRISIS_AI_AUDIT_* names are still accepted.
22-
ai_audit_enabled = true
23-
ai_audit_codex_enabled = true
24-
ai_audit_model = "gpt-5.4-mini"
25-
# ai_audit_base_url = "https://api.openai.com/v1"
26-
# ai_audit_fallback_base_url = "https://fallback.example.com/v1"
27-
# ai_audit_fallback_model = "fallback-model"
28-
# Anthropic fallback follows the CryptoCodexAuditBridge provider-fallback style.
29-
# ai_audit_anthropic_model = "claude-sonnet-4-6"
30-
# ai_audit_anthropic_base_url = "https://api.anthropic.com/v1"
31-
# ai_audit_anthropic_version = "2023-06-01"
3292

3393
[strategy_plugins.outputs]
3494
output_dir = "data/output/tqqq_growth_income/plugins/crisis_response_shadow"
3595

96+
[[strategy_plugins]]
97+
strategy = "tqqq_growth_income"
98+
plugin = "macro_risk_governor"
99+
enabled = false
100+
101+
[strategy_plugins.inputs]
102+
prices = "data/output/macro_risk_governor/input/price_history.csv"
103+
external_context = "data/output/macro_risk_governor/input/external_context.csv"
104+
benchmark_symbol = "QQQ"
105+
attack_symbol = "TQQQ"
106+
vix_symbols = ["VIX", "^VIX", "VIXCLS"]
107+
vix3m_symbols = ["VIX3M", "^VIX3M", "VXV", "^VXV"]
108+
credit_pairs = ["HYG:IEF", "LQD:IEF"]
109+
external_stress_actionable = false
110+
111+
[strategy_plugins.outputs]
112+
output_dir = "data/output/tqqq_growth_income/plugins/macro_risk_governor"
113+
36114
[[strategy_plugins]]
37115
strategy = "tqqq_growth_income"
38116
plugin = "taco_rebound_shadow"
39-
enabled = true
40-
# Notification-only TACO context. The artifact may trigger manual-review alerts,
41-
# but it never recommends position size or mutates allocations.
42-
# Manual-review alerts require post-event price rebound confirmation by default.
117+
enabled = false
43118

44119
[strategy_plugins.inputs]
45120
prices = "data/output/taco_rebound_shadow/input/price_history.csv"
46121
event_set = "geopolitical-deescalation"
47122
start_date = "2026-01-01"
48-
# Optional shadow-only AI audit for event/source quality. It cannot alter the
49-
# manual-review/watch-only route or generate allocation/order instructions.
50-
ai_audit_enabled = true
51-
ai_audit_codex_enabled = true
52-
ai_audit_model = "gpt-5.4-mini"
53-
# ai_audit_anthropic_model = "claude-sonnet-4-6"
54123

55124
[strategy_plugins.outputs]
56125
output_dir = "data/output/tqqq_growth_income/plugins/taco_rebound_shadow"

0 commit comments

Comments
 (0)