옵투나 8방 후처리를 재탐색으로 변경하기 전전
This commit is contained in:
@@ -123,6 +123,7 @@ def recommend_whipsaw_parameters(
|
||||
date_from: Optional[str] = None,
|
||||
date_to: Optional[str] = None,
|
||||
market: str = "KR",
|
||||
progress_label: str = "",
|
||||
) -> Dict[str, Any]:
|
||||
lg = log or logger
|
||||
if int(n_trials or 0) <= 0:
|
||||
@@ -132,6 +133,7 @@ def recommend_whipsaw_parameters(
|
||||
days = max(1, int(get_env_int("OPTUNA_WHIPSAW_LOOKBACK_DAYS", 7)))
|
||||
strat_upper = str(strategy).strip().upper()
|
||||
mk = "US" if "US" in strat_upper else str(market or "KR").strip().upper() or "KR"
|
||||
axis_name = str(progress_label or f"휩쏘-{strat_upper}").strip() or "휩쏘"
|
||||
|
||||
db = TradeDB()
|
||||
try:
|
||||
@@ -258,7 +260,19 @@ def recommend_whipsaw_parameters(
|
||||
return score
|
||||
|
||||
study = optuna.create_study(direction="maximize")
|
||||
study.optimize(obj_func, n_trials=n_trials)
|
||||
from kis_trader.backtest import optuna_post_progress as opp
|
||||
|
||||
def _cb(_study: Any, _trial: Any) -> None:
|
||||
n = len(_study.trials)
|
||||
if n == 1 or n == int(n_trials) or n % 5 == 0:
|
||||
try:
|
||||
opp.on_ob_axis_trial(n, int(n_trials), axis_name)
|
||||
except Exception:
|
||||
pass
|
||||
if n == 1 or n == int(n_trials) or n % 20 == 0:
|
||||
lg.info("📡 [%s] trial %d/%d", axis_name, n, int(n_trials))
|
||||
|
||||
study.optimize(obj_func, n_trials=n_trials, callbacks=[_cb])
|
||||
|
||||
valid_records.sort(key=lambda x: x["score"], reverse=True)
|
||||
top5 = valid_records[: min(5, len(valid_records))]
|
||||
@@ -345,13 +359,43 @@ def attach_whipsaw_recommend(
|
||||
_WHIPSAW_DB_SKIP_STRATS = {"TAIL", "SHORT", "BREAKOUT"}
|
||||
|
||||
|
||||
def _whipsaw_env_prefix(strategy: str) -> str:
|
||||
"""전략명 → env 접두사 (SCALPING→SCALP, SHORT→TAIL)."""
|
||||
u = str(strategy or "").strip().upper()
|
||||
if u in ("SHORT", "TAIL"):
|
||||
return "TAIL"
|
||||
if u in ("SCALPING", "SCALP"):
|
||||
return "SCALP"
|
||||
if u in ("US_MOMENTUM", "USMOMENTUM"):
|
||||
return "US_MOMENTUM"
|
||||
return u
|
||||
|
||||
|
||||
def build_whipsaw_clear_patch(strategy: str) -> Dict[str, str]:
|
||||
"""
|
||||
「호가만」적용 시 휩쏘 OFF + 수치 비움.
|
||||
(이전에는 MOMENTUM만 ENABLED=false 해서 스캘 등에 휩쏘가 남았음)
|
||||
"""
|
||||
pfx = _whipsaw_env_prefix(strategy)
|
||||
if not pfx:
|
||||
return {}
|
||||
return {
|
||||
f"{pfx}_WHIPSAW_FILTER_ENABLED": "false",
|
||||
f"{pfx}_WHIPSAW_MODE": "",
|
||||
f"{pfx}_WHIPSAW_SUBBAR_SEC": "",
|
||||
f"{pfx}_WHIPSAW_LOOKBACK_SEC": "",
|
||||
f"{pfx}_WHIPSAW_DIP_PCT": "",
|
||||
f"{pfx}_WHIPSAW_RECOVERY_TOL_PCT": "",
|
||||
}
|
||||
|
||||
|
||||
def build_whipsaw_env_patch(rec: Dict[str, Any]) -> Dict[str, str]:
|
||||
"""휩쏘 추천 결과를 DB env 패치 dict로 변환."""
|
||||
if not rec or not rec.get("ok"):
|
||||
return {}
|
||||
|
||||
strat = str(rec.get("strategy") or "").strip().upper()
|
||||
pfx = "TAIL" if strat in ("SHORT", "TAIL") else strat
|
||||
pfx = _whipsaw_env_prefix(strat)
|
||||
p = rec.get("params", {})
|
||||
if not pfx or not p:
|
||||
return {}
|
||||
|
||||
Reference in New Issue
Block a user