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
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,23 @@ The mainline runtime now follows one path only:
- `tqqq_growth_income`
- `soxl_soxx_trend_income`
- `tech_communication_pullback_enhancement`
- `mega_cap_leader_rotation_aggressive`
- `mega_cap_leader_rotation_dynamic_top20`
- `dynamic_mega_leveraged_pullback`


**IBKR profile status**

| Canonical profile | Display name | Eligible | Enabled | Default | Rollback | Domain | Runtime note |
| --- | --- | --- | --- | --- | --- | --- | --- |
| `global_etf_rotation` | Global ETF Rotation | Yes | Yes | Yes | Yes | `us_equity` | current rollback line |
| `russell_1000_multi_factor_defensive` | Russell 1000 Multi-Factor | Yes | Yes | No | No | `us_equity` | defensive stock baseline |
| `tqqq_growth_income` | TQQQ Growth Income | Yes | Yes | No | No | `us_equity` | enabled value-mode alternative |
| `soxl_soxx_trend_income` | SOXL/SOXX Semiconductor Trend Income | Yes | Yes | No | No | `us_equity` | current IBKR live line |
| `tech_communication_pullback_enhancement` | Tech/Communication Pullback Enhancement | Yes | Yes | No | No | `us_equity` | enabled feature-snapshot alternative |
| `mega_cap_leader_rotation_dynamic_top20` | Mega Cap Leader Rotation Dynamic Top20 | Yes | Yes | No | No | `us_equity` | enabled concentrated leader rotation |
| `dynamic_mega_leveraged_pullback` | Dynamic Mega Leveraged Pullback | Yes | Yes | No | No | `us_equity` | enabled 2x mega-cap pullback line |
| Canonical profile | Display name | Eligible | Enabled | Domain | Runtime note |
| --- | --- | --- | --- | --- | --- |
| `global_etf_rotation` | Global ETF Rotation | Yes | Yes | `us_equity` | enabled weight-mode rotation line |
| `russell_1000_multi_factor_defensive` | Russell 1000 Multi-Factor | Yes | Yes | `us_equity` | defensive stock baseline |
| `tqqq_growth_income` | TQQQ Growth Income | Yes | Yes | `us_equity` | enabled value-mode alternative |
| `soxl_soxx_trend_income` | SOXL/SOXX Semiconductor Trend Income | Yes | Yes | `us_equity` | current IBKR live line |
| `tech_communication_pullback_enhancement` | Tech/Communication Pullback Enhancement | Yes | Yes | `us_equity` | enabled feature-snapshot alternative |
| `mega_cap_leader_rotation_aggressive` | Mega Cap Leader Rotation Aggressive | Yes | Yes | `us_equity` | enabled aggressive leader rotation |
| `mega_cap_leader_rotation_dynamic_top20` | Mega Cap Leader Rotation Dynamic Top20 | Yes | Yes | `us_equity` | enabled concentrated leader rotation |
| `dynamic_mega_leveraged_pullback` | Dynamic Mega Leveraged Pullback | Yes | Yes | `us_equity` | enabled 2x mega-cap pullback line |

Check the current matrix locally:

