Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions application/signal_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
"dual_drive_volatility_delever_retention_context_found",
"dual_drive_volatility_delever_retention_reason_codes",
"dual_drive_volatility_delever_redirect_symbol",
"dual_drive_volatility_delever_source_value",
"dual_drive_volatility_delever_retained_value",
"dual_drive_volatility_delever_removed_value",
"dual_drive_volatility_delever_retained_ratio",
"dual_drive_volatility_delever_redirected_ratio",
"dual_drive_macro_risk_governor_enabled",
"dual_drive_macro_risk_governor_found",
"dual_drive_macro_risk_governor_route",
Expand Down
36 changes: 36 additions & 0 deletions notifications/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ def _format_percent(value) -> str:
return "n/a"


def _as_float_or_none(value):
try:
return float(value)
except (TypeError, ValueError):
return None


def _format_percentile(value) -> str:
try:
percentile = float(value) * 100
Expand Down Expand Up @@ -406,6 +413,27 @@ def _format_volatility_delever_threshold_detail(signal_metadata, *, prefix: str,
)


def _format_tqqq_volatility_delever_allocation_detail(
signal_metadata,
*,
prefix: str,
redirect_symbol: str,
translator,
) -> str:
retained_ratio = _as_float_or_none(signal_metadata.get(f"{prefix}_retained_ratio"))
redirected_ratio = _as_float_or_none(signal_metadata.get(f"{prefix}_redirected_ratio"))
if retained_ratio is None:
retained_ratio = _as_float_or_none(signal_metadata.get(f"{prefix}_retention_ratio"))
if redirected_ratio is None and retained_ratio is not None:
redirected_ratio = max(0.0, min(1.0, 1.0 - retained_ratio))
return translator(
"tqqq_volatility_delever_allocation_detail",
retained_ratio=_format_percent(retained_ratio),
redirected_ratio=_format_percent(redirected_ratio),
redirect_symbol=redirect_symbol or "QQQ",
)


def _build_tqqq_risk_control_lines(signal_metadata, *, translator) -> list[str]:
prefix = "dual_drive_volatility_delever"
if not _is_truthy(signal_metadata.get(f"{prefix}_applied")):
Expand All @@ -418,6 +446,12 @@ def _build_tqqq_risk_control_lines(signal_metadata, *, translator) -> list[str]:
prefix=prefix,
translator=translator,
)
allocation_detail = _format_tqqq_volatility_delever_allocation_detail(
signal_metadata,
prefix=prefix,
redirect_symbol=redirect_symbol or "QQQ",
translator=translator,
Comment on lines +449 to +453

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include allocation detail in sent compact notifications

In the live IBKR Telegram path with TQQQ delever applied, this new allocation_detail only reaches the detailed dashboard built here; run_strategy_core still passes render_trade_notification a strategy_dashboard from application.rebalance_service._strategy_dashboard_text (application/rebalance_service.py:357), which only includes dashboard/timing, and NotificationPublisher sends compact_text (tests/test_notification_events.py:12). As a result, the retained/redirected allocation is logged but omitted from the actual sent Telegram compact message, so the feature does not show up for users unless the compact path uses the enriched renderer dashboard too.

Useful? React with 👍 / 👎.

)
if str(signal_metadata.get(f"{prefix}_trigger_reason") or "").strip() == "hysteresis_hold":
return [
translator(
Expand All @@ -429,6 +463,7 @@ def _build_tqqq_risk_control_lines(signal_metadata, *, translator) -> list[str]:
threshold_detail=threshold_detail,
source_symbol="TQQQ",
redirect_symbol=redirect_symbol or "QQQ",
allocation_detail=allocation_detail,
)
]
return [
Expand All @@ -440,6 +475,7 @@ def _build_tqqq_risk_control_lines(signal_metadata, *, translator) -> list[str]:
threshold_detail=threshold_detail,
source_symbol="TQQQ",
redirect_symbol=redirect_symbol or "QQQ",
allocation_detail=allocation_detail,
)
]

