import re html_path = 'templates/backtest.html' with open(html_path, 'r', encoding='utf-8') as f: html = f.read() def extract_and_remove(html_str, input_id): pattern = r'(\s*
\s*\s*]*id="' + input_id + r'"[^>]*>\s*
)' match = re.search(pattern, html_str, re.DOTALL) if match: block = match.group(1) html_str = html_str.replace(block, '') return html_str, block # Try col-auto fallback pattern = r'(\s*
\s*\s*]*id="' + input_id + r'"[^>]*>\s*
)' match = re.search(pattern, html_str, re.DOTALL) if match: block = match.group(1) html_str = html_str.replace(block, '') return html_str, block print(f"Could not find {input_id}") return html_str, "" # For each prefix, we will extract the optuna variables and group them. prefixes = { 'bt': ['bt_stop', 'bt_target', 'bt_smin', 'bt_scut', 'bt_vol_mult'], 'tl': ['tl_dip', 'tl_stop', 'tl_target', 'tl_smin', 'tl_scut'], 'bo': ['bo_sl', 'bo_tp', 'bo_smin', 'bo_scut', 'bo_tp_max'], 'mom': ['mom_sl', 'mom_tp', 'mom_smin', 'mom_scut', 'mom_tp_max'], } for prefix, ids in prefixes.items(): blocks = [] for pid in ids: html, block = extract_and_remove(html, pid) if block: blocks.append(block.strip()) if blocks: # Create a new container for Optuna values group_html = f"""
🔬 Optuna 탐색 변수 (TPE 최적화 대상)
{''.join(blocks)}
""" # Insert this group right before the EOD block we created earlier, # or before the TRIGGER block. if prefix == 'bt': html = html.replace('', group_html + '\n ') elif prefix == 'tl': html = html.replace('', group_html + '\n ') elif prefix == 'bo': html = html.replace('', group_html + '\n ') elif prefix == 'mom': html = html.replace('
\n
EOD 장마감 청산
', group_html + '\n
\n
EOD 장마감 청산
') # Fix EOD_BLOCK markers html = html.replace('', '') html = html.replace('', '') html = html.replace('', '') html = html.replace('', '') with open(html_path, 'w', encoding='utf-8') as f: f.write(html) print("Optuna variables grouped.")