Expand Down Expand Up @@ -98,8 +100,8 @@ The selected `ACCOUNT_GROUP` is now the runtime identity. Keep broker-specific i
|----------|----------|-------------|
| `IB_GATEWAY_ZONE` | Optional fallback | GCE zone (for example `us-central1-a`). Recommended to keep in the selected account-group entry; this env var is only a transition fallback. |
| `IB_GATEWAY_IP_MODE` | Optional fallback | `internal` (default) or `external`. Recommended to keep in the selected account-group entry; this env var is only a transition fallback. |
| `STRATEGY_PROFILE` | Yes | Strategy profile selector. Supported `us_equity` values: `global_etf_rotation`, `russell_1000_multi_factor_defensive`, `tqqq_growth_income`, `soxl_soxx_trend_income`, `tech_communication_pullback_enhancement`, `mega_cap_leader_rotation_dynamic_top20`, `dynamic_mega_leveraged_pullback` |
| `ACCOUNT_GROUP` | Yes | Account-group selector. No default fallback. |
| `STRATEGY_PROFILE` | Yes | Strategy profile selector. Supported `us_equity` values: `global_etf_rotation`, `russell_1000_multi_factor_defensive`, `tqqq_growth_income`, `soxl_soxx_trend_income`, `tech_communication_pullback_enhancement`, `mega_cap_leader_rotation_aggressive`, `mega_cap_leader_rotation_dynamic_top20`, `dynamic_mega_leveraged_pullback` |
| `ACCOUNT_GROUP` | Yes | Account-group selector. Set explicitly for each deployment. |
| `IBKR_FEATURE_SNAPSHOT_PATH` | Conditionally required | Required for snapshot-backed profiles such as `russell_1000_multi_factor_defensive`, `tech_communication_pullback_enhancement`, and `mega_cap_leader_rotation_dynamic_top20`. Path to the latest feature snapshot file (`.csv`, `.json`, `.jsonl`, `.parquet`). |
| `IB_ACCOUNT_GROUP_CONFIG_SECRET_NAME` | Yes for Cloud Run | Secret Manager secret name for account-group config JSON. Recommended production source. |
| `IB_ACCOUNT_GROUP_CONFIG_JSON` | No | Local/dev JSON fallback for account-group config. Not recommended for production Cloud Run. |
Expand Down Expand Up @@ -218,7 +220,7 @@ Recommended setup:
- `CLOUD_RUN_REGION`
- `CLOUD_RUN_SERVICE`
- `TELEGRAM_TOKEN_SECRET_NAME` (recommended when Cloud Run already uses Secret Manager for `TELEGRAM_TOKEN`)
- `STRATEGY_PROFILE` (recommended: `global_etf_rotation`)
- `STRATEGY_PROFILE` (set explicitly to one enabled profile, such as `soxl_soxx_trend_income`)
- `ACCOUNT_GROUP` (recommended: `default`)
- `IB_ACCOUNT_GROUP_CONFIG_SECRET_NAME`
- `GLOBAL_TELEGRAM_CHAT_ID`
Expand Down Expand Up @@ -288,7 +290,7 @@ gcloud run services update ibkr-quant \

IBKR runtime 负责把共享的 `us_equity` 策略档位部署到 GCP Cloud Run,并连接 GCE 上的 IB Gateway 执行。策略逻辑、策略频率、标的池、参数和研究/回测说明都放在 `UsEquityStrategies`;这个仓库只维护 IBKR 运行时、账号组、Gateway 连接、下单和通知。

当前 `global_etf_rotation`、`russell_1000_multi_factor_defensive`、`tqqq_growth_income`、`soxl_soxx_trend_income`、`tech_communication_pullback_enhancement` 和 `mega_cap_leader_rotation_dynamic_top20` 的策略实现都来自 `UsEquityStrategies`。
当前 `global_etf_rotation`、`russell_1000_multi_factor_defensive`、`tqqq_growth_income`、`soxl_soxx_trend_income`、`tech_communication_pullback_enhancement`、`mega_cap_leader_rotation_aggressive`、`mega_cap_leader_rotation_dynamic_top20` 和 `dynamic_mega_leveraged_pullback` 的策略实现都来自 `UsEquityStrategies`。

