'''
APPS = [
("crypto_monitor_okx", 4, "_market_open_for_key_monitor", True),
("crypto_monitor_gate", 2, "_market_open_for_key_monitor", True),
("crypto_monitor_gate_bot", 4, None, False),
]
def patch_app(app_dir: str, funds_dec: int, market_fn: str | None, has_fib: bool):
path = ROOT / app_dir / "app.py"
text = path.read_text(encoding="utf-8")
if "POSITION_SIZING_MODE" in text:
print(f"SKIP {app_dir}/app.py (already patched)")
return
if "from position_sizing_lib import" not in text:
anchor = "from key_monitor_lib import ("
if anchor not in text:
anchor = "from form_submit_lib import"
text = text.replace(
anchor,
IMPORT_BLOCK + "\n" + anchor,
1,
)
else:
text = text.replace(anchor, IMPORT_BLOCK + anchor, 1)
if "POSITION_SIZING_MODE = load_position_sizing_mode()" not in text:
text = text.replace(
"AUTO_TRANSFER_BJ_HOUR = int(os.getenv(\"AUTO_TRANSFER_BJ_HOUR\", \"8\"))\n",
"AUTO_TRANSFER_BJ_HOUR = int(os.getenv(\"AUTO_TRANSFER_BJ_HOUR\", \"8\"))\n" + ENV_LINE,
1,
)
if "_purge_key_monitors_if_full_margin" not in text:
text = text.replace("init_db()\n\n\ndef get_db():", "init_db()" + PURGE_FN + "\ndef get_db():", 1)
text = text.replace(
"install_strategy_trend(app,",
"_purge_key_monitors_if_full_margin()\n\ninstall_strategy_trend(app,",
1,
)
if market_fn and MARKET_OPEN_GUARD.strip() not in text:
text = text.replace(
f"def {market_fn}(\n",
f"def {market_fn}(\n",
1,
)
text = text.replace(
' """\n 与手动',
MARKET_OPEN_GUARD + ' """\n 与手动',
1,
)
# fallback: after docstring closing
if MARKET_OPEN_GUARD.strip() not in text:
pat = rf"(def {market_fn}\([^)]+\):\s*\n\s*\"\"\"[^\"\"]*\"\"\"\s*\n)"
text = re.sub(pat, r"\1" + MARKET_OPEN_GUARD, text, count=1)
if has_fib and ADD_KEY_GUARD.strip() not in text:
text = text.replace(
' if mt not in allowed_types:',
ADD_KEY_GUARD + ' if mt not in allowed_types:',
1,
) if "if mt not in allowed_types:" in text else text.replace(
' rank, total = _daily_volume_rank(symbol)',
ADD_KEY_GUARD + ' rank, total = _daily_volume_rank(symbol)',
1,
)
# render_template risk_percent= add template vars
if "position_sizing_mode=POSITION_SIZING_MODE" not in text:
text = text.replace(
"risk_percent=RISK_PERCENT,\n",
"risk_percent=RISK_PERCENT,\n"
" position_sizing_mode=POSITION_SIZING_MODE,\n"
" position_sizing_mode_label=mode_label_zh(POSITION_SIZING_MODE),\n"
" open_position_button_label=(\n"
' "开仓(全仓杠杆)" if is_full_margin_mode(POSITION_SIZING_MODE) else "开仓(以损定仓)"\n'
" ),\n",
1,
)
path.write_text(text, encoding="utf-8")
print(f"DONE {app_dir}/app.py (partial — verify add_order block manually if needed)")
def patch_template(app_dir: str):
tpl = ROOT / app_dir / "templates" / "index.html"
if not tpl.exists():
return
text = tpl.read_text(encoding="utf-8")
if "position_sizing_mode_label" in text:
print(f"SKIP {tpl}")
return
old = re.search(
r'
\s*以损定仓:风险 \{\{ risk_percent \}\}%.*?
',
text,
re.S,
)
if old:
text = text[: old.start()] + TEMPLATE_RULE + text[old.end() :]
text = text.replace(
'',
'',
)
text = text.replace(
'',
'{% if position_sizing_mode != \'full_margin\' %}\n'
' \n'
' {% endif %}',
1,
)
tpl.write_text(text, encoding="utf-8")
print(f"DONE {tpl}")
def main():
for app_dir, funds, mfn, fib in APPS:
patch_app(app_dir, funds, mfn, fib)
patch_template(app_dir)
if __name__ == "__main__":
main()