fix: hot-reload trade direction/whitelist on env save without forced restart.

EOF

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-17 19:24:10 +08:00
parent b7966fd935
commit 4d381e9104
12 changed files with 111 additions and 15 deletions
+19 -4
View File
@@ -12,6 +12,7 @@ from lib.env.env_schema import (
_mask_value,
_restart_required,
normalize_position_sizing_mode,
normalize_trade_direction,
parse_env_example_schema,
)
@@ -61,10 +62,10 @@ _SHARED_SECTIONS: list[dict[str, Any]] = [
("FULL_MARGIN_BUFFER_RATIO", "全仓资金缓冲比例", "如 0.98"),
("BTC_LEVERAGE", "BTC 默认杠杆", ""),
("ALT_LEVERAGE", "山寨默认杠杆", ""),
("TRADE_DIRECTION_RESTRICT_ENABLED", "方向限制开关", ""),
("TRADE_DIRECTION", "允许方向", "long_only / short_only / both"),
("TRADE_SYMBOL_RESTRICT_ENABLED", "币种白名单开关", ""),
("TRADE_SYMBOL_WHITELIST", "白名单币种", "逗号分隔,如 BTC,ETH"),
("TRADE_DIRECTION_RESTRICT_ENABLED", "方向限制开关", "关闭=双向均可;开启后按下方「允许方向」限制。保存后立即生效,一般无需重启。"),
("TRADE_DIRECTION", "允许方向", "仅在方向限制开启时生效"),
("TRADE_SYMBOL_RESTRICT_ENABLED", "币种白名单开关", "关闭=可手输任意币种;开启后仅白名单可选。保存后立即生效,一般无需重启。"),
("TRADE_SYMBOL_WHITELIST", "白名单币种", "逗号分隔如 BTC,ETH;开启白名单时勿留空"),
("TRADING_DAY_RESET_HOUR", "交易日切点(北京时间)", "整点,默认 8"),
("TRADING_DAY_RESET_OPEN_GUARD_ENABLED", "切点前禁止新开仓", ""),
("MAX_ACTIVE_POSITIONS", "最大同时持仓", ""),
@@ -163,6 +164,12 @@ _POSITION_SIZING_OPTIONS: list[dict[str, str]] = [
{"value": "full_margin", "label": "全仓杠杆 (full_margin)"},
]
_TRADE_DIRECTION_OPTIONS: list[dict[str, str]] = [
{"value": "both", "label": "双向 (both)"},
{"value": "long_only", "label": "仅做多 (long_only)"},
{"value": "short_only", "label": "仅做空 (short_only)"},
]
def _effective_env_value(key: str, file_values: dict[str, str], schema_default: str = "") -> str:
if key in file_values:
@@ -219,6 +226,14 @@ def _build_field(
cur = "risk"
out["current"] = cur
out["default"] = cur
if key == "TRADE_DIRECTION":
out["type"] = "enum"
out["options"] = list(_TRADE_DIRECTION_OPTIONS)
cur = normalize_trade_direction(out["current"] or out["default"] or "both")
if cur not in ("long_only", "short_only", "both"):
cur = "both"
out["current"] = cur
out["default"] = cur
return out