Expand Down
34 changes: 26 additions & 8 deletions notifications/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,19 @@
"signal_blend_gate_defensive": "{trend_symbol} 跌破门槛线,防守持有 SOXX {soxx_ratio}",
"market_status_blend_gate_overlay_capped": "🧯 风控降档({asset})",
"signal_blend_gate_overlay_capped": "{trend_symbol} 仍在 {window} 日门槛线上方,但触发风控降档({reasons}),目标仓位 {allocation_text}",
"risk_control_tqqq_volatility_delever_applied": "🛡️ 风控: QQQ {window} 日年化波动率 {volatility} 高于 {threshold},{source_symbol} 转向 {redirect_symbol}",
"risk_control_tqqq_volatility_delever_applied_dynamic": "🛡️ 风控: QQQ {window} 日年化波动率 {volatility} 高于实际阈值 {threshold}({threshold_detail}),{source_symbol} 转向 {redirect_symbol}",
"risk_control_tqqq_volatility_delever_hysteresis": "🛡️ 风控: QQQ {window} 日年化波动率 {volatility} 仍高于退出阈值 {exit_threshold},维持 {source_symbol} 转向 {redirect_symbol}",
"risk_control_tqqq_volatility_delever_hysteresis_dynamic": "🛡️ 风控: QQQ {window} 日年化波动率 {volatility} 仍高于退出阈值 {exit_threshold};入场实际阈值 {threshold}({threshold_detail}),维持 {source_symbol} 转向 {redirect_symbol}",
"risk_control_tqqq_volatility_delever_applied": "🛡️ 风控: QQQ {window} 日年化波动率 {volatility} 高于 {threshold},{source_symbol} 转向 {redirect_symbol}({allocation_detail})",
"risk_control_tqqq_volatility_delever_applied_dynamic": "🛡️ 风控: QQQ {window} 日年化波动率 {volatility} 高于实际阈值 {threshold}({threshold_detail}),{source_symbol} 转向 {redirect_symbol}({allocation_detail})",
"risk_control_tqqq_volatility_delever_hysteresis": "🛡️ 风控: QQQ {window} 日年化波动率 {volatility} 仍高于退出阈值 {exit_threshold},维持 {source_symbol} 转向 {redirect_symbol}({allocation_detail})",
"risk_control_tqqq_volatility_delever_hysteresis_dynamic": "🛡️ 风控: QQQ {window} 日年化波动率 {volatility} 仍高于退出阈值 {exit_threshold};入场实际阈值 {threshold}({threshold_detail}),维持 {source_symbol} 转向 {redirect_symbol}({allocation_detail})",
"tqqq_volatility_delever_allocation_detail": "杠杆仓位:TQQQ 保留 {retained_ratio},{redirect_symbol} {redirected_ratio}",
"tqqq_signal_reason_entry_trend": "原因:QQQ 高于 MA200,MA20 斜率为正",
"tqqq_signal_reason_entry_pullback": "原因:QQQ 低于 MA200,但站上 MA20 且回撤反弹确认",
"tqqq_signal_reason_hold_trend": "原因:已持有风险仓位,QQQ 仍高于 MA200",
"tqqq_signal_reason_exit_ma200": "原因:QQQ 跌破 MA200 退出线",
"tqqq_signal_reason_idle_waiting": "原因:等待 QQQ 站上 MA200 且 MA20 斜率转正",
"tqqq_signal_reason_macro_delever": "原因:宏观风控降低杠杆",
"tqqq_signal_reason_macro_defense": "原因:宏观风控转入防守",
"tqqq_signal_reason_crisis_defense": "原因:危机防御转入避险仓位",
"blend_gate_reason_rsi_cap": "RSI 超阈值",
"blend_gate_reason_bollinger_cap": "突破布林上轨",
"blend_gate_reason_volatility_delever": "{symbol} {window} 日年化波动率 {volatility} 高于 {threshold},SOXL 转向 {redirect_symbol}",
Expand Down Expand Up @@ -253,10 +262,19 @@
"signal_blend_gate_defensive": "{trend_symbol} below gated entry, hold defensive SOXX {soxx_ratio}",
"market_status_blend_gate_overlay_capped": "🧯 RISK-CAP ({asset})",
"signal_blend_gate_overlay_capped": "{trend_symbol} stays above the {window}d gate, but risk cap ({reasons}) cuts exposure to {allocation_text}",
"risk_control_tqqq_volatility_delever_applied": "🛡️ Risk control: QQQ {window}d annualized volatility {volatility} is above {threshold}; {source_symbol} redirects to {redirect_symbol}",
"risk_control_tqqq_volatility_delever_applied_dynamic": "🛡️ Risk control: QQQ {window}d annualized volatility {volatility} is above effective threshold {threshold} ({threshold_detail}); {source_symbol} redirects to {redirect_symbol}",
"risk_control_tqqq_volatility_delever_hysteresis": "🛡️ Risk control: QQQ {window}d annualized volatility {volatility} remains above the exit threshold {exit_threshold}; keep {source_symbol} redirected to {redirect_symbol}",
"risk_control_tqqq_volatility_delever_hysteresis_dynamic": "🛡️ Risk control: QQQ {window}d annualized volatility {volatility} remains above exit threshold {exit_threshold}; entry effective threshold {threshold} ({threshold_detail}); keep {source_symbol} redirected to {redirect_symbol}",
"risk_control_tqqq_volatility_delever_applied": "🛡️ Risk control: QQQ {window}d annualized volatility {volatility} is above {threshold}; {source_symbol} redirects to {redirect_symbol} ({allocation_detail})",
"risk_control_tqqq_volatility_delever_applied_dynamic": "🛡️ Risk control: QQQ {window}d annualized volatility {volatility} is above effective threshold {threshold} ({threshold_detail}); {source_symbol} redirects to {redirect_symbol} ({allocation_detail})",
"risk_control_tqqq_volatility_delever_hysteresis": "🛡️ Risk control: QQQ {window}d annualized volatility {volatility} remains above the exit threshold {exit_threshold}; keep {source_symbol} redirected to {redirect_symbol} ({allocation_detail})",
"risk_control_tqqq_volatility_delever_hysteresis_dynamic": "🛡️ Risk control: QQQ {window}d annualized volatility {volatility} remains above exit threshold {exit_threshold}; entry effective threshold {threshold} ({threshold_detail}); keep {source_symbol} redirected to {redirect_symbol} ({allocation_detail})",
"tqqq_volatility_delever_allocation_detail": "leveraged sleeve: TQQQ retained {retained_ratio}, {redirect_symbol} {redirected_ratio}",
"tqqq_signal_reason_entry_trend": "reason: QQQ is above MA200 and MA20 slope is positive",
"tqqq_signal_reason_entry_pullback": "reason: QQQ is below MA200 but above MA20 with a confirmed pullback rebound",
"tqqq_signal_reason_hold_trend": "reason: existing risk sleeve remains active while QQQ stays above MA200",
"tqqq_signal_reason_exit_ma200": "reason: QQQ fell below the MA200 exit line",
"tqqq_signal_reason_idle_waiting": "reason: waiting for QQQ to reclaim MA200 with positive MA20 slope",
"tqqq_signal_reason_macro_delever": "reason: macro risk governor reduced leverage",
"tqqq_signal_reason_macro_defense": "reason: macro risk governor moved the strategy defensive",
"tqqq_signal_reason_crisis_defense": "reason: crisis defense moved the strategy to the safe sleeve",
"blend_gate_reason_rsi_cap": "RSI over threshold",
"blend_gate_reason_bollinger_cap": "price above upper band",
"blend_gate_reason_volatility_delever": "{symbol} {window}d annualized volatility {volatility} is above {threshold}; redirect SOXL to {redirect_symbol}",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
flask
gunicorn
quant-platform-kit @ git+https://github.com/QuantStrategyLab/QuantPlatformKit.git@2a711adf60b585ca02932bab9ee1bac7ce1df7c6
us-equity-strategies @ git+https://github.com/QuantStrategyLab/UsEquityStrategies.git@085f6010883b3f7c66a1c16f96749c0251410f93
us-equity-strategies @ git+https://github.com/QuantStrategyLab/UsEquityStrategies.git@fdd39ef0313181bee9083319b87b7175c32b364d
hk-equity-strategies @ git+https://github.com/QuantStrategyLab/HkEquityStrategies.git@02af62bc7af7b8ffdbe8575421434e455ab00d66
pandas
numpy
Expand Down
21 changes: 18 additions & 3 deletions tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ def test_build_translator_supports_chinese():
),
source_symbol="TQQQ",
redirect_symbol="QQQM",
allocation_detail=translate(
"tqqq_volatility_delever_allocation_detail",
retained_ratio="25.0%",
redirect_symbol="QQQM",
redirected_ratio="75.0%",
),
)
== "🛡️ 风控: QQQ 5 日年化波动率 31.2% 高于实际阈值 30.0%(动态 p90,252日窗口,范围 24.0%-36.0%,样本 252),TQQQ 转向 QQQM"
== "🛡️ 风控: QQQ 5 日年化波动率 31.2% 高于实际阈值 30.0%(动态 p90,252日窗口,范围 24.0%-36.0%,样本 252),TQQQ 转向 QQQM(杠杆仓位:TQQQ 保留 25.0%,QQQM 75.0%)"
)
assert (
en_translate(
Expand All @@ -112,8 +118,14 @@ def test_build_translator_supports_chinese():
),
source_symbol="TQQQ",
redirect_symbol="QQQM",
allocation_detail=en_translate(
"tqqq_volatility_delever_allocation_detail",
retained_ratio="0.0%",
redirect_symbol="QQQM",
redirected_ratio="100.0%",
),
)
== "🛡️ Risk control: QQQ 5d annualized volatility 26.2% remains above exit threshold 24.0%; entry effective threshold 30.0% (dynamic p90, 252d lookback, bounded 24.0%-36.0%, samples 252); keep TQQQ redirected to QQQM"
== "🛡️ Risk control: QQQ 5d annualized volatility 26.2% remains above exit threshold 24.0%; entry effective threshold 30.0% (dynamic p90, 252d lookback, bounded 24.0%-36.0%, samples 252); keep TQQQ redirected to QQQM (leveraged sleeve: TQQQ retained 0.0%, QQQM 100.0%)"
)
assert (
translate(
Expand Down Expand Up @@ -214,14 +226,17 @@ def test_dashboard_renders_tqqq_volatility_delever_risk_control():
"dual_drive_volatility_delever_dynamic_floor": 0.24,
"dual_drive_volatility_delever_dynamic_cap": 0.36,
"dual_drive_volatility_delever_redirect_symbol": "QQQM",
"dual_drive_volatility_delever_retained_ratio": 0.0,
"dual_drive_volatility_delever_redirected_ratio": 1.0,
},
translator=build_translator("en"),
separator="━━━━━━━━━━━━━━━━━━",
)

assert (
"🛡️ Risk control: QQQ 5d annualized volatility 31.2% is above effective threshold 30.0% "
"(dynamic p90, 252d lookback, bounded 24.0%-36.0%, samples 252); TQQQ redirects to QQQM"
"(dynamic p90, 252d lookback, bounded 24.0%-36.0%, samples 252); TQQQ redirects to QQQM "
"(leveraged sleeve: TQQQ retained 0.0%, QQQM 100.0%)"
) in dashboard


Expand Down