完整策略说明现在放在 [`UsEquityStrategies`](https://github.com/QuantStrategyLab/UsEquityStrategies)。这个 README 只保留 IBKR 运行时、profile 启用状态、部署和凭据说明。

Expand Down Expand Up @@ -329,8 +331,8 @@ IBKR 账户
|------|------|------|
| `IB_GATEWAY_ZONE` | 可选过渡项 | GCE zone(如 `us-central1-a`)。推荐直接放进选中的账号组配置里;这里只保留过渡 fallback。 |
| `IB_GATEWAY_IP_MODE` | 可选过渡项 | `internal`(默认)或 `external`。推荐直接放进选中的账号组配置里;这里只保留过渡 fallback。 |
| `STRATEGY_PROFILE` | 是 | 策略档位选择。当前可用的 `us_equity` 值:`global_etf_rotation`、`russell_1000_multi_factor_defensive`、`tqqq_growth_income`、`soxl_soxx_trend_income`、`tech_communication_pullback_enhancement`、`mega_cap_leader_rotation_dynamic_top20`、`dynamic_mega_leveraged_pullback` |
| `ACCOUNT_GROUP` | 是 | 账号组选择器,不再提供默认回退。 |
| `STRATEGY_PROFILE` | 是 | 策略档位选择。当前可用的 `us_equity` 值:`global_etf_rotation`、`russell_1000_multi_factor_defensive`、`tqqq_growth_income`、`soxl_soxx_trend_income`、`tech_communication_pullback_enhancement`、`mega_cap_leader_rotation_aggressive`、`mega_cap_leader_rotation_dynamic_top20`、`dynamic_mega_leveraged_pullback` |
| `ACCOUNT_GROUP` | 是 | 账号组选择器,每个部署都要显式设置。 |
| `IB_ACCOUNT_GROUP_CONFIG_SECRET_NAME` | Cloud Run 建议必填 | 账号组配置 JSON 在 Secret Manager 里的密钥名。生产环境推荐使用。 |
| `IB_ACCOUNT_GROUP_CONFIG_JSON` | 否 | 本地开发用的账号组配置 JSON fallback。不建议在生产 Cloud Run 直接使用。 |
| `TELEGRAM_TOKEN` | 是 | Telegram 机器人 Token。Cloud Run 上更推荐走 Secret Manager 引用,不要直接写成明文 env。 |
Expand Down Expand Up @@ -415,7 +417,7 @@ IB_GATEWAY_IP_MODE=internal
- `CLOUD_RUN_REGION`
- `CLOUD_RUN_SERVICE`
- `TELEGRAM_TOKEN_SECRET_NAME`(如果 Cloud Run 上的 `TELEGRAM_TOKEN` 已经改成 Secret Manager,建议配置)
- `STRATEGY_PROFILE`(建议设为 `global_etf_rotation`)
- `STRATEGY_PROFILE`(显式设置为任一已启用 profile,例如 `soxl_soxx_trend_income`)
- `ACCOUNT_GROUP`(建议设为 `default`)
- `IB_ACCOUNT_GROUP_CONFIG_SECRET_NAME`
- `GLOBAL_TELEGRAM_CHAT_ID`
Expand Down
4 changes: 4 additions & 0 deletions notifications/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"strategy_name_russell_1000_multi_factor_defensive": "罗素1000多因子",
"strategy_name_tech_communication_pullback_enhancement": "科技通信回调增强",
"strategy_name_qqq_tech_enhancement": "科技通信回调增强",
"strategy_name_mega_cap_leader_rotation_dynamic_top20": "Mega Cap 动态 Top20 龙头轮动",
"strategy_name_dynamic_mega_leveraged_pullback": "Mega Cap 2x 回调策略",
"strategy_name_tqqq_growth_income": "TQQQ 增长收益",
"strategy_name_soxl_soxx_trend_income": "SOXL/SOXX 半导体趋势收益",
},
Expand Down Expand Up @@ -133,6 +135,8 @@
"strategy_name_russell_1000_multi_factor_defensive": "Russell 1000 Multi-Factor",
"strategy_name_tech_communication_pullback_enhancement": "Tech/Communication Pullback Enhancement",
"strategy_name_qqq_tech_enhancement": "Tech/Communication Pullback Enhancement",
"strategy_name_mega_cap_leader_rotation_dynamic_top20": "Mega Cap Leader Rotation Dynamic Top20",
"strategy_name_dynamic_mega_leveraged_pullback": "Dynamic Mega Leveraged Pullback",
"strategy_name_tqqq_growth_income": "TQQQ Growth Income",
"strategy_name_soxl_soxx_trend_income": "SOXL/SOXX Semiconductor Trend Income",
},
Expand Down
2 changes: 0 additions & 2 deletions runtime_config_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

from quant_platform_kit.common.strategies import derive_strategy_artifact_paths
from strategy_registry import (
DEFAULT_STRATEGY_PROFILE as PLATFORM_DEFAULT_STRATEGY_PROFILE,
IBKR_PLATFORM,
resolve_strategy_definition,
resolve_strategy_metadata,
)
from us_equity_strategies import get_strategy_catalog

