import subprocess
import os
ROOT = "/home/hoon/kis_bot"
def run(cmd):
subprocess.run(cmd, shell=True, check=True, cwd=ROOT)
print("Restoring bloated files...")
run("git checkout templates/backtest.html static/js/backtest.js")
print("Done restoring files.")
# Re-apply HTML changes
html_path = f"{ROOT}/templates/backtest.html"
with open(html_path, "r", encoding="utf-8") as f:
html = f.read()
html = html.replace(
"
๐ KIS Quant ๋ฐฑํ
์คํธ ๋์๋ณด๋",
"๐ KIS Quant ๋ฐฑํ
์คํธ ๋์๋ณด๋\n"
)
eod_block = """
""",
f"""
{eod_block}"""
)
with open(html_path, "w", encoding="utf-8") as f:
f.write(html)
print("Applied HTML changes.")
# Re-apply JS changes
js_path = f"{ROOT}/static/js/backtest.js"
with open(js_path, "r", encoding="utf-8") as f:
js = f.read()
js = js.replace(
"cumReturnPct: Number(t.cum_return_pct ?? NaN),",
"cumReturnPct: Number(t.cum_return_pct ?? NaN),\n entryOb: t.entry_ob || null,\n exitOb: t.exit_ob || null,"
)
js = js.replace(
"const nameCell = (r.name || r.code)\n ? `${r.name || r.code}
${r.code || ''}`\n : (r.code || '-');",
"const nameCell = (r.name || r.code)\n ? `${r.name || r.code}
${r.code || ''}`\n : (r.code || '-');\n const linksHtml = _tradeExtLinksHtml(r);"
)
js = js.replace(
"${causeBadge}${nameCell} | ",
"${causeBadge}${nameCell}${linksHtml} | "
)
js = js.replace(
"if (opts.showDebug) html += `${debugHtml} | `;",
"if (opts.showDebug) html += `${debugHtml} | `;\n html += `${_entryObCellHtml(r.entryOb)} | `;\n html += `${_exitObCellHtml(r.exitOb, r.isOpen)} | `;"
)
js = js.replace(
" html += `${reasonHtml} | `;",
" html += `${reasonHtml} | `;" # unchanged but kept for context, already handled above
)
helper_funcs = """/** KR 6์๋ฆฌ ์ข
๋ชฉ์ฝ๋ */
function _normKrStockCode(code) {
const d = String(code || '').replace(/\\D/g, '');
if (!d) return '';
return d.length >= 6 ? d.slice(-6) : d.padStart(6, '0');
}
function _tradeExtLinksHtml(r) {
if (r.isUsd || !r.code) return '';
const c = _normKrStockCode(r.code);
return `
`;
}
function _entryObCellHtml(ob) {
if (!ob) return '-';
const mr = (ob.mid_ratio || 0).toFixed(1);
const wr = (ob.whale_ratio || 0).toFixed(1);
return `M:${mr}%
W:${wr}%`;
}
function _exitObCellHtml(ob, isOpen) {
if (isOpen) return '๋ณด์ ์ค';
if (!ob) return '-';
const mr = (ob.mid_ratio || 0).toFixed(1);
const wr = (ob.whale_ratio || 0).toFixed(1);
return `M:${mr}%
W:${wr}%`;
}
"""
js = js.replace(
"function renderVirtualTrades(tbodyId, trades, opts) {",
helper_funcs + "\nfunction renderVirtualTrades(tbodyId, trades, opts) {"
)
with open(js_path, "w", encoding="utf-8") as f:
f.write(js)
print("Applied JS changes.")