Files
kis_bot/scratch/extract_optuna.py

71 lines
3.4 KiB
Python

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*<div class="col-4 col-md-1">\s*<label class="form-label param-row"[^>]*>.*?</label>\s*<input[^>]*id="' + input_id + r'"[^>]*>\s*</div>)'
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*<div class="col-auto">\s*<label class="form-label param-row"[^>]*>.*?</label>\s*<input[^>]*id="' + input_id + r'"[^>]*>\s*</div>)'
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 Parameters Group -->
<div class="row g-2 align-items-center mt-2 p-2 rounded" style="background:rgba(248, 81, 73, 0.05);border:1px solid rgba(248, 81, 73, 0.3)">
<div class="col-12"><span class="text-muted" style="font-size:11px"><b style="color:#f85149">🔬 Optuna 탐색 변수 (TPE 최적화 대상)</b></span></div>
{''.join(blocks)}
</div>"""
# Insert this group right before the EOD block we created earlier,
# or before the TRIGGER block.
if prefix == 'bt':
html = html.replace('<!-- EOD_BLOCK_bt -->', group_html + '\n <!-- EOD_BLOCK_bt -->')
elif prefix == 'tl':
html = html.replace('<!-- EOD_BLOCK_tl -->', group_html + '\n <!-- EOD_BLOCK_tl -->')
elif prefix == 'bo':
html = html.replace('<!-- EOD_BLOCK_bo -->', group_html + '\n <!-- EOD_BLOCK_bo -->')
elif prefix == 'mom':
html = html.replace(' <div class="row g-2 align-items-center mt-2 p-2 rounded" style="background:#161b22;border:1px solid #30363d">\n <div class="col-12"><span class="text-muted" style="font-size:11px"><b style="color:#79c0ff">EOD 장마감 청산</b></span></div>',
group_html + '\n <div class="row g-2 align-items-center mt-2 p-2 rounded" style="background:#161b22;border:1px solid #30363d">\n <div class="col-12"><span class="text-muted" style="font-size:11px"><b style="color:#79c0ff">EOD 장마감 청산</b></span></div>')
# Fix EOD_BLOCK markers
html = html.replace('<!-- EOD_BLOCK_bt -->', '')
html = html.replace('<!-- EOD_BLOCK_tl -->', '')
html = html.replace('<!-- EOD_BLOCK_bo -->', '')
html = html.replace('<!-- EOD_BLOCK_usmom -->', '')
with open(html_path, 'w', encoding='utf-8') as f:
f.write(html)
print("Optuna variables grouped.")