DEFAULT_ACCOUNT_GROUP = "default"
DEFAULT_STRATEGY_PROFILE = PLATFORM_DEFAULT_STRATEGY_PROFILE


@dataclass(frozen=True)
Expand Down
2 changes: 0 additions & 2 deletions scripts/print_strategy_profile_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def _print_table(rows: list[dict[str, object]]) -> None:
"requires_strategy_config_path",
"eligible",
"enabled",
"is_default",
"is_rollback",
"domain",
)
widths = {
Expand Down
30 changes: 19 additions & 11 deletions strategy_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

IBKR_PLATFORM = "ibkr"

DEFAULT_STRATEGY_PROFILE = "global_etf_rotation"
ROLLBACK_STRATEGY_PROFILE = DEFAULT_STRATEGY_PROFILE

IBKR_ROLLOUT_ALLOWLIST = get_runtime_enabled_profiles()

PLATFORM_SUPPORTED_DOMAINS: dict[str, frozenset[str]] = {
Expand Down Expand Up @@ -61,12 +58,17 @@
platform_id=IBKR_PLATFORM,
supported_domains=PLATFORM_SUPPORTED_DOMAINS[IBKR_PLATFORM],
enabled_profiles=IBKR_ENABLED_PROFILES,
default_profile=DEFAULT_STRATEGY_PROFILE,
rollback_profile=ROLLBACK_STRATEGY_PROFILE,
default_profile="",
rollback_profile="",
require_explicit_profile=True,
)

SUPPORTED_STRATEGY_PROFILES = IBKR_ENABLED_PROFILES
_SELECTION_ROLE_FIELDS = frozenset({"is_default", "is_rollback"})


def _without_selection_role_fields(row: dict[str, object]) -> dict[str, object]:
return {key: value for key, value in row.items() if key not in _SELECTION_ROLE_FIELDS}


def get_eligible_profiles_for_platform(platform_id: str) -> frozenset[str]:
Expand All @@ -80,15 +82,21 @@ def get_supported_profiles_for_platform(platform_id: str) -> frozenset[str]:


def get_platform_profile_matrix() -> list[dict[str, object]]:
return build_platform_profile_matrix(STRATEGY_CATALOG, policy=PLATFORM_POLICY)
return [
_without_selection_role_fields(row)
for row in build_platform_profile_matrix(STRATEGY_CATALOG, policy=PLATFORM_POLICY)
]


def get_platform_profile_status_matrix() -> list[dict[str, object]]:
return build_platform_profile_status_matrix(
STRATEGY_CATALOG,
policy=PLATFORM_POLICY,
eligible_profiles=ELIGIBLE_STRATEGY_PROFILES,
)
return [
_without_selection_role_fields(row)
for row in build_platform_profile_status_matrix(
STRATEGY_CATALOG,
policy=PLATFORM_POLICY,
eligible_profiles=ELIGIBLE_STRATEGY_PROFILES,
)
]


def resolve_strategy_definition(
Expand Down
12 changes: 11 additions & 1 deletion tests/test_notifications.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from notifications.telegram import build_translator, send_telegram_message
from notifications.telegram import build_strategy_display_name, build_translator, send_telegram_message


def test_build_translator_supports_chinese():
Expand All @@ -9,6 +9,16 @@ def test_build_translator_supports_chinese():
assert translate("signal_risk_on", window=150, ratio="40.2%") == "SOXL 站上 150 日均线,持有 SOXL,交易层风险仓位 40.2%"


def test_strategy_display_name_translates_new_live_profiles():
zh_name = build_strategy_display_name(build_translator("zh"))
en_name = build_strategy_display_name(build_translator("en"))

assert zh_name("mega_cap_leader_rotation_dynamic_top20") == "Mega Cap 动态 Top20 龙头轮动"
assert zh_name("dynamic_mega_leveraged_pullback") == "Mega Cap 2x 回调策略"
assert en_name("mega_cap_leader_rotation_dynamic_top20") == "Mega Cap Leader Rotation Dynamic Top20"
assert en_name("dynamic_mega_leveraged_pullback") == "Dynamic Mega Leveraged Pullback"


def test_send_telegram_message_logs_non_200_response(capsys):
class FakeResponse:
status_code = 401
Expand Down
Loading